func (this *SendEmailToGetBackPasswordController) Get() { username := this.GetString("username") if "" == username { this.Data["json"] = map[string]interface{}{"result": false, "msg": "username could not be empty", "refer": "/"} this.ServeJson() return } time := time.Now() code := com.Md5(com.RandString(20) + time.String()) err := AddVerify(username, code, time) if nil != err { this.Data["json"] = map[string]interface{}{"result": false, "msg": "create varify failed", "refer": "/"} this.ServeJson() } else { host := beego.AppConfig.String("host") subject := "blog system get your password back" body := `click the following link to get your password back <font color="red"><a href="` + host + `/password/reset/` + code + `">` + host + `/password/reset/` + code + `</a></font>` currentUser, _ := FindUser(username) email := currentUser.Email err := utils.SendMail(email, subject, body) if nil != err { this.Data["json"] = map[string]interface{}{"result": false, "msg": "send mail failed", "refer": "/"} this.ServeJson() } else { this.Data["json"] = map[string]interface{}{"result": true, "msg": "create varify success", "refer": "/"} this.ServeJson() } } }
// Returns cpu and memory usage percentiles. func GetPercentiles(stats []*info.ContainerStats) (Percentiles, Percentiles) { lastCpu := uint64(0) lastTime := time.Time{} memorySamples := make(uint64Slice, 0, len(stats)) cpuSamples := make(uint64Slice, 0, len(stats)-1) numSamples := 0 memoryMean := Mean{count: 0, Mean: 0} cpuMean := Mean{count: 0, Mean: 0} memoryPercentiles := Percentiles{} cpuPercentiles := Percentiles{} for _, stat := range stats { var elapsed int64 time := stat.Timestamp if !lastTime.IsZero() { elapsed = time.UnixNano() - lastTime.UnixNano() if elapsed < 10*milliSecondsToNanoSeconds { glog.Infof("Elapsed time too small: %d ns: time now %s last %s", elapsed, time.String(), lastTime.String()) continue } } numSamples++ cpuNs := stat.Cpu.Usage.Total // Ignore actual usage and only focus on working set. memory := stat.Memory.WorkingSet if memory > memoryPercentiles.Max { memoryPercentiles.Max = memory } glog.V(2).Infof("Read sample: cpu %d, memory %d", cpuNs, memory) memoryMean.Add(memory) memorySamples = append(memorySamples, memory) if lastTime.IsZero() { lastCpu = cpuNs lastTime = time continue } cpuRate := (cpuNs - lastCpu) * secondsToMilliSeconds / uint64(elapsed) if cpuRate < 0 { glog.Infof("cpu rate too small: %f ns", cpuRate) continue } glog.V(2).Infof("Adding cpu rate sample : %d", cpuRate) lastCpu = cpuNs lastTime = time cpuSamples = append(cpuSamples, cpuRate) if cpuRate > cpuPercentiles.Max { cpuPercentiles.Max = cpuRate } cpuMean.Add(cpuRate) } cpuPercentiles.Mean = uint64(cpuMean.Mean) memoryPercentiles.Mean = uint64(memoryMean.Mean) cpuPercentiles.Ninety = cpuSamples.Get90Percentile() memoryPercentiles.Ninety = memorySamples.Get90Percentile() return cpuPercentiles, memoryPercentiles }
func TestParseTime(t *testing.T) { for given, expected := range parseTable { time, err := toki.ParseTime(given) if err != nil { t.Errorf("ParseTime: %s → expected %s, got error: %v", given, expected, time) } result := time.String() if result != expected { t.Errorf("ParseTime: %s → expected %s, got: %s", given, expected, result) } } _, err := toki.ParseTime("12:abcdef") if err == nil { t.Errorf("ParseTime: expected error, got: nil") } }
func TestMustParseTime(t *testing.T) { panicked := false defer func() { if r := recover(); r != nil { panicked = true } if !panicked { t.Error("MustParseTime: didn't panic") } }() for given, expected := range parseTable { time := toki.MustParseTime(given) result := time.String() if result != expected { t.Errorf("MustParseTime: %s → expected %s, got: %s", given, expected, result) } } toki.MustParseTime("invalid input") }
func (c Hello) HelloNotFound() revel.Result { time := time.Now() msg := "Error Message" return c.NotFound("Hello NotFound!, %s, [%s]", msg, time.String()) }
func getHour(t time.Time) string { time := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, time.UTC) return time.String() }