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
48
49
50
51
52
53
|
#lang cl-yesql/sqlite
-- name: create-stats-table
-- Creates table for tracking visits
CREATE TABLE IF NOT EXISTS STATS (
timestamp int DEFAULT (unixepoch()) NOT NULL,
click text,
page text,
referrer text,
ip text,
user_agent text,
title text)
-- name: drop-stats-table
-- Removes table tracking visits
DROP TABLE IF EXISTS STATS
-- name: add-ip
INSERT OR IGNORE INTO source (ip)
VALUES (:ip)
-- name: add-user-agent
INSERT OR IGNORE INTO useragents (name)
VALUES (:user_agent)
-- name: insert @last-id
-- Insert a data point
INSERT INTO STATS (click, page, referrer, title, ipid, agentid)
VALUES (:click, :page, :referer, :title,
(SELECT id FROM source WHERE ip = :ip),
(SELECT id FROM useragents WHERE name = :user_agent))
-- name: activity-stats
-- timeseries of activity
SELECT
(timestamp / 600) * 600 AS time,
count(*)
FROM
STATS
GROUP BY
1;
-- name: activity-places
-- timeseries of loaded places
SELECT
(timestamp / 600) * 600 AS time,
count(page) FILTER (WHERE page LIKE 'http://localhost:4252%') AS stats,
count(page) FILTER (WHERE page LIKE 'http://localhost/%') AS stats_proxy,
count(page) FILTER (WHERE page LIKE 'http://localhost:1313%') AS blog
FROM
STATS
GROUP BY
1
|