Skip to content

Commit

Permalink
Add posting of summary stats daily at midnight
Browse files Browse the repository at this point in the history
  • Loading branch information
mqp committed May 3, 2019
1 parent 3ef9284 commit c337a88
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ SHARD_ID=0
# The shard count for this client. (0 if you aren't using multiple shards.)
SHARD_COUNT=0

# The BCP 47 locale for bot output.
LOCALE=en-US

# The IANA time zone for bot output.
TIMEZONE=America/Los_Angeles

# True if verbose (e.g. per-message) logging should be enabled.
VERBOSE=false
2 changes: 2 additions & 0 deletions habitat/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ hubs_hosts = "localhost,hubs.local"
shard_id = "0"
shard_count = "0"
verbose = "false"
locale = "en-US"
timezone = "America/Los_Angeles"
2 changes: 2 additions & 0 deletions habitat/hooks/run
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export HUBS_HOSTS="{{ cfg.general.hubs_hosts }}"
export SHARD_ID="{{ cfg.general.shard_id }}"
export SHARD_COUNT="{{ cfg.general.shard_count }}"
export VERBOSE="{{ cfg.general.verbose }}"
export LOCALE="{{ cfg.general.locale }}"
export TIMEZONE="{{ cfg.general.timezone }}"

cd {{pkg.svc_path}}

Expand Down
54 changes: 51 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"discord.js": "github:discordjs/discord.js#11.4-dev",
"dotenv": "^6.2.0",
"escape-string-regexp": "^1.0.5",
"node-schedule": "^1.3.2",
"phoenix": "^1.4.2",
"ws": "^6.2.1"
},
Expand Down
29 changes: 29 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const MEDIA_DEDUPLICATE_MS = 60 * 60 * 1000; // 1 hour
const IMAGE_URL_RE = /\.(png)|(gif)|(jpg)|(jpeg)$/;

const discord = require('discord.js');
const schedule = require('node-schedule');
const { ChannelBindings, HubState } = require("./bindings.js");
const { ReticulumClient } = require("./reticulum.js");
const { TopicManager } = require("./topic.js");
Expand Down Expand Up @@ -217,6 +218,33 @@ function establishBridge(binding) {
});
}

function scheduleSummaryPosting(bindings, queue) {
// only enable on hubs discord and test server until we're sure we like this
const whitelistedGuilds = new Set(["525537221764317195", "498741086295031808"]);
const rule = new schedule.RecurrenceRule(null, null, null, null, 23, 59, 59);
return schedule.scheduleJob(rule, function(date) {
var start = date;
var end = new Date(start.getTime());
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
queue.enqueue(async () => {
for (const [hubId, { discordCh, hubState }] of Object.entries(bindings.bindingsByHub)) {
if (discordCh.guild && whitelistedGuilds.has(discordCh.guild.id)) {
const summary = hubState.stats.summarize(start.getTime(), end.getTime());
const when = start.toLocaleDateString(process.env.LOCALE, {
timeZone: process.env.TIMEZONE,
weekday: "long",
year: "numeric",
month: "numeric",
day: "numeric"
});
await discordCh.send(formatStats(summary, hubState.url, when));
}
}
});
});
}

async function start() {

const shardId = parseInt(process.env.SHARD_ID, 10);
Expand All @@ -233,6 +261,7 @@ async function start() {
const bindings = new ChannelBindings();
const topicManager = new TopicManager(HOSTNAMES);
const q = new DiscordEventQueue();
const summaryJob = scheduleSummaryPosting(bindings, q);

// one-time scan through all channels to look for existing bindings
console.info(ts(`Monitoring channels for Hubs hosts: ${HOSTNAMES.join(", ")}`));
Expand Down

0 comments on commit c337a88

Please sign in to comment.