package job import ( uuid2 "app.yhyue.com/moapp/jybase/go-xweb/uuid" "context" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/gutil" "github.com/pkg/errors" "regexp" "strings" "time" "workTasks/common/match" "workTasks/urlStatic/clickIterSource" ) type ( UserId string UUid string MatchObj struct { urlMapping map[string][]*matchItem result map[UUid]map[UserId]int64 //统计计算次数 uuidCodeMapping map[UUid]string matchObj *match.TrieNode } matchItem struct { UUid UUid Name string Code string Rule map[string]interface{} } dbStruct struct { UserId string `json:"userId"` Code string `json:"code"` Num int64 `json:"num"` Date time.Time `json:"date"` } ) var re = regexp.MustCompile(`\[[^\]]*\]`) func NewMatchObj(ctx context.Context) *MatchObj { var ( urlMapping = map[string][]*matchItem{} treeMatch = &match.TrieNode{} uuidCodeMapping = map[UUid]string{} result = map[UUid]map[UserId]int64{} ) for _, mObj := range g.Cfg().MustGet(ctx, "match").Maps() { var ( name = gconv.String(mObj["name"]) code = gconv.String(mObj["code"]) rule = gconv.Map(mObj["rule"]) urlArr []string newMatchItem []*matchItem ) for _, s := range gconv.Strings(rule["url"]) { urlArr = append(urlArr, s) } arr := re.FindAllStringSubmatch(code, -1) if len(arr) > 0 { for _, str := range arr { var ( rep = str[0] key = rep[1 : len(rep)-1] repVal = gconv.Strings(rule[key]) ) for _, val := range repVal { ruleItem := gconv.Map(gutil.Copy(rule)) ruleItem[key] = val var ( uuid = UUid(uuid2.New()) finalCode = strings.ReplaceAll(code, rep, val) ) uuidCodeMapping[uuid] = finalCode newMatchItem = append(newMatchItem, &matchItem{ UUid: uuid, Name: name, Code: strings.ReplaceAll(code, rep, val), Rule: ruleItem, }) } } } else { uuid := UUid(uuid2.New()) uuidCodeMapping[uuid] = code newMatchItem = append(newMatchItem, &matchItem{ UUid: uuid, Name: name, Code: code, Rule: rule, }) } if len(urlArr) > 0 { for _, uStr := range gconv.Strings(rule["url"]) { if uStr != "" { treeMatch.Insert(uStr) } urlMapping[uStr] = newMatchItem } } else { urlMapping[""] = newMatchItem } } for uid, _ := range uuidCodeMapping { result[uid] = make(map[UserId]int64) } return &MatchObj{ matchObj: treeMatch, urlMapping: urlMapping, result: result, uuidCodeMapping: uuidCodeMapping, } } func (o *MatchObj) Match(ctx context.Context, start, end time.Time) error { rows, err := clickIterSource.Ch_analysis.Query(ctx, "SELECT date,url,user_id,mini_program_code,action_id,breaker_name,page_id FROM dwd_f_personnel_behavior WHERE user_id!='' AND date>=? and date= 2000 { var t = r if _, err := g.DB().Save(ctx, "dwd_d_visit", t, 500); err != nil { g.Log().Errorf(ctx, "save errr %v", err) } total += 500 r = []*dbStruct{} } } if len(r) > 0 { if _, err := g.DB().Save(ctx, "dwd_d_visit", r, 500); err != nil { g.Log().Errorf(ctx, "save errr %v", err) } total += len(r) } if total > 0 { g.Log().Infof(ctx, "任务[%s] 共%d条数据", data, total) } return nil }