Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"dev": "BUN_CONFIG_NO_CLEAR_TERMINAL_ON_RELOAD=1 bun with-env bun --watch src/index.ts",
"start": "bun with-env bun src/index.ts",
"typecheck": "tsc --noEmit",
"db:push": "bun with-env drizzle-kit push",
"db:push": "bun with-env drizzle-kit push && bun with-env bun src/utils/db/postpush.ts",
"db:studio": "bun with-env drizzle-kit studio",
"with-env": "dotenv -e ../.env --"
},
"dependencies": {
"@hono/zod-openapi": "^0.15.1",
"@hono/zod-validator": "^0.2.2",
"cron": "^3.1.7",
"drizzle-orm": "^0.32.1",
"hono": "^4.5.11",
"postgres": "^3.4.4",
Expand Down
24 changes: 24 additions & 0 deletions api/src/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CronJob } from "cron";
import { db } from "./utils/db";
import { analytics } from "./utils/db/schema";

export async function createAnalyticsCronJob() {
new CronJob(
"* * * * *",
async () => {
const currentDate = new Date();
const channels = await db.query.channels.findMany();
await db.insert(analytics).values(
channels.map((channel) => ({
timestamp: currentDate,
guildId: channel.guildId,
channelId: channel.id,
count: channel.count,
}))
);
},
null,
true,
"Africa/Abidjan"
);
}
3 changes: 3 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import { guildsRouter } from "./router/guilds";
import { channelsRouter } from "./router/channels";
import { createAnalyticsCronJob } from "./analytics";
import { cors } from "hono/cors";

createAnalyticsCronJob();

const app = new OpenAPIHono();

app.use(
Expand Down
17 changes: 17 additions & 0 deletions api/src/utils/db/postpush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { sql } from "drizzle-orm";
import { db } from ".";

async function main() {
try {
await db.execute(
sql`SELECT create_hypertable('analytics', by_range('timestamp'))`
);
} catch (err) {
console.log(
"An error occured while creating hypertable. Hypertable probably already exists."
);
}
process.exit(0);
}

main();
20 changes: 19 additions & 1 deletion api/src/utils/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { relations } from "drizzle-orm";
import { boolean, integer, pgEnum, pgTable, text } from "drizzle-orm/pg-core";
import {
boolean,
integer,
pgEnum,
pgTable,
text,
timestamp,
} from "drizzle-orm/pg-core";

export const guilds = pgTable("guilds", {
id: text("id").primaryKey(),
Expand Down Expand Up @@ -35,3 +42,14 @@ export const channelRelations = relations(channels, ({ one }) => ({
references: [guilds.id],
}),
}));

export const analytics = pgTable("analytics", {
timestamp: timestamp("timestamp", { withTimezone: true }).primaryKey(),
guildId: text("guild_id")
.notNull()
.references(() => guilds.id),
channelId: text("channel_id")
.notNull()
.references(() => channels.id),
count: integer("count").notNull().default(0),
});
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
db:
image: postgres:16
image: timescale/timescaledb-ha:pg16
restart: always
environment:
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
Expand Down