chore: fix related books + deduplication

- fixed total plays formatting
- formatted queries
- improved proxy server
This commit is contained in:
Cory Dransfeldt 2024-10-21 12:04:17 -07:00
parent ab50866bb8
commit d2c4c78ff7
No known key found for this signature in database
40 changed files with 985 additions and 1357 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "1.6.0", "version": "1.6.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "1.6.0", "version": "1.6.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@cdransf/api-text": "^1.5.0", "@cdransf/api-text": "^1.5.0",

View file

@ -1,6 +1,6 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "1.6.1", "version": "1.6.2",
"description": "The source for my personal site. Built using 11ty (and other tools).", "description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module", "type": "module",
"engines": { "engines": {

View file

@ -1,21 +1,18 @@
CREATE OR REPLACE FUNCTION public.search_optimized_index( CREATE OR REPLACE FUNCTION public.search_optimized_index(search_query text, page_size integer, page_offset integer, types text[])
search_query text, RETURNS TABLE(
page_size integer, result_id integer,
page_offset integer, url text,
types text[] title text,
) RETURNS TABLE( description text,
result_id integer, tags text,
url text, genre_name text,
title text, genre_url text,
description text, type text,
tags text, total_plays text, -- Changed to text
genre_name text, rank real,
genre_url text, total_count bigint
type text, )
total_plays integer, AS $$
rank real,
total_count bigint
) AS $$
BEGIN BEGIN
RETURN QUERY RETURN QUERY
SELECT SELECT
@ -28,20 +25,19 @@ BEGIN
s.genre_url, s.genre_url,
s.type, s.type,
s.total_plays, s.total_plays,
ts_rank_cd( ts_rank_cd(to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' ')), plainto_tsquery('english', search_query)) AS rank,
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' ')), COUNT(*) OVER() AS total_count
plainto_tsquery('english', search_query) FROM
) AS rank, optimized_search_index s
COUNT(*) OVER () AS total_count WHERE(types IS NULL
FROM optimized_search_index s OR s.type = ANY(types))
WHERE AND plainto_tsquery('english', search_query) @@ to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' '))
(types IS NULL OR s.type = ANY(types))
AND plainto_tsquery('english', search_query) @@
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' '))
ORDER BY ORDER BY
s.type = 'post' DESC, s.type = 'post' DESC,
s.content_date DESC NULLS LAST, s.content_date DESC NULLS LAST,
rank DESC rank DESC
LIMIT page_size OFFSET page_offset; LIMIT page_size OFFSET page_offset;
END; END;
$$ LANGUAGE plpgsql; $$
LANGUAGE plpgsql;

View file

@ -7,30 +7,20 @@ SELECT
l.link, l.link,
a.mastodon, a.mastodon,
a.name, a.name,
json_build_object( json_build_object('name', a.name, 'url', a.url, 'mastodon', a.mastodon) AS author,
'name', a.name,
'url', a.url,
'mastodon', a.mastodon
) AS author,
'link' AS type, 'link' AS type,
(
( SELECT
SELECT array_agg(t.name) array_agg(t.name)
FROM links_tags lt FROM
links_tags lt
LEFT JOIN tags t ON lt.tags_id = t.id LEFT JOIN tags t ON lt.tags_id = t.id
WHERE lt.links_id = l.id WHERE
) AS tags, lt.links_id = l.id) AS tags,
json_build_object('title', CONCAT(l.title, ' via ', a.name), 'url', l.link, 'description', l.description, 'date', l.date) AS feed
json_build_object(
'title', CONCAT(l.title, ' via ', a.name),
'url', l.link,
'description', l.description,
'date', l.date
) AS feed
FROM FROM
links l links l
JOIN JOIN authors a ON l.author = a.id
authors a ON l.author = a.id
ORDER BY ORDER BY
l.date DESC; l.date DESC;

View file

@ -8,145 +8,119 @@ SELECT
p.featured, p.featured,
p.slug AS url, p.slug AS url,
p.mastodon_url, p.mastodon_url,
CASE WHEN df.filename_disk IS NOT NULL
CASE AND df.filename_disk != ''
WHEN df.filename_disk IS NOT NULL AND df.filename_disk != '' AND df.filename_disk != '/' AND df.filename_disk != '/' THEN
THEN CONCAT('/', df.filename_disk) CONCAT('/', df.filename_disk)
ELSE NULL ELSE
NULL
END AS image, END AS image,
p.image_alt, p.image_alt,
CASE WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, p.date)) > 3 THEN
CASE TRUE
WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, p.date)) > 3 THEN TRUE ELSE
ELSE FALSE FALSE
END AS old_post, END AS old_post,
(
( SELECT
SELECT json_agg( json_agg(
CASE CASE WHEN pb.collection = 'youtube_player' THEN
WHEN pb.collection = 'youtube_player' THEN json_build_object( json_build_object('type', pb.collection, 'url', yp.url)
'type', pb.collection, WHEN pb.collection = 'github_banner' THEN
'url', yp.url json_build_object('type', pb.collection, 'url', gb.url)
) WHEN pb.collection = 'npm_banner' THEN
WHEN pb.collection = 'github_banner' THEN json_build_object( json_build_object('type', pb.collection, 'url', nb.url, 'command', nb.command)
'type', pb.collection, WHEN pb.collection = 'rss_banner' THEN
'url', gb.url json_build_object('type', pb.collection, 'url', rb.url, 'text', rb.text)
) WHEN pb.collection = 'hero' THEN
WHEN pb.collection = 'npm_banner' THEN json_build_object( json_build_object('type', pb.collection, 'image', CONCAT('/', df_hero.filename_disk), 'alt_text', h.alt_text)
'type', pb.collection, WHEN pb.collection = 'markdown' THEN
'url', nb.url, json_build_object('type', pb.collection, 'text', md.text)
'command', nb.command WHEN pb.collection = 'divider' THEN
) json_build_object('type', pb.collection, 'markup', d.markup)
WHEN pb.collection = 'rss_banner' THEN json_build_object( ELSE
'type', pb.collection, json_build_object('type', pb.collection)
'url', rb.url, END)
'text', rb.text FROM
) posts_blocks pb
WHEN pb.collection = 'hero' THEN json_build_object( LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player'
'type', pb.collection, AND yp.id = pb.item::integer
'image', CONCAT('/', df_hero.filename_disk), LEFT JOIN github_banner gb ON pb.collection = 'github_banner'
'alt_text', h.alt_text AND gb.id = pb.item::integer
) LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner'
WHEN pb.collection = 'markdown' THEN json_build_object( AND nb.id = pb.item::integer
'type', pb.collection, LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner'
'text', md.text AND rb.id = pb.item::integer
) LEFT JOIN hero h ON pb.collection = 'hero'
WHEN pb.collection = 'divider' THEN json_build_object( AND h.id = pb.item::integer
'type', pb.collection, LEFT JOIN directus_files df_hero ON h.image = df_hero.id
'markup', d.markup LEFT JOIN markdown md ON pb.collection = 'markdown'
) AND md.id = pb.item::integer
ELSE json_build_object('type', pb.collection) LEFT JOIN divider d ON pb.collection = 'divider'
END AND d.id = pb.item::integer
) WHERE
FROM posts_blocks pb pb.posts_id = p.id) AS blocks,
LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player' AND yp.id = pb.item::INTEGER (
LEFT JOIN github_banner gb ON pb.collection = 'github_banner' AND gb.id = pb.item::INTEGER SELECT
LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner' AND nb.id = pb.item::INTEGER array_agg(t.name)
LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner' AND rb.id = pb.item::INTEGER FROM
LEFT JOIN hero h ON pb.collection = 'hero' AND h.id = pb.item::INTEGER posts_tags pt
LEFT JOIN directus_files df_hero ON h.image = df_hero.id LEFT JOIN tags t ON pt.tags_id = t.id
LEFT JOIN markdown md ON pb.collection = 'markdown' AND md.id = pb.item::INTEGER WHERE
LEFT JOIN divider d ON pb.collection = 'divider' AND d.id = pb.item::INTEGER pt.posts_id = p.id) AS tags,
WHERE pb.posts_id = p.id (
) AS blocks, SELECT
json_agg(json_build_object('name', g.name, 'url', g.slug))
( FROM
SELECT array_agg(t.name) posts_genres gp
FROM posts_tags pt LEFT JOIN genres g ON gp.genres_id = g.id
LEFT JOIN tags t ON pt.tags_id = t.id WHERE
WHERE pt.posts_id = p.id gp.posts_id = p.id) AS genres,
) AS tags, (
SELECT
( json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays))
SELECT json_agg(json_build_object( FROM
'name', g.name, posts_artists pa
'url', g.slug LEFT JOIN artists a ON pa.artists_id = a.id
)) WHERE
FROM posts_genres gp pa.posts_id = p.id) AS artists,
LEFT JOIN genres g ON gp.genres_id = g.id (
WHERE gp.posts_id = p.id SELECT
) AS genres, json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
ORDER BY b.title)
( FROM
SELECT json_agg(json_build_object( posts_books pbk
'name', a.name_string, LEFT JOIN books b ON pbk.books_id = b.id
'url', a.slug, WHERE
'country', a.country, pbk.posts_id = p.id) AS books,
'total_plays', a.total_plays (
)) SELECT
FROM posts_artists pa json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
LEFT JOIN artists a ON pa.artists_id = a.id ORDER BY m.year DESC)
WHERE pa.posts_id = p.id FROM
) AS artists, posts_movies pm
LEFT JOIN movies m ON pm.movies_id = m.id
( WHERE
SELECT json_agg(json_build_object( pm.posts_id = p.id) AS movies,
'title', b.title, (
'author', b.author, SELECT
'url', b.slug json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug))
) ORDER BY b.title) FROM
FROM posts_books pbk posts_shows ps
LEFT JOIN books b ON pbk.books_id = b.id LEFT JOIN shows s ON ps.shows_id = s.id
WHERE pbk.posts_id = p.id WHERE
) AS books, ps.posts_id = p.id) AS shows,
json_build_object('title', p.title, 'url', CONCAT('https://coryd.dev', p.slug), 'description', p.content, 'date', p.date, 'image', CASE WHEN df.filename_disk IS NOT NULL
( AND df.filename_disk != ''
SELECT json_agg(json_build_object( AND df.filename_disk != '/' THEN
'title', m.title, CONCAT('/', df.filename_disk)
'year', m.year, ELSE
'url', m.slug NULL
) ORDER BY m.year DESC) END) AS feed
FROM posts_movies pm
LEFT JOIN movies m ON pm.movies_id = m.id
WHERE pm.posts_id = p.id
) AS movies,
(
SELECT json_agg(json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug
))
FROM posts_shows ps
LEFT JOIN shows s ON ps.shows_id = s.id
WHERE ps.posts_id = p.id
) AS shows,
json_build_object(
'title', p.title,
'url', CONCAT('https://coryd.dev', p.slug),
'description', p.content,
'date', p.date,
'image', CASE
WHEN df.filename_disk IS NOT NULL AND df.filename_disk != '' AND df.filename_disk != '/'
THEN CONCAT('/', df.filename_disk)
ELSE NULL
END
) AS feed
FROM FROM
posts p posts p
LEFT JOIN LEFT JOIN directus_files df ON p.image = df.id
directus_files df ON p.image = df.id
GROUP BY GROUP BY
p.id, df.filename_disk; p.id,
df.filename_disk;

