Last week now is 0 for me because last week is actually derived from the data of last month and this is totally wrong:
// Last Week (monday - sunday)
year, week := time.Now().ISOWeek()
week = week - 1
// Find Last Monday
start, _ := utils.DateRange(week, year)
mon := start.Day()
sun := mon + 6
// Last Month (all)
lastMonth := time.Now().AddDate(0, -1, 0)
lastMonthData, err := api.PlantDetail(id, string(utils.Month), lastMonth)
if err != nil {
return prevData, err
}
// Go over data map and fill both counters
lastWeekEnergy := 0.0
lastMonthEnergy := 0.0
for d, e := range lastMonthData.Back.Data {
dInt, err := strconv.Atoi(d)
if err != nil {
return prevData, err
}
eFloat, err := strconv.ParseFloat(e, 32)
if err != nil {
return prevData, err
}
// Conditonally calc to Week Energy
for i := mon; i <= sun; i++ {
if dInt == i {
lastWeekEnergy = lastWeekEnergy + eFloat
}
}
// Calc to total Energy
lastMonthEnergy = lastWeekEnergy + eFloat
}
Also this code does not take into account that a week from Monday to Sunday might leap over the change of a month. So if Monday is 28th of March so Sunday is 3rd of April. I need to take this into account as well.
In general lastWeek info has to be derived from current Month data. Then it has to be checked if lastWeek Sunday - 6 is less than 1 in the current month. If so it has to be calculated how much less and then last month data and this month data has to be combined accordingly to calculate the "real" last week.