Support in-memory telemetry/stats database

This commit is contained in:
Maddie Zhan 2021-09-18 02:19:14 +08:00
parent 800760054e
commit 89a5de0105
4 changed files with 56 additions and 2 deletions

View File

@ -106,7 +106,7 @@ manually, you can install newer version of Go into your `GOPATH`:
# redact IP addresses # redact IP addresses
redact_ip_addresses=false redact_ip_addresses=false
# database type for statistics data, currently supports: none, bolt, mysql, postgresql # database type for statistics data, currently supports: none, memory, bolt, mysql, postgresql
# if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated # if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated
database_type="postgresql" database_type="postgresql"
database_hostname="localhost" database_hostname="localhost"

View File

@ -3,6 +3,7 @@ package database
import ( import (
"github.com/librespeed/speedtest/config" "github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database/bolt" "github.com/librespeed/speedtest/database/bolt"
"github.com/librespeed/speedtest/database/memory"
"github.com/librespeed/speedtest/database/mysql" "github.com/librespeed/speedtest/database/mysql"
"github.com/librespeed/speedtest/database/none" "github.com/librespeed/speedtest/database/none"
"github.com/librespeed/speedtest/database/postgresql" "github.com/librespeed/speedtest/database/postgresql"
@ -29,6 +30,8 @@ func SetDBInfo(conf *config.Config) {
DB = mysql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName) DB = mysql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName)
case "bolt": case "bolt":
DB = bolt.Open(conf.DatabaseFile) DB = bolt.Open(conf.DatabaseFile)
case "memory":
DB = memory.Open("")
case "none": case "none":
DB = none.Open("") DB = none.Open("")
default: default:

51
database/memory/memory.go Normal file
View File

@ -0,0 +1,51 @@
package memory
import (
"errors"
"sync"
"time"
"github.com/librespeed/speedtest/database/schema"
)
const (
// just enough records to return for FetchLast100
maxRecords = 100
)
type Memory struct {
lock sync.RWMutex
records []schema.TelemetryData
}
func Open(_ string) *Memory {
return &Memory{}
}
func (mem *Memory) Insert(data *schema.TelemetryData) error {
mem.lock.Lock()
defer mem.lock.Unlock()
data.Timestamp = time.Now()
mem.records = append(mem.records, *data)
if len(mem.records) > maxRecords {
mem.records = mem.records[len(mem.records)-maxRecords:]
}
return nil
}
func (mem *Memory) FetchByUUID(uuid string) (*schema.TelemetryData, error) {
mem.lock.RLock()
defer mem.lock.RUnlock()
for _, record := range mem.records {
if record.UUID == uuid {
return &record, nil
}
}
return nil, errors.New("record not found")
}
func (mem *Memory) FetchLast100() ([]schema.TelemetryData, error) {
mem.lock.RLock()
defer mem.lock.RUnlock()
return mem.records, nil
}

View File

@ -18,7 +18,7 @@ statistics_password="PASSWORD"
# redact IP addresses # redact IP addresses
redact_ip_addresses=false redact_ip_addresses=false
# database type for statistics data, currently supports: none, bolt, mysql, postgresql # database type for statistics data, currently supports: none, memory, bolt, mysql, postgresql
# if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated # if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated
database_type="bolt" database_type="bolt"
database_hostname="" database_hostname=""