View file

@ -1,91 +1,71 @@
CREATE OR REPLACE VIEW optimized_all_activity AS CREATE OR REPLACE VIEW optimized_all_activity AS
WITH feed_data AS ( WITH feed_data AS (
SELECT SELECT
p.date AS content_date, p.date AS content_date,
p.title, p.title,
p.description, p.description,
CONCAT('https://coryd.dev', p.url) AS url, CONCAT('https://coryd.dev', p.url) AS url,
NULL AS image, NULL AS image,
NULL AS rating, NULL AS rating,
p.tags, p.tags,
json_build_object( json_build_object('title', p.title, 'url', CONCAT('https://coryd.dev', p.url), 'description', p.description, 'date', p.date) AS feed
'title', p.title, FROM
'url', CONCAT('https://coryd.dev', p.url), optimized_posts p
'description', p.description, UNION ALL
'date', p.date SELECT
) AS feed l.date AS content_date,
FROM optimized_posts p l.title,
l.description,
UNION ALL l.link AS url,
NULL AS image,
SELECT NULL AS rating,
l.date AS content_date, l.tags,
l.title, json_build_object('title', l.title, 'url', l.link, 'description', l.description, 'date', l.date) AS feed
l.description, FROM
l.link AS url, optimized_links l
NULL AS image, UNION ALL
NULL AS rating, SELECT
l.tags, b.date_finished AS content_date,
json_build_object( b.title,
'title', l.title, b.description,
'url', l.link, CONCAT('https://coryd.dev', b.url) AS url,
'description', l.description, b.image,
'date', l.date b.rating,
) AS feed b.tags,
FROM optimized_links l CASE WHEN LOWER(b.status) = 'finished' THEN
json_build_object('title', b.title, 'url', CONCAT('https://coryd.dev', b.url), 'description', b.description, 'image', b.image, 'rating', b.rating, 'date', b.date_finished)
UNION ALL ELSE
NULL
SELECT END AS feed
b.date_finished AS content_date, FROM
b.title, optimized_books b
b.description, UNION ALL
CONCAT('https://coryd.dev', b.url) AS url, SELECT
b.image, m.last_watched AS content_date,
b.rating, m.title,
b.tags, m.description,
CASE CONCAT('https://coryd.dev', m.url) AS url,
WHEN LOWER(b.status) = 'finished' THEN json_build_object( m.image,
'title', b.title, m.rating,
'url', CONCAT('https://coryd.dev', b.url), m.tags,
'description', b.description, CASE WHEN m.last_watched IS NOT NULL THEN
'image', b.image, json_build_object('title', m.title, 'url', CONCAT('https://coryd.dev', m.url), 'description', m.description, 'image', m.image, 'rating', m.rating, 'date', m.last_watched)
'rating', b.rating, ELSE
'date', b.date_finished NULL
) END AS feed
ELSE NULL FROM
END AS feed optimized_movies m
FROM optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
m.description,
CONCAT('https://coryd.dev', m.url) AS url,
m.image,
m.rating,
m.tags,
CASE
WHEN m.last_watched IS NOT NULL THEN json_build_object(
'title', m.title,
'url', CONCAT('https://coryd.dev', m.url),
'description', m.description,
'image', m.image,
'rating', m.rating,
'date', m.last_watched
)
ELSE NULL
END AS feed
FROM optimized_movies m
) )
SELECT
SELECT json_agg(feed_data.* ORDER BY feed_data.content_date DESC) AS feed json_agg(feed_data.* ORDER BY feed_data.content_date DESC) AS feed
FROM ( FROM (
SELECT * SELECT
FROM feed_data *
WHERE feed IS NOT NULL FROM
ORDER BY content_date DESC feed_data
LIMIT 20 WHERE
) AS feed_data; feed IS NOT NULL
ORDER BY
content_date DESC
LIMIT 20) AS feed_data;

View file

@ -1,97 +1,98 @@
CREATE OR REPLACE VIEW optimized_search_index AS CREATE OR REPLACE VIEW optimized_search_index AS
WITH search_data AS ( WITH search_data AS (
SELECT SELECT
'post' AS type, 'post' AS type,
CONCAT('📝 ', p.title) AS title, CONCAT('📝 ', p.title) AS title,
CONCAT('https://coryd.dev', p.url) AS url, CONCAT('https://coryd.dev', p.url) AS url,
p.description AS description, p.description AS description,
p.tags, p.tags,
NULL AS genre_name, NULL AS genre_name,
NULL AS genre_url, NULL AS genre_url,
NULL::integer AS total_plays, NULL::text AS total_plays,
p.date AS content_date p.date AS content_date
FROM optimized_posts p FROM
optimized_posts p
UNION ALL UNION ALL
SELECT
SELECT 'link' AS type,
'link' AS type, CONCAT('🔗 ', l.title, ' via ', l.name) AS title,
CONCAT('🔗 ', l.title, ' via ', l.name) AS title, l.link AS url,
l.link AS url, l.description AS description,
l.description AS description, l.tags,
l.tags, NULL AS genre_name,
NULL AS genre_name, NULL AS genre_url,
NULL AS genre_url, NULL::text AS total_plays,
NULL::integer AS total_plays, l.date AS content_date
l.date AS content_date FROM
FROM optimized_links l optimized_links l
UNION ALL
UNION ALL SELECT
'book' AS type,
SELECT CASE WHEN b.rating IS NOT NULL THEN
'book' AS type, CONCAT('📖 ', b.title, ' (', b.rating, ')')
CASE ELSE
WHEN b.rating IS NOT NULL THEN CONCAT('📖 ', b.title, ' (', b.rating, ')') CONCAT('📖 ', b.title)
ELSE CONCAT('📖 ', b.title) END AS title,
END AS title, CONCAT('https://coryd.dev', b.url) AS url,
CONCAT('https://coryd.dev', b.url) AS url, b.description AS description,
b.description AS description, b.tags,
b.tags, NULL AS genre_name,
NULL AS genre_name, NULL AS genre_url,
NULL AS genre_url, NULL::text AS total_plays,
NULL::integer AS total_plays, b.date_finished AS content_date
b.date_finished AS content_date FROM
FROM optimized_books b optimized_books b
WHERE LOWER(b.status) = 'finished' WHERE
LOWER(b.status) = 'finished'
UNION ALL UNION ALL
SELECT
SELECT 'artist' AS type,
'artist' AS type, CONCAT(COALESCE(ar.emoji, ar.genre_emoji, '🎧'), ' ', ar.name) AS title,
CONCAT(COALESCE(ar.emoji, ar.genre_emoji, '🎧'), ' ', ar.name) AS title, CONCAT('https://coryd.dev', ar.url) AS url,
CONCAT('https://coryd.dev', ar.url) AS url, ar.description AS description,
ar.description AS description, ARRAY[ar.genre_name] AS tags,
ARRAY[ar.genre_name] AS tags, ar.genre_name,
ar.genre_name, CONCAT('https://coryd.dev', ar.genre_slug) AS genre_url,
CONCAT('https://coryd.dev', ar.genre_slug) AS genre_url, to_char(ar.total_plays::numeric, 'FM999,999,999,999') AS total_plays,
ar.total_plays, NULL AS content_date
NULL AS content_date FROM
FROM optimized_artists ar optimized_artists ar
UNION ALL
UNION ALL SELECT
'genre' AS type,
SELECT CONCAT(COALESCE(g.emoji, '🎵'), ' ', g.name) AS title,
'genre' AS type, CONCAT('https://coryd.dev', g.url) AS url,
CONCAT(COALESCE(g.emoji, '🎵'), ' ', g.name) AS title, g.description AS description,
CONCAT('https://coryd.dev', g.url) AS url, NULL AS tags,
g.description AS description, g.name AS genre_name,
NULL AS tags, CONCAT('https://coryd.dev', g.url) AS genre_url,
g.name AS genre_name, NULL::text AS total_plays,
CONCAT('https://coryd.dev', g.url) AS genre_url, NULL AS content_date
NULL::integer AS total_plays, FROM
NULL AS content_date optimized_genres g
FROM optimized_genres g UNION ALL
SELECT
UNION ALL 'show' AS type,
CASE WHEN s.review IS NOT NULL THEN
SELECT CONCAT('📺 ', s.title, ' (', s.year, ') - ', s.review)
'show' AS type, ELSE
CASE CONCAT('📺 ', s.title, ' (', s.year, ')')
WHEN s.review IS NOT NULL THEN CONCAT('📺 ', s.title, ' (', s.year, ') - ', s.review) END AS title,
ELSE CONCAT('📺 ', s.title, ' (', s.year, ')') CONCAT('https://coryd.dev', s.url) AS url,
END AS title, s.description AS description,
CONCAT('https://coryd.dev', s.url) AS url, s.tags,
s.description AS description, NULL AS genre_name,
s.tags, NULL AS genre_url,
NULL AS genre_name, NULL::text AS total_plays,
NULL AS genre_url, s.last_watched_at AS content_date
NULL::integer AS total_plays, FROM
s.last_watched_at AS content_date optimized_shows s
FROM optimized_shows s WHERE
WHERE s.last_watched_at IS NOT NULL s.last_watched_at IS NOT NULL
) )
SELECT SELECT
ROW_NUMBER() OVER (ORDER BY url) AS id, ROW_NUMBER() OVER (ORDER BY url) AS id,
* *
FROM search_data; FROM
search_data;

View file

