123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package operation_log
- import (
- "context"
- "github.com/jaryhe/gopkgs/database"
- "smart-log/errors"
- "smart-log/model"
- "smart-log/pb/v1"
- )
- func LogList(ctx context.Context, req *v1.LogListRequest) (reply *v1.LogListReply, err error) {
- p := &model.TLog{}
- p.Type = req.Type
- list, total, err := p.ListWithProject(database.DB(), req.Page, req.Start, req.End)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply = &v1.LogListReply{
- Total:total,
- Page:req.Page,
- PageSize:int32(model.PageSize),
- }
- reply.List = make([]*v1.LogInfo, len(list))
- for i, v := range list {
- item := &v1.LogInfo{
- Name:v.Name,
- Uid:v.Uid,
- Operation:v.Operation,
- Detail:v.Detail,
- Result:"成功",
- Time:v.CreatedAt.Format("2006-01-02 15:04:05"),
- ProjectId:v.ProjectId,
- ProjectNo:v.ProjectNo,
- ProjectName:v.ProjectName,
- }
- if v.Result == 2 {
- item.Result = "失败"
- }
- reply.List[i] = item
- }
- return reply, nil
- }
- /*
- func getLogListWhere(req *v1.LogListRequest) string {
- whereArray := []string{}
- if req.Result != 0 {
- whereArray = append(whereArray, fmt.Sprintf("result = %d", req.Result))
- }
- if req.Name != "" {
- whereArray = append(whereArray, fmt.Sprintf("user_name=~/%s/", req.Name))
- }
- if req.Start > 0 {
- str := time.Unix(req.Start, 0).Format("2006-01-02 15:04:05")
- whereArray = append(whereArray, fmt.Sprintf("time >= '%s'", str))
- }
- if req.End > 0 {
- str := time.Unix(req.End, 0).Format("2006-01-02 15:04:05")
- whereArray = append(whereArray, fmt.Sprintf("time < '%s'", str))
- }
- if req.Operation > 0 {
- whereArray = append(whereArray, fmt.Sprintf("operation = %d", req.Operation))
- }
- where := ""
- for _, v := range whereArray {
- if where == "" {
- where = fmt.Sprintf("where %s", v)
- } else {
- where = fmt.Sprintf(" %s and %s", where, v)
- }
- }
- return where
- }
- type LogInfo struct {
- Uid string `json:"uid"`
- UserName string `json:"user_name"`
- Operation int `json:"operation"`
- OperationName string `json:"operation_name"`
- Result int `json:"result"`
- Detail string `json:"detail"`
- Time string `json:"time"`
- }
- */
- /*
- func LogListFromInflux(ctx context.Context, req *v1.LogListRequest) (reply *v1.LogListReply, err error) {
- if req.Page == 0 {
- req.Page = 1
- }
- if req.ProjectId == 0 {
- return nil, errors.ParamsError
- }
- reply = &v1.LogListReply{
- Page:req.Page,
- PageSize:model.PageSize,
- }
- offset := (req.Page - 1) *model.PageSize
- // prepare sql
- where := getLogListWhere(req)
- sql := fmt.Sprintf("select * from \"%d\" %s limit %d offset %d",req.ProjectId, where, model.PageSize, offset)
- countSql := fmt.Sprintf("select count(operation) as count from \"%d\" %s",req.ProjectId, where)
- // get count
- type Result struct {
- Count int64
- }
- array := []Result{}
- _, err = model.QueryInfluxdb(countSql, "operation_log", &array)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if len(array) == 0 {
- return reply, nil
- }
- reply.Total = array[0].Count
- // get list
- lArray := []LogInfo{}
- _, err = model.QueryInfluxdb(sql, "operation_log", &lArray)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if len(lArray) == 0 {
- return reply, nil
- }
- reply.LogList = make([]*v1.LogInfo, len(lArray))
- for i, v := range lArray {
- item := &v1.LogInfo{}
- item.Result = "失败"
- if v.Result == 1 {
- item.Result = "成功"
- }
- item.Operation = v.OperationName
- item.Name = v.UserName
- item.Detail = v.Detail
- item.Time = v.Time
- item.Uid,_ = strconv.ParseInt(v.Uid, 10, 64)
- reply.LogList[i] = item
- }
- return reply, nil
- }
- */
- /*
- func LogList(ctx context.Context, req *v1.LogListRequest) (reply *v1.LogListReply, err error) {
- filter := model.FilterInfo{}
- if req.Page == 0 {
- req.Page = 1
- }
- filter.Page = (req.Page - 1) *model.PageSize
- filter.Start = req.Start
- filter.End = req.End
- filter.Name = req.Name
- filter.Operation = req.Operation
- filter.Result = req.Result
- reply = &v1.LogListReply{}
- p := &model.TLog{}
- list, total, err := p.List(database.DB(), filter)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.PageSize = model.PageSize
- reply.Total = total
- reply.LogList = make([]*v1.LogInfo, len(list))
- for i, v := range list {
- item := &v1.LogInfo{}
- item.Result = "失败"
- if v.Result == 1 {
- item.Result = "成功"
- }
- item.Operation = v.OperationName
- item.Name = v.Name
- item.Detail = v.Detail
- item.Uid = v.Uid
- reply.LogList[i] = item
- }
- return reply, nil
- }
- */
|