// @Title Get All // @Description get FaultData // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." // @Param fields query string false "Fields returned. e.g. col1,col2 ..." // @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..." // @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..." // @Param limit query string false "Limit the size of result set. Must be an integer" // @Param offset query string false "Start position of result set. Must be an integer" // @Success 200 {object} models.FaultData // @Failure 403 // @router / [get] func (c *FaultDataController) GetAll() { var fields []string var sortby []string var order []string var query map[string]string = make(map[string]string) var limit int64 = 10 var offset int64 = 0 var result models.Result // fields: col1,col2,entity.col3 if v := c.GetString("fields"); v != "" { fields = strings.Split(v, ",") } // limit: 10 (default is 10) if v, err := c.GetInt64("limit"); err == nil { limit = v } // offset: 0 (default is 0) if v, err := c.GetInt64("offset"); err == nil { offset = v } // sortby: col1,col2 if v := c.GetString("sortby"); v != "" { sortby = strings.Split(v, ",") } // order: desc,asc if v := c.GetString("order"); v != "" { order = strings.Split(v, ",") } // query: k:v,k:v if v := c.GetString("query"); v != "" { for _, cond := range strings.Split(v, ",") { kv := strings.Split(cond, ":") if len(kv) != 2 { result.Success = false result.Data = errors.New("Error: invalid query key/value pair") c.Data["json"] = result c.Abort("400") } k, v := kv[0], kv[1] query[k] = v } } l, err := models.GetAllFaultData(query, fields, sortby, order, offset, limit) if err != nil { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Abort("400") } else { result.Success = true result.Data = l c.Data["json"] = result } c.ServeJson() }
// @Title Get Error Time detail // @Description get a period of time error detail // @Param starttime query string "service starttime" // @Param endtime query string "service endtime" // @Param ns query string "ns name" // @Success 200 {object} models.FaultData // @Failure 403 body is empty // @router /errtime/detail/ func (c *FaultDataController) GetErrDetail() { var starttime int64 var endtime int64 var ns string var detail interface{} var err error var result models.Result //获取ns ns = c.GetString("ns") nslist := strings.Split(ns, ".") if nslist[len(nslist)-1] != "com" { result.Success = false result.Data = "ns is not correct! len is too short or .com should be at the end of ns" c.Data["json"] = result c.Abort("400") } now := time.Now() days := time.Date(now.Year(), now.Month()+1, 0, 0, 0, 0, 0, time.UTC).Day() next_time := now.AddDate(0, 0, days) start_month := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local) end_month := time.Date(next_time.Year(), next_time.Month(), 1, 0, 0, 0, 0, time.Local) //获取starttime starttime, err = c.GetInt64("starttime") if err != nil { //如果为空,设置初始值为当月第一天 starttime = start_month.Unix() } //获取endtime endtime, err = c.GetInt64("endtime") if err != nil { //如果为空,设置截止时间为当前时间 endtime = end_month.Unix() } //获取故障详情 detail, err = models.GetErrTimeDetail(starttime, endtime, ns) if err != nil { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Abort("400") } else { result.Success = true result.Data = detail c.Data["json"] = result } c.ServeJson() }
// @Title Post // @Description create FaultData // @Param body body models.FaultData true "body for FaultData content" // @Success 200 {int} models.FaultData.Id // @Failure 403 body is empty // @router / [post] func (c *FaultDataController) Post() { var result models.Result var v models.FaultData json.Unmarshal(c.Ctx.Input.RequestBody, &v) if id, err := models.AddFaultData(&v); err == nil { result.Success = true result.Data = map[string]int64{"id": id} c.Data["json"] = result } else { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Abort("400") } c.ServeJson() }
// @Title Get // @Description get FaultData by id // @Param id path string true "The key for staticblock" // @Success 200 {object} models.FaultData // @Failure 403 :id is empty // @router /:id [get] func (c *FaultDataController) GetOne() { var result models.Result idStr := c.Ctx.Input.Params[":id"] id, _ := strconv.Atoi(idStr) v, err := models.GetFaultDataById(id) if err != nil { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Abort("400") } else { result.Success = true result.Data = v c.Data["json"] = result } c.ServeJson() }
// @Title Delete // @Description delete the FaultData // @Param id path string true "The id you want to delete" // @Success 200 {string} delete success! // @Failure 403 id is empty // @router /:id [delete] func (c *FaultDataController) Delete() { var result models.Result idStr := c.Ctx.Input.Params[":id"] id, _ := strconv.Atoi(idStr) if err := models.DeleteFaultData(id); err == nil { result.Success = true result.Data = "ok" c.Data["json"] = result } else { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Abort("400") } c.ServeJson() }
// @Title Update // @Description update the FaultData // @Param id path string true "The id you want to update" // @Param body body models.FaultData true "body for FaultData content" // @Success 200 {object} models.FaultData // @Failure 403 :id is not int // @router /:id [put] func (c *FaultDataController) Put() { var result models.Result idStr := c.Ctx.Input.Params[":id"] id, _ := strconv.Atoi(idStr) v := models.FaultData{Id: id} json.Unmarshal(c.Ctx.Input.RequestBody, &v) if err := models.UpdateFaultDataById(&v); err == nil { result.Success = true result.Data = "ok" c.Data["json"] = result } else { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Abort("400") } c.ServeJson() }
// @Title Get Error Time Percent // @Description get rangetime to caculate errtime percent // @Param starttime query string "service starttime" // @Param endtime query string "service endtime" // @Param ns query string "ns name" // @Success 200 {object} models.FaultData // @Failure 403 body is empty // @router / func (c *DownController) GetErrPercent() { var starttime int64 var endtime int64 var ns string var err error var result models.Result ns = c.GetString("ns") nslist := strings.Split(ns, ".") if nslist[len(nslist)-1] != "com" { result.Success = false result.Data = "ns is not correct" c.Data["json"] = result c.Abort("400") } now := time.Now() days := time.Date(now.Year(), now.Month()+1, 0, 0, 0, 0, 0, time.UTC).Day() next_time := now.AddDate(0, 0, days) start_month := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local) end_month := time.Date(next_time.Year(), next_time.Month(), 1, 0, 0, 0, 0, time.Local) starttime, err = c.GetInt64("starttime") if err != nil { starttime = start_month.Unix() } endtime, err = c.GetInt64("endtime") if err != nil { endtime = end_month.Unix() } detail, err := models.GetErrTimeDetail(starttime, endtime, ns) c.Ctx.Output.Header("Content-Disposition", "attachment;filename=myfilename.csv") c.Ctx.Output.Header("Content-Type", "application/CSV") result2 := bytes.NewBufferString("") csvResult := csv.NewWriter(result2) csvResult.Write([]string{"影响业务", "发生时间", "事故内容", "影响面", "影响时长(min)", "事故原因", "对应方案", "未来对策", "定级"}) for _, oneDetail := range detail { csvResult.Write(oneDetail.GenCsv()) } csvResult.Flush() enc := mahonia.NewEncoder("gbk") output := enc.ConvertString(result2.String()) c.Ctx.Output.Body([]byte(output)) c.ServeJson() }
// @Title Get Error Time Percent // @Description get rangetime to caculate errtime percent // @Param starttime query string "service starttime" // @Param endtime query string "service endtime" // @Param ns query string "ns name" // @Success 200 {object} models.FaultData // @Failure 403 body is empty // @router /availability func (c *FaultDataController) GetErrPercent() { var starttime int64 var endtime int64 var ns string var percent float64 var err error var result models.Result //获取ns ns = c.GetString("ns") if ns == "" { result.Success = true result.Data = "100.00%" c.Data["json"] = result c.ServeJson() return } nslist := strings.Split(ns, ".") if nslist[len(nslist)-1] != "com" { result.Success = false result.Data = "ns is not correct! len is too short or .com should be at the end of ns" c.Data["json"] = result c.Ctx.Output.SetStatus(400) c.Abort("400") } now := time.Now() days := time.Date(now.Year(), now.Month()+1, 0, 0, 0, 0, 0, time.UTC).Day() next_time := now.AddDate(0, 0, days) start_month := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local) end_month := time.Date(next_time.Year(), next_time.Month(), 1, 0, 0, 0, 0, time.Local) //获取starttime starttime, err = c.GetInt64("starttime") if err != nil { //如果为空,设置初始值为当月第一天 starttime = start_month.Unix() } //获取endtime endtime, err = c.GetInt64("endtime") if err != nil { //如果为空,设置截止时间为当前时间 endtime = end_month.Unix() } //获取故障时间百分比 percent, err = models.GetErrTimePercent(starttime, endtime, ns) if err != nil { result.Success = false result.Data = err.Error() c.Data["json"] = result c.Ctx.Output.SetStatus(400) } else { result.Success = true result.Data = fmt.Sprintf("%.5f%%", (1-percent)*100) c.Data["json"] = result } c.ServeJson() }