Skip to content

Commit 89a5de0

Browse files
committed
Support in-memory telemetry/stats database
1 parent 8007600 commit 89a5de0

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ manually, you can install newer version of Go into your `GOPATH`:
106106
# redact IP addresses
107107
redact_ip_addresses=false
108108
109-
# database type for statistics data, currently supports: none, bolt, mysql, postgresql
109+
# database type for statistics data, currently supports: none, memory, bolt, mysql, postgresql
110110
# if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated
111111
database_type="postgresql"
112112
database_hostname="localhost"

database/database.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"github.com/librespeed/speedtest/config"
55
"github.com/librespeed/speedtest/database/bolt"
6+
"github.com/librespeed/speedtest/database/memory"
67
"github.com/librespeed/speedtest/database/mysql"
78
"github.com/librespeed/speedtest/database/none"
89
"github.com/librespeed/speedtest/database/postgresql"
@@ -29,6 +30,8 @@ func SetDBInfo(conf *config.Config) {
2930
DB = mysql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName)
3031
case "bolt":
3132
DB = bolt.Open(conf.DatabaseFile)
33+
case "memory":
34+
DB = memory.Open("")
3235
case "none":
3336
DB = none.Open("")
3437
default:

database/memory/memory.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package memory
2+
3+
import (
4+
"errors"
5+
"sync"
6+
"time"
7+
8+
"github.com/librespeed/speedtest/database/schema"
9+
)
10+
11+
const (
12+
// just enough records to return for FetchLast100
13+
maxRecords = 100
14+
)
15+
16+
type Memory struct {
17+
lock sync.RWMutex
18+
records []schema.TelemetryData
19+
}
20+
21+
func Open(_ string) *Memory {
22+
return &Memory{}
23+
}
24+
25+
func (mem *Memory) Insert(data *schema.TelemetryData) error {
26+
mem.lock.Lock()
27+
defer mem.lock.Unlock()
28+
data.Timestamp = time.Now()
29+
mem.records = append(mem.records, *data)
30+
if len(mem.records) > maxRecords {
31+
mem.records = mem.records[len(mem.records)-maxRecords:]
32+
}
33+
return nil
34+
}
35+
36+
func (mem *Memory) FetchByUUID(uuid string) (*schema.TelemetryData, error) {
37+
mem.lock.RLock()
38+
defer mem.lock.RUnlock()
39+
for _, record := range mem.records {
40+
if record.UUID == uuid {
41+
return &record, nil
42+
}
43+
}
44+
return nil, errors.New("record not found")
45+
}
46+
47+
func (mem *Memory) FetchLast100() ([]schema.TelemetryData, error) {
48+
mem.lock.RLock()
49+
defer mem.lock.RUnlock()
50+
return mem.records, nil
51+
}

settings.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ statistics_password="PASSWORD"
1818
# redact IP addresses
1919
redact_ip_addresses=false
2020

21-
# database type for statistics data, currently supports: none, bolt, mysql, postgresql
21+
# database type for statistics data, currently supports: none, memory, bolt, mysql, postgresql
2222
# if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated
2323
database_type="bolt"
2424
database_hostname=""

0 commit comments

Comments
 (0)