Skip to content

Commit a9e0b0a

Browse files
committed
update project
1 parent ba4c1ea commit a9e0b0a

9 files changed

+292
-110
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
SCHEMA >
3+
`date` Date,
4+
`session_id` String,
5+
`device` SimpleAggregateFunction(any, String),
6+
`browser` SimpleAggregateFunction(any, String),
7+
`location` SimpleAggregateFunction(any, String),
8+
`first_hit` SimpleAggregateFunction(min, DateTime),
9+
`latest_hit` SimpleAggregateFunction(max, DateTime),
10+
`hits` AggregateFunction(count)
11+
12+
ENGINE "AggregatingMergeTree"
13+
ENGINE_PARTITION_KEY "toYYYYMM(date)"
14+
ENGINE_SORTING_KEY "date, session_id"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
NODE analytics_sessions_1
2+
DESCRIPTION >
3+
Aggregate by session_id and calculate session metrics
4+
5+
SQL >
6+
7+
SELECT
8+
toDate(timestamp) AS date,
9+
session_id,
10+
anySimpleState(device) AS device,
11+
anySimpleState(browser) AS browser,
12+
anySimpleState(location) AS location,
13+
minSimpleState(timestamp) AS first_hit,
14+
maxSimpleState(timestamp) AS latest_hit,
15+
countState() AS hits
16+
FROM analytics_hits
17+
GROUP BY
18+
date,
19+
session_id
20+
21+
TYPE materialized
22+
DATASOURCE analytics_sessions_mv
23+
24+
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
DESCRIPTION >
2+
Summary with general KPIs per date, including visits, page views, bounce rate and average session duration.
3+
Accepts `date_from` and `date_to` date filter, all historical data if not passed.
4+
Daily granularity, except when filtering one single day (hourly)
5+
6+
7+
TOKEN "dashboard" READ
8+
9+
NODE timeseries
10+
DESCRIPTION >
11+
Generate a timeseries for the specified time range, so we call fill empty data points.
12+
Filters "future" data points.
13+
14+
SQL >
15+
16+
%
17+
{% set _single_day = defined(date_from) and day_diff(date_from, date_to) == 0 %}
18+
with
19+
{% if defined(date_from) %}
20+
toStartOfDay(toDate({{Date(date_from, description="Starting day for filtering a date range", required=False)}})) as start,
21+
{% else %}
22+
toStartOfDay(timestampAdd(today(), interval -7 day)) as start,
23+
{% end %}
24+
{% if defined(date_to) %}
25+
toStartOfDay(toDate({{Date(date_to, description="Finishing day for filtering a date range", required=False)}})) as end
26+
{% else %}
27+
toStartOfDay(today()) as end
28+
{% end %}
29+
{% if _single_day %}
30+
select arrayJoin(arrayMap(x -> toDateTime(x), range(toUInt32(toDateTime(start)), toUInt32(timestampAdd(end, interval 1 day)), 3600))) as date
31+
{% else %}
32+
select arrayJoin(arrayMap(x -> toDate(x), range(toUInt32(start), toUInt32(timestampAdd(end, interval 1 day)), 24 * 3600))) as date
33+
{% end %}
34+
where date <= now()
35+
36+
37+
38+
NODE hits
39+
DESCRIPTION >
40+
Group by sessions and calculate metrics at that level
41+
42+
SQL >
43+
44+
%
45+
{% if defined(date_from) and day_diff(date_from, date_to) == 0 %}
46+
select
47+
toStartOfHour(timestamp) as date,
48+
session_id,
49+
uniq(session_id) as visits,
50+
count() as pageviews,
51+
case when min(timestamp) = max(timestamp) then 1 else 0 end as is_bounce,
52+
max(timestamp) as latest_hit_aux,
53+
min(timestamp) as first_hit_aux
54+
from
55+
analytics_hits
56+
where
57+
toDate(timestamp) = {{Date(date_from)}}
58+
group by
59+
toStartOfHour(timestamp), session_id
60+
{% else %}
61+
select
62+
date,
63+
session_id,
64+
uniq(session_id) as visits,
65+
countMerge(hits) as pageviews,
66+
case when min(first_hit) = max(latest_hit) then 1 else 0 end as is_bounce,
67+
max(latest_hit) as latest_hit_aux,
68+
min(first_hit) as first_hit_aux
69+
from
70+
analytics_sessions_mv
71+
where
72+
{% if defined(date_from) %}
73+
date >= {{Date(date_from)}}
74+
{% else %}
75+
date >= timestampAdd(today(), interval -7 day)
76+
{% end %}
77+
{% if defined(date_to) %}
78+
and date <= {{Date(date_to)}}
79+
{% else %}
80+
and date <= today()
81+
{% end %}
82+
group by
83+
date, session_id
84+
{% end %}
85+
86+
87+
88+
NODE data
89+
DESCRIPTION >
90+
General KPIs per date, works for both summary metrics and trends charts.
91+
92+
SQL >
93+
94+
select
95+
date,
96+
uniq(session_id) as visits,
97+
sum(pageviews) as pageviews,
98+
sum(case when latest_hit_aux = first_hit_aux then 1 end) / visits as bounce_rate,
99+
avg(latest_hit_aux - first_hit_aux) as avg_session_sec
100+
from
101+
hits
102+
group by
103+
date
104+
105+
106+
107+
NODE endpoint
108+
DESCRIPTION >
109+
Join and generate timeseries with metrics
110+
111+
SQL >
112+
113+
select
114+
a.date,
115+
b.visits,
116+
b.pageviews,
117+
b.bounce_rate,
118+
b.avg_session_sec
119+
from
120+
timeseries a
121+
left join data b using date
122+
123+

