summaryrefslogtreecommitdiff
path: root/src/daemons
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-06-11 06:48:25 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-06-11 06:48:25 +0900
commitdc3c80e3cef48bbbb1de2c5d8c6fb462b25de6e0 (patch)
tree74f6a2b922bb07c646705aebe7f8908771cf2ef7 /src/daemons
parentUpdate example.yml (diff)
downloadsharkey-dc3c80e3cef48bbbb1de2c5d8c6fb462b25de6e0.tar.gz
sharkey-dc3c80e3cef48bbbb1de2c5d8c6fb462b25de6e0.tar.bz2
sharkey-dc3c80e3cef48bbbb1de2c5d8c6fb462b25de6e0.zip
wip
Diffstat (limited to 'src/daemons')
-rw-r--r--src/daemons/hashtags-stats-child.ts60
-rw-r--r--src/daemons/hashtags-stats.ts20
-rw-r--r--src/daemons/notes-stats-child.ts26
-rw-r--r--src/daemons/notes-stats.ts20
-rw-r--r--src/daemons/server-stats.ts42
5 files changed, 168 insertions, 0 deletions
diff --git a/src/daemons/hashtags-stats-child.ts b/src/daemons/hashtags-stats-child.ts
new file mode 100644
index 0000000000..3f7f4d6e9e
--- /dev/null
+++ b/src/daemons/hashtags-stats-child.ts
@@ -0,0 +1,60 @@
+import Note from '../models/note';
+
+// 10分
+const interval = 1000 * 60 * 10;
+
+async function tick() {
+ const res = await Note.aggregate([{
+ $match: {
+ createdAt: {
+ $gt: new Date(Date.now() - interval)
+ },
+ tags: {
+ $exists: true,
+ $ne: []
+ }
+ }
+ }, {
+ $unwind: '$tags'
+ }, {
+ $group: {
+ _id: '$tags',
+ count: {
+ $sum: 1
+ }
+ }
+ }, {
+ $group: {
+ _id: null,
+ tags: {
+ $push: {
+ tag: '$_id',
+ count: '$count'
+ }
+ }
+ }
+ }, {
+ $project: {
+ _id: false,
+ tags: true
+ }
+ }]) as {
+ tags: Array<{
+ tag: string;
+ count: number;
+ }>
+ };
+
+ const stats = res.tags
+ .sort((a, b) => a.count - b.count)
+ .map(tag => [tag.tag, tag.count])
+ .slice(0, 10);
+
+ console.log(stats);
+
+ process.send(stats);
+}
+
+tick();
+
+setInterval(tick, interval);
diff --git a/src/daemons/hashtags-stats.ts b/src/daemons/hashtags-stats.ts
new file mode 100644
index 0000000000..5ed028ac33
--- /dev/null
+++ b/src/daemons/hashtags-stats.ts
@@ -0,0 +1,20 @@
+import * as childProcess from 'child_process';
+import Xev from 'xev';
+
+const ev = new Xev();
+
+export default function() {
+ const log = [];
+
+ const p = childProcess.fork(__dirname + '/hashtags-stats-child.js');
+
+ p.on('message', stats => {
+ ev.emit('hashtagsStats', stats);
+ log.push(stats);
+ if (log.length > 30) log.shift();
+ });
+
+ ev.on('requestHashTagsStatsLog', id => {
+ ev.emit('hashtagsStatsLog:' + id, log);
+ });
+}
diff --git a/src/daemons/notes-stats-child.ts b/src/daemons/notes-stats-child.ts
new file mode 100644
index 0000000000..7f54a36bff
--- /dev/null
+++ b/src/daemons/notes-stats-child.ts
@@ -0,0 +1,26 @@
+import Note from '../models/note';
+
+const interval = 5000;
+
+async function tick() {
+ const [all, local] = await Promise.all([Note.count({
+ createdAt: {
+ $gte: new Date(Date.now() - interval)
+ }
+ }), Note.count({
+ createdAt: {
+ $gte: new Date(Date.now() - interval)
+ },
+ '_user.host': null
+ })]);
+
+ const stats = {
+ all, local
+ };
+
+ process.send(stats);
+}
+
+tick();
+
+setInterval(tick, interval);
diff --git a/src/daemons/notes-stats.ts b/src/daemons/notes-stats.ts
new file mode 100644
index 0000000000..3094c34af0
--- /dev/null
+++ b/src/daemons/notes-stats.ts
@@ -0,0 +1,20 @@
+import * as childProcess from 'child_process';
+import Xev from 'xev';
+
+const ev = new Xev();
+
+export default function() {
+ const log = [];
+
+ const p = childProcess.fork(__dirname + '/notes-stats-child.js');
+
+ p.on('message', stats => {
+ ev.emit('notesStats', stats);
+ log.push(stats);
+ if (log.length > 100) log.shift();
+ });
+
+ ev.on('requestNotesStatsLog', id => {
+ ev.emit('notesStatsLog:' + id, log);
+ });
+}
diff --git a/src/daemons/server-stats.ts b/src/daemons/server-stats.ts
new file mode 100644
index 0000000000..1403402508
--- /dev/null
+++ b/src/daemons/server-stats.ts
@@ -0,0 +1,42 @@
+import * as os from 'os';
+const osUtils = require('os-utils');
+import * as diskusage from 'diskusage';
+import Xev from 'xev';
+
+const ev = new Xev();
+
+const interval = 1000;
+
+/**
+ * Report server stats regularly
+ */
+export default function() {
+ const log = [];
+
+ ev.on('requestServerStatsLog', id => {
+ ev.emit('serverStatsLog:' + id, log);
+ });
+
+ async function tick() {
+ osUtils.cpuUsage(cpuUsage => {
+ const disk = diskusage.checkSync(os.platform() == 'win32' ? 'c:' : '/');
+ const stats = {
+ cpu_usage: cpuUsage,
+ mem: {
+ total: os.totalmem(),
+ free: os.freemem()
+ },
+ disk,
+ os_uptime: os.uptime(),
+ process_uptime: process.uptime()
+ };
+ ev.emit('serverStats', stats);
+ log.push(stats);
+ if (log.length > 50) log.shift();
+ });
+ }
+
+ tick();
+
+ setInterval(tick, interval);
+}