-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (70 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import BaseModule from './structures/BaseModule.js'
import Constants, { UpdateInterval } from './util/Constants.js'
import Util from './util/Utils.js'
import Model, { createIfNotExists, increasePlayCount } from './structures/models/MetricModel.js'
export default class Metrics extends BaseModule {
/**
* @param {Main} main
*/
constructor(main) {
super(main);
this.register(Metrics, {
name: 'metrics',
requires: ['commandHandler'],
events: [
{
name: 'ready',
call: '_onReady'
},
{
mod: 'mongodb',
name: 'ready',
call: '_mongoReady'
}
]
});
}
get constants() {
return Constants;
}
get model() {
return Model;
}
get util() {
return Util;
}
/**
* @private
*/
_onReady() {
this.log.info('METRICS', 'Starting Metrics loop.');
this._m.on('trackPlayed', _ => {
increasePlayCount('track');
});
this._m.on('playlistPlayed', _ => {
increasePlayCount('playlist');
});
this._m.on('albumPlayed', _ => {
increasePlayCount('album');
});
this._updateLoop();
}
/**
* @private
*/
async _updateLoop() {
this.globalStorage.set('serverCount', await Util.getServerCount(this._m));
await Util.delay(UpdateInterval);
this._updateLoop();
}
/**
* @private
*/
async _mongoReady() {
if (await createIfNotExists().length < 1) {
this.log.error('METRICS', 'Could not create initial collection.');
return false;
}
return true;
}
}