package esutil import ( "bytes" "encoding/json" "errors" "fmt" "io/ioutil" "net/http" "time" "basic.com/pubsub/protomsg.git" ) 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 Videopersonsbyid(sources []map[string]interface{}) (tmpinfos []protomsg.Videopersons) { var ok bool //var tmpinfo protomsg.Videopersons for _, source := range sources{ tmpinfo := protomsg.Videopersons{} 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.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) 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 } //解析底库人员结构 func Dbpersonbyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbperson) { var ok bool //var tmpinfo protomsg.Dbperson for _, source := range sources{ tmpinfo := protomsg.Dbperson{} 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 } 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) 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) //根据 tableid 获取 tablename //name, _:= Dbtablename(tmpinfo.TableId) //tmpinfo.TableName= name tmpinfos = append(tmpinfos, tmpinfo) } return tmpinfos } //解析底库结构 func Dbtablebyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbtable) { 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) 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) 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) 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{}") } 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 } //slice scroll 解析工具函数 func Sourcelistforscroll(buf []byte)(datasource map[string]interface{}, err error){ var data = make(map[string]interface{}) 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{}") } scroll_id, ok := out["_scroll_id"].(string) if !ok { return nil, errors.New("first hits change error!") } //fmt.Println("middle: ",scroll_id) middle, ok := out["hits"].(map[string]interface{}) //fmt.Println("middle: ",out) if !ok { return nil, errors.New("first hits change error!") } 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!") continue } source, ok := tmpbuf["_source"].(map[string]interface{}) if !ok { fmt.Println("change _source error!") continue } sources = append(sources, source ) } data["sourcelist"] = sources data["scroll_id"] = scroll_id return data,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") 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 } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return nil , err } return body, nil } // 计算时间 func elapsed(what string) func() { start := time.Now() return func() { fmt.Printf("%s took %v\n", what, time.Since(start)) } } // 赋值时检测是否能够赋值 func Isnil(key string, ok bool){ if !ok { fmt.Println(key, "is nil can not asign") } }