delete_simple_resource/pipes/top_browsers.pipe

+22-22
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ DESCRIPTION >
1313
SQL >
1414

1515
%
16-
select
17-
browser,
18-
uniqMerge(visits) as visits,
19-
countMerge(hits) as hits
20-
from
21-
analytics_sources_mv
22-
where
23-
{% if defined(date_from) %}
24-
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25-
{% else %}
26-
date >= timestampAdd(today(), interval -7 day)
27-
{% end %}
28-
{% if defined(date_to) %}
29-
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30-
{% else %}
31-
and date <= today()
32-
{% end %}
33-
group by
34-
browser
35-
order by
36-
visits desc
37-
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
16+
select
17+
browser,
18+
uniqMerge(visits) as visits,
19+
countMerge(hits) as hits
20+
from
21+
analytics_sources_mv
22+
where
23+
{% if defined(date_from) %}
24+
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25+
{% else %}
26+
date >= timestampAdd(today(), interval -7 day)
27+
{% end %}
28+
{% if defined(date_to) %}
29+
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30+
{% else %}
31+
and date <= today()
32+
{% end %}
33+
group by
34+
browser
35+
order by
36+
visits desc
37+
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
3838

3939

delete_simple_resource/pipes/top_devices.pipe

+22-22
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ DESCRIPTION >
1313
SQL >
1414

1515
%
16-
select
17-
device,
18-
uniqMerge(visits) as visits,
19-
countMerge(hits) as hits
20-
from
21-
analytics_sources_mv
22-
where
23-
{% if defined(date_from) %}
24-
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25-
{% else %}
26-
date >= timestampAdd(today(), interval -7 day)
27-
{% end %}
28-
{% if defined(date_to) %}
29-
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30-
{% else %}
31-
and date <= today()
32-
{% end %}
33-
group by
34-
device
35-
order by
36-
visits desc
37-
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
16+
select
17+
device,
18+
uniqMerge(visits) as visits,
19+
countMerge(hits) as hits
20+
from
21+
analytics_sources_mv
22+
where
23+
{% if defined(date_from) %}
24+
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25+
{% else %}
26+
date >= timestampAdd(today(), interval -7 day)
27+
{% end %}
28+
{% if defined(date_to) %}
29+
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30+
{% else %}
31+
and date <= today()
32+
{% end %}
33+
group by
34+
device
35+
order by
36+
visits desc
37+
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
3838

3939

delete_simple_resource/pipes/top_locations.pipe

+22-22
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ DESCRIPTION >
1313
SQL >
1414

1515
%
16-
select
17-
location,
18-
uniqMerge(visits) as visits,
19-
countMerge(hits) as hits
20-
from
21-
analytics_pages_mv
22-
where
23-
{% if defined(date_from) %}
24-
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25-
{% else %}
26-
date >= timestampAdd(today(), interval -7 day)
27-
{% end %}
28-
{% if defined(date_to) %}
29-
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30-
{% else %}
31-
and date <= today()
32-
{% end %}
33-
group by
34-
location
35-
order by
36-
visits desc
37-
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
16+
select
17+
location,
18+
uniqMerge(visits) as visits,
19+
countMerge(hits) as hits
20+
from
21+
analytics_pages_mv
22+
where
23+
{% if defined(date_from) %}
24+
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25+
{% else %}
26+
date >= timestampAdd(today(), interval -7 day)
27+
{% end %}
28+
{% if defined(date_to) %}
29+
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30+
{% else %}
31+
and date <= today()
32+
{% end %}
33+
group by
34+
location
35+
order by
36+
visits desc
37+
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
3838

3939

delete_simple_resource/pipes/top_pages.pipe

+22-22
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ DESCRIPTION >
1313
SQL >
1414

1515
%
16-
select
17-
pathname,
18-
uniqMerge(visits) as visits,
19-
countMerge(hits) as hits
20-
from
21-
analytics_pages_mv
22-
where
23-
{% if defined(date_from) %}
24-
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25-
{% else %}
26-
date >= timestampAdd(today(), interval -7 day)
27-
{% end %}
28-
{% if defined(date_to) %}
29-
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30-
{% else %}
31-
and date <= today()
32-
{% end %}
33-
group by
34-
pathname
35-
order by
36-
visits desc
37-
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
16+
select
17+
pathname,
18+
uniqMerge(visits) as visits,
19+
countMerge(hits) as hits
20+
from
21+
analytics_pages_mv
22+
where
23+
{% if defined(date_from) %}
24+
date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}}
25+
{% else %}
26+
date >= timestampAdd(today(), interval -7 day)
27+
{% end %}
28+
{% if defined(date_to) %}
29+
and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}}
30+
{% else %}
31+
and date <= today()
32+
{% end %}
33+
group by
34+
pathname
35+
order by
36+
visits desc
37+
limit {{Int32(skip, 0)}},{{Int32(limit, 50)}}
3838

3939

0 commit comments

Comments
 (0)