Skip to content

Commit

Permalink
Remove video_views_per_second_per_day_aggregate and add individual query
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvanleeuwen committed Mar 4, 2024
1 parent 0617cbe commit 793917d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release_server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ jobs:
VERSION_NUM=$(echo "$LATEST_TAG" | sed 's/server-v//')
echo "VERSION_NUM: $VERSION_NUM"
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION_NUM"
((PATCH++))
echo "Before increment PATCH value is: $PATCH"
PATCH=$((PATCH + 1))
echo "MAJOR: $MAJOR - MINOR: $MINOR - PATCH: $PATCH"
NEW_TAG="server-v${MAJOR}.${MINOR}.${PATCH}"
fi
Expand Down
59 changes: 37 additions & 22 deletions server/lib/mave_metrics/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ defmodule MaveMetrics.API do

import Ecto.Query, warn: false
import EctoCase
alias MaveMetrics.Repo
alias MaveMetrics.Video
alias MaveMetrics.{Repo, Video, Event}

@default_timeframe "7 days"
@default_interval "12 months"
Expand Down Expand Up @@ -154,26 +153,42 @@ defmodule MaveMetrics.API do
end

def query_individual_video_engagement(video_id, timeframe, interval) do
result =
"video_views_per_second_per_day_aggregate"
|> where([d], d.video_id == ^video_id)
|> apply_timeframe(timeframe, :event_date)
|> group_by([d], [
fragment(~s|time_bucket('?', ?)|, literal(^interval), d.event_date),
d.video_second
])
|> select([d], %{
interval: fragment(~s|time_bucket('?', ?)|, literal(^interval), d.event_date),
second: d.video_second,
views: type(sum(d.views), :integer)
})
|> order_by([d],
asc: fragment(~s|time_bucket('?', ?)|, literal(^interval), d.event_date),
asc: d.video_second
)
|> Repo.all()

nest_engagement_data_by_interval(result)
Event
|> where([e], e.type == ^:play and e.video_id == ^video_id)
|> apply_timeframe(timeframe, :timestamp)
|> join(:inner, [e], e_next in Event,
on:
e_next.session_id == e.session_id and e_next.type == ^:pause and
e_next.timestamp > e.timestamp
)
|> join(
:inner_lateral,
[e, e_next],
gs in fragment(
"SELECT generate_series(
floor(?)::int,
ceil(?)::int - 1
) AS second",
e.video_time,
e_next.video_time
),
on: true
)
|> group_by([e, e_next, s], [
fragment(~s|time_bucket('?', ?)|, literal(^interval), e.timestamp),
s.second
])
|> order_by([e, e_next, s],
asc: fragment(~s|time_bucket('?', ?)|, literal(^interval), e.timestamp),
asc: s.second
)
|> select([e, e_next, s], %{
interval: fragment(~s|time_bucket('?', ?)|, literal(^interval), e.timestamp),
second: s.second,
views: count()
})
|> Repo.all()
|> nest_engagement_data_by_interval()
end

defp nest_engagement_data_by_interval(results) do
Expand Down
1 change: 0 additions & 1 deletion server/lib/mave_metrics/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ defmodule MaveMetrics.Stats do

def refresh_daily_aggregation() do
Repo.query!("REFRESH MATERIALIZED VIEW daily_session_aggregation;")
# Repo.query!("REFRESH MATERIALIZED VIEW video_views_per_second_per_day_aggregate;")
end

defp float_video_time(attrs) do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule MaveMetrics.Repo.Migrations.RecreateAggregationView do
use Ecto.Migration

def change do
execute("""
DROP MATERIALIZED VIEW IF EXISTS video_views_per_second_per_day_aggregate;
""")
end
end

0 comments on commit 793917d

Please sign in to comment.