Skip to content

Commit

Permalink
Merge pull request #747 from podverse/develop
Browse files Browse the repository at this point in the history
Release v4.16.14
  • Loading branch information
mitchdowney authored Apr 27, 2024
2 parents 02f8458 + a1fd370 commit b6acef3
Show file tree
Hide file tree
Showing 22 changed files with 628 additions and 407 deletions.
9 changes: 9 additions & 0 deletions migrations/0056_set_timestamps_trigger.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Create the function to set timestamps with triggers

CREATE FUNCTION set_timestamps()
RETURNS TRIGGER AS $$
BEGIN
NEW."updatedAt" := NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
81 changes: 81 additions & 0 deletions migrations/0057_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

-- Timeframe enum
CREATE TYPE timeframe_enum AS ENUM ('daily', 'weekly', 'monthly', 'yearly', 'all_time');

-- Podcasts Stats
CREATE TABLE IF NOT EXISTS stats_podcast (
id SERIAL PRIMARY KEY,
play_count INTEGER DEFAULT 0,
timeframe timeframe_enum NOT NULL,
podcast_id VARCHAR NOT NULL REFERENCES podcasts(id) ON DELETE CASCADE,
CONSTRAINT unique_timeframe_podcast UNIQUE (timeframe, podcast_id),
"createdAt" timestamp without time zone DEFAULT now() NOT NULL,
"updatedAt" timestamp without time zone DEFAULT now() NOT NULL
);

CREATE INDEX "stats_podcast_play_count_idx" ON stats_podcast (play_count);
CREATE INDEX "stats_podcast_timeframe_idx" ON stats_podcast (timeframe);
CREATE INDEX "stats_podcast_podcast_id_idx" ON stats_podcast (podcast_id);
CREATE INDEX "stats_podcast_updated_at" on stats_podcast ("updatedAt");

CREATE TRIGGER set_timestamps_before_insert
BEFORE INSERT ON stats_podcast
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();

CREATE TRIGGER set_timestamps_before_update
BEFORE UPDATE ON stats_podcast
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();

-- Episodes Stats
CREATE TABLE IF NOT EXISTS stats_episode (
id SERIAL PRIMARY KEY,
play_count INTEGER DEFAULT 0,
timeframe timeframe_enum NOT NULL,
episode_id VARCHAR NOT NULL REFERENCES episodes(id) ON DELETE CASCADE,
CONSTRAINT unique_timeframe_episode UNIQUE (timeframe, episode_id),
"createdAt" timestamp without time zone DEFAULT now() NOT NULL,
"updatedAt" timestamp without time zone DEFAULT now() NOT NULL
);

CREATE INDEX "stats_episode_play_count_idx" ON stats_episode (play_count);
CREATE INDEX "stats_episode_timeframe_idx" ON stats_episode (timeframe);
CREATE INDEX "stats_episode_episode_id_idx" ON stats_episode (episode_id);
CREATE INDEX "stats_episode_updated_at" on stats_episode ("updatedAt");

CREATE TRIGGER set_timestamps_before_insert
BEFORE INSERT ON stats_episode
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();

CREATE TRIGGER set_timestamps_before_update
BEFORE UPDATE ON stats_episode
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();

-- MediaRef Stats
CREATE TABLE IF NOT EXISTS stats_media_ref (
id SERIAL PRIMARY KEY,
play_count INTEGER DEFAULT 0,
timeframe timeframe_enum NOT NULL,
media_ref_id VARCHAR NOT NULL REFERENCES "mediaRefs"(id) ON DELETE CASCADE,
CONSTRAINT unique_timeframe_media_ref UNIQUE (timeframe, media_ref_id),
"createdAt" timestamp without time zone DEFAULT now() NOT NULL,
"updatedAt" timestamp without time zone DEFAULT now() NOT NULL
);

