Skip to content

Commit 010f87a

Browse files
Add data filter to dashboard. (#74)
* fix: add types to simple-builder * feat: Add data filter to dashboard. --------- Co-authored-by: iza <[email protected]>
1 parent 3b1a334 commit 010f87a

File tree

29 files changed

+717
-891
lines changed

29 files changed

+717
-891
lines changed

apps/api/src/query/builders/custom-events.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const CustomEventsBuilders: Record<string, SimpleQueryConfig> = {
2222
orderBy: 'total_events DESC',
2323
limit: 10_000,
2424
timeField: 'time',
25-
allowedFilters: ['event_name', 'path', 'device_type', 'browser_name'],
2625
customizable: true,
2726
},
2827
custom_event_properties: {
@@ -43,7 +42,6 @@ export const CustomEventsBuilders: Record<string, SimpleQueryConfig> = {
4342
orderBy: 'count DESC',
4443
limit: 10_000,
4544
timeField: 'time',
46-
allowedFilters: ['event_name', 'path', 'device_type', 'browser_name'],
4745
customizable: true,
4846
},
4947
};

apps/api/src/query/builders/devices.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
5252
orderBy: 'pageviews DESC',
5353
limit: 100,
5454
timeField: 'time',
55-
allowedFilters: ['path', 'device_type', 'country'],
5655
customizable: true,
5756
},
5857

@@ -106,7 +105,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
106105
orderBy: 'pageviews DESC',
107106
limit: 100,
108107
timeField: 'time',
109-
allowedFilters: ['path', 'referrer', 'device_type'],
110108
customizable: true,
111109
},
112110

@@ -152,7 +150,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
152150
orderBy: 'pageviews DESC',
153151
limit: 100,
154152
timeField: 'time',
155-
allowedFilters: ['path', 'referrer', 'device_type'],
156153
customizable: true,
157154
},
158155

@@ -198,7 +195,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
198195
orderBy: 'pageviews DESC',
199196
limit: 100,
200197
timeField: 'time',
201-
allowedFilters: ['path', 'referrer', 'device_type'],
202198
customizable: true,
203199
},
204200

@@ -270,7 +266,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
270266
orderBy: 'visitors DESC',
271267
limit: 100,
272268
timeField: 'time',
273-
allowedFilters: ['path', 'referrer', 'device_type'],
274269
customizable: true,
275270
},
276271

@@ -319,11 +314,10 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
319314
'ROUND((COUNT(*) / SUM(COUNT(*)) OVER()) * 100, 2) as percentage',
320315
],
321316
where: ["screen_resolution != ''", "event_name = 'screen_view'"],
322-
groupBy: ['screen_resolution'],
317+
groupBy: ['screen_resolution', 'device_type'],
323318
orderBy: 'pageviews DESC',
324319
limit: 100,
325320
timeField: 'time',
326-
allowedFilters: ['path', 'referrer', 'browser_name'],
327321
customizable: true,
328322
plugins: { mapDeviceTypes: true },
329323
},
@@ -341,7 +335,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
341335
orderBy: 'pageviews DESC',
342336
limit: 25,
343337
timeField: 'time',
344-
allowedFilters: ['browser_name', 'path', 'device_type', 'country'],
345338
customizable: true,
346339
},
347340

@@ -361,7 +354,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
361354
orderBy: 'pageviews DESC',
362355
limit: 100,
363356
timeField: 'time',
364-
allowedFilters: ['path', 'referrer', 'device_type'],
365357
customizable: true,
366358
},
367359

@@ -378,7 +370,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
378370
orderBy: 'pageviews DESC',
379371
limit: 100,
380372
timeField: 'time',
381-
allowedFilters: ['path', 'referrer', 'device_type'],
382373
customizable: true,
383374
},
384375

@@ -394,7 +385,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
394385
orderBy: 'pageviews DESC',
395386
limit: 25,
396387
timeField: 'time',
397-
allowedFilters: ['os_name', 'os_version', 'path', 'device_type'],
398388
customizable: true,
399389
},
400390

@@ -410,7 +400,6 @@ export const DevicesBuilders: Record<string, SimpleQueryConfig> = {
410400
orderBy: 'pageviews DESC',
411401
limit: 100,
412402
timeField: 'time',
413-
allowedFilters: ['path', 'referrer', 'device_type'],
414403
customizable: true,
415404
},
416405
};

