($c[0] | map({key: (.epoch|tostring), value: .}) | from_entries) as $cb | [ $a[0][] | select(.delegator_apy_inflation_pct != null and .duration_secs != null and ($cb[.epoch|tostring] // {} | .delegator_apy_infl_p90_pct) != null and ($cb[.epoch|tostring].delegator_apy_infl_max_pct) != null) ] | sort_by(.epoch) | reverse as $rows | [ (30, 90, 180, 365) as $d | reduce $rows[] as $r ({S:0,o:0,p90:0,mx:0,p50:0,n:0,through:null}; if .S >= ($d*86400) then . else ($cb[$r.epoch|tostring]) as $k | .through = (.through // $r.epoch) | .o += $r.duration_secs * ((1 + $r.delegator_apy_inflation_pct/100) | log) | .p90 += $r.duration_secs * ((1 + $k.delegator_apy_infl_p90_pct/100) | log) | .mx += $r.duration_secs * ((1 + $k.delegator_apy_infl_max_pct/100) | log) | .p50 += $r.duration_secs * ((1 + $k.delegator_apy_infl_p50_pct/100) | log) | .S += $r.duration_secs | .n += 1 end) | def apy(x): ((x / .S) | exp) - 1 | . * 100; { window_days: $d, closed_epochs: .n, through_epoch: .through, days: (.S/86400*10|round/10), pct_of_theoretical_max: ((apy(.o) / apy(.mx)) * 100 * 1000 | round / 1000), above_p90_bp: ((apy(.o) - apy(.p90)) * 100 * 1000 | round / 1000), above_median_bp: ((apy(.o) - apy(.p50)) * 100 * 10 | round / 10) } ]