summaryrefslogtreecommitdiff
path: root/internal/mapr/aggregateset.go
blob: d8705bd8d772b0ebfe2cbda6e81aadb499a1b87a (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package mapr

import (
	"context"
	"encoding/base64"
	"fmt"
	"strconv"
	"strings"
)

// AggregateSet represents aggregated key/value pairs from the
// MAPREDUCE log lines. These could be either string values or float
// values.
type AggregateSet struct {
	Samples int
	FValues map[string]float64
	SValues map[string]string
}

// NewAggregateSet creates a new empty aggregate set.
func NewAggregateSet() *AggregateSet {
	return &AggregateSet{
		FValues: make(map[string]float64),
		SValues: make(map[string]string),
	}
}

// String representation of aggregate set.
func (s *AggregateSet) String() string {
	return fmt.Sprintf("AggregateSet(Samples:%d,FValues:%v,SValues:%v)",
		s.Samples, s.FValues, s.SValues)
}

// Merge one aggregate set into this one.
func (s *AggregateSet) Merge(query *Query, set *AggregateSet) error {
	s.Samples += set.Samples
	//logger.Trace("Merge", set)

	for _, sc := range query.Select {
		storage := sc.FieldStorage
		switch sc.Operation {
		case Count:
			fallthrough
		case Sum:
			fallthrough
		case Avg:
			value := set.FValues[storage]
			s.addFloat(storage, value)
		case Min:
			value := set.FValues[storage]
			s.addFloatMin(storage, value)
		case Max:
			value := set.FValues[storage]
			s.addFloatMax(storage, value)
		case Last:
			value := set.SValues[storage]
			s.setString(storage, value)
		case Len:
			s.setString(storage, set.SValues[storage])
			s.setFloat(storage, set.FValues[storage])
		default:
			return fmt.Errorf("Unknown aggregation method '%v'", sc.Operation)
		}
	}
	return nil
}

// Serialize the aggregate set so it can be sent over the wire.
func (s *AggregateSet) Serialize(ctx context.Context, groupKey string, ch chan<- string) {
	//logger.Trace("Serialising mapr.AggregateSet", s)
	var sb strings.Builder

	sb.WriteString(groupKey)
	sb.WriteString("|")
	sb.WriteString(fmt.Sprintf("%d|", s.Samples))

	for k, v := range s.FValues {
		sb.WriteString(k)
		sb.WriteString("=")
		sb.WriteString(fmt.Sprintf("%v|", v))
	}

	for k, v := range s.SValues {
		sb.WriteString(k)
		sb.WriteString("=")
		if k == "$line" {
			sb.WriteString(base64.StdEncoding.EncodeToString([]byte(v)))
			sb.WriteString("|")
			continue
		}
		sb.WriteString(v)
		sb.WriteString("|")
	}

	select {
	case ch <- sb.String():
	case <-ctx.Done():
	}
}

// Add a float value.
func (s *AggregateSet) addFloat(key string, value float64) {
	if _, ok := s.FValues[key]; !ok {
		s.FValues[key] = value
		return
	}
	s.FValues[key] += value
}

// Add a float minimum value.
func (s *AggregateSet) addFloatMin(key string, value float64) {
	f, ok := s.FValues[key]
	if !ok {
		s.FValues[key] = value
		return
	}

	if f > value {
		s.FValues[key] = value
	}
}

// Add a float maximum value.
func (s *AggregateSet) addFloatMax(key string, value float64) {
	f, ok := s.FValues[key]
	if !ok {
		s.FValues[key] = value
		return
	}

	if f < value {
		s.FValues[key] = value
	}
}

// Set a string.
func (s *AggregateSet) setString(key, value string) {
	s.SValues[key] = value
}

// Set a float.
func (s *AggregateSet) setFloat(key string, value float64) {
	s.FValues[key] = value
}

// Aggregate data to the aggregate set.
func (s *AggregateSet) Aggregate(key string, agg AggregateOperation, value string, clientAggregation bool) (err error) {
	var f float64

	// First check if we can aggregate anything without converting value to float.
	switch agg {
	case Count:
		if clientAggregation {
			f, err = strconv.ParseFloat(value, 64)
			if err != nil {
				return
			}
			s.addFloat(key, f)
			return
		}
		s.addFloat(key, 1)
		return
	case Last:
		s.setString(key, value)
		return
	case Len:
		s.setString(key, value)
		s.setFloat(key, float64(len(value)))
		return
	default:
	}

	// No, we have to convert to float.
	f, err = strconv.ParseFloat(value, 64)
	if err != nil {
		return
	}

	switch agg {
	case Sum:
		fallthrough
	case Avg:
		s.addFloat(key, f)
	case Min:
		s.addFloatMin(key, f)
	case Max:
		s.addFloatMax(key, f)
	default:
		err = fmt.Errorf("Unknown aggregation method '%v'", agg)
	}
	return
}