@ -1,68 +1,65 @@
CREATE OR REPLACE VIEW optimized_sitemap AS CREATE OR REPLACE VIEW optimized_sitemap AS
WITH sitemap_data AS ( WITH sitemap_data AS (
SELECT SELECT
p.date AS content_date, p.date AS content_date,
p.title, p.title,
CONCAT('https://coryd.dev', p.url) AS url, CONCAT('https://coryd.dev', p.url) AS url,
'monthly' AS changefreq, 'monthly' AS changefreq,
0.7 AS priority 0.7 AS priority
FROM optimized_posts p FROM
optimized_posts p
UNION ALL UNION ALL
SELECT
SELECT b.date_finished AS content_date,
b.date_finished AS content_date, b.title,
b.title, CONCAT('https://coryd.dev', b.url) AS url,
CONCAT('https://coryd.dev', b.url) AS url, 'monthly' AS changefreq,
'monthly' AS changefreq, 0.5 AS priority
0.5 AS priority FROM
FROM optimized_books b optimized_books b
UNION ALL
UNION ALL SELECT
m.last_watched AS content_date,
SELECT m.title,
m.last_watched AS content_date, CONCAT('https://coryd.dev', m.url) AS url,
m.title, 'weekly' AS changefreq,
CONCAT('https://coryd.dev', m.url) AS url, 0.6 AS priority
'weekly' AS changefreq, FROM
0.6 AS priority optimized_movies m
FROM optimized_movies m UNION ALL
SELECT
UNION ALL NULL AS content_date,
ar.name AS title,
SELECT CONCAT('https://coryd.dev', ar.url) AS url,
NULL AS content_date, 'monthly' AS changefreq,
ar.name AS title, 0.5 AS priority
CONCAT('https://coryd.dev', ar.url) AS url, FROM
'monthly' AS changefreq, optimized_artists ar
0.5 AS priority UNION ALL
FROM optimized_artists ar SELECT
NULL AS content_date,
UNION ALL g.name AS title,
CONCAT('https://coryd.dev', g.url) AS url,
SELECT 'yearly' AS changefreq,
NULL AS content_date, 0.3 AS priority
g.name AS title, FROM
CONCAT('https://coryd.dev', g.url) AS url, optimized_genres g
'yearly' AS changefreq, UNION ALL
0.3 AS priority SELECT
FROM optimized_genres g s.last_watched_at AS content_date,
s.title,
UNION ALL CONCAT('https://coryd.dev', s.url) AS url,
'weekly' AS changefreq,
SELECT 0.8 AS priority
s.last_watched_at AS content_date, FROM
s.title, optimized_shows s
CONCAT('https://coryd.dev', s.url) AS url,
'weekly' AS changefreq,
0.8 AS priority
FROM optimized_shows s
) )
SELECT SELECT
url, url,
title, title,
content_date AS lastmod, content_date AS lastmod,
changefreq, changefreq,
priority priority
FROM sitemap_data; FROM
sitemap_data;

View file

@ -1,105 +1,79 @@
CREATE OR REPLACE VIEW optimized_syndication AS CREATE OR REPLACE VIEW optimized_syndication AS
WITH syndication_data AS ( WITH syndication_data AS (
SELECT SELECT
p.date AS content_date, p.date AS content_date,
p.title, p.title,
p.description, p.description,
CONCAT('https://coryd.dev', p.url) AS url, CONCAT('https://coryd.dev', p.url) AS url,
p.tags, p.tags,
json_build_object( json_build_object('title', CONCAT('📝 ', p.title, ' ',(
'title', CONCAT('📝 ', p.title, ' ', ( SELECT
SELECT array_to_string( array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ' FROM unnest(p.tags) AS t(name))), 'description', p.description, 'url', CONCAT('https://coryd.dev', p.url), 'date', p.date) AS syndication
) FROM
FROM unnest(p.tags) AS t(name) optimized_posts p
)), UNION ALL
'description', p.description, SELECT
'url', CONCAT('https://coryd.dev', p.url), l.date AS content_date,
'date', p.date l.title,
) AS syndication l.description,
FROM optimized_posts p l.link AS url,
l.tags,
UNION ALL json_build_object('title', CONCAT('🔗 ', l.title, CASE WHEN l.mastodon IS NOT NULL THEN
' via @' || split_part(l.mastodon, '@', 2) || '@' || split_part(split_part(l.mastodon, 'https://', 2), '/', 1)
SELECT ELSE
l.date AS content_date, CONCAT(' via ', l.name)
l.title, END, ' ',(
l.description, SELECT
l.link AS url, array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
l.tags, FROM unnest(l.tags) AS t(name))), 'description', l.description, 'url', l.link, 'date', l.date) AS syndication
json_build_object( FROM
'title', CONCAT('🔗 ', l.title, optimized_links l
CASE UNION ALL
WHEN l.mastodon IS NOT NULL THEN SELECT
' via @' || split_part(l.mastodon, '@', 2) || '@' || split_part(split_part(l.mastodon, 'https://', 2), '/', 1) b.date_finished AS content_date,
ELSE CONCAT(' via ', l.name) b.title,
END, ' ', ( b.description,
SELECT array_to_string( CONCAT('https://coryd.dev', b.url) AS url,
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ' b.tags,
) CASE WHEN LOWER(b.status) = 'finished' THEN
FROM unnest(l.tags) AS t(name) json_build_object('title', CONCAT('📖 ', b.title, ' ',(
) SELECT
), array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
'description', l.description, FROM unnest(b.tags) AS t(name))), 'description', b.description, 'url', CONCAT('https://coryd.dev', b.url), 'date', b.date_finished)
'url', l.link, ELSE
'date', l.date NULL
) AS syndication END AS syndication
FROM optimized_links l FROM
optimized_books b
UNION ALL UNION ALL
SELECT
SELECT m.last_watched AS content_date,
b.date_finished AS content_date, m.title,
b.title, m.description,
b.description, CONCAT('https://coryd.dev', m.url) AS url,
CONCAT('https://coryd.dev', b.url) AS url, m.tags,
b.tags, CASE WHEN m.last_watched IS NOT NULL THEN
CASE json_build_object('title', CONCAT('🎥 ', m.title, ' ',(
WHEN LOWER(b.status) = 'finished' THEN json_build_object( SELECT
'title', CONCAT('📖 ', b.title, ' ', ( array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
SELECT array_to_string( FROM unnest(m.tags) AS t(name))), 'description', m.description, 'url', CONCAT('https://coryd.dev', m.url), 'date', m.last_watched)
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ' ELSE
) NULL
FROM unnest(b.tags) AS t(name) END AS syndication
)), FROM
'description', b.description, optimized_movies m
'url', CONCAT('https://coryd.dev', b.url),
'date', b.date_finished
)
ELSE NULL
END AS syndication
FROM optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
m.description,
CONCAT('https://coryd.dev', m.url) AS url,
m.tags,
CASE
WHEN m.last_watched IS NOT NULL THEN json_build_object(
'title', CONCAT('🎥 ', m.title, ' ', (
SELECT array_to_string(
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' '
)
FROM unnest(m.tags) AS t(name)
)),
'description', m.description,
'url', CONCAT('https://coryd.dev', m.url),
'date', m.last_watched
)
ELSE NULL
END AS syndication
FROM optimized_movies m
) )
SELECT
SELECT json_agg(limited_data.*) AS syndication json_agg(limited_data.*) AS syndication
FROM ( FROM (
SELECT * SELECT
FROM syndication_data *
WHERE syndication IS NOT NULL FROM
ORDER BY content_date DESC syndication_data
LIMIT 20 WHERE
) AS limited_data; syndication IS NOT NULL
ORDER BY
content_date DESC
LIMIT 20) AS limited_data;

View file

@ -10,5 +10,5 @@ SELECT
p.permalink AS page_permalink p.permalink AS page_permalink
FROM FROM
navigation n navigation n
LEFT JOIN LEFT JOIN pages p ON n.pages = p.id;
pages p ON n.pages = p.id;

View file

@ -6,58 +6,49 @@ SELECT
p.description, p.description,
CONCAT('/', df.filename_disk) AS open_graph_image, CONCAT('/', df.filename_disk) AS open_graph_image,
p.updated, p.updated,
(
( SELECT
SELECT json_agg( json_agg(
CASE CASE WHEN pb.collection = 'youtube_player' THEN
WHEN pb.collection = 'youtube_player' THEN json_build_object( json_build_object('type', pb.collection, 'url', yp.url)
'type', pb.collection, WHEN pb.collection = 'github_banner' THEN
'url', yp.url json_build_object('type', pb.collection, 'url', gb.url)
) WHEN pb.collection = 'npm_banner' THEN
WHEN pb.collection = 'github_banner' THEN json_build_object( json_build_object('type', pb.collection, 'url', nb.url, 'command', nb.command)
'type', pb.collection, WHEN pb.collection = 'rss_banner' THEN
'url', gb.url json_build_object('type', pb.collection, 'url', rb.url, 'text', rb.text)
) WHEN pb.collection = 'hero' THEN
WHEN pb.collection = 'npm_banner' THEN json_build_object( json_build_object('type', pb.collection, 'image', CONCAT('/', df_hero.filename_disk), 'alt', h.alt_text)
'type', pb.collection, WHEN pb.collection = 'markdown' THEN
'url', nb.url, json_build_object('type', pb.collection, 'text', md.text)
'command', nb.command WHEN pb.collection = 'divider' THEN
) json_build_object('type', pb.collection, 'markup', d.markup)
WHEN pb.collection = 'rss_banner' THEN json_build_object( ELSE
'type', pb.collection, json_build_object('type', pb.collection)
'url', rb.url, END ORDER BY pb.sort)
'text', rb.text FROM
) pages_blocks pb
WHEN pb.collection = 'hero' THEN json_build_object( LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player'
'type', pb.collection, AND yp.id = pb.item::integer
'image', CONCAT('/', df_hero.filename_disk), LEFT JOIN github_banner gb ON pb.collection = 'github_banner'
'alt', h.alt_text AND gb.id = pb.item::integer
) LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner'
WHEN pb.collection = 'markdown' THEN json_build_object( AND nb.id = pb.item::integer
'type', pb.collection, LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner'
'text', md.text AND rb.id = pb.item::integer
) LEFT JOIN hero h ON pb.collection = 'hero'
WHEN pb.collection = 'divider' THEN json_build_object( AND h.id = pb.item::integer
'type', pb.collection, LEFT JOIN directus_files df_hero ON h.image = df_hero.id
'markup', d.markup LEFT JOIN markdown md ON pb.collection = 'markdown'
) AND md.id = pb.item::integer
ELSE json_build_object('type', pb.collection) LEFT JOIN divider d ON pb.collection = 'divider'
END AND d.id = pb.item::integer
ORDER BY pb.sort) WHERE
FROM pages_blocks pb pb.pages_id = p.id) AS blocks
LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player' AND yp.id = pb.item::INTEGER
LEFT JOIN github_banner gb ON pb.collection = 'github_banner' AND gb.id = pb.item::INTEGER
LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner' AND nb.id = pb.item::INTEGER
LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner' AND rb.id = pb.item::INTEGER
LEFT JOIN hero h ON pb.collection = 'hero' AND h.id = pb.item::INTEGER
LEFT JOIN directus_files df_hero ON h.image = df_hero.id
LEFT JOIN markdown md ON pb.collection = 'markdown' AND md.id = pb.item::INTEGER
LEFT JOIN divider d ON pb.collection = 'divider' AND d.id = pb.item::INTEGER
WHERE pb.pages_id = p.id
) AS blocks
FROM FROM
pages p pages p
LEFT JOIN LEFT JOIN directus_files df ON p.open_graph_image = df.id
directus_files df ON p.open_graph_image = df.id
GROUP BY GROUP BY
p.id, df.filename_disk; p.id,
df.filename_disk;

View file

@ -1,120 +1,91 @@
CREATE OR REPLACE VIEW optimized_books AS CREATE OR REPLACE VIEW optimized_books AS
SELECT SELECT
b.date_finished, b.date_finished,
EXTRACT(YEAR FROM b.date_finished) AS year, EXTRACT(YEAR FROM b.date_finished) AS year,
b.author, b.author,
b.description, b.description,
b.title, b.title,
b.progress, b.progress,
b.read_status AS status, b.read_status AS status,
b.star_rating AS rating, b.star_rating AS rating,
b.review, b.review,
b.slug AS url, b.slug AS url,
CONCAT('/', df.filename_disk) AS image, CONCAT('/', df.filename_disk) AS image,
b.favorite, b.favorite,
b.tattoo, b.tattoo,
(
( SELECT
SELECT array_agg(t.name) array_agg(t.name)
FROM books_tags bt FROM
LEFT JOIN tags t ON bt.tags_id = t.id books_tags bt
WHERE bt.books_id = b.id LEFT JOIN tags t ON bt.tags_id = t.id
) AS tags, WHERE
bt.books_id = b.id) AS tags,
( (
SELECT json_agg(json_build_object( SELECT
'name', a.name_string, json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays))
'url', a.slug, FROM
'country', a.country, books_artists ba
'total_plays', a.total_plays LEFT JOIN artists a ON ba.artists_id = a.id
)) WHERE
FROM books_artists ba ba.books_id = b.id) AS artists,
LEFT JOIN artists a ON ba.artists_id = a.id (
WHERE ba.books_id = b.id SELECT
) AS artists, json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug))
FROM
( movies_books mb
SELECT json_agg(json_build_object( LEFT JOIN movies m ON mb.movies_id = m.id
'title', m.title, WHERE
'year', m.year, mb.books_id = b.id) AS movies,
'url', m.slug (
)) SELECT
FROM movies_books mb json_agg(json_build_object('name', g.name, 'url', g.slug))
LEFT JOIN movies m ON mb.movies_id = m.id FROM
WHERE mb.books_id = b.id genres_books gb
) AS movies, LEFT JOIN genres g ON gb.genres_id = g.id
WHERE
( gb.books_id = b.id) AS genres,
SELECT json_agg(json_build_object( (
'name', g.name, SELECT
'url', g.slug json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug))
)) FROM
FROM genres_books gb shows_books sb
LEFT JOIN genres g ON gb.genres_id = g.id LEFT JOIN shows s ON sb.shows_id = s.id
WHERE gb.books_id = b.id WHERE
) AS genres, sb.books_id = b.id) AS shows,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
'title', s.title, ORDER BY p.date DESC)
'year', s.year, FROM
'url', s.slug posts_books pb
)) LEFT JOIN posts p ON pb.posts_id = p.id
FROM shows_books sb WHERE
LEFT JOIN shows s ON sb.shows_id = s.id pb.books_id = b.id) AS posts,
WHERE sb.books_id = b.id (
) AS shows, SELECT
json_agg(json_build_object('title', rb.title, 'author', rb.author, 'url', rb.slug)
( ORDER BY rb.title)
SELECT json_agg(json_build_object( FROM
'title', p.title, related_books rbk
'date', p.date, LEFT JOIN books rb ON rbk.related_books_id = rb.id
'url', p.slug WHERE
) ORDER BY p.date DESC) rbk.books_id = b.id) AS related_books,
FROM posts_books pb json_build_object('title', CONCAT(b.title, ' by ', b.author), 'image', CONCAT('/', df.filename_disk), 'url', b.slug, 'alt', CONCAT(b.title, ' by ', b.author), 'subtext', CASE WHEN b.star_rating IS NOT NULL THEN
LEFT JOIN posts p ON pb.posts_id = p.id b.star_rating
WHERE pb.books_id = b.id ELSE
) AS posts, NULL
END) AS grid,
( CASE WHEN LOWER(b.read_status) = 'finished'
SELECT json_agg(json_build_object( AND b.star_rating IS NOT NULL THEN
'title', rb.title, json_build_object('title', b.title, 'url', CONCAT('https://coryd.dev', b.slug), 'date', b.date_finished, 'description', b.description, 'image', CONCAT('/', df.filename_disk), 'rating', b.star_rating)
'author', rb.author, ELSE
'url', rb.slug NULL
) ORDER BY rb.title) END AS feed
FROM related_books rbk
LEFT JOIN books rb ON rbk.related_books_id = rb.id
WHERE rbk.books_id = b.id
) AS related_books,
json_build_object(
'title', CONCAT(b.title, ' by ', b.author),
'image', CONCAT('/', df.filename_disk),
'url', b.slug,
'alt', CONCAT(b.title, ' by ', b.author),
'subtext',
CASE
WHEN b.star_rating IS NOT NULL
THEN b.star_rating
ELSE NULL
END
) AS grid,
CASE
WHEN LOWER(b.read_status) = 'finished' AND b.star_rating IS NOT NULL THEN json_build_object(
'title', b.title,
'url', CONCAT('https://coryd.dev', b.slug),
'date', b.date_finished,
'description', b.description,
'image', CONCAT('/', df.filename_disk),
'rating', b.star_rating
)
ELSE NULL
END AS feed
FROM FROM
books b books b
LEFT JOIN LEFT JOIN directus_files df ON b.art = df.id
directus_files df ON b.art = df.id
GROUP BY GROUP BY
b.id, df.filename_disk; b.id,
df.filename_disk;

