From 793917d07bec8e4f329533af0e3672fb5ef0309a Mon Sep 17 00:00:00 2001 From: davidvanleeuwen Date: Mon, 4 Mar 2024 14:21:35 +0100 Subject: [PATCH] Remove video_views_per_second_per_day_aggregate and add individual query --- .github/workflows/release_server.yml | 3 +- server/lib/mave_metrics/api.ex | 59 ++++++++++++------- server/lib/mave_metrics/stats.ex | 1 - ...240304084433_recreate_aggregation_view.exs | 9 +++ 4 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 server/priv/repo/migrations/20240304084433_recreate_aggregation_view.exs diff --git a/.github/workflows/release_server.yml b/.github/workflows/release_server.yml index 602496a..22d371c 100644 --- a/.github/workflows/release_server.yml +++ b/.github/workflows/release_server.yml @@ -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 diff --git a/server/lib/mave_metrics/api.ex b/server/lib/mave_metrics/api.ex index 8354df9..aef8950 100644 --- a/server/lib/mave_metrics/api.ex +++ b/server/lib/mave_metrics/api.ex @@ -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" @@ -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 diff --git a/server/lib/mave_metrics/stats.ex b/server/lib/mave_metrics/stats.ex index 91753f2..9aef4a4 100644 --- a/server/lib/mave_metrics/stats.ex +++ b/server/lib/mave_metrics/stats.ex @@ -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 diff --git a/server/priv/repo/migrations/20240304084433_recreate_aggregation_view.exs b/server/priv/repo/migrations/20240304084433_recreate_aggregation_view.exs new file mode 100644 index 0000000..eb803dc --- /dev/null +++ b/server/priv/repo/migrations/20240304084433_recreate_aggregation_view.exs @@ -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