Skip to content

Commit

Permalink
Support none database type to disable stats/telemetry
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
maddie committed Sep 17, 2021
1 parent 7204ae2 commit 8c1aa6b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -106,7 +106,8 @@ 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: bolt, mysql, postgresql
# database type for statistics data, currently supports: none, 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"
database_name="speedtest"
Expand Down
4 changes: 1 addition & 3 deletions database/bolt/bolt.go
Expand Up @@ -5,12 +5,10 @@ import (
"errors"
"time"

"go.etcd.io/bbolt"

"github.com/librespeed/speedtest/database/schema"

_ "github.com/go-sql-driver/mysql"
log "github.com/sirupsen/logrus"
"go.etcd.io/bbolt"
)

const (
Expand Down
7 changes: 7 additions & 0 deletions database/database.go
Expand Up @@ -4,8 +4,11 @@ import (
"github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database/bolt"
"github.com/librespeed/speedtest/database/mysql"
"github.com/librespeed/speedtest/database/none"
"github.com/librespeed/speedtest/database/postgresql"
"github.com/librespeed/speedtest/database/schema"

log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -26,5 +29,9 @@ func SetDBInfo(conf *config.Config) {
DB = mysql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName)
case "bolt":
DB = bolt.Open(conf.DatabaseFile)
case "none":
DB = none.Open("")
default:
log.Fatalf("Unsupported database type: %s", conf.DatabaseType)
}
}
23 changes: 23 additions & 0 deletions database/none/none.go
@@ -0,0 +1,23 @@
package none

import (
"github.com/librespeed/speedtest/database/schema"
)

type None struct{}

func Open(_ string) *None {
return &None{}
}

func (n *None) Insert(_ *schema.TelemetryData) error {
return nil
}

func (n *None) FetchByUUID(_ string) (*schema.TelemetryData, error) {
return &schema.TelemetryData{}, nil
}

func (n *None) FetchLast100() ([]schema.TelemetryData, error) {
return []schema.TelemetryData{}, nil
}
7 changes: 7 additions & 0 deletions results/stats.go
Expand Up @@ -4,6 +4,7 @@ import (
"html/template"
"net/http"

"github.com/go-chi/render"
log "github.com/sirupsen/logrus"

"github.com/librespeed/speedtest/config"
Expand All @@ -27,6 +28,12 @@ func Stats(w http.ResponseWriter, r *http.Request) {
}

conf := config.LoadedConfig()

if conf.DatabaseType == "none" {
render.PlainText(w, r, "Statistics are disabled")
return
}

var data StatsData

if conf.StatsPassword == "PASSWORD" {
Expand Down
13 changes: 13 additions & 0 deletions results/telemetry.go
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/go-chi/render"
"github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database"
"github.com/librespeed/speedtest/database/schema"
Expand Down Expand Up @@ -144,6 +145,12 @@ func Initialize(c *config.Config) {
}

func Record(w http.ResponseWriter, r *http.Request) {
conf := config.LoadedConfig()
if conf.DatabaseType == "none" {
render.PlainText(w, r, "Telemetry is disabled")
return
}

ipAddr, _, _ := net.SplitHostPort(r.RemoteAddr)
userAgent := r.UserAgent()
language := r.Header.Get("Accept-Language")
Expand Down Expand Up @@ -201,6 +208,12 @@ func Record(w http.ResponseWriter, r *http.Request) {
}

func DrawPNG(w http.ResponseWriter, r *http.Request) {
conf := config.LoadedConfig()

if conf.DatabaseType == "none" {
return
}

uuid := r.FormValue("id")
record, err := database.DB.FetchByUUID(uuid)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion settings.toml
Expand Up @@ -18,7 +18,8 @@ statistics_password="PASSWORD"
# redact IP addresses
redact_ip_addresses=false

# database type for statistics data, currently supports: bolt, mysql, postgresql
# database type for statistics data, currently supports: none, 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=""
database_name=""
Expand Down

0 comments on commit 8c1aa6b

Please sign in to comment.