I really think all this logic can be replace by using Chinmay's re-roll tool which works out of band and computes (partial) rollups at all granularities. We can then return the data at appropriate granularity. Another PR is fine but just wanted to get that thought out.