Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added /stats backend route to published-schedule.js for stats modal #66

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,56 @@ publishedScheduleRouter.get('/season', async (req, res) => {
}
});

// GET /published-schedule/stats - returns stats of event types and subjects for a specific season
publishedScheduleRouter.get('/stats', async (req, res) => {
try {
const { season, year } = req.query;

const statResult = await db.query(
`
WITH all_event_types AS (
SELECT DISTINCT unnest(event_type) AS event_type
FROM catalog
),
all_subjects AS (
SELECT DISTINCT unnest(subject) AS subject
FROM catalog
),
all_permutations AS (
SELECT aet.event_type, asu.subject
FROM all_event_types aet
CROSS JOIN all_subjects asu
)
SELECT
COALESCE(ap.event_type::text, 'Total') AS event_type,
COALESCE(ap.subject::text, 'Total') AS subject,
COALESCE(COUNT(c.catalog_id), 0) AS total_count
FROM all_permutations ap
LEFT JOIN (
SELECT *,
ps.day_id AS ps_day_id,
c.id AS catalog_id
FROM catalog c
JOIN published_schedule ps ON c.id = ps.event_id
JOIN day d ON PS.day_id = d.id
WHERE $1 = ANY(c.season)
AND EXTRACT(YEAR FROM d.event_date) = $2
) c ON ap.event_type = ANY(c.event_type) AND ap.subject = ANY(c.subject)
GROUP BY ROLLUP (ap.event_type), ROLLUP (ap.subject)
ORDER BY CASE WHEN ap.event_type IS NULL THEN 1 ELSE 0 END,
CASE WHEN ap.subject IS NULL THEN 1 ELSE 0 END,
ap.event_type NULLS FIRST,
ap.subject NULLS FIRST;
`,
[season, year],
);

res.status(200).json(keysToCamel(statResult));
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});

// GET /published-schedule/date - returns all events occurring on a specific date
publishedScheduleRouter.get('/dayId', async (req, res) => {
try {
Expand Down
Loading