sunty
2020-08-20 66d9e889d745b573ad8d67ac39e1d05335424f5c
EsClient.go
@@ -1,366 +1,611 @@
package esutil
import (
    "bytes"
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
    "basic.com/pubsub/protomsg.git"
   "basic.com/pubsub/protomsg.git"
   "bytes"
   "encoding/json"
   "errors"
   "fmt"
   "io"
   "io/ioutil"
   "math"
   "net/http"
   "strings"
   "time"
)
func GetEsDataReq(url string, parama string, isSource bool) map[string]interface{} {
   //fmt.Println("es 查询请求路径" + url) //  配置信息 获取
   var dat map[string]interface{}
   req, err := http.NewRequest("POST", url, strings.NewReader(parama))
   req.Header.Add("Content-Type", "application/json")
   timeout := time.Duration(10 * time.Second) //超时时间50ms
   client := &http.Client{Timeout: timeout}
   resp, err := client.Do(req)
   if err != nil {
      fmt.Println(err)
      return dat
   }
   defer resp.Body.Close()
   dec := json.NewDecoder(resp.Body)
   if err := dec.Decode(&dat); err == io.EOF {
      fmt.Println(err.Error())
      return dat
   } else if err != nil {
      fmt.Println(err.Error())
      return dat
   }
   // 是否需要 解析 es 返回的 source
   if isSource {
      dat = dat["hits"].(map[string]interface{})
      var data = make(map[string]interface{}, 2)
      data["total"] = dat["total"]
      sources := []interface{}{}
      for _, value := range dat["hits"].([]interface{}) {
         source := value.(map[string]interface{})["_source"].(map[string]interface{})
         //source["id"] = source["id"]
         /*sdkType := source["sdkType"]
         if sdkType != nil {
            sdk, _ := strconv.Atoi(sdkType.(string))
            source["sdkType"] = sdkTypeToValue(sdk)
         }*/
         sources = append(sources, source)
      }
      data["datalist"] = sources
      return data
   } else {
      return dat
   }
}
func Parsesources(sources []map[string]interface{}) (esinfos []*protomsg.Esinfo) {
    var ok bool
    for _, source := range sources {
        tmpinfo := protomsg.Esinfo{}
        tmpinfo.FaceFeature, ok =   source["faceFeature"].(string)
        if !ok {
            continue
        }
        tmpinfo.Id, ok    =   source["id"].(string)
        if !ok {
            continue
        }
        tmpinfo.Tableid, ok     =   source["tableId"].(string)
        if !ok {
            continue
        }
        esinfos = append(esinfos, &tmpinfo)
//        tmpinfo.PersonName,ok  =   source["personName"].(string)
//            Isnil("personName", ok)
//        tmpinfo.PersonPicUrl,ok =  source["personPicUrl"].(string)
//            Isnil("personPicUrl", ok)
//        tmpinfo.PhoneNum,ok     =  source["phoneNum"].(string)
//            Isnil("phoneNum", ok)
//        tmpinfo.Sex,ok          =  source["sex"].(string)
//            Isnil("sex", ok)
//        tmpinfo.Idcard,ok       =  source["idCard"].(string)
//            Isnil("idCard", ok)
//        tmpinfo.MonitorLevel,ok =  source["monitorLevel"].(string)
        // 根据 tableid 获取 tablename
//        name, _:= Dbtablename(tmpinfo.TableId)
//        tmpinfo.TableName= name
//        binfos.Infos = append(binfos.Infos, &tmpinfo)
    }
    return
func Parsesources(sources []map[string]interface{}) (multiInfos []*protomsg.MultiFeaCache) {
   var ok bool
   for _, source := range sources {
      var multiInfo protomsg.MultiFeaCache
      var tmpinfo protomsg.Esinfo
      tmpinfo.Id, ok = source["id"].(string)
      if !ok {
         continue
      }
      if source["targetInfo"] != nil {
         for _, v := range source["targetInfo"].([]interface{}) {
            m := v.(map[string]interface{})
            tmpinfo.FaceFeature, ok = m["feature"].(string)
            if !ok {
               continue
            }
            if m["attachTarget"] != nil {
               multiInfo.AttachFeature, ok = m["attachTarget"].(map[string]interface{})["feature"].(string)
            }
            if m["targetLocation"] != nil {
               targetLocation := m["targetLocation"].(map[string]interface{})
               topLeft := targetLocation["topLeft"].(map[string]interface{})
               tx := topLeft["x"].(float64)
               ty := topLeft["y"].(float64)
               bottomRight := targetLocation["bottomRight"].(map[string]interface{})
               bx := bottomRight["x"].(float64)
               by := bottomRight["y"].(float64)
               multiInfo.TargetLocation = &protomsg.TargetLocation{
                  TopLeft: &protomsg.Location{
                     X: tx,
                     Y: ty,
                  },
                  BottomRight: &protomsg.Location{
                     X: bx,
                     Y: by,
                  },
               }
            }
         }
      }
      tmpinfo.AnalyServerId, ok = source["analyServerId"].(string)
      if !ok {
         continue
      }
      multiInfo.EsInfo = &tmpinfo
      multiInfo.CameraId, ok = source["cameraId"].(string)
      //fmt.Println(source["cameraId"])
      leis := make([]*protomsg.LinkEsInfo, 0)
      if source["linkTagInfo"] != nil {
         for _, v := range source["linkTagInfo"].([]interface{}) {
            var lei protomsg.LinkEsInfo
            mi := v.(map[string]interface{})
            lei.CameraId, ok = mi["cameraId"].(string)
            if mi["targetInfo"] != nil {
               for _, val := range mi["targetInfo"].([]interface{}) {
                  fmi := val.(map[string]interface{})
                  lei.Feature, ok = fmi["feature"].(string)
                  if fmi["targetLocation"] != nil {
                     targetLocation := fmi["targetLocation"].(map[string]interface{})
                     topLeft := targetLocation["topLeft"].(map[string]interface{})
                     tx := topLeft["x"].(float64)
                     ty := topLeft["y"].(float64)
                     bottomRight := targetLocation["bottomRight"].(map[string]interface{})
                     bx := bottomRight["x"].(float64)
                     by := bottomRight["y"].(float64)
                     multiInfo.TargetLocation = &protomsg.TargetLocation{
                        TopLeft: &protomsg.Location{
                           X: tx,
                           Y: ty,
                        },
                        BottomRight: &protomsg.Location{
                           X: bx,
                           Y: by,
                        },
                     }
                  }
                  lei.AttachFeature, ok = fmi["attachTarget"].(map[string]interface{})["feature"].(string)
               }
            }
            leis = append(leis, &lei)
         }
      }
      multiInfo.LinkEsInfo = leis
      multiInfos = append(multiInfos, &multiInfo)
   }
   return
}
//解析抓拍库人员结构
func Videopersonsbyid(sources []map[string]interface{}) (tmpinfos []protomsg.Videopersons) {
    var ok bool
func AIOceanAnalysis(sources []map[string]interface{}) (tmpinfos []protomsg.AIOcean) {
   var ok bool
   for _, source := range sources {
      var tmpinfo protomsg.AIOcean
      tmpinfo.Id, ok = source["id"].(string)
      if !ok {
         continue
      }
      tmpinfo.CameraId, ok = source["cameraId"].(string)
      if !ok {
         continue
      }
      tmpinfo.CameraAddr, ok = source["cameraAddr"].(string)
      if !ok {
         continue
      }
      tmpinfo.AnalyServerId, ok = source["analyServerId"].(string)
      tmpinfo.AnalyServerName, ok = source["analyServerName"].(string)
      tmpinfo.AnalyServerIp, ok = source["analyServerIp"].(string)
      tmpinfo.ClusterId, ok = source["clusterId"].(string)
      tmpinfo.TaskId, ok = source["taskId"].(string)
      tmpinfo.TaskName, ok = source["taskName"].(string)
      tmpinfo.SdkName, ok = source["sdkName"].(string)
    //var tmpinfo protomsg.Videopersons
    for _, source := range  sources{
        tmpinfo := protomsg.Videopersons{}
        tmpinfo.Id, ok =   source["id"].(string)
        if !ok {
            continue
        }
      if source["picMaxUrl"] != nil {
         for _, v := range source["picMaxUrl"].([]interface{}) {
            tmpinfo.PicMaxUrl = append(tmpinfo.PicMaxUrl, v.(string))
         }
      }
      tmpinfo.VideoUrl, ok = source["videoUrl"].(string)
      tmpinfo.PicDate, ok = source["picDate"].(string)
      if source["targetInfo"] != nil {
         for _, v := range source["targetInfo"].([]interface{}) {
            m := v.(map[string]interface{})
            var ti protomsg.TargetInfo
            ti.TargetId, ok = m["targetId"].(string)
            ti.TargetType, ok = m["targetType"].(string)
            ti.TargetScore, ok = m["targetScore"].(float64)
            ti.Feature, ok = m["feature"].(string)
            ti.PicSmUrl, ok = m["picSmUrl"].(string)
            targetLocation := m["targetLocation"].(map[string]interface{})
            topLeft := targetLocation["topLeft"].(map[string]interface{})
            tx := topLeft["x"].(float64)
            ty := topLeft["y"].(float64)
            bottomRight := targetLocation["bottomRight"].(map[string]interface{})
            bx := bottomRight["x"].(float64)
            by := bottomRight["y"].(float64)
            ti.TargetLocation = &protomsg.TargetLocation{
               TopLeft: &protomsg.Location{
                  X: tx,
                  Y: ty,
               },
               BottomRight: &protomsg.Location{
                  X: bx,
                  Y: by,
               },
            }
            tmpinfo.TargetInfo = append(tmpinfo.TargetInfo, &ti)
         }
        tmpinfo.CameraId, ok    =   source["cameraId"].(string)
        if !ok {
            continue
        }
      }
      tmpinfo.OtherLabels, ok = source["otherLabels"].(string)
      tmpinfo.ShowLabels, ok = source["showLabels"].(string)
      tmpinfo.LikeDate, ok = source["likeDate"].(string)
      if source["baseInfo"] != nil {
         for _, v := range source["baseInfo"].([]interface{}) {
            m := v.(map[string]interface{})
            var bi protomsg.BaseInfo
            bi.TableId, ok = m["tableId"].(string)
            bi.TableName, ok = m["tableName"].(string)
            bi.CompareScore, ok = m["compareScore"].(float64)
            bi.BwType, ok = m["bwType"].(string)
            bi.TargetId, ok = m["targetId"].(string)
            bi.TargetName, ok = m["targetName"].(string)
            bi.TargetPicUrl, ok = m["targetPicUrl"].(string)
            bi.MonitorLevel, ok = m["monitorLevel"].(string)
            bi.Labels, ok = m["labels"].(string)
            bi.Content, ok = m["content"].(string)
            tmpinfo.BaseInfo = append(tmpinfo.BaseInfo, &bi)
         }
      }
      tmpinfo.IsAlarm, ok = source["isAlarm"].(bool)
      tmpinfo.IsAckAlarm, ok = source["isAckAlarm"].(bool)
      if source["alarmRules"] != nil {
         for _, v := range source["alarmRules"].([]interface{}) {
            m := v.(map[string]interface{})
            var ar protomsg.AlarmRules
            ar.GroupId, ok = m["groupId"].(string)
            ar.AlarmLevel, ok = m["alarmLevel"].(string)
            ar.RuleText, ok = m["ruleText"].(string)
            ar.DefenceState, ok = m["defenceState"].(bool)
            ar.LinkInfo, ok = m["linkInfo"].(string)
            tmpinfo.AlarmRules = append(tmpinfo.AlarmRules, &ar)
         }
        tmpinfo.CameraAddr, ok     =   source["cameraAddr"].(string)
        if !ok {
            continue
        }
      }
      tmpinfo.IsCollect, ok = source["isCollect"].(bool)
      tmpinfo.IsDelete, ok = source["isDelete"].(bool)
      tmpinfo.Content, ok = source["content"].(string)
      tmpinfo.LinkTag, ok = source["linkTag"].(string)
      if source["linkTagInfo"] != nil {
         for _, v := range source["linkTagInfo"].([]interface{}) {
            var lti protomsg.EsDataBase
            lm := v.(map[string]interface{})
            lti.Id, ok = lm["id"].(string)
            if !ok {
               continue
            }
            lti.CameraId, ok = lm["cameraId"].(string)
            if !ok {
               continue
            }
            lti.CameraAddr, ok = lm["cameraAddr"].(string)
            if !ok {
               continue
            }
            lti.AnalyServerId, ok = lm["analyServerId"].(string)
            lti.AnalyServerName, ok = lm["analyServerName"].(string)
            lti.AnalyServerIp, ok = lm["analyServerIp"].(string)
            lti.ClusterId, ok = lm["clusterId"].(string)
            lti.TaskId, ok = lm["taskId"].(string)
            lti.TaskName, ok = lm["taskName"].(string)
            lti.SdkName, ok = lm["sdkName"].(string)
            if lm["picMaxUrl"] != nil {
               for _, v := range lm["picMaxUrl"].([]interface{}) {
                  lti.PicMaxUrl = append(lti.PicMaxUrl, v.(string))
               }
            }
            lti.VideoUrl, ok = lm["videoUrl"].(string)
            lti.PicDate, ok = lm["picDate"].(string)
            if lm["targetInfo"] != nil {
               for _, v := range lm["targetInfo"].([]interface{}) {
                  m := v.(map[string]interface{})
                  var ti protomsg.TargetInfo
                  ti.TargetId, ok = m["targetId"].(string)
                  ti.TargetType, ok = m["targetType"].(string)
                  ti.TargetScore, ok = m["targetScore"].(float64)
                  ti.Feature, ok = m["feature"].(string)
                  ti.PicSmUrl, ok = m["picSmUrl"].(string)
                  targetLocation := m["targetLocation"].(map[string]interface{})
                  topLeft := targetLocation["topLeft"].(map[string]interface{})
                  tx := topLeft["x"].(float64)
                  ty := topLeft["y"].(float64)
                  bottomRight := targetLocation["bottomRight"].(map[string]interface{})
                  bx := bottomRight["x"].(float64)
                  by := bottomRight["y"].(float64)
                  ti.TargetLocation = &protomsg.TargetLocation{
                     TopLeft: &protomsg.Location{
                        X: tx,
                        Y: ty,
                     },
                     BottomRight: &protomsg.Location{
                        X: bx,
                        Y: by,
                     },
                  }
                  lti.TargetInfo = append(lti.TargetInfo, &ti)
               }
        tmpinfo.PicDate,ok  =   source["picDate"].(string)
        Isnil("picDate", ok)
        tmpinfo.PicMaxUrl,ok =  source["picMaxUrl"].(string)
        Isnil("picMaxUrl", ok)
        tmpinfo.TaskId,ok     =  source["taskId"].(string)
        Isnil("taskId", ok)
        tmpinfo.TaskName,ok          =  source["taskName"].(string)
        Isnil("taskName", ok)
        tmpinfo.SdkName,ok       =  source["sdkName"].(string)
        Isnil("sdkName", ok)
        tmpinfo.Content,ok =  source["content"].(string)
        Isnil("content",ok)
        tmpinfo.LikeDate,ok = source["likeDate"].(string)
        Isnil("likeDate",ok)
        tmpinfo.DetectScore,ok =  source["detectScore"].(string)
        Isnil("detectScore", ok)
        tmpinfo.Sex,ok = source["sex"].(string)
        Isnil("sex",ok)
            }
            lti.OtherLabels, ok = lm["otherLabels"].(string)
            lti.ShowLabels, ok = lm["showLabels"].(string)
            lti.LikeDate, ok = lm["likeDate"].(string)
            if lm["baseInfo"] != nil {
               for _, v := range lm["baseInfo"].([]interface{}) {
                  m := v.(map[string]interface{})
                  var bi protomsg.BaseInfo
                  bi.TableId, ok = m["tableId"].(string)
                  bi.TableName, ok = m["tableName"].(string)
                  bi.CompareScore, ok = m["compareScore"].(float64)
                  bi.BwType, ok = m["bwType"].(string)
                  bi.TargetId, ok = m["targetId"].(string)
                  bi.TargetName, ok = m["targetName"].(string)
                  bi.TargetPicUrl, ok = m["targetPicUrl"].(string)
                  bi.MonitorLevel, ok = m["monitorLevel"].(string)
                  bi.Labels, ok = m["labels"].(string)
                  bi.Content, ok = m["content"].(string)
                  tmpinfo.BaseInfo = append(tmpinfo.BaseInfo, &bi)
               }
            }
            lti.IsAlarm, ok = lm["isAlarm"].(bool)
            lti.IsAckAlarm, ok = lm["isAckAlarm"].(bool)
            if lm["alarmRules"] != nil {
               for _, v := range lm["alarmRules"].([]interface{}) {
                  m := v.(map[string]interface{})
                  var ar protomsg.AlarmRules
                  ar.GroupId, ok = m["groupId"].(string)
                  ar.AlarmLevel, ok = m["alarmLevel"].(string)
                  ar.RuleText, ok = m["ruleText"].(string)
                  ar.DefenceState, ok = m["defenceState"].(bool)
                  ar.LinkInfo, ok = m["linkInfo"].(string)
                  lti.AlarmRules = append(lti.AlarmRules, &ar)
               }
        age,ok  :=   source["age"].(float64)
        if ok {
            tmpinfo.Age= int32(age)
        }
        Isnil("age", ok)
        tmpinfo.AgeDescription,ok = source["ageDescription"].(string)
        Isnil("ageDescription",ok)
        tmpinfo.Race,ok = source["race"].(string)
        Isnil("race",ok)
        tmpinfo.SmileLevel,ok = source["smileLevel"].(string)
        Isnil("smileLevel",ok)
        tmpinfo.BeautyLevel,ok = source["beautyLevel"].(string)
        Isnil("beautyLevel",ok)
        if source["picSmUrl"] != nil {
            for _,v := range source["picSmUrl"].([]interface{}) {
                tmpinfo.PicSmUrl = append(tmpinfo.PicSmUrl, v.(string))
            }
        }
        tmpinfo.VideoUrl,ok = source["videoUrl"].(string)
        Isnil("videoUrl",ok)
        tmpinfo.AnalyServerId,ok = source["analyServerId"].(string)
        Isnil("analyServerId",ok)
        tmpinfo.AnalyServerName,ok = source["analyServerName"].(string)
        Isnil("analyServerName",ok)
        tmpinfo.AnalyServerIp,ok = source["analyServerIp"].(string)
        Isnil("analyServerIp",ok)
        tmpinfo.ClusterId,ok = source["clusterId"].(string)
        Isnil("clusterId",ok)
        tmpinfo.IsAlarm,ok = source["isAlarm"].(string)
        Isnil("isAlarm",ok)
        tmpinfo.IsAckAlarm,ok = source["isAckAlarm"].(string)
        Isnil("isAckAlarm",ok)
        isCollect,ok  :=   source["isCollect"].(float64)
        if ok {
            tmpinfo.IsDelete= int32(isCollect)
        }
        Isnil("isDelete", ok)
        isDelete,ok  :=   source["isDelete"].(float64)
        if ok {
            tmpinfo.IsDelete= int32(isDelete)
        }
        Isnil("isDelete", ok)
        if source["alarmRules"] != nil {
            fmt.Println(source["alarmRules"])
            for _,v := range source["alarmRules"].([]interface{}) {
                m := v.(map[string]interface{})
                ar := protomsg.AlarmRules{}
                ar.GroupId,ok = m["groupId"].(string)
                Isnil("groupId",ok)
                ar.AlarmLevel,ok = m["alarmLevel"].(string)
                Isnil("alarmLevel",ok)
                ar.RuleText,ok = m["ruleText"].(string)
                Isnil("ruleText",ok)
                ar.DefenceState,ok = m["defenceState"].(bool)
                Isnil("DefenceState",ok)
                tmpinfo.AlarmRules = append( tmpinfo.AlarmRules, &ar)
            }
        }
        if source["baseInfo"] != nil {
            fmt.Println(source["baseInfo"])
            for _,v := range source["baseInfo"].([]interface{}) {
                m := v.(map[string]interface{})
                fmt.Println("m:",m)
                bi := protomsg.BaseInfo{}
                bi.TableId,ok = m["tableId"].(string)
                Isnil("tableId",ok)
                bi.TableName,ok = m["tableName"].(string)
                Isnil("tableName",ok)
                compareScore,ok := m["compareScore"].(float64)
                if ok {
                    bi.CompareScore= float32(compareScore)
                }
                Isnil("compareScore", ok)
                bi.PersonId,ok = m["personId"].(string)
                Isnil("personId",ok)
                bi.PersonName,ok = m["personName"].(string)
                Isnil("personName",ok)
                bi.PersonPicUrl,ok = m["personPicUrl"].(string)
                Isnil("personPicUrl",ok)
                bi.PhoneNum,ok = m["phoneNum"].(string)
                Isnil("phoneNum",ok)
                bi.Sex,ok = m["sex"].(string)
                Isnil("sex",ok)
                bi.IdCard,ok = m["idCard"].(string)
                Isnil("idCard",ok)
                bi.MonitorLevel,ok = m["monitorLevel"].(string)
                Isnil("monitorLevel",ok)
                bi.Content,ok = m["content"].(string)
                Isnil("content",ok)
                bwType,ok := m["bwType"].(float64)
                if ok {
                    bi.BwType= int32(bwType)
                }
                Isnil("bwType", ok)
                tmpinfo.BaseInfo = append(tmpinfo.BaseInfo, &bi)
            }
        }
        //根据 tableid 获取 tablename
        //name, _:= Dbtablename(tmpinfo.TableId)
        //tmpinfo.TableName= name
        tmpinfos = append(tmpinfos, tmpinfo)
    }
    return tmpinfos
            }
            lti.IsCollect, ok = lm["isCollect"].(bool)
            lti.IsDelete, ok = lm["isDelete"].(bool)
            lti.Content, ok = lm["content"].(string)
            tmpinfo.LinkTagInfo = append(tmpinfo.LinkTagInfo, &lti)
         }
      }
      tmpinfos = append(tmpinfos, tmpinfo)
   }
   return tmpinfos
}
//解析底库人员结构
func Dbpersonbyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbperson) {
        var ok bool
   var ok bool
        //var tmpinfo protomsg.Dbperson
        for _, source := range  sources{
            tmpinfo := protomsg.Dbperson{}
            tmpinfo.FaceFeature, ok =   source["faceFeature"].(string)
            if !ok {
                continue
            }
   var tmpinfo protomsg.Dbperson
   for _, source := range sources {
      tmpinfo.FaceFeature, ok = source["faceFeature"].(string)
      if !ok {
         continue
      }
            tmpinfo.Id, ok    =   source["id"].(string)
            if !ok {
                continue
            }
      tmpinfo.Id, ok = source["id"].(string)
      if !ok {
         continue
      }
            tmpinfo.TableId, ok     =   source["tableId"].(string)
            if !ok {
                continue
            }
      tmpinfo.TableId, ok = source["tableId"].(string)
      if !ok {
         continue
      }
            tmpinfo.PersonName,ok  =   source["personName"].(string)
                Isnil("personName", ok)
            tmpinfo.PersonPicUrl,ok =  source["personPicUrl"].(string)
                Isnil("personPicUrl", ok)
            tmpinfo.PhoneNum,ok     =  source["phoneNum"].(string)
                Isnil("phoneNum", ok)
            tmpinfo.Sex,ok          =  source["sex"].(string)
                Isnil("sex", ok)
            tmpinfo.IdCard,ok       =  source["idCard"].(string)
                Isnil("idCard", ok)
            tmpinfo.MonitorLevel,ok =  source["monitorLevel"].(string)
                Isnil("monitorLevel",ok)
            tmpinfo.UpdateTime,ok = source["updateTime"].(string)
                Isnil("updateTime",ok)
            tmpinfo.Age,ok          =  source["age"].(string)
                Isnil("age", ok)
      tmpinfo.PersonName, ok = source["personName"].(string)
      //Isnil("personName", ok)
      tmpinfo.PersonPicUrl, ok = source["personPicUrl"].(string)
      //Isnil("personPicUrl", ok)
      tmpinfo.PhoneNum, ok = source["phoneNum"].(string)
      //Isnil("phoneNum", ok)
      tmpinfo.Sex, ok = source["sex"].(string)
      //Isnil("sex", ok)
      tmpinfo.IdCard, ok = source["idCard"].(string)
      //Isnil("idCard", ok)
      tmpinfo.MonitorLevel, ok = source["monitorLevel"].(string)
      //Isnil("monitorLevel",ok)
      tmpinfo.UpdateTime, ok = source["updateTime"].(string)
      //Isnil("updateTime",ok)
      tmpinfo.Age, ok = source["age"].(string)
      //Isnil("age", ok)
            isDelete,ok  :=   source["isDelete"].(float64)
            if ok {
                tmpinfo.IsDelete= int32(isDelete)
            }
            Isnil("isDelete", ok)
      isDelete, ok := source["isDelete"].(float64)
      if ok {
         tmpinfo.IsDelete = int32(isDelete)
      }
      //Isnil("isDelete", ok)
            enable,ok  :=   source["enable"].(float64)
            if ok {
                tmpinfo.Enable= int32(enable)
            }
            Isnil("endTime", ok)
      enable, ok := source["enable"].(float64)
      if ok {
         tmpinfo.Enable = int32(enable)
      }
      //Isnil("endTime", ok)
            tmpinfo.Reserved,ok = source["reserved"].(string)
                Isnil("reserved",ok)
            tmpinfo.PicDesc,ok = source["picDesc"].(string)
                Isnil("picDesc",ok)
            tmpinfo.CreateTime,ok = source["createTime"].(string)
                Isnil("createTime",ok)
            tmpinfo.CreateBy,ok = source["createBy"].(string)
                Isnil("createBy",ok)
      tmpinfo.Reserved, ok = source["reserved"].(string)
      //Isnil("reserved",ok)
      tmpinfo.PicDesc, ok = source["picDesc"].(string)
      //Isnil("picDesc",ok)
      tmpinfo.CreateTime, ok = source["createTime"].(string)
      //Isnil("createTime",ok)
      tmpinfo.CreateBy, ok = source["createBy"].(string)
      //Isnil("createBy",ok)
            //根据 tableid 获取 tablename
            //name, _:= Dbtablename(tmpinfo.TableId)
            //tmpinfo.TableName= name
            tmpinfos = append(tmpinfos, tmpinfo)
        }
      //根据 tableid 获取 tablename
      //name, _:= Dbtablename(tmpinfo.TableId)
      //tmpinfo.TableName= name
      tmpinfos = append(tmpinfos, tmpinfo)
   }
        return tmpinfos
   return tmpinfos
}
//解析底库结构
func Dbtablebyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbtable) {
    var ok bool
   var ok bool
    //var tmpinfo protomsg.Dbtable
    for _, source := range  sources{
        tmpinfo := protomsg.Dbtable{}
        tmpinfo.Id, ok    =   source["id"].(string)
        if !ok {
            continue
        }
        tmpinfo.TableName,ok  =   source["tableName"].(string)
        Isnil("tableName", ok)
        tmpinfo.TableType,ok =  source["tableType"].(string)
        Isnil("tableType", ok)
        tmpinfo.CreateBy,ok     =  source["createBy"].(string)
        Isnil("createBy", ok)
        tmpinfo.TableDesc,ok  =   source["tableDesc"].(string)
        Isnil("tableDesc", ok)
        tmpinfo.BwType,ok =  source["bwType"].(string)
        Isnil("bwType", ok)
        tmpinfo.StartTime,ok     =  source["startTime"].(string)
        Isnil("startTime", ok)
        tmpinfo.EndTime,ok  =   source["endTime"].(string)
        Isnil("endTime", ok)
   var tmpinfo protomsg.Dbtable
   for _, source := range sources {
        isDelete,ok  :=   source["isDelete"].(float64)
        if ok {
            tmpinfo.IsDelete= int32(isDelete)
        }
        Isnil("isDelete", ok)
      tmpinfo.Id, ok = source["id"].(string)
      if !ok {
         continue
      }
      tmpinfo.TableName, ok = source["tableName"].(string)
      //Isnil("tableName", ok)
      tmpinfo.TableType, ok = source["tableType"].(string)
      //Isnil("tableType", ok)
      tmpinfo.CreateBy, ok = source["createBy"].(string)
      //Isnil("createBy", ok)
      tmpinfo.TableDesc, ok = source["tableDesc"].(string)
      //Isnil("tableDesc", ok)
      tmpinfo.BwType, ok = source["bwType"].(string)
      //Isnil("bwType", ok)
      tmpinfo.StartTime, ok = source["startTime"].(string)
      //Isnil("startTime", ok)
      tmpinfo.EndTime, ok = source["endTime"].(string)
      //Isnil("endTime", ok)
        enable,ok  :=   source["enable"].(float64)
        if ok {
            tmpinfo.Enable= int32(enable)
        }
        Isnil("endTime", ok)
      isDelete, ok := source["isDelete"].(float64)
      if ok {
         tmpinfo.IsDelete = int32(isDelete)
      }
      //Isnil("isDelete", ok)
        tmpinfo.CreateTime,ok     =  source["createTime"].(string)
        Isnil("createTime", ok)
        tmpinfo.UpdateTime,ok  =   source["updateTime"].(string)
        Isnil("updateTime", ok)
        tmpinfo.IsForever,ok =  source["isForever"].(string)
        Isnil("isForever", ok)
        tmpinfo.IsSync,ok =  source["isSync"].(string)
        Isnil("isSync", ok)
        tmpinfo.AnalyServerId,ok     =  source["analyServerId"].(string)
        Isnil("analyServerId", ok)
      enable, ok := source["enable"].(float64)
      if ok {
         tmpinfo.Enable = int32(enable)
      }
      //Isnil("endTime", ok)
        tmpinfos = append(tmpinfos, tmpinfo)
    }
      tmpinfo.CreateTime, ok = source["createTime"].(string)
      //Isnil("createTime", ok)
      tmpinfo.UpdateTime, ok = source["updateTime"].(string)
      //Isnil("updateTime", ok)
      tmpinfo.IsForever, ok = source["isForever"].(string)
      //Isnil("isForever", ok)
      tmpinfo.IsSync, ok = source["isSync"].(string)
      //Isnil("isSync", ok)
      tmpinfo.AnalyServerId, ok = source["analyServerId"].(string)
      //Isnil("analyServerId", ok)
    return tmpinfos
      tmpinfos = append(tmpinfos, tmpinfo)
   }
   return tmpinfos
}
func Sourcelist(buf []byte)(sources []map[string]interface{}, err error){
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        return nil, errors.New("http response interface can not change map[string]interface{}")
    }
