Home > Blockchain >  Find all the subranges for a date range api request with capped max offset
Find all the subranges for a date range api request with capped max offset

Time:09-21

I've to extract data from an API that has a max offset (1000) and limit (20). So it's possible that between two dates (from and to) there are more results than the returned from the request.

I'd like to get the complete set of data between those two dates so I'm trying to come up with a solution to accomplish this.

My idea is to start by making a request like:

https://api.com/?from=2021-07-20&to=2021-07-24&limit=20&offset=1000

If the request returns less than 20 elements the search is over, I've all the data available between the ranges. But if the request returns 20 elements, then it's probable that there's more data in this range, so I have to find a way to keep splitting ranges until this condition is false.

I've thought about splitting the ranges like:

from = from
to_1 = 2021-07-22
from_1 = 2021-07-22
to = 2021-07-24

And then pass those ranges to the recursive function until finding all the needed subranges.

The output would be something like: [(2021-07-21,2021-07-22),(2021-07-22,2021-07-23),(2021-07-23, 2021-07-24)]

The problem with this solution is that I'm expanding the from's and to's so I can't manage to use a recursive function and I'm struggling about how to fix this issue.

Edit: I've added the "javascript" tag as the solution is going to be implemented in that language but ideas/pseudocode is welcome too.

CodePudding user response:

Seems like you're looking for binary search.

Your recursive function should look like this (it's JS pseudo-code):

function get_ranges(from, to)
{
    const items = request(from, to)
    if (items.length < 20) {
       return [[from, to]]
    }

   const date_between = calculate_date_between(from, to)
   const left_ranges = get_ranges(from, date_between)
   const right_ranges = get_ranges(date_between, to)

   return [...left_ranges, ...right_ranges]
}

const result = get_ranges(min_date, max_date)
  • Related