Skip to content

Commit 6db12aa

Browse files
committed
i forgot what my code does
1 parent 540625f commit 6db12aa

File tree

7 files changed

+50
-20
lines changed

7 files changed

+50
-20
lines changed

new.dbml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ Table twitch {
6060
}
6161

6262
Table bot_info {
63+
time datetime [not null, default: `now()`]
6364
guilds_total int [not null, default: 0]
6465
channels_tracked int [not null, default: 0]
6566
total_members int [not null, default: 0]
66-
updated_at datetime [not null, default: `now()`]
67-
extended_info_updated_at datetime [not null, default: `now()`]
6867
}
6968

7069
Table bot_info_notifications {

src/utils/db/botinfo.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import type { PoolClient, QueryResult } from "pg";
2-
import { pool } from "../database";
32
import type { dbBotInfo } from "../../types/database";
43

4+
import { pool } from "../database";
5+
56
export async function updateBotInfo(
67
guilds_total: number = 0,
78
channels_tracked: number = 0,
89
total_members: number = 0,
910
): Promise<void> {
1011
const query = `
11-
UPDATE bot_info
12-
SET guilds_total = $1, channels_tracked = $2, total_members = $3, updated_at = NOW()
13-
WHERE locked_row = true;
12+
INSERT INTO bot_info (guilds_total, channels_tracked, total_members, time)
13+
VALUES ($1, $2, $3, NOW())
14+
ON CONFLICT (time) DO UPDATE
15+
SET guilds_total = EXCLUDED.guilds_total,
16+
channels_tracked = EXCLUDED.channels_tracked,
17+
total_members = EXCLUDED.total_members;
1418
`;
1519

1620
try {

src/utils/db/cron.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { pool } from "../database";
2+
3+
export async function cronUpdateTopChannels(): Promise<void> {
4+
const query = `
5+
TRUNCATE TABLE bot_info_top_channels;
6+
7+
INSERT INTO bot_info_top_channels (youtube_channel_id, guilds_tracking)
8+
SELECT youtube_channel_id, COUNT(*) AS guilds_tracking
9+
FROM guild_youtube_subscriptions
10+
GROUP BY youtube_channel_id
11+
ORDER BY guilds_tracking DESC;
12+
`;
13+
14+
try {
15+
const client = await pool.connect();
16+
17+
await client.query(query);
18+
client.release();
19+
} catch (error) {
20+
console.error("Error updating top channels:", error);
21+
}
22+
}

src/utils/db/init.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ export default async function initTables(): Promise<boolean> {
7777

7878
const createBotInfoTable = `
7979
CREATE TABLE IF NOT EXISTS bot_info (
80-
locked_row BOOLEAN NOT NULL DEFAULT TRUE,
8180
guilds_total INTEGER NOT NULL DEFAULT 0,
8281
channels_tracked INTEGER NOT NULL DEFAULT 0,
8382
total_members INTEGER NOT NULL DEFAULT 0,
84-
updated_at TIMESTAMP NOT NULL DEFAULT now(),
85-
extended_info_updated_at TIMESTAMP NOT NULL DEFAULT now()
83+
time TIMESTAMP NOT NULL DEFAULT now()
8684
);
8785
`;
8886

@@ -128,7 +126,7 @@ export default async function initTables(): Promise<boolean> {
128126
`;
129127

130128
const seedBotInfoTable = `
131-
INSERT INTO bot_info (locked_row, guilds_total, channels_tracked, total_members) VALUES (true, 0, 0, 0)
129+
INSERT INTO bot_info (time, guilds_total, channels_tracked, total_members) VALUES (now(), 0, 0, 0)
132130
`;
133131

134132
try {

src/utils/db/youtube.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { dbYouTube } from "../../types/database";
33
import { pool } from "../database";
44
import getSinglePlaylistAndReturnVideoId, {
55
PlaylistType,
6-
} from "../youtube/getSinglePlaylistAndReturnVideoId";
6+
} from "../youtube/getSinglePlaylistAndReturnVideoData";
77

88
export async function dbYouTubeGetAllChannelsToTrack(): Promise<
99
dbYouTube[] | []
@@ -38,7 +38,8 @@ export async function checkIfChannelIsAlreadyTracked(
3838
} catch (err) {
3939
console.error("Error checking if channel is already tracked:", err);
4040

41-
return false;
41+
// Return true if there's an error as we don't want to add the channel if we can't check it
42+
return true;
4243
}
4344
}
4445

@@ -61,16 +62,19 @@ export async function addNewChannelToTrack(
6162
PlaylistType.Stream,
6263
);
6364

64-
const query = `INSERT INTO youtube (youtube_channel_id, latest_video_id, latest_short_id, latest_stream_id) VALUES (?, ?, ?, ?)`;
65+
const query = `INSERT INTO youtube (youtube_channel_id, latest_video_id_updated, latest_video_id, latest_short_id, latest_short_id_updated, latest_stream_id, latest_stream_id_updated) VALUES (?, ?, ?, ?, ?, ?, ?)`;
6566

6667
try {
6768
const client = await pool.connect();
6869

6970
await client.query(query, [
7071
channelId,
71-
longId || null,
72-
shortId || null,
73-
liveId || null,
72+
longId?.videoId || null,
73+
longId?.datePublished || null,
74+
shortId?.videoId || null,
75+
shortId?.datePublished || null,
76+
liveId?.videoId || null,
77+
liveId?.datePublished || null,
7478
]);
7579

7680
return true;

src/utils/migratedb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ if (botInfoData.length > 0) {
122122

123123
await client.query(
124124
`
125-
INSERT INTO bot_info (guilds_total, channels_tracked, total_members, updated_at, extended_info_updated_at)
126-
VALUES ($1, 0, $2, now(), now())
125+
INSERT INTO bot_info (guilds_total, channels_tracked, total_members, time)
126+
VALUES ($1, 0, $2, now())
127127
`,
128128
[row.total_servers, row.total_members],
129129
);

src/utils/youtube/getSinglePlaylistAndReturnVideoId.ts renamed to src/utils/youtube/getSinglePlaylistAndReturnVideoData.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const playlistIdPrefixes: Record<PlaylistType, string> = {
1717
export default async function (
1818
channelId: string,
1919
playlistType?: PlaylistType,
20-
): Promise<string | null> {
20+
): Promise<{ videoId: string; datePublished: Date } | null> {
2121
const playlistIdPrefix = !playlistType
2222
? "UU"
2323
: playlistIdPrefixes[playlistType];
@@ -43,5 +43,8 @@ export default async function (
4343
}
4444

4545
// Yes this does actually return the video ID, you'll be surprised how weird YouTube's API is
46-
return atob(json.items[0].id).split(".")[1];
46+
return {
47+
videoId: atob(json.items[0].id).split(".")[1],
48+
datePublished: new Date(json.items[0].snippet.publishedAt),
49+
};
4750
}

0 commit comments

Comments
 (0)