Support in-memory telemetry/stats database
This commit is contained in:
parent
800760054e
commit
89a5de0105
@ -106,7 +106,7 @@ manually, you can install newer version of Go into your `GOPATH`:
|
||||
# redact IP addresses
|
||||
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
|
||||
database_type="postgresql"
|
||||
database_hostname="localhost"
|
||||
|
@ -3,6 +3,7 @@ package database
|
||||
import (
|
||||
"github.com/librespeed/speedtest/config"
|
||||
"github.com/librespeed/speedtest/database/bolt"
|
||||
"github.com/librespeed/speedtest/database/memory"
|
||||
"github.com/librespeed/speedtest/database/mysql"
|
||||
"github.com/librespeed/speedtest/database/none"
|
||||
"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)
|
||||
case "bolt":
|
||||
DB = bolt.Open(conf.DatabaseFile)
|
||||
case "memory":
|
||||
DB = memory.Open("")
|
||||
case "none":
|
||||
DB = none.Open("")
|
||||
default:
|
||||
|
51
database/memory/memory.go
Normal file
51
database/memory/memory.go
Normal 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
|
||||
}
|
@ -18,7 +18,7 @@ statistics_password="PASSWORD"
|
||||
# redact IP addresses
|
||||
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
|
||||
database_type="bolt"
|
||||
database_hostname=""
|
||||
|
Loading…
Reference in New Issue
Block a user