Refactor historic returns calculator so the JSON response only makes one database query