mirror of
https://github.com/BreizhHardware/Jellystat.git
synced 2026-01-18 16:27:20 +01:00
1) Created components for statistic reporting. 2) Database changes and PROC/Function creations. Still need to make MOST VIEWED LIBRARIES/CLIENTS/ MOST ACTIVE USERS dynamically load with date range (Function Creation on DB side)
53 lines
1.8 KiB
PL/PgSQL
53 lines
1.8 KiB
PL/PgSQL
-- FUNCTION: public.fs_most_popular_items(integer, text)
|
|
|
|
-- DROP FUNCTION IF EXISTS public.fs_most_popular_items(integer, text);
|
|
|
|
CREATE OR REPLACE FUNCTION public.fs_most_popular_items(
|
|
days integer,
|
|
itemtype text)
|
|
RETURNS TABLE(unique_viewers bigint, latest_activity_date timestamp with time zone, "Name" text, "Id" text)
|
|
LANGUAGE 'plpgsql'
|
|
COST 100
|
|
VOLATILE PARALLEL UNSAFE
|
|
ROWS 1000
|
|
|
|
AS $BODY$
|
|
BEGIN
|
|
RETURN QUERY
|
|
SELECT
|
|
t.unique_viewers,
|
|
t.latest_activity_date,
|
|
i."Name",
|
|
i."Id"
|
|
FROM (
|
|
SELECT
|
|
jf_playback_activity."NowPlayingItemId",
|
|
count(DISTINCT jf_playback_activity."UserId") AS unique_viewers,
|
|
latest_activity_date.latest_date AS latest_activity_date
|
|
FROM
|
|
jf_playback_activity
|
|
JOIN (
|
|
SELECT
|
|
jf_playback_activity_1."NowPlayingItemId",
|
|
max(jf_playback_activity_1."ActivityDateInserted") AS latest_date
|
|
FROM
|
|
jf_playback_activity jf_playback_activity_1
|
|
GROUP BY jf_playback_activity_1."NowPlayingItemId"
|
|
) latest_activity_date
|
|
ON jf_playback_activity."NowPlayingItemId" = latest_activity_date."NowPlayingItemId"
|
|
WHERE
|
|
jf_playback_activity."ActivityDateInserted" BETWEEN CURRENT_DATE - MAKE_INTERVAL(days => days) and NOW()
|
|
GROUP BY
|
|
jf_playback_activity."NowPlayingItemId", latest_activity_date.latest_date
|
|
) t
|
|
JOIN jf_library_items i
|
|
ON t."NowPlayingItemId" = i."Id"
|
|
AND i."Type" = itemType
|
|
ORDER BY
|
|
t.unique_viewers DESC, t.latest_activity_date DESC;
|
|
END;
|
|
$BODY$;
|
|
|
|
ALTER FUNCTION public.fs_most_popular_items(integer, text)
|
|
OWNER TO postgres;
|