func (s *SDayCollect) Init(startdate epochdate.Date, enddate epochdate.Date) { if !s.isinit { panic("Not in init mode") } if enddate.Before(startdate) { panic("Startdate must be before enddate") } filldate := startdate for filldate.Before(enddate + 1) { s.EmptyDay(filldate) filldate += 1 } s.isinit = false }
// returns the first day from the date, depending on the group func (d Generator) FirstFromDate(date epochdate.Date) epochdate.Date { switch d.Group { case WEEK: // first date is the previous monday of the week wd := date.UTC().Weekday() if wd > d.FirstDayOfWeek { return date - epochdate.Date(wd) + epochdate.Date(d.FirstDayOfWeek) } else if wd < d.FirstDayOfWeek { return date - 6 + epochdate.Date(wd) } case MONTH: // first day of the month year, month, _ := date.Date() ret, _ := epochdate.NewFromDate(year, month, 1) return ret } return date }
// Sets values for a day. Empty periods are zeroed func (s *SDayCollect) ValueDay(date epochdate.Date, value map[string]interface{}) { switch s.Data { case "hour": for di := 0; di < 24; di++ { // don't add if in past if date.UTCTime(di, 0, 0, 0).After(time.Now().UTC()) { return } dy := make(map[string]interface{}) dy["date"] = date.String() dy["hour"] = di var fd map[string]interface{} if value != nil { hvalue := value["_hr"].(map[string]interface{}) hf, ok := hvalue[fmt.Sprintf("h_%d", di)] if ok { fd = hf.(map[string]interface{}) } } s.addImportData(dy, fd) s.setData(fmt.Sprintf("%s@%d", date.String(), di), dy) } case "minute": for di := 0; di < 24; di++ { var mvalue map[string]interface{} if value != nil { hvalue := value["_hr"].(map[string]interface{}) hf, ok := hvalue[fmt.Sprintf("h_%d", di)] if ok { mintf, mok := hf.(map[string]interface{})["mn"] if mok { mvalue = mintf.(map[string]interface{}) } } } // minutes in 15 minutes increments for mi := 0; mi < 4; mi++ { if date.UTCTime(di, mi*15, 0, 0).After(time.Now().UTC()) { return } dy := make(map[string]interface{}) dy["date"] = date.String() dy["hour"] = di dy["minute"] = mi * 15 var fd map[string]interface{} if mvalue != nil { mf, mfok := mvalue[fmt.Sprintf("m_%d", mi*15)] if mfok { fd = mf.(map[string]interface{}) } } s.addImportData(dy, fd) s.setData(fmt.Sprintf("%s@%d@%d", date.String(), di, mi*15), dy) } } default: dy := make(map[string]interface{}) dy["date"] = date.String() var fd map[string]interface{} if value != nil { fd = value["_dy"].(map[string]interface{}) } s.addImportData(dy, fd) s.setData(fmt.Sprintf("%s", date.String()), dy) } }