Skip to content

Commit

Permalink
feat: add simple analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdevv committed Nov 3, 2024
1 parent f0ca61d commit b67cde3
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
76 changes: 76 additions & 0 deletions pocketbase/migrations/1730606688_created_analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package migrations

import (
"encoding/json"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models"
)

func init() {
m.Register(func(db dbx.Builder) error {
jsonData := `{
"id": "3bdiiuiwkf83xrw",
"created": "2024-11-03 04:04:48.559Z",
"updated": "2024-11-03 04:04:48.559Z",
"name": "analytics",
"type": "base",
"system": false,
"schema": [
{
"system": false,
"id": "jttsat3l",
"name": "member_count",
"type": "number",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": null,
"noDecimal": true
}
},
{
"system": false,
"id": "xlkpne3r",
"name": "presence_count",
"type": "number",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": null,
"noDecimal": true
}
}
],
"indexes": [],
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"options": {}
}`

collection := &models.Collection{}
if err := json.Unmarshal([]byte(jsonData), &collection); err != nil {
return err
}

return daos.New(db).SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);

collection, err := dao.FindCollectionByNameOrId("3bdiiuiwkf83xrw")
if err != nil {
return err
}

return dao.DeleteCollection(collection)
})
}
6 changes: 6 additions & 0 deletions src/db/pocketbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ interface ThreadSolvesCollection extends BaseModel {
count: number;
}

interface AnalyticsCollection extends BaseModel {
member_count: number;
presence_count: number;
}

type LeaderboardView = Pick<ThreadSolvesCollection, 'id' | 'user_id' | 'count'>;

interface TypedPocketbase extends Pocketbase {
collection(idOrName: 'tags'): RecordService<TagsCollection>;
collection(idOrName: 'threadSolves'): RecordService<ThreadSolvesCollection>;
collection(idOrName: 'leaderboard'): RecordService<LeaderboardView>;
collection(idOrName: 'analytics'): RecordService<AnalyticsCollection>;
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dotenv/config';
import { DEV_MODE, TEST_GUILD_ID } from './config.js';
import { analyticsTask } from './scheduled/analytics';
import { DEV_MODE, TEST_GUILD_ID } from './config';
import { Scheduler } from './scheduled/_scheduler';
import { JellyCommands } from 'jellycommands';
import { IntentsBitField } from 'discord.js';

Expand Down Expand Up @@ -32,5 +34,7 @@ const client = new JellyCommands({
cache: DEV_MODE,
});

new Scheduler(client).addTask(analyticsTask);

// Auto reads the DISCORD_TOKEN environment variable
client.login();
30 changes: 30 additions & 0 deletions src/scheduled/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DEV_MODE, TEST_GUILD_ID } from '../config';
import { ScheduledTask } from './_scheduler';
import { pb } from '../db/pocketbase';

export const analyticsTask: ScheduledTask = {
interval: 60 * 60 * 6,
name: 'analytics',
async handle(client) {
try {
console.log('Saving Analytics');

const guild = await client.guilds.fetch({
guild: DEV_MODE ? TEST_GUILD_ID : '457912077277855764',
withCounts: true,
force: true,
});

if (!guild) {
throw new Error('Guild not found');
}

await pb.collection('analytics').create({
member_count: guild.approximateMemberCount,
presence_count: guild.approximatePresenceCount,
});
} catch (error) {
console.error('failed to save analytics', error);
}
},
};

0 comments on commit b67cde3

Please sign in to comment.