func NewMonth(n time.Month, y, p int) (m *month) { m = &month{year: y, number: n, payday: p} t := time.Date(y, n, 1, 0, 0, 0, 0, time.Local) for { m.days = append(m.days, t) t = time.Date(y, n, t.Day()+1, 0, 0, 0, 0, time.Local) if t.Month().String() != n.String() { break } } return }
func printCal(month time.Month, year int) { dayw := "Su Mo Tu We Th Fr Sa" s := month.String() + " " + strconv.FormatInt(int64(year), 10) off := (20 - len(s)) / 2 for i := 0; i < off; i++ { s = " " + s } s += "\n" + dayw + "\n" mth := months(year) day := januaryFirst(year) for i := 1; i < int(month); i++ { day += mth[i-1] } for i := 0; i < day%7; i++ { s = s + " " } for i := 1; i <= mth[month-1]; i++ { s = s + " " if i < 10 { s = s + " " } s = s + strconv.Itoa(i) day += 1 if day%7 == 0 { s = s + "\n" } } s = s + "\n\n" fmt.Print(s) }
// List all the days of a given month func datebonanza(cal calendar.Calendar, year int, month time.Month) { fmt.Println(month.String(), year) fmt.Println("====================") current := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC) // As long as we are in the same month for current.Month() == month { if red, desc, flag := cal.RedDay(current); red { fmt.Printf("%s is red: %s (flag: %v)\n", current.String()[:10], desc, flag) } if notable, desc, flag := cal.NotableDay(current); notable { fmt.Printf("%s is notable: %s (flag: %v)\n", current.String()[:10], desc, flag) } // Advance to the next day current = current.AddDate(0, 0, 1) } fmt.Println() }
func buildMonth(month time.Month, year int) *Month { log.Println("building month", month) n := time.Now().Local() m := Month{} m.Name = month.String() days := time.Date(year, month+1, 0, 0, 0, 0, 0, location).Day() start := time.Date(year, month, 1, 0, 0, 0, 0, location) m.StartPos = int(start.Weekday()) for i := 1; i <= days; i++ { d := time.Date(year, month, i, 0, 0, 0, 0, location) day := Day{ d, i, int(d.Weekday()), d.YearDay(), n.YearDay() > d.YearDay(), []meetup.Event{}, } m.Days = append(m.Days, &day) } return &m }
//Tss vs Duration func tvd(user types.UserSettings, filter Filter) ([]Tvd, string) { user_id := user.Id tvd_data_points := make([]Tvd_data_point, 0) tvd_data := make([]Tvd, 0) var user_data types.Metrics var end_summary_json []byte var activity_start time.Time var tvdLegend string cluster := gocql.NewCluster(config.DbHost) cluster.Keyspace = "joulepersecond" cluster.Consistency = gocql.Quorum session, _ := cluster.CreateSession() defer session.Close() //get all of the user's data (at least all for now) TODO limit these queries by date if poss. Done! timeNow := time.Now() timeThen := timeNow.AddDate(0, 0, -filter.Historylen) iter := session.Query(`SELECT activity_start, end_summary_json FROM joulepersecond.user_activity WHERE user_id = ? AND activity_start > ? ORDER BY activity_start ASC`, user_id, timeThen).Iter() for iter.Scan(&activity_start, &end_summary_json) { var tvd_data_point Tvd_data_point json.Unmarshal(end_summary_json, &user_data) tvd_data_point.Date = user_data.StartTime tvd_data_point.Dur = user_data.Dur if user_data.Utss > 0 { tvd_data_point.Tss = user_data.Utss } else if user_data.Tss > 0 { tvd_data_point.Tss = user_data.Tss } else if user_data.Etss > 0 { tvd_data_point.Tss = user_data.Etss } else { tvd_data_point.Tss = 0 } tvd_data_points = append(tvd_data_points, tvd_data_point) } //we now have all the data... Now sort it sumTss := 0 var sumDur time.Duration var lastActivity time.Time //loope through each retrieved activity for i := 1; i < len(tvd_data_points); i++ { //set last activity on first iteration only if i == 1 { lastActivity = tvd_data_points[i].Date } //if we want to show data in monthly format if filter.Historylen > 366 { //show over 365 days as monthly thisDate := tvd_data_points[i].Date prevDate := lastActivity //if we're still in the current month sum these values if thisDate.Month() != prevDate.Month() || i == len(tvd_data_points)-1 { var summedMonthlyTvd Tvd summedMonthlyTvd.TotalTss = sumTss summedMonthlyTvd.TotalDur = utility.Round(sumDur.Hours(), .5, 2) var month time.Month var year string if thisDate.Month() != prevDate.Month() { month = prevDate.Month() year = strconv.Itoa(prevDate.Year()) } else { month = thisDate.Month() year = strconv.Itoa(thisDate.Year()) } monthStr := month.String() summedMonthlyTvd.TimeLabel = monthStr[0:3] + " '" + year[2:4] sumTss = 0 sumDur = 0 tvd_data = append(tvd_data, summedMonthlyTvd) //reset the last activity date for the next loop lastActivity = thisDate } sumTss += tvd_data_points[i].Tss sumDur += tvd_data_points[i].Dur tvdLegend = "By Month" } else { thisDate := tvd_data_points[i].Date //we use this to compare the activity being scanned with the last to see if it is in the same week prevDate := lastActivity //this is the last activity that we scanned that was the first of the new week, last week. Confusing init? we have to get the value now, and change if the weeks are not equal (new week) prevIterDate := tvd_data_points[i-1].Date //get week number for this and last activity _, thisDateWeek := thisDate.ISOWeek() prevDateYear, prevDateWeek := prevDate.ISOWeek() if thisDateWeek != prevDateWeek || i == len(tvd_data_points)-1 { var summedWeeklyTvd Tvd summedWeeklyTvd.TotalTss = sumTss summedWeeklyTvd.TotalDur = utility.Round(sumDur.Hours(), .5, 2) monthS := prevDate.Month() var monthF time.Month var dayF string if thisDateWeek != prevDateWeek { monthF = prevIterDate.Month() dayF = strconv.Itoa(prevIterDate.Day()) } else { monthF = thisDate.Month() dayF = strconv.Itoa(thisDate.Day()) } dayS := strconv.Itoa(prevDate.Day()) monthStrS := monthS.String() monthAbrS := monthStrS[0:3] monthStrF := monthF.String() monthAbrF := monthStrF[0:3] //format labels according to number to displau if filter.Historylen < 120 { summedWeeklyTvd.TimeLabel = dayS + " " + monthAbrS + " - " + dayF + " " + monthAbrF } else { summedWeeklyTvd.TimeLabel = dayS + " " + monthAbrS } sumTss = 0 sumDur = 0 tvd_data = append(tvd_data, summedWeeklyTvd) //reset the last activity date for the next loop lastActivity = thisDate } //sum the values sumTss += tvd_data_points[i].Tss sumDur += tvd_data_points[i].Dur tvdLegend = "By week number: Series ending wk" + strconv.Itoa(prevDateWeek) + ", " + strconv.Itoa(prevDateYear) } } return tvd_data, tvdLegend }
//heart/Power by zone func hpbz(user types.UserSettings, filter Filter) ([]Hbz, []Pbz) { user_id := user.Id var user_data types.Metrics var end_summary_json []byte var heart_json []byte var power_json []byte var cur_ftp int var cur_thr int var power_series []int var heart_series []int var has_power, has_heart bool var activity_id string var activity_start time.Time hbz_data := make([]Hbz, 0) pbz_data := make([]Pbz, 0) var temp_row Hpbz temp_rows := make([]Hpbz, 0) cluster := gocql.NewCluster(config.DbHost) cluster.Keyspace = "joulepersecond" cluster.Consistency = gocql.Quorum session, _ := cluster.CreateSession() defer session.Close() var sH1, sH2, sH3, sH4, sH5a, sH5b, sH5c, sP1, sP2, sP3, sP4, sP5, sP6 int timeNow := time.Now() timeThen := timeNow.AddDate(0, 0, -filter.Historylen) //get all of the user's data (at least all for now) TODO limit these queries by date if poss. iter := session.Query(`SELECT activity_start, activity_id FROM joulepersecond.user_activity WHERE user_id = ? AND activity_start > ? ORDER BY activity_start ASC`, user_id, timeThen).Iter() for iter.Scan(&activity_start, &activity_id) { iter := session.Query(`SELECT power_json, heart_json, end_summary_json, has_power, has_heart, cur_ftp, cur_thr FROM joulepersecond.proc_activity WHERE activity_id = ? `, activity_id).Iter() for iter.Scan(&power_json, &heart_json, &end_summary_json, &has_power, &has_heart, &cur_ftp, &cur_thr) { json.Unmarshal(end_summary_json, &user_data) json.Unmarshal(power_json, &power_series) json.Unmarshal(heart_json, &heart_series) temp_row.StartTime = activity_start //TODO next: Split all time series data in to zones and add it to temp_row/(s) for further date processing if has_power { temp_row.Samples = len(power_series) temp_row.Has_power = true } if has_heart { temp_row.Samples = len(heart_series) temp_row.Has_heart = true } if !has_heart && !has_power { break } //clear the values temp_row.CountPZ1 = 0 temp_row.CountPZ2 = 0 temp_row.CountPZ3 = 0 temp_row.CountPZ4 = 0 temp_row.CountPZ5 = 0 temp_row.CountPZ6 = 0 temp_row.CountHZ1 = 0 temp_row.CountHZ2 = 0 temp_row.CountHZ3 = 0 temp_row.CountHZ4 = 0 temp_row.CountHZ5a = 0 temp_row.CountHZ5b = 0 temp_row.CountHZ5c = 0 if has_power { var sum int var average float64 for i := user.SampleSize; i < temp_row.Samples; i++ { //reset total sum = 0 //get thirty second rolling slice rollingPowerSlice := power_series[i-user.SampleSize : i] for _, val := range rollingPowerSlice { //sum the sliding slice values sum += val } average = float64(sum / user.SampleSize) if average < 0.55*float64(cur_ftp) { temp_row.CountPZ1++ } else if average > 0.55*float64(cur_ftp) && average <= 0.74*float64(cur_ftp) { temp_row.CountPZ2++ } else if average > 0.74*float64(cur_ftp) && average <= 0.89*float64(cur_ftp) { temp_row.CountPZ3++ } else if average > 0.89*float64(cur_ftp) && average <= 1.04*float64(cur_ftp) { temp_row.CountPZ4++ } else if average > 1.04*float64(cur_ftp) && average <= 1.2*float64(cur_ftp) { temp_row.CountPZ5++ } else if average > 1.2*float64(cur_ftp) { temp_row.CountPZ6++ } } } //loop through each sample and post the value into the correct pidgeon hole for i := 0; i < temp_row.Samples; i++ { if has_heart { if float64(heart_series[i]) < 0.81*float64(cur_thr) { temp_row.CountHZ1++ } else if float64(heart_series[i]) > 0.81*float64(cur_thr) && float64(heart_series[i]) <= 0.89*float64(cur_thr) { temp_row.CountHZ2++ } else if float64(heart_series[i]) > 0.89*float64(cur_thr) && float64(heart_series[i]) <= 0.93*float64(cur_thr) { temp_row.CountHZ3++ } else if float64(heart_series[i]) > 0.93*float64(cur_thr) && float64(heart_series[i]) <= 0.99*float64(cur_thr) { temp_row.CountHZ4++ } else if float64(heart_series[i]) > 0.99*float64(cur_thr) && float64(heart_series[i]) <= 1.02*float64(cur_thr) { temp_row.CountHZ5a++ } else if float64(heart_series[i]) > 1.02*float64(cur_thr) && float64(heart_series[i]) <= 1.06*float64(cur_thr) { temp_row.CountHZ5b++ } else if float64(heart_series[i]) > 1.06*float64(cur_thr) { temp_row.CountHZ5c++ } } } temp_rows = append(temp_rows, temp_row) } } clearVals := func() { sH1 = 0 sH2 = 0 sH3 = 0 sH4 = 0 sH5a = 0 sH5b = 0 sH5c = 0 sP1 = 0 sP2 = 0 sP3 = 0 sP4 = 0 sP5 = 0 sP6 = 0 } //so now for each activity we have the sum of each of the zones (value for each second * number seconds) and the number of seconds to divide by later once summed by date //loope through each retrieved activity var lastActivity time.Time var numResult int for i := 1; i < len(temp_rows); i++ { if i == 1 { lastActivity = temp_rows[i].StartTime } //if we want to show data in monthly format if filter.Historylen > 366 { //show over 365 days as monthly //set last activity on first iteration only thisDate := temp_rows[i].StartTime prevDate := lastActivity //if we're still in the current month sum these values if thisDate.Month() != prevDate.Month() || i == len(temp_rows)-1 { var summedMonthlyHbz Hbz var summedMonthlyPbz Pbz summedMonthlyHbz.AvZ1 = utility.Round((float64(sH1) / 3600.0), .5, 2) summedMonthlyHbz.AvZ2 = utility.Round((float64(sH2) / 3600.0), .5, 2) summedMonthlyHbz.AvZ3 = utility.Round((float64(sH3) / 3600.0), .5, 2) summedMonthlyHbz.AvZ4 = utility.Round((float64(sH4) / 3600.0), .5, 2) summedMonthlyHbz.AvZ5a = utility.Round((float64(sH5a) / 3600.0), .5, 2) summedMonthlyHbz.AvZ5b = utility.Round((float64(sH5b) / 3600.0), .5, 2) summedMonthlyHbz.AvZ5c = utility.Round((float64(sH5c) / 3600.0), .5, 2) summedMonthlyPbz.AvZ1 = utility.Round((float64(sP1) / 3600.0), .5, 2) summedMonthlyPbz.AvZ2 = utility.Round((float64(sP2) / 3600.0), .5, 2) summedMonthlyPbz.AvZ3 = utility.Round((float64(sP3) / 3600.0), .5, 2) summedMonthlyPbz.AvZ4 = utility.Round((float64(sP4) / 3600.0), .5, 2) summedMonthlyPbz.AvZ5 = utility.Round((float64(sP5) / 3600.0), .5, 2) summedMonthlyPbz.AvZ6 = utility.Round((float64(sP6) / 3600.0), .5, 2) var month time.Month var year string if thisDate.Month() != prevDate.Month() { month = prevDate.Month() year = strconv.Itoa(prevDate.Year()) } else { month = thisDate.Month() year = strconv.Itoa(thisDate.Year()) } monthStr := month.String() summedMonthlyHbz.TimeLabel = monthStr[0:3] + " '" + year[2:4] summedMonthlyPbz.TimeLabel = monthStr[0:3] + " '" + year[2:4] clearVals() hbz_data = append(hbz_data, summedMonthlyHbz) pbz_data = append(pbz_data, summedMonthlyPbz) //reset the last activity date for the next loop lastActivity = thisDate } } else { thisDate := temp_rows[i].StartTime prevDate := lastActivity prevIterDate := temp_rows[i-1].StartTime _, thisDateWeek := thisDate.ISOWeek() //adding day keeps i in the correct week _, prevDateWeek := prevDate.ISOWeek() // " if thisDateWeek != prevDateWeek || i == len(temp_rows)-1 { //if new week or last activity var summedWeeklyHbz Hbz var summedWeeklyPbz Pbz numResult++ summedWeeklyHbz.AvZ1 = utility.Round((float64(sH1) / 3600.0), .5, 2) summedWeeklyHbz.AvZ2 = utility.Round((float64(sH2) / 3600.0), .5, 2) summedWeeklyHbz.AvZ3 = utility.Round((float64(sH3) / 3600.0), .5, 2) summedWeeklyHbz.AvZ4 = utility.Round((float64(sH4) / 3600.0), .5, 2) summedWeeklyHbz.AvZ5a = utility.Round((float64(sH5a) / 3600.0), .5, 2) summedWeeklyHbz.AvZ5b = utility.Round((float64(sH5b) / 3600.0), .5, 2) summedWeeklyHbz.AvZ5c = utility.Round((float64(sH5c) / 3600.0), .5, 2) summedWeeklyPbz.AvZ1 = utility.Round((float64(sP1) / 3600.0), .5, 2) summedWeeklyPbz.AvZ2 = utility.Round((float64(sP2) / 3600.0), .5, 2) summedWeeklyPbz.AvZ3 = utility.Round((float64(sP3) / 3600.0), .5, 2) summedWeeklyPbz.AvZ4 = utility.Round((float64(sP4) / 3600.0), .5, 2) summedWeeklyPbz.AvZ5 = utility.Round((float64(sP5) / 3600.0), .5, 2) summedWeeklyPbz.AvZ6 = utility.Round((float64(sP6) / 3600.0), .5, 2) monthS := prevDate.Month() dayS := strconv.Itoa(prevDate.Day()) var dayF string var monthF time.Month if thisDateWeek != prevDateWeek { monthF = prevIterDate.Month() dayF = strconv.Itoa(prevIterDate.Day()) } else { monthF = thisDate.Month() dayF = strconv.Itoa(thisDate.Day()) } monthStrS := monthS.String() monthAbrS := monthStrS[0:3] monthStrF := monthF.String() monthAbrF := monthStrF[0:3] //format labels according to number to displau if filter.Historylen < 120 { summedWeeklyHbz.TimeLabel = dayS + " " + monthAbrS + " - " + dayF + " " + monthAbrF summedWeeklyPbz.TimeLabel = dayS + " " + monthAbrS + " - " + dayF + " " + monthAbrF } else { summedWeeklyHbz.TimeLabel = dayS + " " + monthAbrS summedWeeklyPbz.TimeLabel = dayS + " " + monthAbrS } clearVals() hbz_data = append(hbz_data, summedWeeklyHbz) pbz_data = append(pbz_data, summedWeeklyPbz) //reset the last activity date for the next loop lastActivity = thisDate } } sP1 += temp_rows[i].CountPZ1 sP2 += temp_rows[i].CountPZ2 sP3 += temp_rows[i].CountPZ3 sP4 += temp_rows[i].CountPZ4 sP5 += temp_rows[i].CountPZ5 sP6 += temp_rows[i].CountPZ6 sH1 += temp_rows[i].CountHZ1 sH2 += temp_rows[i].CountHZ2 sH3 += temp_rows[i].CountHZ3 sH4 += temp_rows[i].CountHZ4 sH5a += temp_rows[i].CountHZ5a sH5b += temp_rows[i].CountHZ5b sH5c += temp_rows[i].CountHZ5c } return hbz_data, pbz_data }
// Get demands by adspace id func (c *PmpDemandPlatformDeskController) GetDemandByAdspace() { adspaceId := c.GetString("adspaceid") date := c.GetString("startdate") usetpl, err := c.GetBool("usetpl") beego.Info(" **** param adspaceid: " + adspaceId + " *** param startdate: " + date + " **** param usetpl: " + strconv.FormatBool(usetpl)) if err != nil { usetpl = false } c.Data["maingridrowid"] = adspaceId c.Data["startdate"] = date if usetpl { //c.Data["json"] = &map[string]interface{}{"total": 2, "rows": []DemandVo{}} if c.GetTemplatetype() != "easyui" { c.Layout = c.GetTemplatetype() + "/public/layout.tpl" } c.TplNames = c.GetTemplatetype() + "/adspace/demand-easyui.tpl" return } const layout = "2006-1-2" startdate, _ := time.Parse(layout, date) startdate = startdate.Local() enddate := startdate.AddDate(0, 0, 6) dateend := enddate.Format(layout) beego.Info(" **** startdate:"+startdate.Format(layout), " **** enddate:", dateend) var dailyAllocations []models.PmpDailyAllocationVo adspaceIdInt, _ := strconv.Atoi(adspaceId) dailyAllocations = models.GetPmpDailyAllocationByAdspaceIdAndAdDate(adspaceIdInt, date, dateend) var demandVos []DemandVo var days [7]string var y, d int var m time.Month y, m, d = startdate.Date() days[0] = strconv.Itoa(y) + "-" + m.String() + "-" + strconv.Itoa(d) for i := 1; i < 7; i++ { startdate = startdate.AddDate(0, 0, 1) y, m, d = startdate.Date() days[i] = strconv.Itoa(y) + "-" + m.String() + "-" + strconv.Itoa(d) } var lastdemandadspaceid int = -1 for _, v := range dailyAllocations { if lastdemandadspaceid != v.DemandAdspaceId { demandVos = append(demandVos, DemandVo{Name: v.Name, DemandAdspaceId: v.DemandAdspaceId, Proportion: v.Priority, DemandAdspaceName: v.DemandAdspaceName}) lastdemandadspaceid = v.DemandAdspaceId } y, m, d := v.AdDate.Date() addate := strconv.Itoa(y) + "-" + m.String() + "-" + strconv.Itoa(d) for index, val := range days { var allocation int currIndex := len(demandVos) - 1 if val == addate { allocation = v.Imp switch index { case 0: demandVos[currIndex].Day1 = allocation case 1: demandVos[currIndex].Day2 = allocation case 2: demandVos[currIndex].Day3 = allocation case 3: demandVos[currIndex].Day4 = allocation case 4: demandVos[currIndex].Day5 = allocation case 5: demandVos[currIndex].Day6 = allocation case 6: demandVos[currIndex].Day7 = allocation } } } } beego.Info("**** demandVos:", demandVos) c.Data["json"] = &map[string]interface{}{"total": len(demandVos), "rows": &demandVos} c.ServeJson() }
// Finds the US name for a given month func (nc USCalendar) MonthName(month time.Month) string { return month.String() }
//template functions func PrettyMonth(m time.Month) string { return m.String()[0:3] + "." }