apps/api/src/query/builders/geo.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export const GeoBuilders: Record<string, SimpleQueryConfig> = {
5252
orderBy: 'pageviews DESC',
5353
limit: 100,
5454
timeField: 'time',
55-
allowedFilters: ['path', 'device_type', 'browser_name'],
5655
customizable: true,
5756
plugins: { normalizeGeo: true, deduplicateGeo: true },
5857
},
@@ -107,7 +106,6 @@ export const GeoBuilders: Record<string, SimpleQueryConfig> = {
107106
orderBy: 'pageviews DESC',
108107
limit: 100,
109108
timeField: 'time',
110-
allowedFilters: ['country', 'path', 'device_type'],
111109
customizable: true,
112110
},
113111

@@ -124,7 +122,6 @@ export const GeoBuilders: Record<string, SimpleQueryConfig> = {
124122
orderBy: 'pageviews DESC',
125123
limit: 100,
126124
timeField: 'time',
127-
allowedFilters: ['country', 'path', 'device_type'],
128125
customizable: true,
129126
},
130127

@@ -141,7 +138,6 @@ export const GeoBuilders: Record<string, SimpleQueryConfig> = {
141138
orderBy: 'pageviews DESC',
142139
limit: 100,
143140
timeField: 'time',
144-
allowedFilters: ['country', 'path', 'device_type'],
145141
customizable: true,
146142
},
147143

@@ -195,7 +191,6 @@ export const GeoBuilders: Record<string, SimpleQueryConfig> = {
195191
orderBy: 'pageviews DESC',
196192
limit: 100,
197193
timeField: 'time',
198-
allowedFilters: ['country', 'path', 'device_type'],
199194
customizable: true,
200195
},
201196
};

apps/api/src/query/builders/pages.ts

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Analytics } from '../../types/tables';
2-
import type { SimpleQueryConfig } from '../types';
2+
import type { Filter, SimpleQueryConfig } from '../types';
3+
import { buildWhereClause } from '../utils';
34

45
export const PagesBuilders: Record<string, SimpleQueryConfig> = {
56
top_pages: {
@@ -17,7 +18,6 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
1718
orderBy: 'pageviews DESC',
1819
limit: 100,
1920
timeField: 'time',
20-
allowedFilters: ['referrer', 'device_type', 'browser_name'],
2121
customizable: true,
2222
meta: {
2323
title: 'Top Pages',
@@ -67,30 +67,22 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
6767
},
6868

6969
entry_pages: {
70-
table: Analytics.events,
71-
fields: [
72-
'entry_page as name',
73-
'COUNT(*) as pageviews',
74-
'COUNT(DISTINCT anonymous_id) as visitors',
75-
'ROUND((COUNT(*) / SUM(COUNT(*)) OVER()) * 100, 2) as percentage',
76-
],
77-
where: ["event_name = 'screen_view'"],
78-
groupBy: ['entry_page'],
79-
orderBy: 'pageviews DESC',
80-
limit: 100,
81-
timeField: 'time',
82-
allowedFilters: ['referrer', 'device_type'],
83-
customizable: true,
8470
customSql: (
8571
websiteId: string,
8672
startDate: string,
8773
endDate: string,
8874
_filters?: unknown[],
8975
_granularity?: unknown,
9076
limit?: number,
91-
offset?: number
92-
) => ({
93-
sql: `
77+
offset?: number,
78+
_timezone?: string,
79+
filterConditions?: string[],
80+
filterParams?: Record<string, Filter['value']>
81+
) => {
82+
const combinedWhereClause = buildWhereClause(filterConditions);
83+
84+
return {
85+
sql: `
9486
WITH session_entry AS (
9587
SELECT
9688
session_id,
@@ -103,6 +95,7 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
10395
AND time >= parseDateTimeBestEffort({startDate:String})
10496
AND time <= parseDateTimeBestEffort({endDate:String})
10597
AND event_name = 'screen_view'
98+
${combinedWhereClause}
10699
)
107100
SELECT
108101
entry_page as name,
@@ -115,41 +108,35 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
115108
ORDER BY pageviews DESC
116109
LIMIT {limit:Int32} OFFSET {offset:Int32}
117110
`,
118-
params: {
119-
websiteId,
120-
startDate,
121-
endDate,
122-
limit: limit || 100,
123-
offset: offset || 0,
124-
},
125-
}),
111+
params: {
112+
websiteId,
113+
startDate,
114+
endDate,
115+
limit: limit || 100,
116+
offset: offset || 0,
117+
...filterParams,
118+
},
119+
};
120+
},
126121
},
127122

128123
exit_pages: {
129-
table: Analytics.events,
130-
fields: [
131-
'path as name',
132-
'COUNT(DISTINCT session_id) as pageviews',
133-
'COUNT(DISTINCT anonymous_id) as visitors',
134-
'ROUND((COUNT(DISTINCT session_id) / SUM(COUNT(DISTINCT session_id)) OVER()) * 100, 2) as percentage',
135-
],
136-
where: ["event_name = 'screen_view'"],
137-
groupBy: ['path'],
138-
orderBy: 'pageviews DESC',
139-
limit: 100,
140-
timeField: 'time',
141-
allowedFilters: ['referrer', 'device_type'],
142-
customizable: true,
143124
customSql: (
144125
websiteId: string,
145126
startDate: string,
146127
endDate: string,
147128
_filters?: unknown[],
148129
_granularity?: unknown,
149130
limit?: number,
150-
offset?: number
151-
) => ({
152-
sql: `
131+
offset?: number,
132+
_timezone?: string,
133+
filterConditions?: string[],
134+
filterParams?: Record<string, Filter['value']>
135+
) => {
136+
const combinedWhereClause = buildWhereClause(filterConditions);
137+
138+
return {
139+
sql: `
153140
WITH sessions AS (
154141
SELECT
155142
session_id,
@@ -159,6 +146,7 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
159146
AND time >= parseDateTimeBestEffort({startDate:String})
160147
AND time <= parseDateTimeBestEffort({endDate:String})
161148
AND event_name = 'screen_view'
149+
${combinedWhereClause}
162150
GROUP BY session_id
163151
),
164152
exit_pages AS (
@@ -172,6 +160,7 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
172160
AND e.time >= parseDateTimeBestEffort({startDate:String})
173161
AND e.time <= parseDateTimeBestEffort({endDate:String})
174162
AND e.event_name = 'screen_view'
163+
${combinedWhereClause}
175164
)
176165
SELECT
177166
path as name,
@@ -183,14 +172,16 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
183172
ORDER BY pageviews DESC
184173
LIMIT {limit:Int32} OFFSET {offset:Int32}
185174
`,
186-
params: {
187-
websiteId,
188-
startDate,
189-
endDate,
190-
limit: limit || 100,
191-
offset: offset || 0,
192-
},
193-
}),
175+
params: {
176+
websiteId,
177+
startDate,
178+
endDate,
179+
limit: limit || 100,
180+
offset: offset || 0,
181+
...filterParams,
182+
},
183+
};
184+
},
194185
},
195186

196187
page_performance: {
@@ -208,7 +199,6 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
208199
orderBy: 'pageviews DESC',
209200
limit: 100,
210201
timeField: 'time',
211-
allowedFilters: ['referrer', 'device_type'],
212202
customizable: true,
213203
},
214204
};

