File size: 8,734 Bytes
d6f631f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package testutils

import (
	"context"
	"fmt"
	"slices"
	"strings"
	"testing"
	"time"

	"github.com/samber/lo"

	"github.com/openmeterio/openmeter/openmeter/meter"
	"github.com/openmeterio/openmeter/openmeter/streaming"
	"github.com/openmeterio/openmeter/pkg/filter"
)

var _ streaming.Connector = &MockStreamingConnector{}

func NewMockStreamingConnector(t testing.TB) *MockStreamingConnector {
	t.Helper()
	out := &MockStreamingConnector{}
	out.Reset()
	return out
}

type SimpleEvent struct {
	MeterSlug string
	Value     float64
	Time      time.Time
	StoredAt  time.Time
}

type MockStreamingConnector struct {
	rows   map[string][]meter.MeterQueryRow
	events map[string][]SimpleEvent
}

func (m *MockStreamingConnector) Reset() {
	m.rows = map[string][]meter.MeterQueryRow{}
	m.events = map[string][]SimpleEvent{}
}

type AddOption func(event *SimpleEvent)

func WithStoredAt(storedAt time.Time) AddOption {
	return func(event *SimpleEvent) {
		event.StoredAt = storedAt
	}
}

func (m *MockStreamingConnector) AddSimpleEvent(meterSlug string, value float64, at time.Time, opts ...AddOption) {
	event := SimpleEvent{
		MeterSlug: meterSlug,
		Value:     value,
		Time:      at,
		StoredAt:  at,
	}
	for _, opt := range opts {
		opt(&event)
	}
	m.events[meterSlug] = append(m.events[meterSlug], event)
	m.sortMeterEvents(meterSlug)
}

func (m *MockStreamingConnector) SetSimpleEvents(meterSlug string, fn func(events []SimpleEvent) []SimpleEvent) {
	if _, ok := m.events[meterSlug]; !ok {
		m.events[meterSlug] = []SimpleEvent{}
	}
	m.events[meterSlug] = fn(m.events[meterSlug])
	m.sortMeterEvents(meterSlug)
}

func (m *MockStreamingConnector) AddRow(meterSlug string, row meter.MeterQueryRow) {
	m.rows[meterSlug] = append(m.rows[meterSlug], row)
}

func (m *MockStreamingConnector) sortMeterEvents(meterSlug string) {
	// Let's sort events by Time ASC
	slices.SortStableFunc(m.events[meterSlug], func(a, b SimpleEvent) int {
		return a.Time.Compare(b.Time)
	})
}

func (c *MockStreamingConnector) CreateNamespace(ctx context.Context, namespace string) error {
	return nil
}

func (c *MockStreamingConnector) DeleteNamespace(ctx context.Context, namespace string) error {
	return nil
}

func (m *MockStreamingConnector) CountEvents(ctx context.Context, namespace string, params streaming.CountEventsParams) ([]streaming.CountEventRow, error) {
	return []streaming.CountEventRow{}, nil
}

func (m *MockStreamingConnector) ListEvents(ctx context.Context, namespace string, params streaming.ListEventsParams) ([]streaming.RawEvent, error) {
	return []streaming.RawEvent{}, nil
}

func (m *MockStreamingConnector) ListEventsV2(ctx context.Context, params streaming.ListEventsV2Params) ([]streaming.RawEvent, error) {
	return []streaming.RawEvent{}, nil
}

// Returns the result query set for the given params. If the query set is not found,
// it will try to approximate the result by aggregating the simple events
func (m *MockStreamingConnector) QueryMeter(ctx context.Context, namespace string, mm meter.Meter, params streaming.QueryParams) ([]meter.MeterQueryRow, error) {
	rows := []meter.MeterQueryRow{}
	_, rowOk := m.rows[mm.Key]

	if rowOk {
		for _, row := range m.rows[mm.Key] {
			if row.WindowStart.Equal(*params.From) && row.WindowEnd.Equal(*params.To) {
				rows = append(rows, row)
			}
		}
	} else {
		row, err := m.aggregateEvents(mm, params)
		if err != nil {
			return rows, err
		}
		rows = append(rows, row...)
	}

	return rows, nil
}

func (m *MockStreamingConnector) BatchInsert(ctx context.Context, events []streaming.RawEvent) error {
	return nil
}

func (m *MockStreamingConnector) ValidateJSONPath(ctx context.Context, jsonPath string) (bool, error) {
	return strings.HasPrefix(jsonPath, "$."), nil
}

func (m *MockStreamingConnector) windowSizeDuration(windowSize meter.WindowSize) time.Duration {
	switch windowSize {
	case meter.WindowSizeMinute:
		return time.Minute
	case meter.WindowSizeHour:
		return time.Hour
	case meter.WindowSizeDay:
		return 24 * time.Hour
	default:
		return 0
	}
}