View file

@ -14,112 +14,89 @@ SELECT
m.slug AS url, m.slug AS url,
CONCAT('/', df.filename_disk) AS image, CONCAT('/', df.filename_disk) AS image,
CONCAT('/', df2.filename_disk) AS backdrop, CONCAT('/', df2.filename_disk) AS backdrop,
json_build_object('title', m.title, 'url', m.slug, 'image', CONCAT('/', df.filename_disk), 'backdrop', CONCAT('/', df2.filename_disk), 'alt', CONCAT(m.title, ' (', m.year, ')'), 'subtext', CASE WHEN m.star_rating IS NOT NULL THEN
json_build_object( CONCAT(m.star_rating, ' (', m.year, ')')
'title', m.title, ELSE
'url', m.slug, CONCAT('(', m.year, ')')
'image', CONCAT('/', df.filename_disk), END) AS grid,
'backdrop', CONCAT('/', df2.filename_disk), (
'alt', CONCAT(m.title, ' (', m.year, ')'), SELECT
'subtext', CASE array_agg(t.name)
WHEN m.star_rating IS NOT NULL THEN CONCAT(m.star_rating, ' (', m.year, ')') FROM
ELSE CONCAT('(', m.year, ')') movies_tags mt
END
) AS grid,
(
SELECT array_agg(t.name)
FROM movies_tags mt
LEFT JOIN tags t ON mt.tags_id = t.id LEFT JOIN tags t ON mt.tags_id = t.id
WHERE mt.movies_id = m.id WHERE
) AS tags, mt.movies_id = m.id) AS tags,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('name', g.name, 'url', g.slug)
'name', g.name, ORDER BY g.name ASC)
'url', g.slug FROM
) ORDER BY g.name ASC) genres_movies gm
FROM genres_movies gm LEFT JOIN genres g ON gm.genres_id = g.id
LEFT JOIN genres g ON gm.genres_id = g.id WHERE
WHERE gm.movies_id = m.id gm.movies_id = m.id) AS genres,
) AS genres, (
SELECT
( json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays)
SELECT json_agg(json_build_object( ORDER BY a.name_string ASC)
'name', a.name_string, FROM
'url', a.slug, movies_artists ma
'country', a.country, LEFT JOIN artists a ON ma.artists_id = a.id
'total_plays', a.total_plays WHERE
) ORDER BY a.name_string ASC) ma.movies_id = m.id) AS artists,
FROM movies_artists ma (
LEFT JOIN artists a ON ma.artists_id = a.id SELECT
WHERE ma.movies_id = m.id json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
) AS artists, ORDER BY b.title ASC)
FROM
( movies_books mb
SELECT json_agg(json_build_object( LEFT JOIN books b ON mb.books_id = b.id
'title', b.title, WHERE
'author', b.author, mb.movies_id = m.id) AS books,
'url', b.slug (
) ORDER BY b.title ASC) SELECT
FROM movies_books mb json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug)
LEFT JOIN books b ON mb.books_id = b.id ORDER BY s.year DESC)
WHERE mb.movies_id = m.id FROM
) AS books, shows_movies sm
LEFT JOIN shows s ON sm.shows_id = s.id
( WHERE
SELECT json_agg(json_build_object( sm.movies_id = m.id) AS shows,
'title', s.title, (
'year', s.year, SELECT
'url', s.slug json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
) ORDER BY s.year DESC) ORDER BY p.date DESC)
FROM shows_movies sm FROM
LEFT JOIN shows s ON sm.shows_id = s.id posts_movies pm
WHERE sm.movies_id = m.id LEFT JOIN posts p ON pm.posts_id = p.id
) AS shows, WHERE
pm.movies_id = m.id) AS posts,
( (
SELECT json_agg(json_build_object( SELECT
'title', p.title, json_agg(json_build_object('title', rm.title, 'year', rm.year, 'url', rm.slug)
'date', p.date, ORDER BY rm.year DESC)
'url', p.slug FROM
) ORDER BY p.date DESC) related_movies r
FROM posts_movies pm LEFT JOIN movies rm ON r.related_movies_id = rm.id
LEFT JOIN posts p ON pm.posts_id = p.id WHERE
WHERE pm.movies_id = m.id r.movies_id = m.id) AS related_movies,
) AS posts, CASE WHEN m.star_rating IS NOT NULL
AND m.last_watched IS NOT NULL THEN
( json_build_object('title', m.title, 'url', CONCAT('https://coryd.dev/movies/', m.slug), 'date', m.last_watched, 'description', m.description, 'image', CONCAT('/', df.filename_disk), 'rating', m.star_rating)
SELECT json_agg(json_build_object( ELSE
'title', rm.title, NULL
'year', rm.year, END AS feed
'url', rm.slug
) ORDER BY rm.year DESC)
FROM related_movies r
LEFT JOIN movies rm ON r.related_movies_id = rm.id
WHERE r.movies_id = m.id
) AS related_movies,
CASE
WHEN m.star_rating IS NOT NULL AND m.last_watched IS NOT NULL THEN
json_build_object(
'title', m.title,
'url', CONCAT('https://coryd.dev/movies/', m.slug),
'date', m.last_watched,
'description', m.description,
'image', CONCAT('/', df.filename_disk),
'rating', m.star_rating
)
ELSE NULL
END AS feed
FROM FROM
movies m movies m
LEFT JOIN LEFT JOIN directus_files df ON m.art = df.id
directus_files df ON m.art = df.id LEFT JOIN directus_files df2 ON m.backdrop = df2.id
LEFT JOIN
directus_files df2 ON m.backdrop = df2.id
GROUP BY GROUP BY
m.id, df.filename_disk, df2.filename_disk m.id,
df.filename_disk,
df2.filename_disk
ORDER BY ORDER BY
m.last_watched DESC, m.favorite DESC, m.title ASC; m.last_watched DESC,
m.favorite DESC,
m.title ASC;

