From 1278c1f6b6e79238b520513c80ce26882e805f71 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期三, 27 三月 2024 11:28:32 +0800 Subject: [PATCH] add DayNightActivityQuery --- EsApi.go | 325 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 294 insertions(+), 31 deletions(-) diff --git a/EsApi.go b/EsApi.go index eb96adc..65c6766 100644 --- a/EsApi.go +++ b/EsApi.go @@ -31,6 +31,7 @@ endHour int } +//鎸夐渶姹�(activeHourFormat缁撴瀯浣�)鏍煎紡鍖栨椂闂存暟鎹� func formatActiveHour(activeHour string) (activeHourFormat, error) { hours := strings.Split(activeHour, "-") @@ -59,15 +60,240 @@ } -func DayNightActivityQuery(communityId string, startTime string, endTime string, activeHour string, indexName string, serverIp string, serverPort string) ([]string, error) { - activityId := make([]string, 0) +//鍒ゆ柇鏃堕棿鏄惁鍐嶈寖鍥翠箣鍐� +func isTimeInRange(timeStr, startStr, endStr string) bool { + layout := "15:04:05" + + timeStamp, err := time.Parse(layout, timeStr) + if err != nil { + fmt.Println("Error parsing timestamp:", err) + return false + } + + startTime, err := time.Parse(layout, startStr) + if err != nil { + fmt.Println("Error parsing start time:", err) + return false + } + + endTime, err := time.Parse(layout, endStr) + if err != nil { + fmt.Println("Error parsing end time:", err) + return false + } + + if startTime.After(endTime) { + // 璺ㄨ秺鏃ユ湡鐨勬儏鍐� + return timeStamp.After(startTime) || timeStamp.Before(endTime) + } else { + // 涓嶈法瓒婃棩鏈熺殑鎯呭喌 + return timeStamp.After(startTime) && timeStamp.Before(endTime) + } +} + +//鍒ゆ柇涓や釜鏃堕棿鍏堝悗 +func compareTimes(time1Str, time2Str string) int { + layout := "15:04:05" + + time1, err := time.Parse(layout, time1Str) + if err != nil { + fmt.Println("Error parsing time 1:", err) + return 0 + } + + time2, err := time.Parse(layout, time2Str) + if err != nil { + fmt.Println("Error parsing time 2:", err) + return 0 + } + + if time1.Before(time2) { + return -1 // time1 鍦� time2 涔嬪墠 + } else if time1.After(time2) { + return 1 // time1 鍦� time2 涔嬪悗 + } else { + return 0 // time1 鍜� time2 鐩哥瓑 + } +} + +//鍒ゆ柇鏃ユ湡鐩稿樊鍑犲ぉ +func daysBetweenDates(date1Str, date2Str string) int { + layout := "2006-01-02" + + date1, err := time.Parse(layout, date1Str) + if err != nil { + fmt.Println("Error parsing date 1:", err) + return 0 + } + + date2, err := time.Parse(layout, date2Str) + if err != nil { + fmt.Println("Error parsing date 2:", err) + return 0 + } + + duration := date2.Sub(date1) + days := int(duration.Hours() / 24) + + return days +} + +//璁$畻鏃堕棿闃堝�� +func checkTimeDifference(timestampStr1 string, timestampStr2 string, intervalInMinutes int) bool { + layout := "2006-01-02 15:04:05" + timestampStr1 = timestampStr1[:19] + timestampStr2 = timestampStr2[:19] + // 灏嗗瓧绗︿覆瑙f瀽涓烘椂闂� + time1, err := time.Parse(layout, timestampStr1) + if err != nil { + fmt.Println("鏃堕棿瑙f瀽澶辫触:", err) + return false + } + time2, err := time.Parse(layout, timestampStr2) + if err != nil { + fmt.Println("鏃堕棿瑙f瀽澶辫触:", err) + return false + } + + // 璁$畻鏃堕棿宸� + diff := time2.Sub(time1) + + // 妫�鏌ユ椂闂村樊鏄惁灏忎簬绛変簬鎸囧畾鐨勯棿闅� + if diff.Minutes() <= float64(intervalInMinutes) { + return true + } else { + return false + } +} + +////鏍煎紡鍖栨椂闂磆h:mm:ss:zzz to hh:mm:ss +//func formatTime(inputTime string) (string, error) { +// parsedTime, err := time.Parse("15:04:05:000", inputTime) +// if err != nil { +// return "", err +// } +// +// formattedTime := parsedTime.Format("15:04:05") +// return formattedTime, nil +//} +func resetDataId(dataId []string, id, dDate, dTime string, sDate *string, sTime *string) []string { + dataId = make([]string, 0) + *sDate = dDate + *sTime = dTime + dataId = append(dataId, id) + return dataId +} + +func decodeActivityId(aHFormat activeHourFormat, frequency int, intervalInMinutes int, source []map[string]interface{}) ([]map[string]interface{}, error) { + docInfo := make([]map[string]interface{}, 0) + for _, info := range source { + tmpInfo := make(map[string]interface{}) + activeId := make([]string, 0) + sDate := "" + sTime := "" + documentNumber := info["key"].(string) + tmpInfo["documentNumber"] = documentNumber + //fmt.Println("documentNumber: ", documentNumber) + topHits := info["top_hits"].(map[string]interface{}) + hits := topHits["hits"].(map[string]interface{}) + hitsResult := hits["hits"].([]interface{}) + dataId := make([]string, 0) + for sIndex, sourceInfo := range hitsResult { + rSourceInfo := sourceInfo.(map[string]interface{}) + source := rSourceInfo["_source"].(map[string]interface{}) + captureTime := source["picDate"].(string) + dDate := strings.Split(captureTime, " ")[0] + dTime := strings.Split(captureTime[:19], " ")[1] + //fmt.Println(captureTime, dDate, dTime) + id := source["id"].(string) + //fmt.Println("sindex: ", sIndex, "documentNumber", tmpInfo["documentNumber"], "id: ", id, "captureTime: ", captureTime) + if !isTimeInRange(dTime, aHFormat.startTime, aHFormat.endTime) { + if sDate != "" && len(dataId) >= frequency { + activeId = append(activeId, dataId...) + dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime) + } + continue + } + if sDate == "" { + sDate = dDate + sTime = dTime + dataId = append(dataId, id) + if len(dataId) >= frequency { + activeId = append(activeId, dataId...) + dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime) + } + continue + } + if checkTimeDifference(sDate+" "+sTime, captureTime[:19], intervalInMinutes) { + if len(dataId) >= frequency { + activeId = append(activeId, dataId...) + dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime) + } + continue + } + //fmt.Println(daysBetweenDates(sDate, dDate)) + if aHFormat.startHour < aHFormat.endHour && daysBetweenDates(sDate, dDate) == 0 { + dataId = append(dataId, id) + } else if aHFormat.startHour > aHFormat.endHour { + if daysBetweenDates(sDate, dDate) == 0 { + if compareTimes(dTime, aHFormat.startTime) == compareTimes(sTime, aHFormat.startTime) { + // ||compareTimes(dTime,aHFormat.endTime) == compareTimes(sTime, aHFormat.endTime){ + dataId = append(dataId, id) + } + } else if daysBetweenDates(sDate, dDate) == 1 { + //鍒濆鏃堕棿鎴冲湪缁撴潫鑼冨洿涔嬪墠 + if compareTimes(sTime, aHFormat.endTime) == -1 { + if len(dataId) >= frequency { + activeId = append(activeId, dataId...) + dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime) + } + //鍒濆鏃堕棿鎴冲湪寮�濮嬭寖鍥翠箣鍚� + } else if compareTimes(sTime, aHFormat.endTime) == 1 { + //next鏃堕棿鎴冲湪缁撴潫鑼冨洿涔嬪墠 + if compareTimes(dTime, aHFormat.endTime) == -1 { + dataId = append(dataId, id) + //next鏃堕棿鎴冲湪寮�濮嬭寖鍥翠箣鍚� + } else if compareTimes(dTime, aHFormat.startTime) == 1 { + if len(dataId) >= frequency { + activeId = append(activeId, dataId...) + dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime) + } + } + } + } else if daysBetweenDates(sDate, dDate) >= 1 { + //fmt.Println(len(dataId)) + if len(dataId) >= frequency { + activeId = append(activeId, dataId...) + dataId = resetDataId(dataId, id, dDate, dTime, &sDate, &sTime) + } + } + } + if sIndex == len(hitsResult)-1 { + if len(dataId) >= frequency { + activeId = append(activeId, dataId...) + } + } + } + if len(activeId) > 0 { + tmpInfo["id"] = activeId + docInfo = append(docInfo, tmpInfo) + } + } + return docInfo, nil +} + +func DayNightActivityQuery(communityId string, documentNumber string,startTime string, endTime string, activeHour string, frequency int, + intervalInMinutes int, indexName string, serverIp string, serverPort string) (map[string]interface{}, error) { esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search" - activeHourFormat, err := formatActiveHour(activeHour) + aHFormat, err := formatActiveHour(activeHour) if err != nil { return nil, err } - + filterDocIdAttr := "" + if documentNumber != ""{ + filterDocIdAttr = "{\"term\": {\""+documentNumber+"\": \"\"}}," + } queryDSL := ` { "size": 0, @@ -82,6 +308,7 @@ } } }, + `+filterDocIdAttr+` { "term": { "communityId": "` + communityId + `" @@ -90,7 +317,7 @@ { "script": { "script": { - "source": "doc['picDate'].value.hourOfDay >= ` + strconv.Itoa(activeHourFormat.startHour) + ` || doc['picDate'].value.hourOfDay < ` + strconv.Itoa(activeHourFormat.endHour) + `", + "source": "doc['picDate'].value.hourOfDay >= ` + strconv.Itoa(aHFormat.startHour) + ` || doc['picDate'].value.hourOfDay < ` + strconv.Itoa(aHFormat.endHour) + `", "lang": "painless" } } @@ -111,37 +338,30 @@ "field": "documentNumber", "size": 100000 }, - "aggs": { - "group_by_date": { - "date_histogram": { - "field": "picDate", - "interval": "1d", // 鎸夊ぉ鍒嗘《 - "format": "yyyy-MM-dd" - }, - "aggs": { - "top_hits": { - "top_hits": { - "_source": [ - "picDate" - ], - "size": 100000, - "sort": [ - { - "picDate": { - "order": "desc" - } - } - ] + "aggs": { + "top_hits": { + "top_hits": { + "_source": [ + "id", + "picDate" + ], + "size": 100000, + "sort": [ + { + "picDate": { + "order": "asc" + } } - } - } - } + ] + } + } } } } }` //fmt.Println(esURL) //fmt.Println(queryDSL) + var result = make(map[string]interface{}) buf, err := EsReq("POST", esURL, []byte(queryDSL)) if err != nil { return nil, err @@ -150,10 +370,53 @@ if err != nil { return nil, err } - result, _ := decodeDocumentInfos(source) + //fmt.Println(source) + docResult, err := decodeActivityId(aHFormat, frequency, intervalInMinutes, source) + if err != nil { + return nil, err + } + //result, _ := decodeDocumentInfos(source) + //return result, nil + if len(docResult) ==0 { + return result,nil + } + DataInfos, err := GetInfosByIds(docResult[0]["id"].([]string), indexName, serverIp, serverPort) + result["documentNumbers"] = docResult + result["datalist"] = DataInfos return result, nil +} - return activityId, nil +func GetInfosByIds(ids []string, indexName string, serverIp string, serverPort string) ([]map[string]interface{}, error) { + captureIds := strings.Replace(strings.Trim(fmt.Sprint(ids), "[]"), " ", "\",\"", -1) + esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search" + queryDSL := ` + { + "query": { + "bool": { + "filter": [{ + "terms": { + "id": [ + "` + captureIds + `" + ] + } + }] + } + }, + "size":1000000, + "sort":[{"picDate":{"order":"desc"}}], + "_source": {"includes":[],"excludes":["*.feature"]} + } +` + buf, err := EsReq("POST", esURL, []byte(queryDSL)) + if err != nil { + return nil, err + } + sources, err := Sourcelist(buf) + if err != nil { + return nil, err + } + + return sources, nil } // ***********************閲嶅簡End************************************// -- Gitblit v1.8.0