CREATE INDEX "stats_media_ref_play_count_idx" ON stats_media_ref (play_count);
CREATE INDEX "stats_media_ref_timeframe_idx" ON stats_media_ref (timeframe);
CREATE INDEX "stats_media_ref_media_ref_id_idx" ON stats_media_ref (media_ref_id);
CREATE INDEX "stats_media_ref_updated_at" on stats_media_ref ("updatedAt");

CREATE TRIGGER set_timestamps_before_insert
BEFORE INSERT ON stats_media_ref
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();

CREATE TRIGGER set_timestamps_before_update
BEFORE UPDATE ON stats_media_ref
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();
137 changes: 137 additions & 0 deletions migrations/0058_drop_old_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
--
-- NOTE: THIS MUST ONLY BE RUN AFTER THE NEW API IS DEPLOYED.
--

-- Drop the podcasts stats indexes

DO $$
DECLARE
index_name TEXT;
BEGIN
FOR index_name IN
SELECT indexname
FROM pg_indexes
WHERE tablename = 'podcasts'
AND (
indexdef ILIKE '%pastHourTotalUniquePageviews%'
OR indexdef ILIKE '%pastDayTotalUniquePageviews%'
OR indexdef ILIKE '%pastWeekTotalUniquePageviews%'
OR indexdef ILIKE '%pastMonthTotalUniquePageviews%'
OR indexdef ILIKE '%pastYearTotalUniquePageviews%'
OR indexdef ILIKE '%pastAllTimeTotalUniquePageviews%'
)
LOOP
EXECUTE format('DROP INDEX IF EXISTS %I;', index_name);
END LOOP;
END $$;

-- Drop the episodes stats indexes

DO $$
DECLARE
index_name TEXT;
BEGIN
FOR index_name IN
SELECT indexname
FROM pg_indexes
WHERE tablename = 'episodes'
AND (
indexdef ILIKE '%pastHourTotalUniquePageviews%'
OR indexdef ILIKE '%pastDayTotalUniquePageviews%'
OR indexdef ILIKE '%pastWeekTotalUniquePageviews%'
OR indexdef ILIKE '%pastMonthTotalUniquePageviews%'
OR indexdef ILIKE '%pastYearTotalUniquePageviews%'
OR indexdef ILIKE '%pastAllTimeTotalUniquePageviews%'
)
LOOP
EXECUTE format('DROP INDEX IF EXISTS %I;', index_name);
END LOOP;
END $$;

-- Drop the mediaRefs stats indexes

DO $$
DECLARE
index_name TEXT;
BEGIN
FOR index_name IN
SELECT indexname
FROM pg_indexes
WHERE tablename = 'mediaRefs'
AND (
indexdef ILIKE '%pastHourTotalUniquePageviews%'
OR indexdef ILIKE '%pastDayTotalUniquePageviews%'
OR indexdef ILIKE '%pastWeekTotalUniquePageviews%'
OR indexdef ILIKE '%pastMonthTotalUniquePageviews%'
OR indexdef ILIKE '%pastYearTotalUniquePageviews%'
OR indexdef ILIKE '%pastAllTimeTotalUniquePageviews%'
)
LOOP
EXECUTE format('DROP INDEX IF EXISTS %I;', index_name);
END LOOP;
END $$;

-- Drop old podcast stats columns

ALTER TABLE podcasts
DROP COLUMN "pastHourTotalUniquePageviews";

ALTER TABLE podcasts
DROP COLUMN "pastDayTotalUniquePageviews";

ALTER TABLE podcasts
DROP COLUMN "pastWeekTotalUniquePageviews";

ALTER TABLE podcasts
DROP COLUMN "pastMonthTotalUniquePageviews";

ALTER TABLE podcasts
DROP COLUMN "pastYearTotalUniquePageviews";

ALTER TABLE podcasts
DROP COLUMN "pastAllTimeTotalUniquePageviews";

-- Drop old episode stats columns
-- First drop the dependent materialized view

DROP MATERIALIZED VIEW "episodes_most_recent";

ALTER TABLE episodes
DROP COLUMN "pastHourTotalUniquePageviews";

ALTER TABLE episodes
DROP COLUMN "pastDayTotalUniquePageviews";

