作者 yangfu

feat: schema statistics

  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/beego/beego/v2/core/validation"
  6 + "reflect"
  7 + "strings"
  8 +)
  9 +
  10 +type StatisticsQuery struct {
  11 +}
  12 +
  13 +func (q *StatisticsQuery) Valid(validation *validation.Validation) {
  14 + //validation.SetError("CustomValid", "未实现的自定义认证")
  15 +}
  16 +
  17 +func (q *StatisticsQuery) ValidateQuery() error {
  18 + valid := validation.Validation{}
  19 + b, err := valid.Valid(q)
  20 + if err != nil {
  21 + return err
  22 + }
  23 + if !b {
  24 + elem := reflect.TypeOf(q).Elem()
  25 + for _, validErr := range valid.Errors {
  26 + field, isExist := elem.FieldByName(validErr.Field)
  27 + if isExist {
  28 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  29 + } else {
  30 + return fmt.Errorf(validErr.Message)
  31 + }
  32 + }
  33 + }
  34 + return nil
  35 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/utils/xtime"
  6 + "github.com/zeromicro/go-zero/core/fx"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/query"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  10 +)
  11 +
  12 +func (querySetService *QuerySetService) StatisticsQuery(ctx *domain.Context, query *query.StatisticsQuery) (interface{}, error) {
  13 + if err := query.ValidateQuery(); err != nil {
  14 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  15 + }
  16 + transactionContext, err := factory.CreateTransactionContext(nil)
  17 + if err != nil {
  18 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  19 + }
  20 + querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
  21 + var (
  22 + total int64
  23 + totalToday int64
  24 + totalYesterday int64
  25 + totalEnable int64
  26 + platformTotal int64
  27 + platformTotalYesterday int64
  28 + )
  29 + fx.Parallel(func() {
  30 + total, _, err = querySetRepository.Find(map[string]interface{}{
  31 + "context": ctx,
  32 + "limit": 1,
  33 + "type": domain.SchemaTable.ToString(),
  34 + })
  35 + if err != nil {
  36 + return
  37 + }
  38 + totalEnable, _, err = querySetRepository.Find(map[string]interface{}{
  39 + "context": ctx,
  40 + "limit": 1,
  41 + "type": domain.SchemaTable.ToString(),
  42 + "status": domain.StatusOn,
  43 + })
  44 + if err != nil {
  45 + return
  46 + }
  47 + totalToday, _, err = querySetRepository.Find(map[string]interface{}{
  48 + "context": ctx,
  49 + "limit": 1,
  50 + "type": domain.SchemaTable.ToString(),
  51 + "beginTime": xtime.BeginningOfDay().Local(),
  52 + "endTime": xtime.BeginningOfDay().AddDate(0, 0, 1).Local(),
  53 + })
  54 + if err != nil {
  55 + return
  56 + }
  57 + totalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
  58 + "context": ctx,
  59 + "limit": 1,
  60 + "type": domain.SchemaTable.ToString(),
  61 + "beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
  62 + "endTime": xtime.BeginningOfDay().Local(),
  63 + })
  64 + if err != nil {
  65 + return
  66 + }
  67 + }, func() {
  68 + platformTotal, _, err = querySetRepository.Find(map[string]interface{}{
  69 + "limit": 1,
  70 + "type": domain.SchemaTable.ToString(),
  71 + })
  72 + if err != nil {
  73 + return
  74 + }
  75 + platformTotalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
  76 + "limit": 1,
  77 + "type": domain.SchemaTable.ToString(),
  78 + "beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
  79 + "endTime": xtime.BeginningOfDay().Local(),
  80 + })
  81 + if err != nil {
  82 + return
  83 + }
  84 + })
  85 +
  86 + if err != nil {
  87 + return nil, factory.FastError(err)
  88 + }
  89 + return map[string]interface{}{
  90 + "schemaStatistics": map[string]interface{}{
  91 + "total": total,
  92 + "totalToday": totalToday,
  93 + "totalYesterday": totalYesterday,
  94 + "totalEnable": totalEnable,
  95 + },
  96 + "platformSchemaStatistics": map[string]interface{}{
  97 + "total": platformTotal,
  98 + "totalYesterday": platformTotalYesterday,
  99 + },
  100 + }, nil
  101 +}
@@ -5,6 +5,8 @@ import ( @@ -5,6 +5,8 @@ import (
5 "fmt" 5 "fmt"
6 "github.com/go-pg/pg/v10" 6 "github.com/go-pg/pg/v10"
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils" 7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
  8 + "strconv"
  9 + "time"
8 10
9 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" 11 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
10 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 12 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
@@ -178,6 +180,18 @@ func (repository *QuerySetRepository) Find(queryOptions map[string]interface{}) @@ -178,6 +180,18 @@ func (repository *QuerySetRepository) Find(queryOptions map[string]interface{})
178 query.SetOrderDirect("parent_id", "ASC") 180 query.SetOrderDirect("parent_id", "ASC")
179 query.SetOrderDirect("sort", "ASC") 181 query.SetOrderDirect("sort", "ASC")
180 } 182 }
  183 + if v, ok := queryOptions["beginTime"]; ok {
  184 + query.Where("created_at >= ?", v.(time.Time))
  185 + }
  186 + if v, ok := queryOptions["endTime"]; ok {
  187 + query.Where("created_at < ?", v.(time.Time))
  188 + }
  189 + if limit, ok := queryOptions["limit"]; ok {
  190 + limit, _ := strconv.ParseInt(fmt.Sprintf("%v", limit), 10, 64)
  191 + if limit > 0 {
  192 + query.Limit(int(limit))
  193 + }
  194 + }
181 if count, err := query.SelectAndCount(); err != nil { 195 if count, err := query.SelectAndCount(); err != nil {
182 return 0, querySets, err 196 return 0, querySets, err
183 } else { 197 } else {
@@ -154,3 +154,11 @@ func (controller *QuerySetController) CalculateSetExport() { @@ -154,3 +154,11 @@ func (controller *QuerySetController) CalculateSetExport() {
154 data, err := querySetService.CalculateSetExport(ParseContext(controller.BaseController), updateQuerySetCommand) 154 data, err := querySetService.CalculateSetExport(ParseContext(controller.BaseController), updateQuerySetCommand)
155 controller.Response(data, err) 155 controller.Response(data, err)
156 } 156 }
  157 +
  158 +func (controller *QuerySetController) QuerySetStatistics() {
  159 + querySetService := service.NewQuerySetService(nil)
  160 + q := &query.StatisticsQuery{}
  161 + Must(controller.Unmarshal(q))
  162 + data, err := querySetService.StatisticsQuery(ParseContext(controller.BaseController), q)
  163 + controller.Response(data, err)
  164 +}
@@ -37,4 +37,6 @@ func init() { @@ -37,4 +37,6 @@ func init() {
37 37
38 web.Router("/data/query-sets/formula/calculate-item-preview", &controllers.QuerySetController{}, "Post:CalculateItemPreview") 38 web.Router("/data/query-sets/formula/calculate-item-preview", &controllers.QuerySetController{}, "Post:CalculateItemPreview")
39 web.Router("/data/query-sets/formula/calculate-item-export", &controllers.QuerySetController{}, "Post:CalculateItemExport") 39 web.Router("/data/query-sets/formula/calculate-item-export", &controllers.QuerySetController{}, "Post:CalculateItemExport")
  40 +
  41 + web.Router("/data/query-sets/statistics", &controllers.QuerySetController{}, "Post:QuerySetStatistics")
40 } 42 }