View file

@ -6,28 +6,13 @@ SELECT
a.total_plays, a.total_plays,
CONCAT('/', df.filename_disk) AS image, CONCAT('/', df.filename_disk) AS image,
'album-release' AS type, 'album-release' AS type,
json_build_object('name', ar.name_string, 'url', ar.slug, 'description', ar.description) AS artist,
json_build_object(
'name', ar.name_string,
'url', ar.slug,
'description', ar.description
) AS artist,
EXTRACT(EPOCH FROM a.release_date) AS release_timestamp, EXTRACT(EPOCH FROM a.release_date) AS release_timestamp,
json_build_object('title', a.name, 'image', CONCAT('/', df.filename_disk), 'url', a.release_link, 'alt', CONCAT(a.name, ' by ', ar.name_string), 'subtext', CONCAT(ar.name_string, ' / ', TO_CHAR(a.release_date, 'Mon FMDD, YYYY'))) AS grid
json_build_object(
'title', a.name,
'image', CONCAT('/', df.filename_disk),
'url', a.release_link,
'alt', CONCAT(a.name, ' by ', ar.name_string),
'subtext', CONCAT(ar.name_string, ' / ', TO_CHAR(a.release_date, 'Mon FMDD, YYYY'))
) AS grid
FROM FROM
albums a albums a
LEFT JOIN LEFT JOIN directus_files df ON a.art = df.id
directus_files df ON a.art = df.id LEFT JOIN artists ar ON a.artist = ar.id
LEFT JOIN
artists ar ON a.artist = ar.id
WHERE WHERE
a.release_date IS NOT NULL; a.release_date IS NOT NULL;

View file

@ -3,116 +3,92 @@ SELECT
ar.name_string AS name, ar.name_string AS name,
ar.slug AS url, ar.slug AS url,
ar.tentative, ar.tentative,
ar.total_plays, to_char(ar.total_plays, 'FM999,999,999,999') AS total_plays, -- Format total_plays with commas
ar.country, ar.country,
ar.description, ar.description,
ar.favorite, ar.favorite,
g.name AS genre_name, g.name AS genre_name,
g.slug AS genre_slug, g.slug AS genre_slug,
g.emoji AS genre_emoji, g.emoji AS genre_emoji,
json_build_object('name', g.name, 'url', g.slug, 'emoji', g.emoji) AS genre,
json_build_object(
'name', g.name,
'url', g.slug,
'emoji', g.emoji
) AS genre,
ar.emoji, ar.emoji,
ar.tattoo, ar.tattoo,
CONCAT('/', df.filename_disk) AS image, CONCAT('/', df.filename_disk) AS image,
json_build_object('alt', CONCAT(to_char(ar.total_plays, 'FM999,999,999,999'), ' plays of ', ar.name_string), -- Format total_plays in alt text
json_build_object( 'subtext', CONCAT(to_char(ar.total_plays, 'FM999,999,999,999'), ' plays') -- Format total_plays in subtext
'alt', CONCAT(ar.total_plays, ' plays of ', ar.name_string), ) AS grid,
'subtext', CONCAT(ar.total_plays, ' plays') (
) AS grid, SELECT
json_agg(json_build_object('name', a.name, 'release_year', a.release_year, 'total_plays', to_char(a.total_plays, 'FM999,999,999,999'), -- Format total_plays here as well
( 'art', df_album.filename_disk)
SELECT json_agg(json_build_object( ORDER BY a.release_year)
'name', a.name, FROM
'release_year', a.release_year, albums a
'total_plays', a.total_plays,
'art', df_album.filename_disk
) ORDER BY a.release_year)
FROM albums a
LEFT JOIN directus_files df_album ON a.art = df_album.id LEFT JOIN directus_files df_album ON a.art = df_album.id
WHERE a.artist = ar.id WHERE
) AS albums, a.artist = ar.id) AS albums,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('id', c.id, 'date', c.date, 'venue_name', v.name, 'venue_name_short', trim(split_part(v.name, ',', 1)), 'venue_latitude', v.latitude, 'venue_longitude', v.longitude, 'notes', c.notes)
'id', c.id, ORDER BY c.date DESC)
'date', c.date, FROM
'venue_name', v.name, concerts c
'venue_name_short', trim(split_part(v.name, ',', 1)),
'venue_latitude', v.latitude,
'venue_longitude', v.longitude,
'notes', c.notes
) ORDER BY c.date DESC)
FROM concerts c
LEFT JOIN venues v ON c.venue = v.id LEFT JOIN venues v ON c.venue = v.id
WHERE c.artist = ar.id WHERE
) AS concerts, c.artist = ar.id) AS concerts,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
'title', b.title, ORDER BY b.date_finished DESC)
'author', b.author, FROM
'url', b.slug books_artists ba
) ORDER BY b.date_finished DESC)
FROM books_artists ba
LEFT JOIN books b ON ba.books_id = b.id LEFT JOIN books b ON ba.books_id = b.id
WHERE ba.artists_id = ar.id WHERE
) AS books, ba.artists_id = ar.id) AS books,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
'title', m.title, ORDER BY m.year DESC)
'year', m.year, FROM
'url', m.slug movies_artists ma
) ORDER BY m.year DESC)
FROM movies_artists ma
LEFT JOIN movies m ON ma.movies_id = m.id LEFT JOIN movies m ON ma.movies_id = m.id
WHERE ma.artists_id = ar.id WHERE
) AS movies, ma.artists_id = ar.id) AS movies,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug)
'title', s.title, ORDER BY s.year DESC)
'year', s.year, FROM
'url', s.slug shows_artists sa
) ORDER BY s.year DESC)
FROM shows_artists sa
LEFT JOIN shows s ON sa.shows_id = s.id LEFT JOIN shows s ON sa.shows_id = s.id
WHERE sa.artists_id = ar.id WHERE
) AS shows, sa.artists_id = ar.id) AS shows,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
'title', p.title, ORDER BY p.date DESC)
'date', p.date, FROM
'url', p.slug posts_artists pa
) ORDER BY p.date DESC)
FROM posts_artists pa
LEFT JOIN posts p ON pa.posts_id = p.id LEFT JOIN posts p ON pa.posts_id = p.id
WHERE pa.artists_id = ar.id WHERE
) AS posts, pa.artists_id = ar.id) AS posts,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('name', related_ar.name_string, 'url', related_ar.slug, 'country', related_ar.country, 'total_plays', to_char(related_ar.total_plays, 'FM999,999,999,999'))
'name', related_ar.name_string, ORDER BY related_ar.name_string)
'url', related_ar.slug, FROM
'country', related_ar.country, related_artists ra
'total_plays', related_ar.total_plays
) ORDER BY related_ar.name_string)
FROM related_artists ra
LEFT JOIN artists related_ar ON ra.related_artists_id = related_ar.id LEFT JOIN artists related_ar ON ra.related_artists_id = related_ar.id
WHERE ra.artists_id = ar.id WHERE
) AS related_artists ra.artists_id = ar.id) AS related_artists
FROM FROM
artists ar artists ar
LEFT JOIN LEFT JOIN directus_files df ON ar.art = df.id
directus_files df ON ar.art = df.id LEFT JOIN genres g ON ar.genres = g.id
LEFT JOIN
genres g ON ar.genres = g.id
GROUP BY GROUP BY
ar.id, df.filename_disk, g.name, g.slug, g.emoji; ar.id,
df.filename_disk,
g.name,
g.slug,
g.emoji;

View file

@ -3,34 +3,17 @@ SELECT
c.id, c.id,
c.date, c.date,
c.notes, c.notes,
CASE WHEN c.artist IS NOT NULL THEN
CASE json_build_object('name', a.name_string, 'url', a.slug)
WHEN c.artist IS NOT NULL THEN ELSE
json_build_object( json_build_object('name', c.artist_name_string, 'url', NULL)
'name', a.name_string,
'url', a.slug
)
ELSE
json_build_object(
'name', c.artist_name_string,
'url', NULL
)
END AS artist, END AS artist,
json_build_object('name', v.name, 'name_short', trim(split_part(v.name, ',', 1)), 'latitude', v.latitude, 'longitude', v.longitude, 'notes', v.notes) AS venue,
json_build_object(
'name', v.name,
'name_short', trim(split_part(v.name, ',', 1)),
'latitude', v.latitude,
'longitude', v.longitude,
'notes', v.notes
) AS venue,
c.notes AS concert_notes c.notes AS concert_notes
FROM FROM
concerts c concerts c
LEFT JOIN artists a ON c.artist = a.id LEFT JOIN artists a ON c.artist = a.id
LEFT JOIN venues v ON c.venue = v.id LEFT JOIN venues v ON c.venue = v.id
ORDER BY ORDER BY
c.date DESC; c.date DESC;

