sunty
2024-03-25 ebfa610f8c66fd2827a2eec619bfb3e0e22c332f
EsApi.go
@@ -22,6 +22,141 @@
   }
}
//***********************重庆Start**********************************//
type activeHourFormat struct {
   startTime string
   endTime   string
   startHour int
   endHour   int
}
func formatActiveHour(activeHour string) (activeHourFormat, error) {
   hours := strings.Split(activeHour, "-")
   if len(hours) == 2 {
      startHour := hours[0]
      endHour := hours[1]
      // 解析开始时间的小时和分钟
      startParts := strings.Split(startHour, ":")
      startHourInt, _ := strconv.Atoi(startParts[0])
      // 解析结束时间的小时和分钟
      endParts := strings.Split(endHour, ":")
      endHourInt, _ := strconv.Atoi(endParts[0])
      // 输出开始时间的小时
      fmt.Println("开始时间的小时:", startHourInt)
      // 输出结束时间的小时 + 1
      endHourPlusOne := (endHourInt + 1) % 24 // 取余确保不超过24小时
      fmt.Println("结束时间的小时 + 1:", endHourPlusOne)
      activeHourFormat := activeHourFormat{startTime: startHour, endTime: endHour, startHour: startHourInt, endHour: endHourPlusOne}
      return activeHourFormat, nil
   }
   return activeHourFormat{}, errors.New("错误:无法解析开始时间和结束时间")
}
func DayNightActivityQuery(communityId string, startTime string, endTime string, activeHour string, indexName string, serverIp string, serverPort string) ([]string, error) {
   activityId := make([]string, 0)
   esURL := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search"
   activeHourFormat, err := formatActiveHour(activeHour)
   if err != nil {
      return nil, err
   }
   queryDSL := `
   {
       "size": 0,
       "query": {
           "bool": {
               "filter": [
                   {
                       "range": {
                           "picDate": {
                               "gte": "` + startTime + `",
                               "lt": "` + endTime + `"
                           }
                       }
                   },
                   {
                       "term": {
                           "communityId": "` + communityId + `"
                       }
                   },
                   {
                       "script": {
                           "script": {
                               "source": "doc['picDate'].value.hourOfDay >= ` + strconv.Itoa(activeHourFormat.startHour) + ` || doc['picDate'].value.hourOfDay < ` + strconv.Itoa(activeHourFormat.endHour) + `",
                               "lang": "painless"
                           }
                       }
                   }
               ],
               "must_not": [
                   {
                       "term": {
                           "documentNumber": ""
                       }
                   }
               ]
           }
       },
       "aggs": {
           "group_by_documentnumber": {
               "terms": {
                   "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"
                                           }
                                       }
                                   ]
                               }
                           }
                       }
                   }
               }
           }
       }
   }`
   //fmt.Println(esURL)
   //fmt.Println(queryDSL)
   buf, err := EsReq("POST", esURL, []byte(queryDSL))
   if err != nil {
      return nil, err
   }
   source, err := SourceAggregationList(buf)
   if err != nil {
      return nil, err
   }
   result, _ := decodeDocumentInfos(source)
   return result, nil
   return activityId, nil
}
// ***********************重庆End************************************//
// 根据抓拍人员id查询抓拍人员信息
func AIOceaninfosbyid(id []string, indexName string, serverIp string, serverPort string) ([]protomsg.AIOcean, error) {
   var aIOceanInfo []protomsg.AIOcean
@@ -1679,7 +1814,7 @@
}
//按日期范围,服务器Id删除数据
func DeleteAnalyServerData(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (total int, err error, ) {
func DeleteAnalyServerData(serverIp string, serverPort string, indexName string, startTime string, endTime string, analyServerId string) (total int, err error) {
   url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_delete_by_query"
   deleteJson := `{
   "query":{