假设您有一个表 AzureActivity,其中包含 OperationName、TimeGenerated、EventSubmissionTimestamp、MachineId、ActivityStatus 列(我从您的问题中派生列),您可以使用下一个查询:
// Inline data for the purpose of the query demonstration
let AzureActivity = datatable(OperationName:string, TimeGenerated:datetime, EventSubmissionTimestamp:datetime, MachineId:string, ActivityStatus:string)
[
// Machine 1
'Start Virtual Machine', datetime(2019-01-27 00:00), datetime(2019-01-27 00:00), 'Machine1', 'Succeeded',
'Deallocate Virtual Machine', datetime(2019-01-27 00:00), datetime(2019-01-27 01:00), 'Machine1', 'Succeeded',
// Machine 2
'Start Virtual Machine', datetime(2019-01-27 00:00), datetime(2019-01-27 00:00), 'Machine2', 'Succeeded',
];
// Query starts here
let _data = materialize(
AzureActivity
| where TimeGenerated >= ago(30d)
and (OperationName == "Deallocate Virtual Machine" or OperationName == "Start Virtual Machine")
and ActivityStatus == "Succeeded"
| summarize arg_max(EventSubmissionTimestamp, *) by OperationName, MachineId
);
let startEvents = _data | where OperationName == 'Start Virtual Machine' | project StartTime = EventSubmissionTimestamp, MachineId;
let deallocateEvents = _data | where OperationName == 'Deallocate Virtual Machine' | project DeallocateTime = EventSubmissionTimestamp, MachineId;
startEvents | join kind = fullouter (deallocateEvents) on MachineId
| project MachineId, StartTime, DeallocateTime,
UpTime=iif(isnotnull(DeallocateTime),
(DeallocateTime-now()),
(now()-StartTime))