apps/api/src/query/builders/performance.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
1616
],
1717
where: ["event_name = 'screen_view'"],
1818
timeField: 'time',
19-
allowedFilters: ['path', 'device_type', 'browser_name', 'country'],
2019
customizable: true,
2120
},
2221

@@ -39,7 +38,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
3938
orderBy: 'avg_load_time DESC',
4039
limit: 100,
4140
timeField: 'time',
42-
allowedFilters: ['path', 'device_type', 'browser_name'],
4341
customizable: true,
4442
},
4543

@@ -62,7 +60,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
6260
orderBy: 'avg_load_time DESC',
6361
limit: 100,
6462
timeField: 'time',
65-
allowedFilters: ['path', 'device_type', 'browser_name'],
6663
customizable: true,
6764
},
6865

@@ -91,7 +88,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
9188
orderBy: 'avg_load_time DESC',
9289
limit: 100,
9390
timeField: 'time',
94-
allowedFilters: ['path', 'device_type', 'browser_name'],
9591
customizable: true,
9692
},
9793

@@ -114,7 +110,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
114110
orderBy: 'avg_load_time DESC',
115111
limit: 100,
116112
timeField: 'time',
117-
allowedFilters: ['path', 'device_type', 'browser_name'],
118113
customizable: true,
119114
plugins: { normalizeGeo: true, deduplicateGeo: true },
120115
},
@@ -138,7 +133,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
138133
orderBy: 'avg_load_time DESC',
139134
limit: 100,
140135
timeField: 'time',
141-
allowedFilters: ['path', 'device_type', 'browser_name'],
142136
customizable: true,
143137
},
144138

@@ -161,7 +155,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
161155
orderBy: 'avg_load_time DESC',
162156
limit: 100,
163157
timeField: 'time',
164-
allowedFilters: ['path', 'device_type', 'browser_name'],
165158
customizable: true,
166159
},
167160

@@ -182,7 +175,6 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
182175
groupBy: ['toDate(time)'],
183176
orderBy: 'date ASC',
184177
timeField: 'time',
185-
allowedFilters: ['path', 'device_type', 'browser_name'],
186178
customizable: true,
187179
},
188180
};

0 commit comments

Comments
 (0)