ALTER TABLE episodes
DROP COLUMN "pastWeekTotalUniquePageviews";

ALTER TABLE episodes
DROP COLUMN "pastMonthTotalUniquePageviews";

ALTER TABLE episodes
DROP COLUMN "pastYearTotalUniquePageviews";

ALTER TABLE episodes
DROP COLUMN "pastAllTimeTotalUniquePageviews";

-- Drop old mediaRefs stats columns

DROP MATERIALIZED VIEW "mediaRefs_videos";

ALTER TABLE "mediaRefs"
DROP COLUMN "pastHourTotalUniquePageviews";

ALTER TABLE "mediaRefs"
DROP COLUMN "pastDayTotalUniquePageviews";

ALTER TABLE "mediaRefs"
DROP COLUMN "pastWeekTotalUniquePageviews";

ALTER TABLE "mediaRefs"
DROP COLUMN "pastMonthTotalUniquePageviews";

ALTER TABLE "mediaRefs"
DROP COLUMN "pastYearTotalUniquePageviews";

ALTER TABLE "mediaRefs"
DROP COLUMN "pastAllTimeTotalUniquePageviews";
16 changes: 16 additions & 0 deletions migrations/0059_drop_episode_mediaUrl_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Optionally could be done CONCURRENTLY

-- drop episodes.mediaUrl index
DROP INDEX "IDX_da6fc438d37c65927437b3107f";

-- drop episodes.title index
DROP INDEX "IDX_acd3fd6c4dff47ee1cd00ac582";

-- drop podcasts.title index
DROP INDEX "IDX_a65598c2450c4f601ecb341994";

-- drop podcasts.shrunkImageLastUpdated index
DROP INDEX "IDX_30403fff476188bfb18fd38f10";

--drop podcasts.feedLastUpdated index
DROP INDEX "IDX_09ae4505e3b4b2ddb27486187a";
6 changes: 0 additions & 6 deletions src/controllers/episode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ const addSelectsToQueryBuilder = (qb) => {
.addSelect('episode.mediaFilesize')
.addSelect('episode.mediaType')
.addSelect('episode.mediaUrl')
.addSelect('episode.pastHourTotalUniquePageviews')
.addSelect('episode.pastDayTotalUniquePageviews')
.addSelect('episode.pastWeekTotalUniquePageviews')
.addSelect('episode.pastMonthTotalUniquePageviews')
.addSelect('episode.pastYearTotalUniquePageviews')
.addSelect('episode.pastAllTimeTotalUniquePageviews')
.addSelect('episode.pubDate')
.addSelect('episode.socialInteraction')
.addSelect('episode.subtitle')
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/mediaRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const getMediaRefs = async (query, isFromManticoreSearch?, totalOverride?) => {
searchTitle: `%${searchTitle?.toLowerCase().trim()}%`
})

qb.andWhere('"mediaRef"."isOfficialChapter" IS null')
qb.andWhere('"mediaRef"."isOfficialChapter" IS false')

