-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetrics.go
226 lines (190 loc) · 5.1 KB
/
metrics.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
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"log"
"os"
"sync"
"time"
)
// MetricEvent represents a single event that we want to log
type MetricEvent struct {
Timestamp time.Time
UserID string // This will store the hashed user ID
EventType string
Details map[string]interface{}
}
// MetricsManager handles the metrics collection and reporting with detailed logs
type MetricsManager struct {
enabled bool
fileMutex sync.Mutex
logs []MetricEvent
filePath string
ticker *time.Ticker
wg sync.WaitGroup
stopChan chan struct{}
}
// hashUserID creates a SHA-256 hash of the user ID
func hashUserID(userID string) string {
hasher := sha256.New()
hasher.Write([]byte(userID))
return hex.EncodeToString(hasher.Sum(nil))
}
// NewMetricsManager initializes a new metrics manager
func NewMetricsManager(enabled bool, filePath string, interval time.Duration) *MetricsManager {
mm := &MetricsManager{
enabled: enabled,
logs: []MetricEvent{},
filePath: filePath,
ticker: time.NewTicker(interval),
stopChan: make(chan struct{}),
}
if mm.enabled {
mm.loadAndHashExistingData()
mm.wg.Add(1)
go mm.run()
}
return mm
}
// loadAndHashExistingData loads existing data and ensures all user IDs are hashed
func (mm *MetricsManager) loadAndHashExistingData() {
mm.fileMutex.Lock()
defer mm.fileMutex.Unlock()
if _, err := os.Stat(mm.filePath); os.IsNotExist(err) {
return
}
file, err := os.ReadFile(mm.filePath)
if err != nil {
log.Printf("Error reading metrics file: %v", err)
return
}
var existingLogs []MetricEvent
if err := json.Unmarshal(file, &existingLogs); err != nil {
log.Printf("Error parsing metrics file: %v", err)
return
}
needsRehash := false
for _, event := range existingLogs {
if len(event.UserID) != 64 {
needsRehash = true
break
}
}
if needsRehash {
hashedLogs := make([]MetricEvent, len(existingLogs))
for i, event := range existingLogs {
hashedEvent := event
hashedEvent.UserID = hashUserID(event.UserID)
hashedLogs[i] = hashedEvent
}
mm.logs = hashedLogs
mm.saveToFile(false)
} else {
mm.logs = existingLogs
}
}
// logEvent logs an event with its details
func (mm *MetricsManager) logEvent(userID, eventType string, details map[string]interface{}) {
if !mm.enabled {
return
}
event := MetricEvent{
Timestamp: time.Now(),
UserID: hashUserID(userID),
EventType: eventType,
Details: details,
}
mm.fileMutex.Lock()
mm.logs = append(mm.logs, event)
mm.fileMutex.Unlock()
}
// logRequest logs a user request
func (mm *MetricsManager) logRequest(userID string) {
mm.logEvent(userID, "request", nil)
}
func (mm *MetricsManager) logFollow(userID string) {
mm.logEvent(userID, "follow", nil)
}
// logSuccessfulGeneration logs a successful alt-text generation
func (mm *MetricsManager) logSuccessfulGeneration(userID, mediaType string, responseTimeMillis int64) {
details := map[string]interface{}{
"mediaType": mediaType,
"responseTime": responseTimeMillis,
}
mm.logEvent(userID, "successful_generation", details)
}
// logRateLimitHit logs when a rate limit is hit
func (mm *MetricsManager) logRateLimitHit(userID string) {
mm.logEvent(userID, "rate_limit_hit", nil)
}
func (mm *MetricsManager) logNewAccountActivity(userID string) {
mm.logEvent(userID, "new_account_activity", nil)
}
func (mm *MetricsManager) logShadowBan(userID string) {
mm.logEvent(userID, "shadow_ban", nil)
}
func (mm *MetricsManager) logUnBan(userID string) {
mm.logEvent(userID, "un_ban", nil)
}
func (mm *MetricsManager) logWeeklySummary(userID string) {
mm.logEvent(userID, "weekly_summary", nil)
}
func (mm *MetricsManager) logMissingAltText(userID string) {
mm.logEvent(userID, "missing_alt_text", nil)
}
func (mm *MetricsManager) logAltTextReminderSent(userID string) {
mm.logEvent(userID, "alt_text_reminder_sent", nil)
}
// logConsentRequest logs a consent request
func (mm *MetricsManager) logConsentRequest(userID string, granted bool) {
details := map[string]interface{}{
"granted": granted,
}
mm.logEvent(userID, "consent_request", details)
}
// saveToFile writes the current metrics data to a file
func (mm *MetricsManager) saveToFile(lock bool) {
if lock {
mm.fileMutex.Lock()
defer mm.fileMutex.Unlock()
}
file, err := os.OpenFile(mm.filePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Error opening metrics file: %v", err)
return
}
defer file.Close()
if err := file.Truncate(0); err != nil {
log.Printf("Error truncating metrics file: %v", err)
return
}
if _, err := file.Seek(0, 0); err != nil {
log.Printf("Error seeking in metrics file: %v", err)
return
}
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(mm.logs); err != nil {
log.Printf("Error writing metrics to file: %v", err)
return
}
}
func (mm *MetricsManager) run() {
defer mm.wg.Done()
for {
select {
case <-mm.ticker.C:
mm.saveToFile(true)
case <-mm.stopChan:
mm.ticker.Stop()
mm.saveToFile(true)
return
}
}
}
// stop terminates the background metrics manager
func (mm *MetricsManager) stop() {
close(mm.stopChan)
mm.wg.Wait()
}