I have a struct that represents a Purchase:
type Purchase struct {
id int64
UserId int64
CreatedAt time.Time
}
Now I have a collection of these purchases.
On the UI side I am doing this:
So it would look like:
Sunday May 29th
- date/timestamp purchase id, amount, etc.
- date/timestamp purchase id, amount, etc.
Saturday May 28th
- date/timestamp purchase id, amount, etc.
Friday May 27th
..
..
(past 2 weeks).
So my algorithm would be:
So the current date is:
t := time.Now()
How can I traverse the past 2 weeks and then insert it into a map.
Is there a better algorithm that you would suggest?
An approach may be to arrange the entries in time-series structures and use that to filter required entries within some interval. We may use a map like map[timestamp_bucket][]Purchase. time-stamp bucket may be calculated as (time_stamp_corresponding_to_purchase_date_in_second - (time_stamp_corresponding_to_purchase_date_in_second % 3600)). Basically, we arrange the purchases in hourly buckets. When we want to get last two weeks purchases, we just compute the time_bucket keys that fall in last two weeks and retrieve the purchases from the corresponding buckets in the map. We may keep the slice []Purchase sorted if required if we want to use arbitrary time intervals to filter the records.