summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAya Morisawa <AyaMorisawa4869@gmail.com>2018-08-14 07:49:59 +0900
committerAya Morisawa <AyaMorisawa4869@gmail.com>2018-08-14 07:49:59 +0900
commitf443d36dbb6e819626d3feac59d3afd570f0e52f (patch)
tree27d3a6ad4bbfdfa69fdc8eab072b4ddfe46df3a2 /src
parentMerge #2182 (diff)
downloadsharkey-f443d36dbb6e819626d3feac59d3afd570f0e52f.tar.gz
sharkey-f443d36dbb6e819626d3feac59d3afd570f0e52f.tar.bz2
sharkey-f443d36dbb6e819626d3feac59d3afd570f0e52f.zip
Resolve #2176
Diffstat (limited to 'src')
-rw-r--r--src/daemons/notes-stats.ts7
-rw-r--r--src/daemons/server-stats.ts7
-rw-r--r--src/misc/queue.ts33
3 files changed, 41 insertions, 6 deletions
diff --git a/src/daemons/notes-stats.ts b/src/daemons/notes-stats.ts
index 136ccb60c2..3bc0269dde 100644
--- a/src/daemons/notes-stats.ts
+++ b/src/daemons/notes-stats.ts
@@ -1,21 +1,22 @@
import * as childProcess from 'child_process';
import Xev from 'xev';
+import Queue from '../misc/queue';
const ev = new Xev();
export default function() {
- const log: any[] = [];
+ const log = new Queue<any>();
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();
+ if (log.length > 100) log.pop();
});
ev.on('requestNotesStatsLog', id => {
- ev.emit('notesStatsLog:' + id, log);
+ ev.emit('notesStatsLog:' + id, log.toArray());
});
process.on('exit', code => {
diff --git a/src/daemons/server-stats.ts b/src/daemons/server-stats.ts
index 0c0a72f747..b435c12e55 100644
--- a/src/daemons/server-stats.ts
+++ b/src/daemons/server-stats.ts
@@ -2,6 +2,7 @@ import * as os from 'os';
import * as sysUtils from 'systeminformation';
import * as diskusage from 'diskusage';
import Xev from 'xev';
+import Queue from '../misc/queue';
const osUtils = require('os-utils');
const ev = new Xev();
@@ -12,10 +13,10 @@ const interval = 1000;
* Report server stats regularly
*/
export default function() {
- const log: any[] = [];
+ const log = new Queue<any>();
ev.on('requestServerStatsLog', id => {
- ev.emit('serverStatsLog:' + id, log);
+ ev.emit('serverStatsLog:' + id, log.toArray());
});
async function tick() {
@@ -36,7 +37,7 @@ export default function() {
};
ev.emit('serverStats', stats);
log.push(stats);
- if (log.length > 50) log.shift();
+ if (log.length > 50) log.pop();
}
tick();
diff --git a/src/misc/queue.ts b/src/misc/queue.ts
new file mode 100644
index 0000000000..410878ba8b
--- /dev/null
+++ b/src/misc/queue.ts
@@ -0,0 +1,33 @@
+type Node<T> = { value: T, next: Node<T> };
+
+export default class Queue<T> {
+ private top: Node<T> = null;
+ private rear: Node<T> = null;
+ public length: number = 0;
+
+ public push(value: T): void {
+ const node: Node<T> = { value, next: null };
+ if (this.top === null) {
+ this.top = node;
+ this.rear = node;
+ } else {
+ this.rear.next = node;
+ this.rear = node;
+ }
+ this.length++;
+ }
+
+ public pop(): void {
+ this.top = this.top.next;
+ if (this.top == null) this.rear = null;
+ this.length--;
+ }
+
+ public toArray(): T[] {
+ const arr: T[] = Array<T>(this.length);
+ for (let node = this.top, i = 0; node !== null; node = node.next, i++) {
+ arr[i] = node.value;
+ }
+ return arr;
+ }
+}