repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / cmd / scripts / analytics
Antonio Mika · 08 Oct 24

analytics.go

 1package main
 2
 3import (
 4	"log/slog"
 5	"os"
 6
 7	"github.com/picosh/pico/db"
 8	"github.com/picosh/pico/db/postgres"
 9	"github.com/picosh/utils"
10)
11
12func main() {
13	logger := slog.Default()
14	DbURL := os.Getenv("DATABASE_URL")
15	dbpool := postgres.NewDB(DbURL, logger)
16
17	args := os.Args
18	fkID := args[1]
19
20	stats, err := dbpool.VisitSummary(
21		&db.SummaryOpts{
22			FkID: fkID,
23			// By:   "post_id",
24			By:       "user_id",
25			Interval: "day",
26			Origin:   utils.StartOfMonth(),
27			// Where:    "AND (post_id IS NOT NULL OR (post_id IS NULL AND project_id IS NULL))",
28		},
29	)
30	if err != nil {
31		panic(err)
32	}
33
34	for _, s := range stats.Intervals {
35		logger.Info(
36			"interval",
37			"interval", s.Interval,
38			"visitors", s.Visitors,
39			"postID", s.PostID,
40			"projectID", s.ProjectID,
41		)
42	}
43
44	for _, url := range stats.TopUrls {
45		logger.Info(
46			"url",
47			"url", url.Url,
48			"count", url.Count,
49			"postID", url.PostID,
50			"projectID", url.ProjectID,
51		)
52	}
53
54	for _, url := range stats.TopReferers {
55		logger.Info(
56			"referer",
57			"url", url.Url,
58			"count", url.Count,
59			"postID", url.PostID,
60			"projectID", url.ProjectID,
61		)
62	}
63}