-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67174c5
commit f03a7d3
Showing
7 changed files
with
144 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
server/lib/mave_metrics_web/controllers/api/engagement_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule MaveMetricsWeb.API.EngagementController do | ||
use MaveMetricsWeb, :controller | ||
|
||
alias MaveMetrics.API | ||
|
||
def get_engagement(conn, _params) do | ||
{:ok, body, _conn} = Plug.Conn.read_body(conn) | ||
params = Jason.decode!(body) | ||
conn |> engagement(params) | ||
end | ||
|
||
def engagement(conn, %{"query" => query} = params) do | ||
result = API.get_engagement(query, params["interval"], params["timeframe"]) | ||
|
||
conn | ||
|> json(%{engagement: result}) | ||
|> halt | ||
end | ||
|
||
def engagement(conn, _params) do | ||
conn | ||
|> put_status(400) | ||
|> json(%{error: "Requires a valid JSON query struct."}) | ||
|> halt | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
server/priv/repo/migrations/20240226221750_add_engagement.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule MaveMetrics.Repo.Migrations.AddEngagement do | ||
use Ecto.Migration | ||
|
||
def change do | ||
execute(""" | ||
CREATE MATERIALIZED VIEW IF NOT EXISTS video_views_per_second_per_day_aggregate AS | ||
SELECT | ||
e.video_id, | ||
DATE(e.timestamp) AS event_date, | ||
gs.second AS video_second, | ||
COUNT(*) AS views | ||
FROM | ||
events e | ||
JOIN | ||
events e_next ON e.session_id = e_next.session_id | ||
AND e_next.type = 'pause' | ||
AND e_next.timestamp > e.timestamp | ||
AND e.type = 'play', | ||
LATERAL | ||
generate_series( | ||
floor(e.video_time)::int, | ||
ceil(e_next.video_time)::int - 1 | ||
) AS gs(second) | ||
GROUP BY | ||
e.video_id, DATE(e.timestamp), gs.second; | ||
""") | ||
|
||
execute(""" | ||
CREATE INDEX ON video_views_per_second_per_day_aggregate (video_id, event_date, video_second); | ||
""") | ||
end | ||
end |