View file

@ -4,53 +4,45 @@ SELECT
g.name, g.name,
g.description, g.description,
g.emoji, g.emoji,
g.total_plays, to_char(g.total_plays, 'FM999,999,999,999') AS total_plays,
g.wiki_link, g.wiki_link,
g.slug AS url, g.slug AS url,
(
SELECT
json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'total_plays', to_char(a.total_plays, 'FM999,999,999,999'))
ORDER BY a.total_plays DESC)
FROM
artists a
WHERE
a.genres = g.id) AS artists,
(
SELECT
json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug))
FROM
books b
JOIN genres_books gb ON gb.books_id = b.id
WHERE
gb.genres_id = g.id) AS books,
(
SELECT
json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
ORDER BY m.year DESC)
FROM
movies m
JOIN genres_movies gm ON gm.movies_id = m.id
WHERE
gm.genres_id = g.id) AS movies,
(
SELECT
json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
ORDER BY p.date DESC)
FROM
posts_genres pg
LEFT JOIN posts p ON pg.posts_id = p.id
WHERE
pg.genres_id = g.id) AS posts
FROM
genres g
ORDER BY
g.id ASC;
(
SELECT json_agg(json_build_object(
'name', a.name_string,
'url', a.slug,
'total_plays', a.total_plays
) ORDER BY a.total_plays DESC)
FROM artists a
WHERE a.genres = g.id
) AS artists,
(
SELECT json_agg(json_build_object(
'title', b.title,
'author', b.author,
'url', b.slug
))
FROM books b
JOIN genres_books gb ON gb.books_id = b.id
WHERE gb.genres_id = g.id
) AS books,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
) ORDER BY m.year DESC)
FROM movies m
JOIN genres_movies gm ON gm.movies_id = m.id
WHERE gm.genres_id = g.id
) AS movies,
(
SELECT json_agg(json_build_object(
'title', p.title,
'date', p.date,
'url', p.slug
) ORDER BY p.date DESC)
FROM posts_genres pg
LEFT JOIN posts p ON pg.posts_id = p.id
WHERE pg.genres_id = g.id
) AS posts
FROM
genres g
ORDER BY
g.id ASC;

View file

