Skip to content

Commit aad224b

Browse files
authored
refactor: activity heatmap pipe optimized by pre calculating local activity times (#3216)
1 parent 54c3ba5 commit aad224b

3 files changed

Lines changed: 63 additions & 27 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SCHEMA >
2+
`id` String,
3+
`timestamp` DateTime,
4+
`channel` String,
5+
`segmentId` String,
6+
`timezone_offset` Int16,
7+
`weekday` UInt8,
8+
`two_hours_block` UInt16
9+
10+
ENGINE MergeTree
11+
ENGINE_PARTITION_KEY toYear(timestamp)
12+
ENGINE_SORTING_KEY segmentId, timestamp, weekday, two_hours_block

services/libs/tinybird/pipes/activity_heatmap_by_weekday_and_2hours_blocks.pipe

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
DESCRIPTION >
22
Serves the `Development - Contributions outside work hours` widget
33

4-
NODE country_mapping_array
5-
SQL >
6-
SELECT groupArray((country, flag, country_code, timezone_offset)) AS country_data
7-
FROM country_mapping
8-
9-
NODE members_with_location_information
10-
SQL >
11-
SELECT
12-
m.id,
13-
m.location,
14-
arrayFilter(
15-
x -> position(coalesce(nullIf(upper(m.country), ''), upper(m.location)), upper(x .1)) > 0,
16-
(SELECT country_data FROM country_mapping_array)
17-
) AS matched_countries,
18-
arrayJoin(
19-
if(empty(matched_countries), [('Unknown', '❓', 'XX', 0)], matched_countries)
20-
) AS country_data
21-
FROM members_sorted AS m
22-
where country_data .1 != 'Unknown' and m.id in (select memberId from activities_filtered)
23-
244
NODE activities_with_local_timestamp
255
SQL >
26-
select
27-
count(id) as activityCount,
28-
toDayOfWeek(addHours(af.timestamp, mwli.country_data .4)) as weekday,
29-
intDiv(toHour(addHours(af.timestamp, mwli.country_data .4)), 2) * 2 AS two_hours_block
30-
from activities_filtered af
31-
join members_with_location_information mwli on mwli.id = af.memberId
32-
where platform in ('git', 'github', 'gitlab', 'gerrit')
6+
%
7+
select count(id) as activityCount, weekday, two_hours_block
8+
from contributions_with_local_time_ds a
9+
where
10+
segmentId = (SELECT segmentId FROM segments_filtered)
11+
{% if defined(startDate) %}
12+
AND a.timestamp
13+
> {{ DateTime(startDate, description="Filter activity timestamp after", required=False) }}
14+
{% end %}
15+
{% if defined(endDate) %}
16+
AND a.timestamp
17+
< {{ DateTime(endDate, description="Filter activity timestamp before", required=False) }}
18+
{% end %}
19+
{% if defined(repo) %}
20+
AND a.channel = {{ String(repo, description="Filter activity repo", required=False) }}
21+
{% end %}
3322
group by weekday, two_hours_block
3423

3524
NODE weekday_hours
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
NODE country_mapping_array
2+
SQL >
3+
SELECT groupArray((country, country_code, timezone_offset)) AS country_data
4+
FROM country_mapping_no_flags_ds
5+
6+
NODE members_with_location_information
7+
SQL >
8+
WITH
9+
upperUTF8(coalesce(nullIf(m.country, ''), m.location)) AS search_u,
10+
(SELECT arrayMap(x -> upperUTF8(x .1), country_data) FROM country_mapping_array) AS names_u,
11+
(SELECT country_data FROM country_mapping_array) AS mapping,
12+
multiSearchFirstIndexUTF8(search_u, names_u) AS idx,
13+
if(idx = 0, ('Unknown', 'XX', 0), mapping[idx]) AS country_data
14+
SELECT m.id, m.location, m.country, country_data
15+
FROM members_sorted AS m
16+
WHERE idx != 0
17+
18+
NODE result
19+
SQL >
20+
select
21+
af.id,
22+
af.timestamp,
23+
af.channel,
24+
af.segmentId,
25+
mwli.country_data .3 as timezone_offset,
26+
toDayOfWeek(addHours(af.timestamp, mwli.country_data .3)) as weekday,
27+
intDiv(toHour(addHours(af.timestamp, mwli.country_data .3)), 2) * 2 AS two_hours_block
28+
from activities_with_relations_sorted_deduplicated_ds af
29+
join members_with_location_information mwli on mwli.id = af.memberId
30+
where platform in ('git', 'github', 'gitlab', 'gerrit')
31+
32+
TYPE COPY
33+
TARGET_DATASOURCE contributions_with_local_time_ds
34+
COPY_MODE replace
35+
COPY_SCHEDULE 55 * * * *

0 commit comments

Comments
 (0)