xuzhiheng 3 年 前
コミット
b3e9500f06

+ 10 - 1
src/client/client.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	mongoutil "qfw/mongodb"
 	qu "qfw/util"
+	"regexp"
 	"strconv"
 	"time"
 	. "util"
@@ -26,6 +27,12 @@ type Client struct {
 	exportLog       xweb.Mapper `xweb:"/client/exportLog"`   //导出页面
 }
 
+var (
+	replaceField_detail = map[string]bool{`"title"`: true, `"projectname.pname"`: true}
+	reg_detail          = regexp.MustCompile("\"fields\":\\[([^]]*detail[^]]*)\\]")
+	reg_single          = regexp.MustCompile("\"query\":\"[^\"{:]\"")
+)
+
 func (this *Client) AdminIndex() {
 	defer qu.Catch()
 	user := this.GetSession("user").(map[string]interface{})
@@ -139,7 +146,9 @@ func (c *Client) CuserRuleCreate() {
 		delete(data, "id")
 		delete(data, "ids")
 		if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
-			data["s_esquery"] = Utiltags(data)
+			esStr := Utiltags(data)
+			data["s_esquery"] = esStr
+			data["s_esquery_search"] = filter(esStr)
 		}
 		i_createtime := time.Now().Unix()
 		data["i_updatetime"] = i_createtime

+ 109 - 2
src/client/customerRule.go

@@ -2,12 +2,14 @@ package client
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
 	"io/ioutil"
 	"log"
 	mongoutil "qfw/mongodb"
 	qu "qfw/util"
 	"regexp"
+	sql "sqlmodel"
 	"strings"
 	"time"
 	. "util"
@@ -41,7 +43,9 @@ func (c *CustomerRule) CuserRuleCreate() {
 		delete(data, "id")
 		delete(data, "ids")
 		if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
-			data["s_esquery"] = Utiltags(data)
+			esStr := Utiltags(data)
+			data["s_esquery"] = esStr
+			data["s_esquery_search"] = filter(esStr)
 		}
 		i_createtime := time.Now().Unix()
 		data["i_updatetime"] = i_createtime
@@ -108,6 +112,47 @@ func (c *CustomerRule) CuserRuleCreate() {
 	}
 }
 
+func filter(sql string) string {
+	if !checkSingleWord(sql) {
+		replace_map := checkDetail(sql)
+		for k, v := range replace_map {
+			// sql = regexp.MustCompile(k).ReplaceAllString(sql, v)
+			sql = strings.ReplaceAll(sql, k, v)
+		}
+	}
+	return sql
+}
+
+//匹配包含detail的组
+func checkDetail(sql string) (arrMap map[string]string) {
+	arrMap = map[string]string{}
+	res := reg_detail.FindAllStringSubmatch(sql, -1)
+	for _, v := range res {
+		if len(v) == 2 {
+			if v[1] != makeReplace(v[1]) {
+				arrMap[v[1]] = makeReplace(v[1])
+			}
+		}
+	}
+	return
+}
+
+//校验是否有单字
+func checkSingleWord(sql string) bool {
+	return len(reg_single.FindStringSubmatch(sql)) > 0
+}
+
+//计算每组字段的替换值
+func makeReplace(s_match string) (s_replace string) {
+	arr := []string{}
+	for _, v := range strings.Split(s_match, ",") {
+		if !replaceField_detail[v] {
+			arr = append(arr, v)
+		}
+	}
+	return strings.Join(arr, ",")
+}
+
 //导入关键词
 func (r *CustomerRule) RuleImport() {
 	defer qu.Catch()
@@ -152,6 +197,9 @@ func (c *CustomerRule) ProductData() {
 		}
 		// 限制multi_match 个数小于1000个
 		s_esquery := qu.ObjToString((*tag)["s_esquery"])
+		if IsNewSql != 0 {
+			s_esquery = qu.ObjToString((*tag)["s_esquery_search"])
+		}
 		escount := Es.Count(Index, Itype, s_esquery)
 		log.Println("escount: ", escount)
 		if escount > 5000 {
@@ -242,7 +290,6 @@ func (c *CustomerRule) ProductData() {
 func (r *CustomerRule) DemoData() {
 	defer qu.Catch()
 	if r.Method() == "POST" {
-		esquery := r.GetString("esquery")
 		id := r.GetString("id")
 		draw, _ := r.GetInteger("draw")
 		tag, _ := Mgo.FindById("entniche_rule", id, `{}`)
@@ -264,6 +311,13 @@ func (r *CustomerRule) DemoData() {
 				})
 			}
 		}
+		esquery := qu.ObjToString((*tag)["s_esquery"])
+		if IsNewSql != 0 {
+			esquery = qu.ObjToString((*tag)["s_esquery_search"])
+		}
+		startTime := time.Now().Unix() - 86400*60
+		endTime := time.Now().Unix()
+		esquery, _ = FilterTimeSql(esquery, startTime, endTime)
 		esquery = esquery[:len(esquery)-1] + `,"size":` + fmt.Sprintf("%d", 100) + `}`
 		data := Es.Get(InterimIndex, Itype, esquery)
 		count := 0
@@ -323,6 +377,59 @@ func (r *CustomerRule) DemoData() {
 		})
 	}
 }
