sunty
2024-04-01 93b06fcef74655e95fb5500e7d49be8254064f5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package esutil
 
import(
    "math"
    "testing"
        "fmt"
        "encoding/json"
    "time"
)
 
func TestEsReq(t  *testing.T){
    requstbody, err := json.Marshal(map[string]interface{}{
            "content":"v2.0.0",
            "collection": "0",
            })
    if  err != nil {
            fmt.Println("json parse error ", err) 
            return 
 
    }
 
    EsReq("PUT","http://192.168.1.203:9200/personaction/perVideoAction/fcddbddf-4580-43f3-b79a-32343c823df2?pretty", requstbody)
}
 
func TestGetDbinfo(t  *testing.T){
    dbinfos, err := DbPersoninfos(0,10)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(dbinfos)
}
func FaceSourceAggregations(buf []byte, thresholdTime int, thresholdStayTime int) (sources []map[string]interface{}, err error) {
    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 {
        hitsSources := make([]map[string]interface{}, 0)
        topAttentionHits := inf.(map[string]interface{})["top_attention_hits"].(map[string]interface{})
        middleHits := topAttentionHits["hits"].(map[string]interface{})
        finalHits := middleHits["hits"].([]interface{})
        startTime := ""
        indexLength := len(finalHits)
        point := 0
        for _, in := range finalHits {
            point = point + 1
            tmpHitSource := make(map[string]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
            }
            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("时间解析错误")
            }
 
            sTime := tmpTime
            eTime := mTime.Add(time.Second * 1).Format("2006-01-02 15:04:05")
            stayTime := 1.0
            if startTime != "" && point <= indexLength {
                sinTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, loc)
                passTime := math.Abs(mTime.Sub(sinTime).Seconds())
                hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
                //fmt.Println("passTime:   ", passTime)
                if int(passTime) <= thresholdTime {
                    if point == indexLength {
                        hitStartTime := hitsSources[len(hitsSources)-1]["startTime"].(string)
                        realStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitStartTime, loc)
                        stayTime = math.Abs(mTime.Sub(realStartTime).Seconds())
                        hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
                        startTime = ""
                    } else {
                        startTime = tmpTime
                        hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
                        continue
                    }
 
                } else {
                    hitStartTime := hitsSources[len(hitsSources)-1]["startTime"].(string)
                    hitEndTime := hitsSources[len(hitsSources)-1]["endTime"].(string)
                    realStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitStartTime, loc)
                    realEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitEndTime, loc)
                    stayTime = math.Abs(realEndTime.Sub(realStartTime).Seconds())
                    if sinTime.Sub(mTime).Seconds() == 0 {
                        sinTime.Add(time.Second * 1)
                        sinTime.Format("2006-01-02 15:04:05")
                        hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
                        stayTime = 1
                    }
                    hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
                    if point == indexLength {
                        stayTime = 1
                    }
                    startTime = ""
                }
            }
            //fmt.Println("========================================================")
            startTime = tmpTime
            tmpHitSource["faceId"] = baseInfo["targetId"].(string)
            if targetInfo["areaId"] == nil {
                continue
            }
            tmpHitSource["areaId"] = targetInfo["areaId"].(string)
            tmpHitSource["startTime"] = sTime
            tmpHitSource["faceImg"] = targetInfo["picSmUrl"].(string)
            tmpHitSource["endTime"] = eTime
            tmpHitSource["stayTime"] = stayTime
            hitsSources = append(hitsSources, tmpHitSource)
        }
        allSource = append(allSource, hitsSources...)
    }
    return allSource, nil
}