[英]How to perform group-by over JSON data using JSONATA
我有一個下面給出的 JSON 結構。 我想對這些數據執行幾個轉換。 我開始了解 JSONATA,它對我來說看起來很棒,但由於我是新手,我在轉型方面有些掙扎。 我想計算給定日期的每個模塊日期的總計數(總和)。 預期結果如下。
輸入:
{
"id": "6332acbfe13e6063dcb740ef",
"record": [
{
"date": "2022-09-22",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 16
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 3
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 17
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 8
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 8
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 4
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 9
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 6
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 3
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 5
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 4
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 11
}
]
}
]
},
{
"date": "2022-09-19",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 14
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 3
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 15
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 5
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 5
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 6
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 6
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 3
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 4
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 3
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 9
}
]
}
]
},
{
"date": "2022-09-12",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 4
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 2
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 3
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 4
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 2
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 4
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 3
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 7
}
]
}
]
},
{
"date": "2022-09-05",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 12
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 11
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 5
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 1
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 5
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 2
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 2
}
]
}
]
}
],
"metadata": {
"name": "",
"readCountRemaining": 98,
"timeToExpire": 86389,
"createdAt": "2022-09-27T07:56:47.003Z"
}
}
預計 Output:
[
"2022-09-22" : [
{
"name" : "modules/dynatrace",
"count": 11
},
{
"name" : "modules/dynatrace",
"count": 4
},
{
"name" : "modules/splunk/hec-token",
"count": 14
}
....
],
"2022-09-19" : [
{
"name" : "modules/dynatrace",
"count": 52
},
.....
]
.
.
.
]
這應該為你做分組:
(
record{
date: $reduce(entries.prod.name, function($acc, $item) {
$merge([$acc, {
$item: $count(entries.prod.name[$=$item])
}])
}, {}) ~> $each(function($value, $key) {
{ "name": $key, "count": $value }
})
}
)
您可以在這里現場查看: https://stedi.link/LiOttdB
更新 - 更短的解決方案:
(
record{
date: entries.*{ $.name: $sum($.count) }
~> $each(function($c, $n) {{ "name": $n, "count": $c }})
}
)
感謝@mralex 回答我的問題。 但不幸的是,它在最新的 JSON-API Grafana 模塊中使用的舊版本中不起作用。 所以我自己創建了另一個解決方案,如下所示:
(
record.(
$t1 := entries.preprod{ $.name: $sum($.count) };
$t2 := entries.prod{ $.name: $sum($.count) };
$t3 := $distinct($append($t1.$keys(),$t2.$keys()));
$t3.(
$t4 := ($exists($lookup($t1, $)) ? $lookup($t1, $) : 0) + ($exists($lookup($t2, $)) ? $lookup($t2, $): 0);
{
'name': $,
'count' : $t4
}
)
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.