Skip to content

Commit 242a0ae

Browse files
committed
fix for #267 devide by 0 error
1 parent 16f239d commit 242a0ae

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
exports.up = async function (knex) {
2+
try {
3+
await knex.schema.raw(`
4+
DROP FUNCTION IF EXISTS public.jf_recent_playback_activity(integer);
5+
6+
CREATE OR REPLACE FUNCTION public.jf_recent_playback_activity(
7+
hour_offset integer)
8+
RETURNS TABLE("RunTimeTicks" bigint, "Progress" numeric, "Id" text, "IsPaused" boolean, "UserId" text, "UserName" text, "Client" text, "DeviceName" text, "DeviceId" text, "ApplicationVersion" text, "NowPlayingItemId" text, "NowPlayingItemName" text, "SeasonId" text, "SeriesName" text, "EpisodeId" text, "PlaybackDuration" bigint, "ActivityDateInserted" timestamp with time zone, "PlayMethod" text, "MediaStreams" json, "TranscodingInfo" json, "PlayState" json, "OriginalContainer" text, "RemoteEndPoint" text, "ServerId" text, "Imported" boolean, "RowNum" bigint)
9+
LANGUAGE 'plpgsql'
10+
COST 100
11+
VOLATILE PARALLEL UNSAFE
12+
ROWS 1000
13+
14+
AS $BODY$
15+
BEGIN
16+
RETURN QUERY
17+
WITH rankedactivities AS (
18+
SELECT COALESCE(i."RunTimeTicks", e."RunTimeTicks") AS "RunTimeTicks",
19+
CASE
20+
WHEN COALESCE(i."RunTimeTicks"::numeric(100,0), e."RunTimeTicks"::numeric(100,0), 1.0) > 0 THEN ((a."PlaybackDuration" * 10000000)::numeric(100,0) / COALESCE(i."RunTimeTicks"::numeric(100,0), e."RunTimeTicks"::numeric(100,0), 1.0) * 100::numeric)::numeric(100,2)
21+
ELSE 1.0
22+
END AS "Progress",
23+
a."Id",
24+
a."IsPaused",
25+
a."UserId",
26+
a."UserName",
27+
a."Client",
28+
a."DeviceName",
29+
a."DeviceId",
30+
a."ApplicationVersion",
31+
a."NowPlayingItemId",
32+
a."NowPlayingItemName",
33+
a."SeasonId",
34+
a."SeriesName",
35+
a."EpisodeId",
36+
a."PlaybackDuration",
37+
a."ActivityDateInserted",
38+
a."PlayMethod",
39+
a."MediaStreams",
40+
a."TranscodingInfo",
41+
a."PlayState",
42+
a."OriginalContainer",
43+
a."RemoteEndPoint",
44+
a."ServerId",
45+
a.imported,
46+
row_number() OVER (PARTITION BY a."NowPlayingItemId",a."EpisodeId",a."UserId" ORDER BY a."ActivityDateInserted" DESC) AS rownum
47+
FROM jf_playback_activity a
48+
LEFT JOIN jf_library_items i ON a."NowPlayingItemId" = i."Id"
49+
LEFT JOIN jf_library_episodes e ON a."EpisodeId" = e."EpisodeId"
50+
WHERE a."ActivityDateInserted" > (CURRENT_TIMESTAMP - (hour_offset || ' hours')::interval)
51+
ORDER BY a."ActivityDateInserted" DESC
52+
)
53+
SELECT * FROM rankedactivities WHERE rankedactivities.rownum = 1;
54+
END;
55+
56+
$BODY$;
57+
58+
ALTER FUNCTION public.jf_recent_playback_activity(integer)
59+
OWNER TO "${process.env.POSTGRES_ROLE}";
60+
`);
61+
} catch (error) {
62+
console.error(error);
63+
}
64+
};
65+
66+
exports.down = async function (knex) {
67+
try {
68+
await knex.schema.raw(`
69+
DROP FUNCTION IF EXISTS public.jf_recent_playback_activity(integer);
70+
71+
CREATE OR REPLACE FUNCTION public.jf_recent_playback_activity(
72+
hour_offset integer)
73+
RETURNS TABLE("RunTimeTicks" bigint, "Progress" numeric, "Id" text, "IsPaused" boolean, "UserId" text, "UserName" text, "Client" text, "DeviceName" text, "DeviceId" text, "ApplicationVersion" text, "NowPlayingItemId" text, "NowPlayingItemName" text, "SeasonId" text, "SeriesName" text, "EpisodeId" text, "PlaybackDuration" bigint, "ActivityDateInserted" timestamp with time zone, "PlayMethod" text, "MediaStreams" json, "TranscodingInfo" json, "PlayState" json, "OriginalContainer" text, "RemoteEndPoint" text, "ServerId" text, "Imported" boolean, "RowNum" bigint)
74+
LANGUAGE 'plpgsql'
75+
COST 100
76+
VOLATILE PARALLEL UNSAFE
77+
ROWS 1000
78+
79+
AS $BODY$
80+
BEGIN
81+
RETURN QUERY
82+
WITH rankedactivities AS (
83+
SELECT COALESCE(i."RunTimeTicks", e."RunTimeTicks") AS "RunTimeTicks",
84+
((a."PlaybackDuration" * 10000000)::numeric(100,0) / COALESCE(i."RunTimeTicks"::numeric(100,0), e."RunTimeTicks"::numeric(100,0), 1.0) * 100::numeric)::numeric(100,2) AS "Progress",
85+
a."Id",
86+
a."IsPaused",
87+
a."UserId",
88+
a."UserName",
89+
a."Client",
90+
a."DeviceName",
91+
a."DeviceId",
92+
a."ApplicationVersion",
93+
a."NowPlayingItemId",
94+
a."NowPlayingItemName",
95+
a."SeasonId",
96+
a."SeriesName",
97+
a."EpisodeId",
98+
a."PlaybackDuration",
99+
a."ActivityDateInserted",
100+
a."PlayMethod",
101+
a."MediaStreams",
102+
a."TranscodingInfo",
103+
a."PlayState",
104+
a."OriginalContainer",
105+
a."RemoteEndPoint",
106+
a."ServerId",
107+
a.imported,
108+
row_number() OVER (PARTITION BY a."NowPlayingItemId",a."EpisodeId",a."UserId" ORDER BY a."ActivityDateInserted" DESC) AS rownum
109+
FROM jf_playback_activity a
110+
LEFT JOIN jf_library_items i ON a."NowPlayingItemId" = i."Id"
111+
LEFT JOIN jf_library_episodes e ON a."EpisodeId" = e."EpisodeId"
112+
WHERE a."ActivityDateInserted" > (CURRENT_TIMESTAMP - (hour_offset || ' hours')::interval)
113+
ORDER BY a."ActivityDateInserted" DESC
114+
)
115+
SELECT * FROM rankedactivities WHERE rankedactivities.rownum = 1;
116+
END;
117+
118+
$BODY$;
119+
120+
ALTER FUNCTION public.jf_recent_playback_activity(integer)
121+
OWNER TO "${process.env.POSTGRES_ROLE}";`);
122+
} catch (error) {
123+
console.error(error);
124+
}
125+
};

0 commit comments

Comments
 (0)