func SourceAggregations(buf [] byte) (sources map[string]interface{}, err error) {
   s := make(map[string]interface{})
   loc, err := time.LoadLocation("Asia/Shanghai")
   if err != nil {
      return nil, errors.New("时区设置错误")
   }
   var info interface{}
   json.Unmarshal(buf, &info)
   out, ok := info.(map[string]interface{})
   if !ok {
      return nil, errors.New("http response interface can not change map[string]interface{}")
   }
   middle, ok := out["aggregations"].(map[string]interface{})
   if !ok {
      return nil, errors.New("first hits change error!")
   }
   bucketsAggs := middle["buckets_aggs"].(map[string]interface{})
   buckets := bucketsAggs["buckets"].([]interface{})
   if len(buckets) == 0 {
      return nil, nil
   }
   allSource := make([]map[string]interface{},0)
   for _, inf := range buckets {
      //tmpSources := make(map[string]interface{}, 0)
      hitsSources := make([]map[string]interface{}, 0)
      //groupKey := inf.(map[string]interface{})["key"].(map[string]interface{})
      //docCount := int(inf.(map[string]interface{})["doc_count"].(float64))
      topAttentionHits := inf.(map[string]interface{})["top_attention_hits"].(map[string]interface{})
      middleHits := topAttentionHits["hits"].(map[string]interface{})
      finalHits := middleHits["hits"].([]interface{})
      tmpHitSource := make(map[string]interface{})
      startTime := ""
      for _, in := range finalHits {
         tmpbuf, ok := in.(map[string]interface{})
         if !ok {
            fmt.Println("change to source error!")
            continue
         }
         source, ok := tmpbuf["_source"].(map[string]interface{})
         if !ok {
            fmt.Println("change _source error!")
            continue
         }
         baseInfo := source["baseInfo"].([]interface{})[0].(map[string]interface{})
         targetInfo := source["targetInfo"].([]interface{})[0].(map[string]interface{})
         tmpTime := source["picDate"].(string)
         mTime, err := time.ParseInLocation("2006-01-02 15:04:05", tmpTime, loc)
         if err != nil {
            return nil, errors.New("时间解析错误")
         }
    middle, ok := out["hits"].(map[string]interface{})
    if !ok {
        return nil, errors.New("first hits change error!")
    }
    for _, in := range middle["hits"].([]interface{}){
        tmpbuf, ok := in.(map[string]interface{})
        if !ok {
                fmt.Println("change to source error!")
                continue
        }
        source, ok := tmpbuf["_source"].(map[string]interface{})
        if !ok {
            fmt.Println("change _source error!")
            continue
        }
        sources  = append(sources, source )
    }
    return sources,nil
         sTime := tmpTime
         eTime := tmpTime
         if startTime != "" {
            sinTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, loc)
            if math.Abs(sinTime.Sub(mTime).Seconds()) <= 20 {
               startTime = tmpTime
               hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
               continue
            } else {
               if sinTime.Sub(mTime).Seconds() == 0{
                  sinTime.Add(time.Second*1)
                  sinTime.Format("2006-01-02 15:04:05")
                  hitsSources[len(hitsSources)-1]["endTime"] = sinTime
               }
            }
         }
         startTime = tmpTime
         tmpHitSource["personId"] = baseInfo["targetId"].(string)
         tmpHitSource["cameraId"] = source["cameraId"].(string)
         tmpHitSource["cameraName"] = source["cameraName"].(string)
         tmpHitSource["cameraAddr"] = source["cameraAddr"].(string)
         tmpHitSource["targetScore"] = int(targetInfo["targetScore"].(float64))
         tmpHitSource["properties"] = source["showLabels"].(string)
         tmpHitSource["tableId"] = baseInfo["tableId"].(string)
         tmpHitSource["tableName"] = baseInfo["tableName"].(string)
         tmpHitSource["bwType"] = baseInfo["bwType"].(string)
         tmpHitSource["personName"] = baseInfo["targetName"].(string)
         tmpHitSource["compareScore"] = int(baseInfo["compareScore"].(float64))
         tmpHitSource["startTime"] = sTime
         tmpHitSource["startBackGroundPicUrl"] = source["picMaxUrl"].([]interface{})
         tmpHitSource["startFacePicUrl"] = targetInfo["picSmUrl"].(string)
         tmpHitSource["endTime"] = eTime
         tmpHitSource["endTBackGroundPicUrl"] = source["picMaxUrl"].([]interface{})
         tmpHitSource["endTFacePicUrl"] = targetInfo["picSmUrl"].(string)
         hitsSources = append(hitsSources, tmpHitSource)
      }
      allSource = append(allSource,hitsSources...)
   //   tmpSources["groupKey"] = groupKey
   //   tmpSources["doc_count"] = docCount
   //   tmpSources["hits_sources"] = hitsSources
   //   sources = append(sources, tmpSources)
   }
   count := len(allSource)
   fmt.Println(count)
   s["count"] = count
   s["allSource"] = allSource
   return s, nil
}
//slice scroll 解析工具函数
func Sourcelistforscroll(buf []byte)(datasource map[string]interface{}, err error){
func Sourcelist(buf []byte) (sources []map[string]interface{}, err error) {
   var info interface{}
   json.Unmarshal(buf, &info)
   out, ok := info.(map[string]interface{})
   if !ok {
      return nil, errors.New("http response interface can not change map[string]interface{}")
   }
   middle, ok := out["hits"].(map[string]interface{})
   if !ok {
      return nil, errors.New("first hits change error!")
   }
   for _, in := range middle["hits"].([]interface{}) {
      tmpbuf, ok := in.(map[string]interface{})
      if !ok {
         fmt.Println("change to source error!")
         continue
      }
      source, ok := tmpbuf["_source"].(map[string]interface{})
      if !ok {
         fmt.Println("change _source error!")
         continue
      }
      sources = append(sources, source)
   }
   return sources, nil
}
func Sourcelistforscroll(buf []byte) (datasource map[string]interface{}, err error) {
   var data = make(map[string]interface{})
   var info interface{}
   json.Unmarshal(buf, &info)
@@ -379,8 +624,8 @@
   if !ok {
      return nil, errors.New("first hits change error!")
   }
   var sources  = make([]map[string]interface{},0)
   for _, in := range middle["hits"].([]interface{}){
   var sources = make([]map[string]interface{}, 0)
   for _, in := range middle["hits"].([]interface{}) {
      tmpbuf, ok := in.(map[string]interface{})
      if !ok {
         fmt.Println("change to source error!")
@@ -391,41 +636,108 @@
         fmt.Println("change _source error!")
         continue
      }
      sources  = append(sources, source )
      sources = append(sources, source)
   }
   data["sourcelist"] = sources
   data["scroll_id"] = scroll_id
   return data,nil
   return data, nil
}
func SourceCreated(buf []byte) (result bool, err error) {
   var info interface{}
   json.Unmarshal(buf, &info)
   out, ok := info.(map[string]interface{})
   if !ok {
      return false, errors.New("http response interface can not change map[string]interface{}")
   }
   middle, ok := out["result"].(string)
   if !ok {
      return false, errors.New("first total change error!")
   }
   if middle == "created" || middle == "updated" {
      result = true
   }
   return result, nil
}
func SourceDeleted(buf []byte) (total int, err error) {
   var info interface{}
   json.Unmarshal(buf, &info)
   out, ok := info.(map[string]interface{})
   if !ok {
      return -1, errors.New("http response interface can not change map[string]interface{}")
   }
   middle, ok := out["deleted"].(float64)
   if !ok {
      return -1, errors.New("first total change error!")
   }
   total = int(middle)
   return total, nil
}
func SourceUpdated(buf []byte) (total int, err error) {
   var info interface{}
   json.Unmarshal(buf, &info)
   out, ok := info.(map[string]interface{})
   if !ok {
      return -1, errors.New("http response interface can not change map[string]interface{}")
   }
   middle, ok := out["updated"].(float64)
   if !ok {
      return -1, errors.New("first total change error!")
   }
   total = int(middle)
   return total, nil
}
func SourceTotal(buf []byte) (total int, err error) {
   var info interface{}
   json.Unmarshal(buf, &info)
   out, ok := info.(map[string]interface{})
   if !ok {
      return -1, errors.New("http response interface can not change map[string]interface{}")
   }
   middle, ok := out["hits"].(map[string]interface{})
   if !ok {
      return -1, errors.New("first total change error!")
   }
   tmp := middle["total"].(float64)
   total = int(tmp)
   return total, nil
}
func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
    defer elapsed("page")()
    timeout := time.Duration(10 * time.Second)
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
    request.Header.Set("Content-type", "application/json")
   //defer elapsed("page")()
   timeout := time.Duration(100 * time.Second)
   client := http.Client{
      Timeout: timeout,
   }
   request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
   request.Header.Set("Content-type", "application/json")
    if err != nil {
        fmt.Println("build request fail !")
        return nil, err
    }
   if err != nil {
      fmt.Println("build request fail !")
      return nil, err
   }
    resp, err := client.Do(request)
    if err != nil{
        fmt.Println("request error: ", err)
        return nil, err
    }
   resp, err := client.Do(request)
   if err != nil {
      fmt.Println("request error: ", err)
      return nil, err
   }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return nil , err
    }
    return body, nil
   defer resp.Body.Close()
   body, err := ioutil.ReadAll(resp.Body)
   if err != nil {
      fmt.Println(err)
      return nil, err
   }
   return body, nil
}
// 计算时间
@@ -437,8 +749,8 @@
}
// 赋值时检测是否能够赋值
func Isnil(key string, ok bool){
    if !ok {
            fmt.Println(key, "is nil can not asign")
    }
}
//func //Isnil(key string, ok bool){
//    if !ok {
//            fmt.Println(key, "is nil can not asign")
//    }
//}