gocast
gocast copied to clipboard
Influxdb viewer stats
Pushing viewer stats into the influxdb instead of mysql.
To test:
- setup a local influxdb
- retrieve api token from web interface (Load Data > API Token)
- add api token to
config.yaml
Looks good so far! The next step would be to get the stats back from influxdb to display them (e.g. at
/admin/course/123/stats
). Are you familiar with the flux query language? If you want I can write a few useful queries for you to use :)
Not yet, but I love to look into that. You would still appreciate the few useful queries 💯
Let's start with this for testing :)
Live viewers for a course:
from(bucket: "live_stats")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "viewers")
|> filter(fn: (r) => r["course"] == "224")
|> filter(fn: (r) => r["live"] == "true")
|> group(columns: ["stream"])
|> max()
|> group()
|> aggregateWindow(every: 1d, fn: median, createEmpty: false)
Produces 2022-10-04_09 36_influxdb_data.csv
Which looks like this:
This can be fetched and cleaned up for chart.js like this:
query, err := c.QueryAPI("rbg").Query(context.Background(), "from(bucket: \"live_statistics\")"+
"|> range(start: -1y, stop: now())"+
"|> filter(fn: (r) => r[\"_measurement\"] == \"viewers\")"+
"|> filter(fn: (r) => r[\"course\"] == \"224\")"+
"|> filter(fn: (r) => r[\"live\"] == \"true\")"+
"|> group(columns: [\"stream\"])"+
"|> max()"+
"|> group()"+
"|> aggregateWindow(every: 1d, fn: median, createEmpty: false)")
if err != nil {
panic(err)
}
var xAxis = make([]string, 0)
var yAxis = make([]int, 0)
for query.Next() {
xAxis = append(xAxis, query.Record().Time().Format("2006-01-02"))
strVal, ok := query.Record().Value().(string)
if !ok {
yAxis = append(yAxis, 0)
continue
}
n, err := strconv.Atoi(strVal)
if err != nil {
yAxis = append(yAxis, 0)
continue
}
yAxis = append(yAxis, n)
}
T'm afraid this needs to be closed. Sorry for letting this go stale, I suppose the best way to move forward here is integrate our grafana stack here at some point.