Problem statement : I have written a Kibana Vega script to show sum of latest balanceusd
field having a unique orgid
. I am getting balance logs frequently and every latest log has the updated balance. There can be a number of wallets in one organisation(orgid). Currently, it is showing whatever latest amount comes in balanceusd
field.
Expected output : a metric visualization that shows total net-worth of an organization. It means it should sum all the latest balanceusd
data across different wallets under an orgid
Vega script :
{
$schema: https://vega.github.io/schema/vega/v3.0.json
title:
{
"text": "Networth",
"fontSize": 30,
"dy" : 20
}
data: [
{
name: latestTotalBalance
url: {
%context%: true
%timefield%: @timestamp
index: wallet-*
body: {
aggs: {
label: {
terms: {
field: event.data.orgid
size: 10
order: {_key: "desc"}
}
aggs: {
balance: {
top_hits: {
_source: event.data.balanceusd
size: 10
sort: [
{
@timestamp: {order: "desc"}
}
]
}
}
}
}
}
size: 0
}
}
format: {property: "aggregations.label.buckets"}
transform: [
{
type: aggregate
ops: ["sum"]
fields: ["balance.hits.hits[0]._source.event.data.balanceusd"]
as: ["networth"]
}
]
}
]
marks: [
{
type: text
formatType: number
from: {data: "latestTotalBalance"}
encode: {
update: {
title: "Networth"
text: {signal: "format(datum.networth, ',')"}
align: {value: "center"}
baseline: {value: "middle"}
xc: {signal: "width/2"}
yc: {signal: "height/2"}
fontSize: {signal: "33"}
"fill": {"value": "white"}
"fontWeight":{"value": "bold"}
text: {signal: "format(datum.networth, ',.2f')"}
}
}
}
]
}