@ -1,5 +1,4 @@
CREATE OR REPLACE VIEW optimized_listens AS CREATE OR REPLACE VIEW optimized_listens AS SELECT DISTINCT ON (l.id, l.listened_at, l.track_name, l.artist_name, l.album_name)
SELECT DISTINCT ON (l.id, l.listened_at, l.track_name, l.artist_name, l.album_name)
l.id, l.id,
l.listened_at, l.listened_at,
l.track_name, l.track_name,
@ -15,10 +14,15 @@ SELECT DISTINCT ON (l.id, l.listened_at, l.track_name, l.artist_name, l.album_na
CONCAT('/', df_album.filename_disk) AS album_art CONCAT('/', df_album.filename_disk) AS album_art
FROM FROM
listens l listens l
LEFT JOIN artists a ON (l.artist_name = a.name_string) LEFT JOIN artists a ON (l.artist_name = a.name_string)
LEFT JOIN albums al ON (l.album_key = al.key) LEFT JOIN albums al ON (l.album_key = al.key)
LEFT JOIN directus_files df_art ON (a.art = df_art.id) LEFT JOIN directus_files df_art ON (a.art = df_art.id)
LEFT JOIN directus_files df_album ON (al.art = df_album.id) LEFT JOIN directus_files df_album ON (al.art = df_album.id)
LEFT JOIN genres g ON (a.genres = g.id) LEFT JOIN genres g ON (a.genres = g.id)
ORDER BY ORDER BY
l.id, l.listened_at, l.track_name, l.artist_name, l.album_name; l.id,
l.listened_at,
l.track_name,
l.artist_name,
l.album_name;

View file

@ -5,18 +5,16 @@ SELECT
COUNT(*) AS plays, COUNT(*) AS plays,
ol.album_art, ol.album_art,
ol.artist_url, ol.artist_url,
json_build_object( json_build_object('title', ol.album_name, 'image', ol.album_art, 'url', ol.artist_url, 'alt', CONCAT(ol.album_name, ' by ', ol.artist_name), 'subtext', ol.artist_name) AS grid
'title', ol.album_name,
'image', ol.album_art,
'url', ol.artist_url,
'alt', CONCAT(ol.album_name, ' by ', ol.artist_name),
'subtext', ol.artist_name
) AS grid
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY GROUP BY
ol.album_name, ol.artist_name, ol.album_art, ol.artist_url ol.album_name,
ol.artist_name,
ol.album_art,
ol.artist_url
ORDER BY ORDER BY
plays DESC; plays DESC;

View file

@ -5,18 +5,15 @@ SELECT
ol.artist_art, ol.artist_art,
ol.artist_url, ol.artist_url,
ARRAY_AGG(DISTINCT ol.genre_name) AS genres, ARRAY_AGG(DISTINCT ol.genre_name) AS genres,
json_build_object( json_build_object('title', ol.artist_name, 'image', ol.artist_art, 'url', ol.artist_url, 'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name), 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
'title', ol.artist_name,
'image', ol.artist_art,
'url', ol.artist_url,
'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name),
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY GROUP BY
ol.artist_name, ol.artist_art, ol.artist_url ol.artist_name,
ol.artist_art,
ol.artist_url
ORDER BY ORDER BY
plays DESC; plays DESC;

View file

@ -3,15 +3,14 @@ SELECT
ol.genre_name, ol.genre_name,
ol.genre_url, ol.genre_url,
COUNT(*) AS plays, COUNT(*) AS plays,
json_build_object( json_build_object('alt', ol.genre_name, 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
'alt', ol.genre_name,
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY GROUP BY
ol.genre_name, ol.genre_url ol.genre_name,
ol.genre_url
ORDER BY ORDER BY
plays DESC; plays DESC;

View file

@ -14,7 +14,11 @@ WITH track_stats AS (
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY GROUP BY
ol.track_name, ol.artist_name, ol.album_name, ol.album_art, ol.artist_url ol.track_name,
ol.artist_name,
ol.album_name,
ol.album_art,
ol.artist_url
) )
SELECT SELECT
track_name, track_name,
@ -24,14 +28,10 @@ SELECT
last_listened, last_listened,
album_art, album_art,
artist_url, artist_url,
json_build_object( json_build_object('title', track_name, 'artist', artist_name, 'url', artist_url, 'plays', plays, 'alt', CONCAT(track_name, ' by ', artist_name), 'subtext', CONCAT(album_name, ' (', plays, ' plays)'), 'percentage', ROUND((plays::decimal / most_played) * 100, 2)) AS chart
'title', track_name, FROM
'artist', artist_name, track_stats
'url', artist_url, ORDER BY
'plays', plays, plays DESC,
'alt', CONCAT(track_name, ' by ', artist_name), last_listened DESC;
'subtext', CONCAT(album_name, ' (', plays, ' plays)'),
'percentage', ROUND((plays::decimal / most_played) * 100, 2)
) AS chart
FROM track_stats
ORDER BY plays DESC, last_listened DESC;

View file

@ -9,8 +9,8 @@ WITH latest_listen AS (
ROW_NUMBER() OVER (ORDER BY l.listened_at DESC) AS row_num ROW_NUMBER() OVER (ORDER BY l.listened_at DESC) AS row_num
FROM FROM
listens l listens l
JOIN artists a ON l.artist_name = a.name_string JOIN artists a ON l.artist_name = a.name_string
LEFT JOIN genres g ON a.genres = g.id LEFT JOIN genres g ON a.genres = g.id
) )
SELECT SELECT
track_name, track_name,
@ -22,3 +22,4 @@ FROM
latest_listen latest_listen
WHERE WHERE
row_num = 1; row_num = 1;

View file

@ -13,17 +13,11 @@ SELECT
ol.album_art, ol.album_art,
ol.artist_url, ol.artist_url,
ol.genre_url, ol.genre_url,
json_build_object( json_build_object('title', ol.track_name, 'subtext', ol.artist_name, 'alt', CONCAT(ol.track_name, ' by ', ol.artist_name), 'url', ol.artist_url, 'image', ol.album_art, 'played_at', ol.listened_at) AS chart
'title', ol.track_name,
'subtext', ol.artist_name,
'alt', CONCAT(ol.track_name, ' by ', ol.artist_name),
'url', ol.artist_url,
'image', ol.album_art,
'played_at', ol.listened_at
) AS chart
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
ORDER BY ORDER BY
TO_TIMESTAMP(ol.listened_at) DESC; TO_TIMESTAMP(ol.listened_at) DESC;

View file

@ -1,22 +0,0 @@
CREATE OR REPLACE VIEW three_month_albums AS
SELECT
ol.album_name,
ol.artist_name,
COUNT(*) AS plays,
ol.album_art,
ol.artist_url,
json_build_object(
'title', ol.album_name,
'image', ol.album_art,
'url', ol.artist_url,
'alt', CONCAT(ol.album_name, ' by ', ol.artist_name),
'subtext', ol.artist_name
) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.album_name, ol.artist_name, ol.album_art, ol.artist_url
ORDER BY
plays DESC;

View file

@ -1,22 +0,0 @@
CREATE OR REPLACE VIEW three_month_artists AS
SELECT
ol.artist_name,
COUNT(*) AS plays,
ol.artist_art,
ol.artist_url,
ARRAY_AGG(DISTINCT ol.genre_name) AS genres,
json_build_object(
'title', ol.artist_name,
'image', ol.artist_art,
'url', ol.artist_url,
'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name),
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.artist_name, ol.artist_art, ol.artist_url
ORDER BY
plays DESC;

View file

@ -1,17 +0,0 @@
CREATE OR REPLACE VIEW three_month_genres AS
SELECT
ol.genre_name,
ol.genre_url,
COUNT(*) AS plays,
json_build_object(
'alt', ol.genre_name,
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.genre_name, ol.genre_url
ORDER BY
plays DESC;

View file

@ -1,37 +0,0 @@
CREATE OR REPLACE VIEW three_month_tracks AS
WITH track_stats AS (
SELECT
ol.track_name,
ol.artist_name,
ol.album_name,
COUNT(*) AS plays,
MAX(ol.listened_at) AS last_listened,
ol.album_art,
ol.artist_url,
MAX(COUNT(*)) OVER () AS most_played
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.track_name, ol.artist_name, ol.album_name, ol.album_art, ol.artist_url
)
SELECT
track_name,
artist_name,
album_name,
plays,
last_listened,
album_art,
artist_url,
json_build_object(
'title', track_name,
'artist', artist_name,
'url', artist_url,
'plays', plays,
'alt', CONCAT(track_name, ' by ', artist_name),
'subtext', CONCAT(album_name, ' (', plays, ' plays)'),
'percentage', ROUND((plays::decimal / most_played) * 100, 2)
) AS chart
FROM track_stats
ORDER BY plays DESC, last_listened DESC;

View file

@ -5,18 +5,16 @@ SELECT
COUNT(*) AS plays, COUNT(*) AS plays,
ol.album_art, ol.album_art,
ol.artist_url, ol.artist_url,
json_build_object( json_build_object('title', ol.album_name, 'image', ol.album_art, 'url', ol.artist_url, 'alt', CONCAT(ol.album_name, ' by ', ol.artist_name), 'subtext', ol.artist_name) AS grid
'title', ol.album_name,
'image', ol.album_art,
'url', ol.artist_url,
'alt', CONCAT(ol.album_name, ' by ', ol.artist_name),
'subtext', ol.artist_name
) AS grid
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY GROUP BY
ol.album_name, ol.artist_name, ol.album_art, ol.artist_url ol.album_name,
ol.artist_name,
ol.album_art,
ol.artist_url
ORDER BY ORDER BY
plays DESC; plays DESC;

View file

@ -5,18 +5,15 @@ SELECT
ol.artist_art, ol.artist_art,
ol.artist_url, ol.artist_url,
ARRAY_AGG(DISTINCT ol.genre_name) AS genres, ARRAY_AGG(DISTINCT ol.genre_name) AS genres,
json_build_object( json_build_object('title', ol.artist_name, 'image', ol.artist_art, 'url', ol.artist_url, 'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name), 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
'title', ol.artist_name,
'image', ol.artist_art,
'url', ol.artist_url,
'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name),
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY GROUP BY
ol.artist_name, ol.artist_art, ol.artist_url ol.artist_name,
ol.artist_art,
ol.artist_url
ORDER BY ORDER BY
plays DESC; plays DESC;

View file

@ -3,15 +3,14 @@ SELECT
ol.genre_name, ol.genre_name,
ol.genre_url, ol.genre_url,
COUNT(*) AS plays, COUNT(*) AS plays,
json_build_object( json_build_object('alt', ol.genre_name, 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
'alt', ol.genre_name,
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM FROM
optimized_listens ol optimized_listens ol
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY GROUP BY
ol.genre_name, ol.genre_url ol.genre_name,
ol.genre_url
ORDER BY ORDER BY
plays DESC; plays DESC;

View file

@ -14,7 +14,11 @@ WITH track_stats AS (
WHERE WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days' TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY GROUP BY
ol.track_name, ol.artist_name, ol.album_name, ol.album_art, ol.artist_url ol.track_name,
ol.artist_name,
ol.album_name,
ol.album_art,
ol.artist_url
) )
SELECT SELECT
track_name, track_name,
@ -24,14 +28,10 @@ SELECT
last_listened, last_listened,
album_art, album_art,
artist_url, artist_url,
json_build_object( json_build_object('title', track_name, 'artist', artist_name, 'url', artist_url, 'plays', plays, 'alt', CONCAT(track_name, ' by ', artist_name), 'subtext', CONCAT(album_name, ' (', plays, ' plays)'), 'percentage', ROUND((plays::decimal / most_played) * 100, 2)) AS chart
'title', track_name, FROM
'artist', artist_name, track_stats
'url', artist_url, ORDER BY
'plays', plays, plays DESC,
'alt', CONCAT(track_name, ' by ', artist_name), last_listened DESC;
'subtext', CONCAT(album_name, ' (', plays, ' plays)'),
'percentage', ROUND((plays::decimal / most_played) * 100, 2)
) AS chart
FROM track_stats
ORDER BY plays DESC, last_listened DESC;

View file

@ -11,121 +11,84 @@ SELECT
s.slug AS url, s.slug AS url,
CONCAT('/', df_art.filename_disk) AS image, CONCAT('/', df_art.filename_disk) AS image,
CONCAT('/', df_backdrop.filename_disk) AS backdrop, CONCAT('/', df_backdrop.filename_disk) AS backdrop,
json_build_object('title', s.title, 'image', CONCAT('/', df_art.filename_disk), 'backdrop', CONCAT('/', df_backdrop.filename_disk), 'url', s.slug, 'alt', CONCAT(s.title, ' (', s.year, ')'), 'subtext', COALESCE((
json_build_object( SELECT
'title', s.title, CASE WHEN e1.last_watched_at >= NOW() - INTERVAL '90 days' THEN
'image', CONCAT('/', df_art.filename_disk), CONCAT('S', e1.season_number, 'E', e1.episode_number)
'backdrop', CONCAT('/', df_backdrop.filename_disk), ELSE
'url', s.slug, CONCAT('(', s.year::text, ')')
'alt', CONCAT(s.title, ' (', s.year, ')'), END FROM episodes e1
'subtext', COALESCE( WHERE
( e1.show = s.id ORDER BY e1.last_watched_at DESC, e1.season_number DESC, e1.episode_number DESC LIMIT 1), CONCAT('(', s.year::text, ')'))) AS grid,
SELECT json_build_object('title', s.title, 'year', s.year, 'url', s.slug, 'image', CONCAT('/', df_art.filename_disk), 'backdrop', CONCAT('/', df_backdrop.filename_disk), 'formatted_episode', COALESCE((
CASE SELECT
WHEN e1.last_watched_at >= NOW() - INTERVAL '90 days' THEN CONCAT('S', e2.season_number, 'E', e2.episode_number)
CONCAT('S', e1.season_number, 'E', e1.episode_number) FROM episodes e2
ELSE WHERE
CONCAT('(', s.year::TEXT, ')') e2.show = s.id ORDER BY e2.season_number DESC, e2.episode_number DESC LIMIT 1)), 'last_watched_at', MAX(e.last_watched_at)) AS episode,
END (
FROM episodes e1 SELECT
WHERE e1.show = s.id json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
ORDER BY e1.last_watched_at DESC, e1.season_number DESC, e1.episode_number DESC ORDER BY m.year DESC)
LIMIT 1 FROM
), shows_movies sm
CONCAT('(', s.year::TEXT, ')')
)
) AS grid,
json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug,
'image', CONCAT('/', df_art.filename_disk),
'backdrop', CONCAT('/', df_backdrop.filename_disk),
'formatted_episode', COALESCE(
(SELECT CONCAT('S', e2.season_number, 'E', e2.episode_number)
FROM episodes e2
WHERE e2.show = s.id
ORDER BY e2.season_number DESC, e2.episode_number DESC
LIMIT 1)
),
'last_watched_at', MAX(e.last_watched_at)
) AS episode,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
) ORDER BY m.year DESC)
FROM shows_movies sm
LEFT JOIN movies m ON sm.movies_id = m.id LEFT JOIN movies m ON sm.movies_id = m.id
WHERE sm.shows_id = s.id WHERE
) AS movies, sm.shows_id = s.id) AS movies,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
'title', b.title, ORDER BY b.title)
'author', b.author, FROM
'url', b.slug shows_books sb
) ORDER BY b.title)
FROM shows_books sb
LEFT JOIN books b ON sb.books_id = b.id LEFT JOIN books b ON sb.books_id = b.id
WHERE sb.shows_id = s.id WHERE
) AS books, sb.shows_id = s.id) AS books,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
'title', p.title, ORDER BY p.date DESC)
'date', p.date, FROM
'url', p.slug posts_shows ps
) ORDER BY p.date DESC)
FROM posts_shows ps
LEFT JOIN posts p ON ps.posts_id = p.id LEFT JOIN posts p ON ps.posts_id = p.id
WHERE ps.shows_id = s.id WHERE
) AS posts, ps.shows_id = s.id) AS posts,
(
( SELECT
SELECT array_agg(t.name) array_agg(t.name)
FROM shows_tags st FROM
shows_tags st
LEFT JOIN tags t ON st.tags_id = t.id LEFT JOIN tags t ON st.tags_id = t.id
WHERE st.shows_id = s.id WHERE
) AS tags, st.shows_id = s.id) AS tags,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('title', rs.title, 'year', rs.year, 'url', rs.slug)
'title', rs.title, ORDER BY rs.year DESC)
'year', rs.year, FROM
'url', rs.slug related_shows sr
) ORDER BY rs.year DESC)
FROM related_shows sr
LEFT JOIN shows rs ON sr.related_shows_id = rs.id LEFT JOIN shows rs ON sr.related_shows_id = rs.id
WHERE sr.shows_id = s.id WHERE
) AS related_shows, sr.shows_id = s.id) AS related_shows,
(
( SELECT
SELECT json_agg(json_build_object( json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays)
'name', a.name_string, ORDER BY a.name_string)
'url', a.slug, FROM
'country', a.country, shows_artists sa
'total_plays', a.total_plays
) ORDER BY a.name_string)
FROM shows_artists sa
LEFT JOIN artists a ON sa.artists_id = a.id LEFT JOIN artists a ON sa.artists_id = a.id
WHERE sa.shows_id = s.id WHERE
) AS artists, sa.shows_id = s.id) AS artists,
MAX(e.last_watched_at) AS last_watched_at MAX(e.last_watched_at) AS last_watched_at
FROM FROM
shows s shows s
LEFT JOIN LEFT JOIN episodes e ON s.id = e.show
episodes e ON s.id = e.show LEFT JOIN directus_files df_art ON s.art = df_art.id
LEFT JOIN LEFT JOIN directus_files df_backdrop ON s.backdrop = df_backdrop.id
directus_files df_art ON s.art = df_art.id
LEFT JOIN
directus_files df_backdrop ON s.backdrop = df_backdrop.id
GROUP BY GROUP BY
s.id, df_art.filename_disk, df_backdrop.filename_disk s.id,
df_art.filename_disk,
df_backdrop.filename_disk
ORDER BY ORDER BY
MAX(e.last_watched_at) DESC; MAX(e.last_watched_at) DESC;

