Skip to content

Commit 4625a15

Browse files
committed
Implement Guild Snapshots
Includes a few accuracy changes
1 parent 766a862 commit 4625a15

File tree

9 files changed

+71
-41
lines changed

9 files changed

+71
-41
lines changed

src/bot/commands/reply.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default async ({ aquarius, analytics }) => {
3636

3737
RESPONSES.set(guild.id, new Map());
3838

39+
// TODO: Move this to a bulk op
3940
const records = await aquarius.database.replies.find({
4041
guildId: guild.id,
4142
});

src/global/commands/guild.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const info = {
1616
};
1717

1818
function formatGuild(guild, idx) {
19-
const members = `${guild.memberCount} ${pluralize('Member', guild.memberCount)}`;
19+
const members = `${guild.members.size} ${pluralize('Member', guild.members.size)}`;
2020
return `${idx + 1}. ${guild.name} -- *(${members})*\n`;
2121
}
2222

src/global/plugins/notification.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const info = {
1616
/** @type {import('../../typedefs').Command} */
1717
export default async ({ aquarius, analytics }) => {
1818
aquarius.on('guildCreate', async (guild) => {
19-
log(`Joined Server ${guild.name} (${guild.memberCount} members)`);
19+
log(`Joined Server ${guild.name} (${guild.members.size} members)`);
2020
const channel = aquarius.channels.get(aquarius.config.home.channel);
2121
const check = aquarius.permissions.check(channel.guild, ...info.permissions);
2222

@@ -34,7 +34,7 @@ export default async ({ aquarius, analytics }) => {
3434
});
3535

3636
aquarius.on('guildDelete', async (guild) => {
37-
log(`Left Server ${guild.name} (${guild.memberCount} members)`);
37+
log(`Left Server ${guild.name} (${guild.members.size} members)`);
3838
const channel = aquarius.channels.get(aquarius.config.home.channel);
3939
const check = aquarius.permissions.check(channel.guild, ...info.permissions);
4040

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import TriggerMap from './lib/settings/trigger-map';
1717
import CommandConfig from './lib/settings/command-config';
1818
import Settings from './lib/commands/settings';
1919
import Analytics from './lib/commands/analytics';
20+
import { setupWeeklyGuildLoop } from './lib/metrics/guilds';
2021

2122

2223
const log = debug('Aquarius');
@@ -131,6 +132,7 @@ export class Aquarius extends Discord.Client {
131132
*/
132133
initialize() { // TODO: Make Private
133134
this.guildManager.initialize();
135+
setupWeeklyGuildLoop();
134136
}
135137

136138
/**

src/lib/database/setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const COLLECTION_NAMES = [
1212
'replies',
1313
'karma',
1414
'quotes',
15+
'guildSnapshots',
1516
];
1617

1718
// TODO: Document

src/lib/helpers/embeds.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function guildEmbed(guild, ...fields) {
5454
`, true)
5555
.addField('Members', dedent`
5656
${activeMembers.size} Online
57-
${guild.memberCount} Total
57+
${guild.members.size} Total
5858
`, true)
5959
.setFooter(`Server ID: ${guild.id}`);
6060

src/lib/metrics/guilds.js

+50-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import debug from 'debug';
22
import aquarius from '../..';
3-
import { ONE_WEEK } from '../helpers/times';
3+
import { ONE_WEEK, ONE_HOUR } from '../helpers/times';
4+
import { startOfWeek } from 'date-fns/esm';
45

5-
const log = debug('GuildMetrics');
6+
const log = debug('Guild Metrics');
67

7-
// TODO: Implement
8-
export function saveSnapshot(guild) {
9-
console.log(guild);
10-
}
11-
// TODO: Implement
12-
export function saveSnapshots() {
8+
export async function saveSnapshots() {
139
log('Saving snapshots');
14-
aquarius.guilds.forEach(guild => saveSnapshot(guild));
10+
11+
const bulk = aquarius.database.guildSnapshots.initializeOrderedBulkOp();
12+
13+
aquarius.guilds.forEach(guild => {
14+
bulk.insert({
15+
channels: guild.channels.size,
16+
users: guild.members.size,
17+
bots: guild.members.filter(member => member.user.bot).size,
18+
date: Date.now(),
19+
guildId: guild.id,
20+
name: guild.name,
21+
icon: guild.iconURL,
22+
ownerId: guild.ownerID,
23+
vip: !!guild.splash,
24+
verified: guild.verified,
25+
admin: aquarius.permissions.isGuildAdmin(guild, guild.me),
26+
});
27+
});
28+
29+
return bulk.execute();
1530
}
1631

1732
// TODO: Implement
@@ -25,7 +40,8 @@ export async function getGuildMetrics() {
2540
const snapshot = await aquarius.database.guildSnapshots
2641
.findAsCursor({ guildId: guild.id })
2742
.sort({ date: -1 })
28-
.limit(5);
43+
.limit(5)
44+
.toArray();
2945

3046
return {
3147
name: guild.name,
@@ -46,14 +62,33 @@ export async function getGuildMetrics() {
4662
export async function setupWeeklyGuildLoop() {
4763
log('Registering Metric Tracking');
4864

49-
// TODO: Load from settings the last weekly snapshot
50-
const lastSnapshot = null;
51-
const target = lastSnapshot.timestamp + ONE_WEEK - Date.now();
65+
// Retrieve the last time we updated
66+
const [lastSnapshot] = await aquarius.database.guildSnapshots.findAsCursor()
67+
.sort({ date: -1 })
68+
.limit(1)
69+
.toArray();
70+
71+
let target = 0;
72+
73+
if (lastSnapshot) {
74+
// We want to target 1:00 on Sunday of the next week
75+
target = startOfWeek(new Date(lastSnapshot.date + ONE_WEEK)).getTime() + ONE_HOUR;
76+
}
77+
78+
// If we missed it, save immediately and push to next week
79+
if (target <= Date.now()) {
80+
await saveSnapshots();
81+
target = startOfWeek(new Date()).getTime() + ONE_WEEK + ONE_HOUR;
82+
}
5283

84+
// Create a loop that looks to further Sundays!
5385
setTimeout(() => {
5486
saveSnapshots();
55-
setInterval(saveSnapshots, ONE_WEEK);
56-
}, target);
87+
setInterval(
88+
saveSnapshots,
89+
(ONE_WEEK + ONE_HOUR) - (Date.now() - startOfWeek(new Date()).getTime())
90+
);
91+
}, target - Date.now());
5792
}
5893

5994
export function getTotalGuildCount() {

src/lib/metrics/messages.js

+10-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
// TODO: Implement
2-
export function getSentMessages() {
3-
return 0;
4-
}
5-
6-
// TODO: Implement
7-
export function getReadMessages() {
8-
return 0;
9-
}
10-
11-
// TODO: Listen for new messages, increase counter
12-
13-
14-
export function getWeeklyActivations() {
15-
16-
}
17-
18-
export function saveWeeklyActivationSnapshot() {
19-
1+
export function getWeeklyUsage(weeksAgo) {
2+
const startTarget = getDateAgo(ONE_WEEK * weeksAgo);
3+
const endTarget = getDateAgo(ONE_WEEK * (weeksAgo - 1));
4+
5+
return aquarius.database.analytics.count({
6+
date: {
7+
$gte: startTarget,
8+
$lte: endTarget,
9+
},
10+
});
2011
}

src/lib/metrics/users.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ONE_WEEK, getDateAgo } from '../helpers/times';
55
export function getTotalUserCount() {
66
return aquarius.guilds
77
.array()
8-
.reduce((val, guild) => val + guild.memberCount, 0);
8+
.reduce((val, guild) => val + guild.members.size, 0);
99
}
1010

1111
// TODO: Document
@@ -18,10 +18,10 @@ export async function getWeeklyUserCount(weeksAgo) {
1818
const startTarget = getDateAgo(ONE_WEEK * weeksAgo);
1919
const endTarget = getDateAgo(ONE_WEEK * (weeksAgo - 1));
2020

21-
// TODO: Make this real
2221
const records = await aquarius.database.guildSnapshots.find({
2322
date: {
24-
$between: [startTarget, endTarget],
23+
$gte: startTarget,
24+
$lte: endTarget,
2525
},
2626
});
2727

0 commit comments

Comments
 (0)