-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat.go
More file actions
146 lines (119 loc) · 3.55 KB
/
format.go
File metadata and controls
146 lines (119 loc) · 3.55 KB
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
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"strings"
)
// Structures returned by OpenSearch for specific aggregations
type PresentAggregation struct {
Buckets struct {
Present struct {
Count int `json:"doc_count"`
} `json:"present"`
Absent struct {
Count int `json:"doc_count"`
} `json:"absent"`
} `json:"buckets"`
}
type DistributionAggregation struct {
DocCountErrorUpperBound int `json:"doc_count_error_upper_bound"`
OtherCount int `json:"sum_other_doc_count"`
Buckets []struct {
Value string `json:"key"`
Count int `json:"doc_count"`
} `json:"buckets"`
}
// Structures for API response
type PresentResponse struct {
Field string `json:"field"`
Count int `json:"count"`
Absent int `json:"absent_count"`
Percent float32 `json:"percent"`
}
type DistributionResponse struct {
Field string `json:"field"`
Values []DistributionValue `json:"values"`
}
type DistributionValue struct {
Value string `json:"value"`
Count int `json:"count"`
Percent float32 `json:"percent"`
}
type APIResponse struct {
Present []PresentResponse `json:"present"`
Distribution []DistributionResponse `json:"distribution"`
}
func ParseSearchResp(resp OSResponse) (*APIResponse, error) {
var aggs map[string]json.RawMessage
if err := json.Unmarshal(resp.Aggregations, &aggs); err != nil {
return nil, fmt.Errorf("unmarshal aggregations: %w", err)
}
var response APIResponse
for key, value := range aggs {
isPresentAgg := strings.HasPrefix(key, "present_")
isDistributionAgg := strings.HasPrefix(key, "distribution_")
if !isPresentAgg && !isDistributionAgg {
continue
}
if isPresentAgg {
present, err := ParsePresentAgg(key, value)
if err != nil {
return nil, err
}
response.Present = append(response.Present, *present)
}
if isDistributionAgg {
distribution, err := ParseDistributionAgg(key, value)
if err != nil {
return nil, err
}
response.Distribution = append(response.Distribution, *distribution)
}
}
return &response, nil
}
func ParsePresentAgg(key string, value json.RawMessage) (*PresentResponse, error) {
var present PresentAggregation
if err := json.Unmarshal(value, &present); err != nil {
return nil, fmt.Errorf("unmarshal present aggregation %s: %w", key, err)
}
presentCount := present.Buckets.Present.Count
absentCount := present.Buckets.Absent.Count
totalCount := presentCount + absentCount
response := &PresentResponse{
Field: strings.TrimPrefix(key, "present_"),
Count: presentCount,
Absent: absentCount,
Percent: calcPercent(presentCount, totalCount),
}
return response, nil
}
func ParseDistributionAgg(key string, value json.RawMessage) (*DistributionResponse, error) {
var distribution DistributionAggregation
if err := json.Unmarshal(value, &distribution); err != nil {
return nil, fmt.Errorf("unmarshal distribution aggregation %s: %w", key, err)
}
totalCount := distribution.OtherCount
for _, value := range distribution.Buckets {
totalCount += value.Count
}
response := &DistributionResponse{
Field: strings.TrimPrefix(key, "distribution_"),
Values: make([]DistributionValue, 0, len(distribution.Buckets)),
}
for _, value := range distribution.Buckets {
value := DistributionValue{
Value: value.Value,
Count: value.Count,
Percent: calcPercent(value.Count, totalCount),
}
response.Values = append(response.Values, value)
}
return response, nil
}
func calcPercent(part, total int) float32 {
if total == 0 {
return 0
}
return float32(part) * 100 / float32(total)
}