Kusto查询需求:合并多时间范围的消息计数查询(含Azure/未授权消息)
Kusto查询合并需求与格式要求
需求说明
需求一
获取过去7天每日、过去30天每日的Unauthorized消息计数,输出单一格式的结果。
需求二
获取以下三类数据的统一输出结果:
- 过去24小时内
message列包含"azure"的消息总数; - 过去7天内每日
message列包含"azure"的消息计数; - 过去30天内每日
message列包含"azure"的消息计数。
现有独立查询语句
针对需求二,目前有三个独立的Kusto查询:
过去24小时查询
| where message has "azure" | where timestamp > ago(24h) | summarize count() by message = "azure"
过去7天每日计数查询
| where message has "azure" | where timestamp >= startofday(ago(7d)) | where timestamp < startofday(now()) | summarize count() by startofday(timestamp) , message ="azure"
过去30天每日计数查询
| where message has "azure" | where timestamp >= startofday(ago(30d)) | where timestamp < startofday(now()) | summarize count() by startofday(timestamp) , message = "azure"
合并查询请求
是否可以将上述三个查询合并为一个,输出如下格式的单一结果?要求结果覆盖过去30天的所有日期。
示例输入数据
| Timestamp | message |
|---|---|
| 2009-09-17 12:45:37 | Azure |
| 2009-09-18 12:45:39 | Aws |
| 2009-09-29 13:29:12 | |
| 2009-09-12 13:29:14 | Aws |
| 2009-09-19 13:29:17 | Azure |
| 2009-09-14 13:29:23 |
预期输出格式
| Timestamp | message | count_24hrs | count_7days | count_30days |
|---|---|---|---|---|
| 2009-09-16 12:45:37 | Azure | 152 | 152 | 152 |
| 2009-09-15 12:45:39 | Azure | 65 | 65 | |
| 2009-09-14 13:29:12 | Azure | 6587 | 6587 | |
| 2009-09-13 13:29:14 | Azure | 98 | 98 | |
| 2009-09-12 13:29:17 | Azure | 54365 | 54365 | |
| 2009-09-11 13:29:23 | Azure | 12 | 12 | |
| 97 | 97 | |||
| 987 | ||||
| 98 |
解决方案:合并后的Kusto查询
可以通过生成日期序列结合条件聚合实现合并查询,确保覆盖过去30天的所有日期,同时输出三个维度的计数。以下是完整查询语句:
// 生成过去30天的每日日期序列,保证无数据日期也能输出 let date_range = range day from startofday(ago(30d)) to startofday(now()) step 1d; // 筛选包含azure的消息并按日期分组预处理 let azure_data = your_table_name | where message has "azure" | extend day = startofday(timestamp); // 关联日期序列,计算各维度计数 date_range | join kind=leftouter (azure_data) on day | summarize count_24hrs = countif(timestamp > ago(24h)), count_7days = countif(timestamp >= startofday(ago(7d))), count_30days = count() by day, message = "azure" // 处理空值,匹配预期输出格式 | project Timestamp = iif(isnotempty(day), day, datetime(null)), message, count_24hrs = iif(count_24hrs == 0, int(null), count_24hrs), count_7days = iif(count_7days == 0, int(null), count_7days), count_30days = iif(count_30days == 0, int(null), count_30days) // 按日期倒序排列 | sort by Timestamp desc
关键逻辑说明
- 日期序列生成:用
range函数生成过去30天的每日起始时间,确保结果覆盖所有日期,避免无数据日期丢失。 - 左连接关联:将日期序列与筛选后的azure数据左连接,保证每个日期都出现在结果集中。
- 条件聚合:通过
countif分别统计符合24小时、7天范围的消息数,count()直接统计30天内的每日总数。 - 空值格式化:用
iif将0值转换为null,匹配预期输出中的空单元格样式。
需求一的扩展实现
针对需求一(Unauthorized消息的7天/30天每日计数),只需修改筛选条件与聚合逻辑:
let date_range = range day from startofday(ago(30d)) to startofday(now()) step 1d; let unauthorized_data = your_table_name | where message has "Unauthorized" | extend day = startofday(timestamp); date_range | join kind=leftouter (unauthorized_data) on day | summarize count_7days = countif(timestamp >= startofday(ago(7d))), count_30days = count() by day, message = "Unauthorized" | project Timestamp = day, message, count_7days = iif(count_7days == 0, int(null), count_7days), count_30days = iif(count_30days == 0, int(null), count_30days) | sort by Timestamp desc
内容的提问来源于stack exchange,提问作者OoO
相关产品推荐
相关产品推荐

