【问题标题】:Azure Application Insights Kusto Language Summurize by where TimeGenerated ValueAzure Application Insights Kusto 语言按时间生成值汇总
【发布时间】:2019-03-28 08:30:57
【问题描述】:

有没有办法使用 Kusto 语言将 where 分句放在不同的列中。我知道 SQL 也使用“Pivot”语法来创建基于唯一值的列。但不要认为这对我有帮助。还有另一个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 个不同的地方(一个表内的时间生成 where 语句)得到这种外观

尝试了 SO 问题解决方案,但没有按计划进行(在将这些代码行添加到我的原始查询中时出现 Failed to resolve scalar expression named '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

有谁知道我在这里缺少或监督什么。这可能与 kusto 吗? 我是否使用查询的开销来处理可以在几行内完成的事情。

【问题讨论】:

    标签: mysql azure azure-application-insights azure-data-explorer


    【解决方案1】:

    如果我正确理解您的情况,假设您提前知道您的目标月份,您可能会使用 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
    

    【讨论】:

    • 我根据您的建议使用了以下查询。 paste.ee/p/3yIez 但是因为 Timegenerated 不是列,kusto 给了我以下错误。 'summarize' operator: Failed to resolve column or scalar expression named 'TimeGenerated' 知道如何避免这种情况吗?
    • 您当前的查询仅投影 2 个特定列,其中不包括名为 TimeGenerated 的列。我的例子就是这样 - 一个例子。您应该检查数据的架构是否与其对齐 - 如果可以,请尝试使用 datatable operator 提供示例来演示 input 数据/架构的外观(在您的初始过滤器/聚合/ joins) - 这样做可能有助于针对您的具体情况调整示例
    • 我猜 sumif 不会像我预期的那样做。我需要 3 个不同的列,每列都有不同的 where 子句。无需为 TimeGenerated 值创建列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    相关资源
    最近更新 更多