I am working on a project that stores a little bit of analytics data. Everything is working oké but I am just facing one issue where I have to sort the contents of, for example: TotalsPerDay["2022-02-01"] on the amount of clicks.
I cannot figure out how to properly do this. The reason I chose for a map[string]struct{} is that I need the keys as dates and the values as structs so I can, somewhere else in the program, loop through all the records and sum up a total for that specific day.
dataModel := types.DataModel{
Totals: types.DataModelTotals{
Clicks: 0,
Impressions: 0,
Position: 0,
ClickThroughRate: 0,
TotalsPerDay: map[string]types.DataModelTotalsPerDay{},
},
Searches: map[string]types.Searches{},
Pages: map[string]types.Pages{},
Countries: map[string]types.Countries{},
Devices: map[string]types.Devices{},
SearchFilter: map[string]types.SearchFilter{},
Dates: map[string]types.Dates{},
}
What TotalsPerDay could look like:
TotalsPerDay {
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 17 },
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 9 },
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 82 }
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 52 }
}
What I would like it to be:
TotalsPerDay {
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 82 },
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 52 },
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 17 },
DataModelTotalsPerDay["2022-02-01"]{ Clicks: 9 }
}
The reason I have the keys as a date (string) is because in the front-end I need to loop through the dates to display them and this way it was very easy to do.
Hope someone can help me out with this as I have been stuck for this for the past few days.
I tried looking into various sort functions but none got me the result I wanted so far. I also read somewhere (not sure if this is true) that the elements of a map cannot be sorted? if that is true, how am I supposed to get my data summed up in a way so when send to the front-end they can simply loop over the dates and for each date easily get the amount of clicks and other statistics?
Below is a JSON version of TotalsPerDay.
{"2022-08-27 00:00:00 +0000 UTC":{"clicks":6,"impressions":7558,"position":280683.4432563555,"clickThroughRate":280669.4432563555,"totalRrows":5436},"2022-08-28 00:00:00 +0000 UTC":{"clicks":1,"impressions":8061,"position":289043.214145452,"clickThroughRate":288990.8808121187,"totalRrows":5665},"2022-08-29 00:0 0:00 +0000 UTC":{"clicks":8,"impressions":8283,"position":303046.8245871944,"clickThroughRate":302952.8245871944,"totalRrows":5905},"2022-08-30 00:00:00 +0000 UTC":{"clicks":4,"impressions":8447,"position":300142.1673121948,"clickThroughRate":300071.1673121948,"totalRrows":5904},"2022-08-31 00:00:00 +0000 UTC": {"clicks":7,"impressions":8114,"position":285296.87973927736,"clickThroughRate":285158.87973927736,"totalRrows":5648},"2022-09-01 00:00:00 +0000 UTC":{"clicks":6,"impressions":8306,"position":297513.4591694122,"clickThroughRate":297337.4591694122,"totalRrows":5932},"2022-09-02 00:00:00 +0000 UTC":{"clicks":4,"impressions":7938,"position":284144.3877642734,"clickThroughRate":284102.3877642734,"totalRrows":5590},"2022-09-03 00:00:00 +0000 UTC":{"clicks":0,"impressions":7024,"position":266604.6929695027,"clickThroughRate":266536.6929695027,"totalRrows":5205}}