/
query_processor_bool.go
61 lines (51 loc) · 1.65 KB
/
query_processor_bool.go
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
/* _ _
*__ _____ __ ___ ___ __ _| |_ ___
*\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
* \ V V / __/ (_| |\ V /| | (_| | || __/
* \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
*
* Copyright © 2016 - 2019 Weaviate. All rights reserved.
* LICENSE: https://github.com/creativesoftwarefdn/weaviate/blob/develop/LICENSE.md
* DESIGN & CONCEPT: Bob van Luijt (@bobvanluijt)
* CONTACT: hello@creativesoftwarefdn.org
*/
package meta
import "fmt"
func (p *Processor) postProcessBoolGroupCount(m map[string]interface{}) (map[string]interface{}, error) {
// we can safely ignore the check, as this method is only ever called if we
// can be sure that we have this prop
boolProps, _ := m[BoolGroupCount]
boolPropsMap, ok := boolProps.(map[string]interface{})
if !ok {
return nil, fmt.Errorf(
"could not post-process boolean totals, expected result to be a map, but was '%#v'", boolProps)
}
totalTrue, err := getTotal(boolPropsMap, "true")
if err != nil {
return nil, err
}
totalFalse, err := getTotal(boolPropsMap, "false")
if err != nil {
return nil, err
}
total := totalTrue + totalFalse
return map[string]interface{}{
"totalTrue": totalTrue,
"totalFalse": totalFalse,
"percentageTrue": totalTrue / total,
"percentageFalse": totalFalse / total,
}, nil
}
func getTotal(m map[string]interface{}, key string) (float64, error) {
total, ok := m[key]
if !ok {
// this would happen if every single prop is "false"
total = float64(0)
}
totalFloat, ok := total.(float64)
if !ok {
return 0.0, fmt.Errorf(
"total %s must be an float64, but we got '%t'", key, total)
}
return totalFloat, nil
}