Skip to content

Commit

Permalink
MMI-244 Fixed total running time issues
Browse files Browse the repository at this point in the history
  • Loading branch information
laidaoyu committed Dec 21, 2024
1 parent 3540b59 commit 9b9be5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
DO $$
BEGIN

DROP FUNCTION IF EXISTS public.fn_cbra_report_total_entries(date, date);
DROP FUNCTION IF EXISTS public.fn_cbra_report_totals_by_broadcaster(date, date);
DROP FUNCTION IF EXISTS public.fn_cbra_report_totals_by_program(date, date);

-- Function: fn_cbra_report_total_entries
CREATE OR REPLACE FUNCTION public.fn_cbra_report_total_entries (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ RETURNS TABLE (
)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE total_running_time NUMERIC;
DECLARE sum_total_running_time NUMERIC;
begin

CREATE TEMPORARY TABLE IF NOT EXISTS temp_table_content_by_broadcaster (
Expand All @@ -92,16 +92,16 @@ begin
LEFT JOIN file_reference f ON f.content_id = c.content_id
GROUP BY c.source;

select sum(tt.total_running_time) INTO total_running_time
select sum(COALESCE(tt.total_running_time,0)) INTO sum_total_running_time
from temp_table_by_broadcaster tt;

RETURN query
SELECT
tt.source
, TO_CHAR((CAST(COALESCE(tt.total_running_time,0) AS DECIMAL)/1000)*'1 SECOND'::INTERVAL, 'HH24:MI:SS') AS total_running_time
, COALESCE(tt.total_running_time,0) / CASE COALESCE(tt.total_running_time,0)
, COALESCE(tt.total_running_time,0) / CASE COALESCE(sum_total_running_time,0)
WHEN 0 THEN 1
ELSE COALESCE(tt.total_running_time,0)
ELSE COALESCE(sum_total_running_time,0)
END AS percentage_of_total_running_time
FROM temp_table_by_broadcaster tt;

Expand All @@ -125,7 +125,7 @@ CREATE OR REPLACE FUNCTION public.fn_cbra_report_totals_by_program(
)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE total_running_time NUMERIC;
DECLARE sum_total_running_time NUMERIC;
BEGIN

CREATE TEMPORARY TABLE IF NOT EXISTS temp_table_content_by_program (
Expand Down Expand Up @@ -164,7 +164,7 @@ BEGIN
LEFT JOIN file_reference f ON f.content_id = c.content_id
GROUP BY c.media_type_id, c.source, c.source_id, c.series_id, c.series_name, c.media_type_name;

SELECT SUM(tt.total_running_time) INTO total_running_time
SELECT SUM(COALESCE(tt.total_running_time,0)) INTO sum_total_running_time
FROM temp_table_by_program tt;

RETURN query
Expand All @@ -174,9 +174,9 @@ BEGIN
tt.series_name,
tt.total_count,
TO_CHAR((cast(COALESCE(tt.total_running_time,0) AS DECIMAL)/1000)*'1 SECOND'::INTERVAL, 'HH24:MI:SS') AS total_running_time,
COALESCE(tt.total_running_time,0) / CASE COALESCE(tt.total_running_time,0)
COALESCE(tt.total_running_time,0) / CASE COALESCE(sum_total_running_time,0)
WHEN 0 THEN 1
ELSE COALESCE(tt.total_running_time,0)
ELSE COALESCE(sum_total_running_time,0)
END AS percentage_of_total_running_time
FROM temp_table_by_program tt;

Expand Down

0 comments on commit 9b9be5d

Please sign in to comment.