-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathresult_handler.go
More file actions
151 lines (132 loc) · 3.94 KB
/
Copy pathresult_handler.go
File metadata and controls
151 lines (132 loc) · 3.94 KB
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
// Copyright (c) 2024-2026, s0up and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package speedtest
import (
"context"
"fmt"
"net/url"
"time"
"github.com/rs/zerolog/log"
"github.com/autobrr/netronome/internal/database"
"github.com/autobrr/netronome/internal/notifications"
"github.com/autobrr/netronome/internal/types"
)
type DefaultResultHandler struct {
db database.Service
notifier *notifications.Notifier
}
func NewResultHandler(db database.Service, notifier *notifications.Notifier) *DefaultResultHandler {
return &DefaultResultHandler{
db: db,
notifier: notifier,
}
}
func (h *DefaultResultHandler) SaveResult(ctx context.Context, result *Result, testType string, opts *types.TestOptions) error {
log.Debug().
Str("test_type", testType).
Str("server", result.Server).
Float64("download_speed", result.DownloadSpeed).
Float64("upload_speed", result.UploadSpeed).
Str("latency", result.Latency).
Float64("jitter", result.Jitter).
Msg("Preparing to save test results to database")
var serverHost *string
var serverID string
switch testType {
case "iperf3":
serverHost = &opts.ServerHost
serverID = fmt.Sprintf("iperf3-%s", opts.ServerHost)
case "librespeed":
serverHost = &result.Server
serverID = fmt.Sprintf("librespeed-%s", result.Server)
case "url_download":
if opts.DownloadURL != "" {
// Custom URL
u, err := url.Parse(opts.DownloadURL)
if err == nil && u.Host != "" {
host := u.Host
serverHost = &host
serverID = fmt.Sprintf("url-custom-%s", u.Host)
} else {
serverID = "url-custom-unknown"
}
} else if len(opts.ServerIDs) > 0 {
// Built-in IDC
serverID = opts.ServerIDs[0]
serverHost = &opts.ServerHost
} else {
serverID = "url-unknown"
}
case "speedtest":
// For speedtest.net, we'll need to extract host info from the result
serverID = result.Server
}
var jitterPtr *float64
if result.Jitter > 0 {
jitterPtr = &result.Jitter
}
// Give the DB write up to 10s while still respecting upstream cancellations and values.
saveCtx, saveCancel := context.WithTimeout(ctx, 10*time.Second)
defer saveCancel()
createdAt := time.Now().UTC()
dbResult, err := h.db.SaveSpeedTest(saveCtx, types.SpeedTestResult{
ServerName: result.Server,
ServerID: serverID,
ServerHost: serverHost,
TestType: testType,
DownloadSpeed: result.DownloadSpeed,
UploadSpeed: result.UploadSpeed,
Latency: result.Latency,
Jitter: jitterPtr,
IsScheduled: opts.IsScheduled,
CreatedAt: createdAt,
})
if err != nil {
log.Error().Err(err).
Str("test_type", testType).
Str("server", result.Server).
Msg("Failed to save test result to database")
return err
}
if dbResult != nil {
result.ID = dbResult.ID
log.Debug().
Int64("result_id", dbResult.ID).
Str("test_type", testType).
Msg("Successfully saved test result to database")
h.SendNotification(dbResult)
}
return nil
}
func (h *DefaultResultHandler) SendNotification(result *types.SpeedTestResult) {
if h.notifier != nil {
// Convert types.SpeedTestResult to notifications.SpeedTestResult
notifResult := ¬ifications.SpeedTestResult{
ServerName: result.ServerName,
Provider: result.TestType,
Download: result.DownloadSpeed,
Upload: result.UploadSpeed,
Ping: parsePingValue(result.Latency),
Jitter: getJitterValue(result.Jitter),
ISP: "", // ISP not available in types.SpeedTestResult
Failed: false, // Assuming successful test if we got here
}
h.notifier.SendSpeedTestNotification(notifResult)
}
}
// parsePingValue extracts the numeric ping value from a latency string like "10.5ms"
func parsePingValue(latency string) float64 {
if latency == "" {
return 0
}
var value float64
fmt.Sscanf(latency, "%fms", &value)
return value
}
// getJitterValue safely dereferences a jitter pointer
func getJitterValue(jitter *float64) float64 {
if jitter == nil {
return 0
}
return *jitter
}