2024-12-19 23:36:50 +01:00
|
|
|
# Calculate the change in percentage of a numeric key
|
2024-12-17 23:15:39 +01:00
|
|
|
#
|
|
|
|
# Where .[0] is the new value, and .[1] is the old value
|
|
|
|
def calc_change($key):
|
2024-12-22 13:51:51 +01:00
|
|
|
. as $input | ($input[0][$key] // 0) - ($input[1][$key] // 0);
|
2024-12-17 23:15:39 +01:00
|
|
|
|
|
|
|
# Calculate the change in percentage for multiple keys
|
|
|
|
#
|
|
|
|
# Returns an array of {"key": $key, "value": change%}
|
|
|
|
def keys_change_perc($keys):
|
|
|
|
. as $input | $keys | map(. as $key | $input | {"key": $key, "value": . | calc_change($key)});
|
|
|
|
|
|
|
|
# For a set of keys, calculate the change percentage and return it as
|
|
|
|
# "\($key)Change" = $value in the original object.
|
|
|
|
def set_change_perc($keys):
|
|
|
|
. as $input | reduce keys_change_perc($keys)[] as $change ($input[0]; .["\($change.key)Change"] = $change.value);
|
|
|
|
|
|
|
|
# Bring everything together
|
|
|
|
reduce .[] as $x ({}; . as $acc | reduce ($x | keys[]) as $key ($acc; .[$key] |= . + $x[$key])) |
|
|
|
|
map_values(group_by(.name) | map(set_change_perc(["size", "narSize"])))
|