if (mediaRefIds?.length) {
qb.andWhere('mediaRef.id IN (:...mediaRefIds)', { mediaRefIds })
Expand Down
6 changes: 0 additions & 6 deletions src/controllers/podcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,6 @@ const getPodcasts = async (query, countOverride?, isFromManticoreSearch?) => {
.addSelect('podcast.latestLiveItemStatus')
.addSelect('podcast.linkUrl')
.addSelect('podcast.medium')
.addSelect('podcast.pastHourTotalUniquePageviews')
.addSelect('podcast.pastWeekTotalUniquePageviews')
.addSelect('podcast.pastDayTotalUniquePageviews')
.addSelect('podcast.pastMonthTotalUniquePageviews')
.addSelect('podcast.pastYearTotalUniquePageviews')
.addSelect('podcast.pastAllTimeTotalUniquePageviews')
.addSelect('podcast.shrunkImageUrl')
.addSelect('podcast.sortableTitle')
.addSelect('podcast.subtitle')
Expand Down
52 changes: 4 additions & 48 deletions src/entities/episode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
LiveItem,
MediaRef,
Podcast,
StatsEpisode,
UserHistoryItem,
UserNowPlayingItem,
UserQueueItem
Expand All @@ -38,12 +39,6 @@ import { generateShortId } from '~/lib/utility'

@Entity('episodes')
@Index(['isPublic', 'pubDate'])
@Index(['mediaType', 'pastAllTimeTotalUniquePageviews'])
@Index(['mediaType', 'pastHourTotalUniquePageviews'])
@Index(['mediaType', 'pastDayTotalUniquePageviews'])
@Index(['mediaType', 'pastWeekTotalUniquePageviews'])
@Index(['mediaType', 'pastMonthTotalUniquePageviews'])
@Index(['mediaType', 'pastYearTotalUniquePageviews'])
export class Episode {
@PrimaryColumn('varchar', {
default: generateShortId(),
Expand Down Expand Up @@ -139,48 +134,6 @@ export class Episode {
@Column()
mediaUrl: string

@Index()
@ValidateIf((a) => a.pastHourTotalUniquePageviews != null)
@IsInt()
@Min(0)
@Column({ default: 0 })
pastHourTotalUniquePageviews: number

@Index()
@ValidateIf((a) => a.pastDayTotalUniquePageviews != null)
@IsInt()
@Min(0)
@Column({ default: 0 })
pastDayTotalUniquePageviews: number

@Index()
@ValidateIf((a) => a.pastWeekTotalUniquePageviews != null)
@IsInt()
@Min(0)
@Column({ default: 0 })
pastWeekTotalUniquePageviews: number

@Index()
@ValidateIf((a) => a.pastMonthTotalUniquePageviews != null)
@IsInt()
@Min(0)
@Column({ default: 0 })
pastMonthTotalUniquePageviews: number

@Index()
@ValidateIf((a) => a.pastYearTotalUniquePageviews != null)
@IsInt()
@Min(0)
@Column({ default: 0 })
pastYearTotalUniquePageviews: number

@Index()
@ValidateIf((a) => a.pastAllTimeTotalUniquePageviews != null)
@IsInt()
@Min(0)
@Column({ default: 0 })
pastAllTimeTotalUniquePageviews: number

@Column({ nullable: true })
pubDate?: Date

Expand Down Expand Up @@ -237,6 +190,9 @@ export class Episode {
@OneToMany((type) => UserQueueItem, (userQueueItem) => userQueueItem.episode)
userQueueItems: UserQueueItem[]

@OneToOne(() => StatsEpisode, (stats_episode) => stats_episode.episode)
stats_episode?: StatsEpisode

@CreateDateColumn()
createdAt: Date

Expand Down
18 changes: 0 additions & 18 deletions src/entities/episodes_most_recent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,6 @@ export class EpisodeMostRecent {
@ViewColumn()
mediaUrl: string

@ViewColumn()
pastHourTotalUniquePageviews: number

@ViewColumn()
pastDayTotalUniquePageviews: number

@ViewColumn()
pastWeekTotalUniquePageviews: number

@ViewColumn()
pastMonthTotalUniquePageviews: number

@ViewColumn()
pastYearTotalUniquePageviews: number

@ViewColumn()
pastAllTimeTotalUniquePageviews: number

@ViewColumn()
pubDate?: Date

Expand Down
3 changes: 3 additions & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export { Playlist } from './playlist'
export { Podcast } from './podcast'
export { RecentEpisodeByCategory } from './recentEpisodeByCategory'
export { RecentEpisodeByPodcast } from './recentEpisodeByPodcast'
export { StatsEpisode } from './statsEpisode'
export { StatsMediaRef } from './statsMediaRef'
export { StatsPodcast } from './statsPodcast'
export { UPDevice } from './upDevice'
export { User } from './user'
export { UserHistoryItem } from './userHistoryItem'
Expand Down
Loading

0 comments on commit b6acef3

Please sign in to comment.