Skip to content

Commit

Permalink
Implement Guild Snapshots
Browse files Browse the repository at this point in the history
Includes a few accuracy changes
  • Loading branch information
IanMitchell committed Jan 22, 2019
1 parent 766a862 commit 4625a15
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/bot/commands/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async ({ aquarius, analytics }) => {

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

// TODO: Move this to a bulk op
const records = await aquarius.database.replies.find({
guildId: guild.id,
});
Expand Down
2 changes: 1 addition & 1 deletion src/global/commands/guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const info = {
};

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

Expand Down
4 changes: 2 additions & 2 deletions src/global/plugins/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const info = {
/** @type {import('../../typedefs').Command} */
export default async ({ aquarius, analytics }) => {
aquarius.on('guildCreate', async (guild) => {
log(`Joined Server ${guild.name} (${guild.memberCount} members)`);
log(`Joined Server ${guild.name} (${guild.members.size} members)`);
const channel = aquarius.channels.get(aquarius.config.home.channel);
const check = aquarius.permissions.check(channel.guild, ...info.permissions);

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

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

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import TriggerMap from './lib/settings/trigger-map';
import CommandConfig from './lib/settings/command-config';
import Settings from './lib/commands/settings';
import Analytics from './lib/commands/analytics';
import { setupWeeklyGuildLoop } from './lib/metrics/guilds';


const log = debug('Aquarius');
Expand Down Expand Up @@ -131,6 +132,7 @@ export class Aquarius extends Discord.Client {
*/
initialize() { // TODO: Make Private
this.guildManager.initialize();
setupWeeklyGuildLoop();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/lib/database/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const COLLECTION_NAMES = [
'replies',
'karma',
'quotes',
'guildSnapshots',
];

// TODO: Document
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/embeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function guildEmbed(guild, ...fields) {
`, true)
.addField('Members', dedent`
${activeMembers.size} Online
${guild.memberCount} Total
${guild.members.size} Total
`, true)
.setFooter(`Server ID: ${guild.id}`);

Expand Down
65 changes: 50 additions & 15 deletions src/lib/metrics/guilds.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import debug from 'debug';
import aquarius from '../..';
import { ONE_WEEK } from '../helpers/times';
import { ONE_WEEK, ONE_HOUR } from '../helpers/times';
import { startOfWeek } from 'date-fns/esm';

const log = debug('GuildMetrics');
const log = debug('Guild Metrics');

// TODO: Implement
export function saveSnapshot(guild) {
console.log(guild);
}
// TODO: Implement
export function saveSnapshots() {
export async function saveSnapshots() {
log('Saving snapshots');
aquarius.guilds.forEach(guild => saveSnapshot(guild));

const bulk = aquarius.database.guildSnapshots.initializeOrderedBulkOp();

aquarius.guilds.forEach(guild => {
bulk.insert({
channels: guild.channels.size,
users: guild.members.size,
bots: guild.members.filter(member => member.user.bot).size,
date: Date.now(),
guildId: guild.id,
name: guild.name,
icon: guild.iconURL,
ownerId: guild.ownerID,
vip: !!guild.splash,
verified: guild.verified,
admin: aquarius.permissions.isGuildAdmin(guild, guild.me),
});
});

return bulk.execute();
}

// TODO: Implement
Expand All @@ -25,7 +40,8 @@ export async function getGuildMetrics() {
const snapshot = await aquarius.database.guildSnapshots
.findAsCursor({ guildId: guild.id })
.sort({ date: -1 })
.limit(5);
.limit(5)
.toArray();

return {
name: guild.name,
Expand All @@ -46,14 +62,33 @@ export async function getGuildMetrics() {
export async function setupWeeklyGuildLoop() {
log('Registering Metric Tracking');

// TODO: Load from settings the last weekly snapshot
const lastSnapshot = null;
const target = lastSnapshot.timestamp + ONE_WEEK - Date.now();
// Retrieve the last time we updated
const [lastSnapshot] = await aquarius.database.guildSnapshots.findAsCursor()
.sort({ date: -1 })
.limit(1)
.toArray();

let target = 0;

if (lastSnapshot) {
// We want to target 1:00 on Sunday of the next week
target = startOfWeek(new Date(lastSnapshot.date + ONE_WEEK)).getTime() + ONE_HOUR;
}

// If we missed it, save immediately and push to next week
if (target <= Date.now()) {
await saveSnapshots();
target = startOfWeek(new Date()).getTime() + ONE_WEEK + ONE_HOUR;
}

// Create a loop that looks to further Sundays!
setTimeout(() => {
saveSnapshots();
setInterval(saveSnapshots, ONE_WEEK);
}, target);
setInterval(
saveSnapshots,
(ONE_WEEK + ONE_HOUR) - (Date.now() - startOfWeek(new Date()).getTime())
);
}, target - Date.now());
}

export function getTotalGuildCount() {
Expand Down
29 changes: 10 additions & 19 deletions src/lib/metrics/messages.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// TODO: Implement
export function getSentMessages() {
return 0;
}

// TODO: Implement
export function getReadMessages() {
return 0;
}

// TODO: Listen for new messages, increase counter


export function getWeeklyActivations() {

}

export function saveWeeklyActivationSnapshot() {

export function getWeeklyUsage(weeksAgo) {
const startTarget = getDateAgo(ONE_WEEK * weeksAgo);
const endTarget = getDateAgo(ONE_WEEK * (weeksAgo - 1));

return aquarius.database.analytics.count({
date: {
$gte: startTarget,
$lte: endTarget,
},
});
}
6 changes: 3 additions & 3 deletions src/lib/metrics/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ONE_WEEK, getDateAgo } from '../helpers/times';
export function getTotalUserCount() {
return aquarius.guilds
.array()
.reduce((val, guild) => val + guild.memberCount, 0);
.reduce((val, guild) => val + guild.members.size, 0);
}

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

// TODO: Make this real
const records = await aquarius.database.guildSnapshots.find({
date: {
$between: [startTarget, endTarget],
$gte: startTarget,
$lte: endTarget,
},
});

Expand Down

0 comments on commit 4625a15

Please sign in to comment.