gocast icon indicating copy to clipboard operation
gocast copied to clipboard

Influxdb viewer stats

Open mono424 opened this issue 2 years ago • 2 comments

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

mono424 avatar Oct 02 '22 15:10 mono424

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 💯

mono424 avatar Oct 03 '22 13:10 mono424

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:

image

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)
	}

joschahenningsen avatar Oct 04 '22 07:10 joschahenningsen

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.

joschahenningsen avatar Sep 20 '23 15:09 joschahenningsen