作者 yangfu

feat: schema statistics

package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"reflect"
"strings"
)
type StatisticsQuery struct {
}
func (q *StatisticsQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (q *StatisticsQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(q)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(q).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/xtime"
"github.com/zeromicro/go-zero/core/fx"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
)
func (querySetService *QuerySetService) StatisticsQuery(ctx *domain.Context, query *query.StatisticsQuery) (interface{}, error) {
if err := query.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
var (
total int64
totalToday int64
totalYesterday int64
totalEnable int64
platformTotal int64
platformTotalYesterday int64
)
fx.Parallel(func() {
total, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
})
if err != nil {
return
}
totalEnable, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
"status": domain.StatusOn,
})
if err != nil {
return
}
totalToday, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
"beginTime": xtime.BeginningOfDay().Local(),
"endTime": xtime.BeginningOfDay().AddDate(0, 0, 1).Local(),
})
if err != nil {
return
}
totalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
"beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
"endTime": xtime.BeginningOfDay().Local(),
})
if err != nil {
return
}
}, func() {
platformTotal, _, err = querySetRepository.Find(map[string]interface{}{
"limit": 1,
"type": domain.SchemaTable.ToString(),
})
if err != nil {
return
}
platformTotalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
"limit": 1,
"type": domain.SchemaTable.ToString(),
"beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
"endTime": xtime.BeginningOfDay().Local(),
})
if err != nil {
return
}
})
if err != nil {
return nil, factory.FastError(err)
}
return map[string]interface{}{
"schemaStatistics": map[string]interface{}{
"total": total,
"totalToday": totalToday,
"totalYesterday": totalYesterday,
"totalEnable": totalEnable,
},
"platformSchemaStatistics": map[string]interface{}{
"total": platformTotal,
"totalYesterday": platformTotalYesterday,
},
}, nil
}
... ...
... ... @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"strconv"
"time"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -178,6 +180,18 @@ func (repository *QuerySetRepository) Find(queryOptions map[string]interface{})
query.SetOrderDirect("parent_id", "ASC")
query.SetOrderDirect("sort", "ASC")
}
if v, ok := queryOptions["beginTime"]; ok {
query.Where("created_at >= ?", v.(time.Time))
}
if v, ok := queryOptions["endTime"]; ok {
query.Where("created_at < ?", v.(time.Time))
}
if limit, ok := queryOptions["limit"]; ok {
limit, _ := strconv.ParseInt(fmt.Sprintf("%v", limit), 10, 64)
if limit > 0 {
query.Limit(int(limit))
}
}
if count, err := query.SelectAndCount(); err != nil {
return 0, querySets, err
} else {
... ...
... ... @@ -154,3 +154,11 @@ func (controller *QuerySetController) CalculateSetExport() {
data, err := querySetService.CalculateSetExport(ParseContext(controller.BaseController), updateQuerySetCommand)
controller.Response(data, err)
}
func (controller *QuerySetController) QuerySetStatistics() {
querySetService := service.NewQuerySetService(nil)
q := &query.StatisticsQuery{}
Must(controller.Unmarshal(q))
data, err := querySetService.StatisticsQuery(ParseContext(controller.BaseController), q)
controller.Response(data, err)
}
... ...
... ... @@ -37,4 +37,6 @@ func init() {
web.Router("/data/query-sets/formula/calculate-item-preview", &controllers.QuerySetController{}, "Post:CalculateItemPreview")
web.Router("/data/query-sets/formula/calculate-item-export", &controllers.QuerySetController{}, "Post:CalculateItemExport")
web.Router("/data/query-sets/statistics", &controllers.QuerySetController{}, "Post:QuerySetStatistics")
}
... ...