func (this *TimeFrame) parseThisNUnits(n int, units string) bool { n = n - 1 switch units { case "minutes": this.Start = now.BeginningOfMinute().Add(time.Duration(-n) * time.Minute) this.End = now.EndOfMinute() case "hours": this.Start = now.BeginningOfHour().Add(time.Duration(-n) * time.Hour) this.End = now.EndOfHour() case "days": this.Start = now.BeginningOfDay().AddDate(0, 0, -n) this.End = now.EndOfDay() case "weeks": this.Start = now.BeginningOfWeek().AddDate(0, 0, -n*7) this.End = now.EndOfWeek() case "months": this.Start = now.BeginningOfMonth().AddDate(0, -n, 0) this.End = now.EndOfMonth() case "years": this.Start = now.BeginningOfYear().AddDate(-n, 0, 0) this.End = now.EndOfYear() default: return false } return true }
/* date format 2015-01-23 */ func GetChartData(table, start, end string) (res []Chart) { startdate, err := now.Parse(start) if err != nil { return } enddate, err := now.Parse(end) if err != nil || enddate.UnixNano() < startdate.UnixNano() { enddate = now.EndOfDay() } else { enddate = enddate.AddDate(0, 0, 1) } db.DB.Table(table).Where("created_at > ? AND created_at < ?", startdate, enddate).Select("date(created_at) as date, count(*) as total").Group("date(created_at)").Order("date(created_at)").Scan(&res) return }
func createUsers() { totalCount := 600 for i := 0; i < totalCount; i++ { user := models.User{} user.Email = fake.Email() user.Name = fake.Name() user.Gender = []string{"Female", "Male"}[i%2] if err := db.DB.Create(&user).Error; err != nil { log.Fatalf("create user (%v) failure, got err %v", user, err) } day := (-14 + i/45) user.CreatedAt = now.EndOfDay().Add(time.Duration(day*rand.Intn(24)) * time.Hour) if user.CreatedAt.After(time.Now()) { user.CreatedAt = time.Now() } if err := db.DB.Save(&user).Error; err != nil { log.Fatalf("Save user (%v) failure, got err %v", user, err) } } }