-
Notifications
You must be signed in to change notification settings - Fork 15
/
fact_watched_video_segments.sql
110 lines (109 loc) · 3.74 KB
/
fact_watched_video_segments.sql
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
with
video_events as (
select
emission_time,
org,
course_key,
splitByString('/xblock/', object_id)[-1] as video_id,
object_id,
actor_id,
verb_id,
video_position,
video_duration
from {{ ASPECTS_XAPI_DATABASE }}.video_playback_events
where 1 = 1 {% include 'openedx-assets/queries/common_filters.sql' %}
),
starts as (
select *
from video_events
where verb_id = 'https://w3id.org/xapi/video/verbs/played'
),
ends as (
select *
from video_events
where
verb_id in (
'http://adlnet.gov/expapi/verbs/completed',
'https://w3id.org/xapi/video/verbs/seeked',
'https://w3id.org/xapi/video/verbs/paused',
'http://adlnet.gov/expapi/verbs/terminated'
)
),
segments as (
select
starts.org as org,
starts.course_key as course_key,
starts.video_id as video_id,
starts.actor_id,
starts.object_id as object_id,
cast(starts.video_position as Int32) as start_position,
cast(ends.video_position as Int32) as end_position,
starts.emission_time as started_at,
ends.emission_time as ended_at,
ends.verb_id as end_type,
starts.video_duration as video_duration
from starts left
asof join
ends
on (
starts.org = ends.org
and starts.course_key = ends.course_key
and starts.video_id = ends.video_id
and starts.actor_id = ends.actor_id
and starts.emission_time < ends.emission_time
)
),
enriched_segments as (
select
segments.org as org,
segments.course_key as course_key,
blocks.course_name as course_name,
blocks.course_run as course_run,
blocks.section_with_name as section_with_name,
blocks.subsection_with_name as subsection_with_name,
blocks.block_name as video_name,
blocks.display_name_with_location as video_name_with_location,
segments.actor_id as actor_id,
segments.object_id as object_id,
segments.started_at as started_at,
segments.start_position - (segments.start_position % 5) as start_position,
segments.end_position - (segments.end_position % 5) as end_position,
segments.video_duration as video_duration,
segments.video_id as video_id
from segments
join
{{ DBT_PROFILE_TARGET_DATABASE }}.dim_course_blocks_extended blocks
on (
segments.course_key = blocks.course_key
and segments.video_id = blocks.block_id
)
where 1 = 1 {% include 'openedx-assets/queries/common_filters.sql' %}
)
select
org,
course_key,
course_name,
course_run,
section_with_name,
subsection_with_name,
video_name,
video_name_with_location,
video_id,
concat(
'<a href="', object_id, '" target="_blank">', video_name_with_location, '</a>'
) as video_link,
actor_id,
started_at,
arrayJoin(range(start_position, end_position, 5)) as segment_start,
video_duration,
CONCAT(toString(segment_start), '-', toString(segment_start + 4)) as segment_range,
start_position,
username,
name,
email
from enriched_segments
left outer join
{{ DBT_PROFILE_TARGET_DATABASE }}.dim_user_pii users
on (actor_id like 'mailto:%' and SUBSTRING(actor_id, 8) = users.email)
or actor_id = toString(users.external_user_id)
order by segment_start