Skip to content

Commit

Permalink
feat: Implement download stats
Browse files Browse the repository at this point in the history
  • Loading branch information
otomir23 committed Jun 15, 2024
1 parent c6c3e0e commit d3984a6
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 3 deletions.
5 changes: 4 additions & 1 deletion locales/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ setting-attribution-0 = nah
setting-attribution-1 = sure
setting-lang = language
setting-lang-unset = same as telegram
setting-lang-unset = same as telegram
stats-personal = i helped you with downloading { $count } times! (˶ᵔ ᵕ ᵔ˶)
stats-global = i helped with downloading { $count } times! (˶ᵔ ᵕ ᵔ˶)
5 changes: 4 additions & 1 deletion locales/ru.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ setting-attribution-0 = не
setting-attribution-1 = давай
setting-lang = язык
setting-lang-unset = как в тг
setting-lang-unset = как в тг
stats-personal = я помог тебе с загрузкой { $count } раз! (˶ᵔ ᵕ ᵔ˶)
stats-global = я помог с загрузкой { $count } раз! (˶ᵔ ᵕ ᵔ˶)
1 change: 1 addition & 0 deletions migrations/0002_dry_screwball.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD `downloads` integer;
97 changes: 97 additions & 0 deletions migrations/meta/0002_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"version": "5",
"dialect": "sqlite",
"id": "7cf9cdab-b059-4b49-b8ff-c3b620bc0151",
"prevId": "e6eab90c-c703-4ac7-907f-2ad96aedb224",
"tables": {
"requests": {
"name": "requests",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"author_id": {
"name": "author_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"output": {
"name": "output",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"attribution": {
"name": "attribution",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"language": {
"name": "language",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"downloads": {
"name": "downloads",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"users_id_unique": {
"name": "users_id_unique",
"columns": [
"id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
7 changes: 7 additions & 0 deletions migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"when": 1708695593228,
"tag": "0001_giant_vin_gonzales",
"breakpoints": true
},
{
"idx": 2,
"version": "5",
"when": 1718465870677,
"tag": "0002_dry_screwball",
"breakpoints": true
}
]
}
1 change: 1 addition & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const users = sqliteTable("users", {
preferredOutput: text("output"),
preferredAttribution: int("attribution").notNull().default(0),
languageOverride: text("language"),
downloadCount: int("downloads"),
})
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
settingsMiddleware,
updateSetting,
} from "#settings"
import { getDownloadStats, incrementDownloadCount } from "#stats"

type CoboldContext = Context & I18nFlavor & TextFlavor & SettingsFlavor
const bot = new Bot<CoboldContext>(env.BOT_TOKEN)
Expand Down Expand Up @@ -53,6 +54,18 @@ const settingsReplyMarkup = (ctx: CoboldContext, settingsOverride?: Settings): I
])),
})

bot.command("stats", async (ctx) => {
const count = await getDownloadStats()
await ctx.reply(ctx.t("stats-global", { count }))
})

bot.command("mystats", async (ctx) => {
const id = ctx.from?.id
if (!id) return
const count = await getDownloadStats(id)
await ctx.reply(ctx.t("stats-personal", { count }))
})

bot.command("settings", async (ctx) => {
await ctx.reply(ctx.t("settings-title"), {
reply_markup: settingsReplyMarkup(ctx),
Expand Down Expand Up @@ -145,6 +158,9 @@ const onOutputSelected = async (
caption: ctx.t("error", { message: ctx.evaluateText(result.error) }),
})

const fromId = ctx.from?.id
if (fromId) incrementDownloadCount(fromId).catch(() => { /* noop */ })

await ctx.editMessageCaption({
caption: ctx.t("uploading-title"),
})
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { literal, translatable } from "#text"
import { I18nFlavor } from "@grammyjs/i18n"

export const settingCallbackPrefix = "setting"
export type Settings = Omit<InferSelectModel<typeof users>, "id">
export type Settings = Omit<InferSelectModel<typeof users>, "id" | "downloadCount">
export type SettingsFlavor = {
userSettings: Settings,
}
Expand Down
18 changes: 18 additions & 0 deletions src/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { db } from "#db/database"
import { users } from "#db/schema"
import { eq, sql, sum } from "drizzle-orm"

export async function incrementDownloadCount(user: number) {
await db.insert(users)
.values({ id: user, downloadCount: 1 })
.onConflictDoUpdate({ target: users.id, set: { downloadCount: sql`${users.downloadCount} + 1` } })
.returning()
}

export async function getDownloadStats(user?: number) {
const res = await db
.select({ downloadCount: sum(users.downloadCount) })
.from(users)
.where(user !== undefined ? eq(users.id, user) : undefined)
return res.reduce((p, c) => p + +(c.downloadCount || ""), 0)
}

0 comments on commit d3984a6

Please sign in to comment.