summaryrefslogtreecommitdiff
path: root/internal/ingester/auto_test.go
blob: fc1c423af157694899284e12b7fe20a69912bc1d (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
package ingester

import (
	"context"
	"testing"
	"time"

	"epimetheus/internal/config"
	"epimetheus/internal/metrics"
)

func TestDetermineMode(t *testing.T) {
	tests := []struct {
		name      string
		timestamp time.Time
		want      config.Mode
	}{
		{
			name:      "current time is realtime",
			timestamp: time.Now(),
			want:      config.ModeRealtime,
		},
		{
			name:      "1 minute ago is realtime",
			timestamp: time.Now().Add(-1 * time.Minute),
			want:      config.ModeRealtime,
		},
		{
			name:      "4 minutes ago is realtime",
			timestamp: time.Now().Add(-4 * time.Minute),
			want:      config.ModeRealtime,
		},
		{
			name:      "6 minutes ago is historic",
			timestamp: time.Now().Add(-6 * time.Minute),
			want:      config.ModeHistoric,
		},
		{
			name:      "1 hour ago is historic",
			timestamp: time.Now().Add(-1 * time.Hour),
			want:      config.ModeHistoric,
		},
		{
			name:      "1 day ago is historic",
			timestamp: time.Now().Add(-24 * time.Hour),
			want:      config.ModeHistoric,
		},
		{
			name:      "exactly 5 minutes is historic (edge case)",
			timestamp: time.Now().Add(-5 * time.Minute),
			want:      config.ModeHistoric,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := DetermineMode(tt.timestamp)
			if got != tt.want {
				age := time.Since(tt.timestamp)
				t.Errorf("DetermineMode() = %v, want %v (age: %v)", got, tt.want, age)
			}
		})
	}
}

func TestGroupSamplesByMode(t *testing.T) {
	now := time.Now()
	samples := []metrics.Sample{
		{MetricName: "metric1", Timestamp: now.Add(-1 * time.Minute)},  // realtime
		{MetricName: "metric2", Timestamp: now.Add(-2 * time.Minute)},  // realtime
		{MetricName: "metric3", Timestamp: now.Add(-10 * time.Minute)}, // historic
		{MetricName: "metric4", Timestamp: now.Add(-1 * time.Hour)},    // historic
		{MetricName: "metric5", Timestamp: now.Add(-30 * time.Second)}, // realtime
	}

	realtime, historic := groupSamplesByMode(samples)

	if len(realtime) != 3 {
		t.Errorf("Got %d realtime samples, want 3", len(realtime))
	}
	if len(historic) != 2 {
		t.Errorf("Got %d historic samples, want 2", len(historic))
	}

	// Verify correct grouping
	for _, s := range realtime {
		if DetermineMode(s.Timestamp) != config.ModeRealtime {
			t.Errorf("Sample %s incorrectly grouped as realtime (age: %v)", s.MetricName, s.Age())
		}
	}
	for _, s := range historic {
		if DetermineMode(s.Timestamp) != config.ModeHistoric {
			t.Errorf("Sample %s incorrectly grouped as historic (age: %v)", s.MetricName, s.Age())
		}
	}
}

func TestFormatDuration(t *testing.T) {
	tests := []struct {
		name     string
		duration time.Duration
		want     string
	}{
		{
			name:     "seconds",
			duration: 45 * time.Second,
			want:     "45 seconds",
		},
		{
			name:     "minutes",
			duration: 5 * time.Minute,
			want:     "5 minutes",
		},
		{
			name:     "hours",
			duration: 2*time.Hour + 30*time.Minute,
			want:     "2.5 hours",
		},
		{
			name:     "days",
			duration: 36 * time.Hour,
			want:     "1.5 days",
		},
		{
			name:     "less than minute",
			duration: 30 * time.Second,
			want:     "30 seconds",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := formatDuration(tt.duration)
			if got != tt.want {
				t.Errorf("formatDuration() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestAutoIngester_Ingest_EmptySamples(t *testing.T) {
	collectors := metrics.NewCollectors()
	autoIngester := NewAutoIngester(collectors)
	ctx := context.Background()
	cfg := config.NewConfig()

	err := autoIngester.Ingest(ctx, []metrics.Sample{}, cfg)
	if err == nil {
		t.Error("Expected error for empty samples, got nil")
	}
	if err.Error() != "no samples to ingest" {
		t.Errorf("Expected 'no samples to ingest' error, got: %v", err)
	}
}

func TestAutoIngester_New(t *testing.T) {
	collectors := metrics.NewCollectors()
	ingester := NewAutoIngester(collectors)

	// Verify ingester was created with components
	if ingester.collectors.RequestsTotal == nil {
		t.Error("AutoIngester.collectors not initialized properly")
	}
}