View file

@ -34,7 +34,6 @@ const startEleventy = () =>
["eleventy", "--serve", "--port", ELEVENTY_PORT], ["eleventy", "--serve", "--port", ELEVENTY_PORT],
"Eleventy" "Eleventy"
); );
const startWorker = () => const startWorker = () =>
startProcess( startProcess(
"npx", "npx",
@ -44,30 +43,30 @@ const startWorker = () =>
const app = express(); const app = express();
app.use((req, res, next) => { const setContentType = (req, res) => {
if (req.path === "/js/script.js") { const contentTypeMap = {
res.setHeader("Content-Type", "application/javascript"); ".css": "text/css",
return res.send(""); ".js": "application/javascript",
".json": "application/json",
"/api/": "application/json",
"/feeds/all": "application/xml",
"/feeds/books": "application/xml",
"/feeds/links": "application/xml",
"/feeds/movies": "application/xml",
"/feeds/posts": "application/xml",
"/feeds/syndication": "application/xml",
};
for (const [key, value] of Object.entries(contentTypeMap)) {
if (req.path.endsWith(key) || req.path.startsWith(key)) {
res.setHeader("Content-Type", value);
break;
}
} }
if (req.path.endsWith(".css")) res.setHeader("Content-Type", "text/css"); };
else if (req.path.endsWith(".js"))
res.setHeader("Content-Type", "application/javascript"); app.use((req, res, next) => {
else if (req.path.endsWith(".json")) setContentType(req, res);
res.setHeader("Content-Type", "application/json");
else if (req.path.startsWith("/api/"))
res.setHeader("Content-Type", "application/json");
else if (req.path.startsWith("/feeds/all"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/books"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/links"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/movies"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/posts"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/syndication"))
res.setHeader("Content-Type", "application/xml");
next(); next();
}); });

View file

@ -1,53 +1,45 @@
{%- assign fullUrl = globals.url | append: page.url -%} {%- assign fullUrl = globals.url | append: page.url -%}
{%- assign pageTitle = globals.site_name -%} {%- assign pageTitle = globals.site_name -%}
{%- if schema == 'blog' -%}
{%- assign pageTitle = post.title -%}
{%- elsif title -%}
{%- assign pageTitle = title | append: ' / ' | append: globals.site_name -%}
{%- elsif schema == 'music-index' -%}
{%- assign pageTitle = 'Music / ' | append: globals.site_name -%}
{%- elsif schema == 'music-period' -%}
{%- assign pageTitle = 'Music / ' | append: page.title | append: globals.site_name -%}
{%- elsif year.value -%}
{%- assign pageTitle = ' / Books ' | prepend: year.value | append: ' / ' | append: globals.site_name -%}
{%- elsif page.description -%}
{%- assign pageTitle = page.title | append: ' / ' | append: globals.site_name -%}
{%- endif -%}
{%- assign pageDescription = globals.site_description -%} {%- assign pageDescription = globals.site_description -%}
{%- if schema == 'blog' -%}
{%- assign pageDescription = post.description | markdown | strip_html -%} {%- case schema -%}
{%- elsif page.description -%} {%- when 'blog' -%}
{%- assign pageTitle = post.title -%}
{%- assign pageDescription = post.description | markdown | strip_html | default: globals.site_description -%}
{%- when 'music-index' -%}
{%- assign pageTitle = 'Music / ' | append: globals.site_name -%}
{%- when 'music-period' -%}
{%- assign pageTitle = 'Music / ' | append: page.title | append: ' / ' | append: globals.site_name -%}
{%- when 'books-year' -%}
{%- assign pageTitle = year.value | append: ' / Books / ' | append: globals.site_name -%}
{%- else -%}
{%- assign pageTitle = title | append: ' / ' | append: globals.site_name -%}
{%- endcase -%}
{%- if page.description -%}
{%- assign pageDescription = page.description -%} {%- assign pageDescription = page.description -%}
{%- elsif description -%} {%- elsif description -%}
{%- assign pageDescription = description -%} {%- assign pageDescription = description -%}
{%- endif -%} {%- endif -%}
{%- assign ogImage = globals.cdn_url | append: globals.avatar -%} {%- assign ogImage = globals.cdn_url | append: globals.avatar -%}
{%- case schema -%} {%- case schema -%}
{%- when 'music' -%} {%- when 'music' -%}
{%- assign ogImage = globals.cdn_url | append: page.image -%} {%- assign ogImage = globals.cdn_url | append: page.image -%}
{%- when 'music-index' -%} {%- when 'music-index' -%}
{%- assign ogImage = globals.cdn_url | append: music.week.artists[0].grid.image -%} {%- assign ogImage = globals.cdn_url | append: music.week.artists[0].grid.image -%}
{%- when 'watching' -%} {%- when 'watching' or 'favorite-movies' -%}
{%- assign featuredMovie = movies.recentlyWatched | first -%} {%- assign featuredMovie = movies.recentlyWatched | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredMovie.grid.backdrop -%} {%- assign ogImage = globals.cdn_url | append: featuredMovie.grid.backdrop -%}
{%- when 'watching-shows' -%} {%- when 'watching-shows' or 'favorite-shows' -%}
{%- assign featuredShow = tv.recentlyWatched | first -%} {%- assign featuredShow = tv.recentlyWatched | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredShow.grid.backdrop -%} {%- assign ogImage = globals.cdn_url | append: featuredShow.grid.backdrop -%}
{%- when 'favorite-movies' -%} {%- when 'books' or 'books-year' -%}
{%- assign featuredMovie = movies.favorites| shuffleArray | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredMovie.grid.backdrop -%}
{%- when 'favorite-shows' -%}
{%- assign featuredShow = tv.favorites | shuffleArray | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredShow.grid.backdrop -%}
{%- when 'books' -%}
{%- assign featuredBook = books.all | filterBooksByStatus: 'started' | reverse | first -%} {%- assign featuredBook = books.all | filterBooksByStatus: 'started' | reverse | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredBook.grid.image -%} {%- assign ogImage = globals.cdn_url | append: featuredBook.grid.image -%}
{%- when 'books-year' -%}
{%- assign featuredBook = books.currentYear | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredBook.grid.image -%}
{%- endcase -%} {%- endcase -%}
{%- assign escapedPageDescription = pageDescription | escape -%} {%- assign escapedPageDescription = pageDescription | escape -%}
<!doctype html> <!doctype html>

View file

@ -1,4 +0,0 @@
---
layout: default
permalink: "/dynamic.html"
---

View file

@ -64,7 +64,7 @@ export default {
break; break;
} }
const templateResponse = await fetch(`${BASE_URL}/dynamic.html`); const templateResponse = await fetch(`${BASE_URL}`);
const template = await templateResponse.text(); const template = await templateResponse.text();
const metadata = generateMetadata(data, type, globals); const metadata = generateMetadata(data, type, globals);
const html = updateDynamicContent(template, metadata, mediaHtml); const html = updateDynamicContent(template, metadata, mediaHtml);

View file

@ -1,11 +1,11 @@
import truncateHtml from "truncate-html"; import truncateHtml from "truncate-html";
import { convert } from "html-to-text"; import { convert } from "html-to-text";
import { parseCountryField } from "./countries.js"; import { parseCountryField } from "./countries.js";
import { formatDate, md } from "./formatters.js"; import { formatDate, md } from "./formatters.js";
import { ICON_MAP } from "./icons.js"; import { ICON_MAP } from "./icons.js";
const warningBanner = `<div class="banner warning"><p>${ICON_MAP["alertTriangle"]}There are probably spoilers after this banner — this is a warning about them.</p></div>`; const warningBanner = `<div class="banner warning"><p>${ICON_MAP["alertTriangle"]}There are probably spoilers after this banner — this is a warning about them.</p></div>`;
const generateAssociatedMediaHTML = (data, isGenre = false) => { const generateAssociatedMediaHTML = (data, isGenre = false) => {
const sections = [ const sections = [
{ {
@ -50,6 +50,12 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
category: "article", category: "article",
title: "Related Post(s)", title: "Related Post(s)",
}, },
{
key: "books",
icon: "books",
category: "books",
title: "Related Book(s)",
},
{ {
key: "related_books", key: "related_books",
icon: "books", icon: "books",
@ -75,10 +81,11 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
const year = item.year ? ` (${item.year})` : ""; const year = item.year ? ` (${item.year})` : "";
const author = item.author ? ` by ${item.author}` : ""; const author = item.author ? ` by ${item.author}` : "";
const totalPlays = item.total_plays const totalPlays = item.total_plays
? ` <strong class="highlight-text">${ ? ` <strong class="highlight-text">${item.total_plays} ${
item.total_plays item.total_plays === 1 ? "play" : "plays"
}</strong> ${item.total_plays === 1 ? "play" : "plays"}` }</strong>`
: ""; : "";
let listItemContent = name; let listItemContent = name;
if (key === "artists" || key === "related_artists") { if (key === "artists" || key === "related_artists") {
@ -89,7 +96,7 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
key === "related_shows" key === "related_shows"
) { ) {
listItemContent = `${name}${year}`; listItemContent = `${name}${year}`;
} else if (key === "related_books") { } else if (key === "books" || key === "related_books") {
listItemContent = `${name}${author}`; listItemContent = `${name}${author}`;
} }
@ -101,6 +108,7 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
}) })
.join(""); .join("");
}; };
const generateMediaLinks = (data, type, count = 10) => { const generateMediaLinks = (data, type, count = 10) => {
if (!data || !type) return ""; if (!data || !type) return "";

View file

@ -17,9 +17,3 @@ routes = [
{ pattern = "https://coryd.dev/music/genres/*", zone_id = "${CF_ZONE_ID}" }, { pattern = "https://coryd.dev/music/genres/*", zone_id = "${CF_ZONE_ID}" },
{ pattern = "https://coryd.dev/books/*", zone_id = "${CF_ZONE_ID}" }, { pattern = "https://coryd.dev/books/*", zone_id = "${CF_ZONE_ID}" },
] ]
[[env.production.excludes]]
routes = [
{ pattern = "https://coryd.dev/books", zone_id = "${CF_ZONE_ID}" },
{ pattern = "https://coryd.dev/books/", zone_id = "${CF_ZONE_ID}" },
]