You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
added `time_from_last_event` and `time_to_next_event` fields to events to
that are precomputed when the event is created.
Signed-off-by: Henry Gressmann <[email protected]>
// total sessions: no time_to_next_event / time_to_next_event is null
195
-
// bounce sessions: time to next / time to prev are both null or both > 1800
195
+
// bounce sessions: time to next / time to prev are both null or both > interval '30 minutes'
196
196
"--sql
197
197
count(distinct sd.visitor_id)
198
-
filter (where (sd.time_until_next_event is null or sd.time_until_next_event > 1800) and
199
-
(sd.time_since_previous_event is null or sd.time_since_previous_event > 1800)) /
200
-
count(distinct sd.visitor_id) filter (where sd.time_until_next_event is null or sd.time_until_next_event > 1800)
198
+
filter (where (sd.time_to_next_event is null or sd.time_to_next_event > interval '30 minutes') and
199
+
(sd.time_from_last_event is null or sd.time_from_last_event > interval '30 minutes')) /
200
+
count(distinct sd.visitor_id) filter (where sd.time_to_next_event is null or sd.time_to_next_event > interval '30 minutes')
201
201
"
202
202
}
203
203
Metric::AvgTimeOnSite => {
204
-
// avg time_until_next_event where time_until_next_event <= 1800 and time_until_next_event is not null
204
+
// avg time_to_next_event where time_to_next_event <= 1800 and time_to_next_event is not null
205
205
"--sql
206
-
coalesce(avg(sd.time_until_next_event) filter (where sd.time_until_next_event is not null and sd.time_until_next_event <= 1800), 0)"
206
+
coalesce(avg(extract(epoch from sd.time_to_next_event)) filter (where sd.time_to_next_event is not null and sd.time_to_next_event <= interval '30 minutes'), 0)"
207
207
}
208
208
}
209
209
.to_owned()
@@ -278,7 +278,8 @@ pub fn overall_report(
278
278
params.extend_from_params(filters_params);
279
279
params.push(range.end);
280
280
281
-
let query = format!("--sql
281
+
let query = format!(
282
+
"--sql
282
283
with
283
284
params as (
284
285
select
@@ -296,10 +297,8 @@ pub fn overall_report(
296
297
select
297
298
visitor_id,
298
299
created_at,
299
-
-- the time to the next event for the same visitor
300
-
extract(epoch from (lead(created_at) over (partition by visitor_id order by created_at) - created_at)) as time_until_next_event,
301
-
-- the time to the previous event for the same visitor
302
-
extract(epoch from (created_at - lag(created_at) over (partition by visitor_id order by created_at))) as time_since_previous_event
300
+
time_from_last_event,
301
+
time_to_next_event,
303
302
from events, params
304
303
where
305
304
event = ?::text and
@@ -326,7 +325,8 @@ pub fn overall_report(
326
325
left join event_bins eb on tb.bin_start = eb.bin_start
327
326
order by
328
327
tb.bin_start;
329
-
");
328
+
"
329
+
);
330
330
331
331
letmut stmt = conn.prepare_cached(&query)?;
332
332
@@ -371,21 +371,20 @@ pub fn overall_stats(
371
371
params.extend(entities);
372
372
params.extend_from_params(filters_params);
373
373
374
-
let query = format!("--sql
374
+
let query = format!(
375
+
"--sql
375
376
with
376
377
params as (
377
378
select
378
379
?::timestamp as start_time,
379
-
?::timestamp as end_time
380
+
?::timestamp as end_time,
380
381
),
381
382
session_data as (
382
383
select
383
384
visitor_id,
384
385
created_at,
385
-
-- the time to the next event for the same visitor
386
-
extract(epoch from (lead(created_at) over (partition by visitor_id order by created_at) - created_at)) as time_until_next_event,
387
-
-- the time to the previous event for the same visitor
388
-
extract(epoch from (created_at - lag(created_at) over (partition by visitor_id order by created_at))) as time_since_previous_event
386
+
time_from_last_event,
387
+
time_to_next_event,
389
388
from events, params
390
389
where
391
390
event = ?::text and
@@ -400,7 +399,8 @@ pub fn overall_stats(
400
399
{metric_avg_time_on_site} as avg_time_on_site
401
400
from
402
401
session_data sd;
403
-
");
402
+
"
403
+
);
404
404
405
405
letmut stmt = conn.prepare_cached(&query)?;
406
406
let result = stmt.query_row(duckdb::params_from_iter(params), |row| {
@@ -456,30 +456,29 @@ pub fn dimension_report(
456
456
params.extend(entities);
457
457
params.extend_from_params(filters_params);
458
458
459
-
let query = format!("--sql
459
+
let query = format!(
460
+
"--sql
460
461
with
461
462
params as (
462
463
select
463
464
?::timestamp as start_time,
464
-
?::timestamp as end_time
465
+
?::timestamp as end_time,
465
466
),
466
467
session_data as (
467
468
select
468
469
coalesce({dimension_column}, 'Unknown') as dimension_value,
469
470
visitor_id,
470
471
created_at,
471
-
-- the time to the next event for the same visitor
472
-
extract(epoch from (lead(created_at) over (partition by visitor_id order by created_at) - created_at)) as time_until_next_event,
473
-
-- the time to the previous event for the same visitor
474
-
extract(epoch from (created_at - lag(created_at) over (partition by visitor_id order by created_at))) as time_since_previous_event
472
+
time_from_last_event,
473
+
time_to_next_event,
475
474
from events sd, params
476
475
where
477
476
event = ?::text and
478
477
created_at between params.start_time and params.end_time and
0 commit comments