aboutsummaryrefslogtreecommitdiff
path: root/backend/stats.go
blob: e2ad08b262be28493c03abb081ac25f2757a698e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package backend

import (
	"context"
	"database/sql"
	"net/http"
	"regexp"
	"strings"
)

var trimRefererReg = regexp.MustCompile(`https?://([a-z-0-9.]+)(:\d+)?/.*`)

func UpdateStats(ctx context.Context, db *sql.DB, r *http.Request) error {
	target := r.URL.Path
	if strings.HasPrefix(target, "/assets") || strings.HasPrefix(target, "/static") {
		return nil
	}
	ref := r.Header.Get("Referer")
	if ref == "" {
		return nil
	}
	subs := trimRefererReg.FindStringSubmatch(ref)
	if len(subs) < 2 {
		return nil
	}
	ref = subs[1]
	if ref == ctx.Value(configKey).(*Config).Domain {
		ref = subs[0][strings.Index(subs[0], ref)+len(ref):]
	}
	rows, err := db.QueryContext(ctx, "SELECT id, visit FROM stats WHERE origin = ? AND target = ?", ref, target)
	if err != nil {
		return err
	}
	if !rows.Next() {
		_, err = db.ExecContext(ctx, "INSERT INTO stats (origin, target, visit) VALUES (?, ?, 1)", ref, target)
		return err
	}
	var id uint
	var nb uint
	rows.Scan(&id, &nb)
	err = rows.Close()
	if err != nil {
		return err
	}
	_, err = db.ExecContext(ctx, "UPDATE stats SET visit = ? WHERE id = ?", nb+1, id)
	return err
}