+
+func FilterTimeSql(esquery string, startTime, endTime int64) (string, error) {
+	query := map[string]*sql.QueryObjecct{}
+	if json.Unmarshal([]byte(esquery), &query) == nil {
+		qb := query["query"]
+		for i := 0; i <= 2; i++ {
+			filter := qb.Filtered.Filter
+			if filter != nil && filter.Bool != nil { //有filter
+				index := -1 //记录range的位置
+				for i, m := range filter.Bool.Must {
+					mMap := m.(map[string]interface{})
+					if esRange, ok := mMap["range"].(map[string]interface{}); ok && esRange != nil { //有range
+						if esRange["publishtime"] != nil {
+							index = i
+							break
+						}
+					}
+				}
+				// 生成新的es语句
+				tmpRange_ := bson.M{
+					"range": bson.M{
+						"publishtime": bson.M{
+							"lte": endTime,
+							"gt":  startTime,
+						},
+					},
+				}
+				if index > -1 {
+					filter.Bool.Must[index] = tmpRange_
+				} else {
+					filter.Bool.Must = append(filter.Bool.Must, tmpRange_)
+				}
+				tmpQuery := query
+				strQuery, err := json.Marshal(tmpQuery)
+				if err == nil {
+					return string(strQuery), nil
+				} else {
+					log.Println("失败", err)
+					return esquery, err
+				}
+			} else {
+				qb.Filtered.Filter = &sql.Filter{}
+				qb.Filtered.Filter.Bool = &sql.BoolObject{}
+				continue
+			}
+		}
+	} else {
+		log.Println(json.Unmarshal([]byte(esquery), &query), "err")
+		return esquery, errors.New("err")
+	}
+	return esquery, nil
+}
+
 func (this *CustomerRule) DataTest(id string) {
 	//获取数据
 	title := this.GetString("title")

+ 2 - 1
src/config.json

@@ -238,5 +238,6 @@
   ],
   "need_filehref_appid": [
     "jyFzZXQQIECAZYQERAPD93"
-  ]
+  ],
+  "isNewSql": 1,
 }

+ 1 - 0
src/main.go

@@ -5,6 +5,7 @@ import (
 	_ "filter"
 	"front"
 	"history"
+
 	"log"
 	qu "qfw/util"
 	"qfw/util/mail"

+ 2 - 0
src/util/config.go

@@ -53,6 +53,7 @@ var (
 	SaveUserMail    string
 	ExtractColl     string
 	JyMysql         *mysql.Mysql
+	IsNewSql        int
 )
 
 var (
@@ -118,6 +119,7 @@ func InitOther() {
 	initdb()
 	// initBuryClassType()
 	// initProvince()
+	IsNewSql = qu.IntAll(Sysconfig["isNewSql"])
 }
 
 func initCitys() {

+ 3 - 0
src/util/utiltag.go

@@ -76,6 +76,9 @@ func UtilEsFind1(tags map[string]interface{}) (error, int64) {
 	arrsync := &sync.RWMutex{}
 	sdataid := util.ObjToString(tags["s_dataid"])
 	esquery := util.ObjToString(tags["s_esquery"])
+	if IsNewSql != 0 {
+		esquery = util.ObjToString(tags["s_esquery_search"])
+	}
 	clearKey := util.ObjToString(tags["s_globalclearkey"])
 	clearKeyMatch := util.ObjToString(tags["s_globalclearkeymatch"])
 	if len(esquery) < 1 || len(sdataid) < 1 {

BIN
src/web/templates/.DS_Store


+ 2 - 1
src/web/templates/client/cuser_rule_create.html

@@ -491,7 +491,8 @@
             if (i < 5) {
                 btnMatchHtml1 += `<input type="button" class="btn btn-default col-md-pull-2" style="margin-left: 10px;margin-top: 10px" onclick="clickMatchBtn(this)"
                             code="${matchTypeMap[i]["code"]}" value="${matchTypeMap[i]["name"]}">`;
-            } else {
+            } 
+            if (i >= 5 && i<= 8) {
                 btnMatchHtml2 += `<input type="button" class="btn btn-default col-md-pull-2" style="margin-left: 10px;margin-top: 10px" onclick="clickMatchBtn2(this)"
                             code="${matchTypeMap[i]["code"]}" value="${matchTypeMap[i]["name"]}">`;
             }

+ 2 - 1
src/web/templates/client/cuser_rule_edit.html

@@ -608,7 +608,8 @@
             if (i < 5) {
                 btnMatchHtml1 += `<input type="button" class="btn btn-default col-md-pull-2" style="margin-left: 10px;margin-top: 10px" onclick="clickMatchBtn(this)"
                             code="${matchTypeMap[i]["code"]}" value="${matchTypeMap[i]["name"]}">`;
-            } else {
+            } 
+            if (i >= 5 && i<= 8) {
                 btnMatchHtml2 += `<input type="button" class="btn btn-default col-md-pull-2" style="margin-left: 10px;margin-top: 10px" onclick="clickMatchBtn2(this)"
                             code="${matchTypeMap[i]["code"]}" value="${matchTypeMap[i]["name"]}">`;
             }

+ 2 - 1
src/web/templates/private/cuser_rule_history_edit.html

@@ -488,7 +488,8 @@
             if (i < 5) {
                 btnMatchHtml1 += `<input type="button" class="btn btn-default col-md-pull-2" style="margin-left: 10px;margin-top: 10px" onclick="clickMatchBtn(this)"
                             code="${matchTypeMap[i]["code"]}" value="${matchTypeMap[i]["name"]}">`;
-            }else {
+            } 
+            if (i >= 5 && i<= 8) {
                 btnMatchHtml2 += `<input type="button" class="btn btn-default col-md-pull-2" style="margin-left: 10px;margin-top: 10px" onclick="clickMatchBtn2(this)"
                             code="${matchTypeMap[i]["code"]}" value="${matchTypeMap[i]["name"]}">`;
             }