代码之家  ›  专栏  ›  技术社区  ›  achahbar

Azure Application Insights Kusto语言按时间生成值汇总

  •  0
  • achahbar  · 技术社区  · 5 年前

    用Kusto语言在不同的列中有where子句的方法吗。我知道“Pivot”语法,它也与SQL一起用于基于唯一值创建列。但别以为这对我有帮助。还有一个 SO question 他几乎和我有同样的问题。但他的解决方案也没用。

    我的查询上下文:这个查询获取每个机器每个月的运行时间。您可能想知道为什么我使用这么长的查询来实现这一点。欢迎任何操作和调整。我对这门语言很陌生。我已经使用top查询来获取另一个项目中每个VM的开始和停止时间。

    原始查询:

    AzureActivity
    | where ResourceProvider == "Microsoft.Compute"
    and ActivityStatus == "Succeeded"
    and OperationName == "Deallocate Virtual Machine"
    | project DeallocateResource=Resource
    ,DeallocatedDate=format_datetime(EventSubmissionTimestamp, 'yyyy-MM-dd')
    ,DeallocatedTime=format_datetime(EventSubmissionTimestamp, 'HH:mm:ss')
    | join kind=fullouter (AzureActivity
    | where ResourceProvider == "Microsoft.Compute"
    and ActivityStatus == "Succeeded"
    and OperationName == "Start Virtual Machine"
    | project StartupResource=Resource
    ,StartDate=format_datetime(EventSubmissionTimestamp, 'yyyy-MM-dd')
    ,StartTime=format_datetime(EventSubmissionTimestamp, 'HH:mm:ss')
    ) on $right.StartupResource == $left.DeallocateResource
    | where StartDate == DeallocatedDate
    | project Resource=coalesce(StartupResource, DeallocateResource) ,
    Runtime = round(todouble(datetime_diff('minute', todatetime(strcat(StartDate , " " , DeallocatedTime )) , todatetime(strcat(StartDate , " " , StartTime )))) / 60)
    | summarize sum(Runtime) by Resource
    

    现在,上面的查询将得到运行时间和您在门户中特别设置的时间范围的总和。 为了得到每个月的运行时间之和(日志分析设置为90天,所以3个月前),我添加了这些where语句。在3 不同的 询问。工作完成了,我得到了3个不同的表,每个月的运行时间是(month1,month2,month3)。

    | where TimeGenerated > ago(30d)
    | where TimeGenerated between(ago(30d) .. ago(60d) )
    | where TimeGenerated between(ago(60d) .. ago(90d) )
    

    但这是3个不同的查询和3个不同的表。我的目标是查看3个不同的地方(一个表中的timegenerated where语句)

    Table That I want to achieve

    尝试了SO问题的解决方案,但没有按计划进行(在将这些代码行添加到原始查询中时,未能解析名为“TimeGenerated”的标量表达式)

    | summarize sum(Runtime) by Resource , bin(TimeGenerated, 1m)
    | summarize Fistmonth = TimeGenerated > ago(30d),  
                SecondMonth = TimeGenerated between(ago(30d) .. ago(60d)) ,
                ThirdMonth = Runtime_,TimeGenerated between(ago(60d) .. ago(90d) ) by Resource
    

    有人知道我在这里遗漏了什么或监督了什么吗。库斯托有可能吗? 我是否会使用查询的开销来完成几行可以完成的事情。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Yoni L.    5 年前

    如果我正确地理解了您的场景,您可以使用 sumif ,假设你提前知道目标月份。

    下面是一个例子:

    datatable(Resource:string, Runtime:double, TimeGenerated:datetime)
    [
        "A", 13.4, datetime(2019-01-01 11:11:11),
        "B", 1.34, datetime(2019-01-01 10:10:10),
        "C", 0.13, datetime(2019-01-01 12:12:12),
        "A", 12.4, datetime(2019-02-01 11:11:11),
        "B", 1.24, datetime(2019-02-01 09:09:09),
        "B", 2.24, datetime(2019-02-01 09:10:09),
        "B", 3.24, datetime(2019-02-01 09:11:09),
        "C", 0.12, datetime(2019-02-01 08:08:08),
        "A", 14.4, datetime(2019-03-01 07:07:07),
        "B", 1.44, datetime(2019-03-01 05:05:05),
        "C", 0.14, datetime(2019-03-01 06:06:06),
    ]
    | summarize Month1 = sumif(Runtime, TimeGenerated between(datetime(2019-01-01)..datetime(2019-02-01))),
                Month2 = sumif(Runtime, TimeGenerated between(datetime(2019-02-01)..datetime(2019-03-01))),
                Month3 = sumif(Runtime, TimeGenerated between(datetime(2019-03-01)..datetime(2019-04-01))) 
             by Resource