// filterStoredAt evaluates a FilterTimeUnix predicate against storedAt using Unix-second precision,
// matching the ClickHouse stored_at column behavior. Composite $and/$or filters are evaluated
// recursively.
func filterStoredAt(f *filter.FilterTimeUnix, storedAt time.Time) bool {
	if f == nil || f.IsEmpty() {
		return true
	}

	unix := storedAt.Unix()

	switch {
	case f.Gt != nil:
		return unix > f.Gt.Unix()
	case f.Gte != nil:
		return unix >= f.Gte.Unix()
	case f.Lt != nil:
		return unix < f.Lt.Unix()
	case f.Lte != nil:
		return unix <= f.Lte.Unix()
	case f.And != nil:
		for _, sub := range *f.And {
			if !filterStoredAt(&filter.FilterTimeUnix{FilterTime: sub}, storedAt) {
				return false
			}
		}
		return true
	case f.Or != nil:
		for _, sub := range *f.Or {
			if filterStoredAt(&filter.FilterTimeUnix{FilterTime: sub}, storedAt) {
				return true
			}
		}
		return false
	default:
		return true
	}
}

// We approximate the actual logic by a simple filter + aggregation for most cases
func (m *MockStreamingConnector) aggregateEvents(mm meter.Meter, params streaming.QueryParams) ([]meter.MeterQueryRow, error) {
	events, ok := m.events[mm.Key]
	if !ok {
		return []meter.MeterQueryRow{}, meter.NewMeterNotFoundError(mm.Key)
	}

	if params.From == nil || params.To == nil {
		return nil, fmt.Errorf("streaming mock connector does not support filtering without from and to")
	}

	if params.FilterStoredAt != nil && !params.FilterStoredAt.IsEmpty() {
		events = lo.Filter(events, func(event SimpleEvent, _ int) bool {
			return filterStoredAt(params.FilterStoredAt, event.StoredAt)
		})
	}

	// Let's truncate the window size to the second, as clickhouse does not support sub-second precision
	from := params.From.Truncate(streaming.MinimumWindowSizeDuration)
	to := params.To.Truncate(streaming.MinimumWindowSizeDuration)

	rows := make([]meter.MeterQueryRow, 0)

	if params.WindowSize != nil && params.WindowTimeZone != nil {
		// TODO: windowtimezone will be ignored

		windowingStart, _ := params.WindowSize.Truncate(from) // The first truncated time that from query falls into
		windowingEnd, _ := params.WindowSize.Truncate(to)     // The last truncated time that to query falls into
		if !to.Equal(windowingEnd) {
			windowingEnd, _ = params.WindowSize.AddTo(windowingEnd)
		}

		numOfWindows := int(windowingEnd.Sub(windowingStart).Seconds()) / int(m.windowSizeDuration(*params.WindowSize).Seconds())

		if numOfWindows == 0 {
			return nil, fmt.Errorf("couldnt calculate windows")
		}

		for i := 0; i < numOfWindows; i++ {
			rows = append(rows, meter.MeterQueryRow{
				Value:       0,
				WindowStart: windowingStart.Add(m.windowSizeDuration(*params.WindowSize) * time.Duration(i)),
				WindowEnd:   windowingStart.Add(m.windowSizeDuration(*params.WindowSize) * time.Duration(i+1)),
				GroupBy:     map[string]*string{},
			})
		}
	} else {
		rows = append(rows, meter.MeterQueryRow{
			Value:       0,
			WindowStart: from,
			WindowEnd:   to,
			GroupBy:     map[string]*string{},
		})
	}

	for i := range rows {
		row := &rows[i]
		var value float64

		effectiveWindowSize := lo.FromPtrOr(params.WindowSize, streaming.MinimumWindowSize)

		for _, event := range events {
			eventWindowStart, err := effectiveWindowSize.Truncate(event.Time)
			if err != nil {
				return nil, fmt.Errorf("failed to truncate by windowsize in event aggregation")
			}
			// windowend is exclusive when doing this rounding
			eventWindowEnd, err := effectiveWindowSize.AddTo(eventWindowStart)
			if err != nil {
				return nil, fmt.Errorf("failed calculate window end in event aggregation")
			}

			if (eventWindowStart.After(row.WindowStart) || eventWindowStart.Equal(row.WindowStart)) &&
				(eventWindowEnd.Before(row.WindowEnd) || eventWindowEnd.Equal(row.WindowEnd)) {
				// TODO: Add support for more aggregation types
				switch mm.Aggregation {
				case meter.MeterAggregationLatest:
					// Note: events are already sorted by time ASC when they are registered
					value = event.Value
				default:
					value += event.Value
				}
			}
		}
		rows[i].Value = value
	}

	// Clickhouse doesn't return tumpled result rows if there are no rows (events) in the tumpled period
	// To simulate this for the SUM behavior, we simply filter out rows that have 0 value
	rows = lo.Filter(rows, func(row meter.MeterQueryRow, _ int) bool {
		return row.Value != 0
	})

	return rows, nil
}

func (m *MockStreamingConnector) ListSubjects(ctx context.Context, params streaming.ListSubjectsParams) ([]string, error) {
	return []string{}, nil
}

func (m *MockStreamingConnector) ListGroupByValues(ctx context.Context, params streaming.ListGroupByValuesParams) ([]string, error) {
	return []string{}, nil
}