CREATE VIEW "ur_ingest_session_tasks_stats" AS
WITH Summary AS (
SELECT
device.device_id AS device_id,
ur_ingest_session.ur_ingest_session_id AS ingest_session_id,
ur_ingest_session.ingest_started_at AS ingest_session_started_at,
ur_ingest_session.ingest_finished_at AS ingest_session_finished_at,
COALESCE(ur_ingest_session_task.ur_status, 'Ok') AS ur_status,
COALESCE(uniform_resource.nature, 'UNKNOWN') AS nature,
COUNT(ur_ingest_session_task.uniform_resource_id) AS total_file_count,
SUM(CASE WHEN uniform_resource.content IS NOT NULL THEN 1 ELSE 0 END) AS file_count_with_content,
SUM(CASE WHEN uniform_resource.frontmatter IS NOT NULL THEN 1 ELSE 0 END) AS file_count_with_frontmatter,
MIN(uniform_resource.size_bytes) AS min_file_size_bytes,
AVG(uniform_resource.size_bytes) AS average_file_size_bytes,
MAX(uniform_resource.size_bytes) AS max_file_size_bytes,
MIN(uniform_resource.last_modified_at) AS oldest_file_last_modified_datetime,
MAX(uniform_resource.last_modified_at) AS youngest_file_last_modified_datetime
FROM
ur_ingest_session
JOIN
device ON ur_ingest_session.device_id = device.device_id
LEFT JOIN
ur_ingest_session_task ON ur_ingest_session.ur_ingest_session_id = ur_ingest_session_task.ingest_session_id
LEFT JOIN
uniform_resource ON ur_ingest_session_task.uniform_resource_id = uniform_resource.uniform_resource_id
GROUP BY
device.device_id,
ur_ingest_session.ur_ingest_session_id,
ur_ingest_session.ingest_started_at,
ur_ingest_session.ingest_finished_at,
ur_ingest_session_task.captured_executable
)
SELECT
device_id,
ingest_session_id,
ingest_session_started_at,
ingest_session_finished_at,
ur_status,
nature,
total_file_count,
file_count_with_content,
file_count_with_frontmatter,
min_file_size_bytes,
CAST(ROUND(average_file_size_bytes) AS INTEGER) AS average_file_size_bytes,
max_file_size_bytes,
oldest_file_last_modified_datetime,
youngest_file_last_modified_datetime
FROM
Summary
ORDER BY
device_id,
ingest_session_finished_at,
ur_status