-
Notifications
You must be signed in to change notification settings - Fork 57
/
StatsMetaverse.ts
executable file
·109 lines (97 loc) · 4.61 KB
/
StatsMetaverse.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2020 Vircadia Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict'
import { Config } from '@Base/config';
import os from 'os';
import { Accounts } from '@Entities/Accounts';
import { Monitoring } from '@Monitoring/Monitoring';
import { Stat } from '@Monitoring/Stat';
import { ValueStat } from '@Monitoring/ValueStat';
import { ValueHistogram } from '@Monitoring/ValueHistogram';
import { Logger } from '@Tools/Logging';
import { GenericFilter } from '@Entities/EntityFilters/GenericFilter';
import { Domain } from 'domain';
import { Domains } from '@Entities/Domains';
// All the OS statistics
export class StatsMetaverse extends Stat {
constructor() {
super('metaverse', 'metaverse', '');
// Count of all accounts that are sending heartbeats
const totalOnline = new ValueStat('totalOnline', 'metaverse', 'count', async (stat) => {
const numOnline = await Accounts.accountCount(
new GenericFilter( { 'timeOfLastHeartbeat': { '$gte': Accounts.dateWhenNotOnline }})
);
Logger.debug(`StatsMetaverse.totalOnline: counting ${numOnline}`);
stat.Event(numOnline);
});
if (Config.monitoring.history) {
totalOnline.AddHistogram('perDay', new ValueHistogram(24*12, 5*60*1000));// one day's worth of 5 minute buckets
totalOnline.AddHistogram('perWeek', new ValueHistogram(24*7, 60*60*1000));// one week per hour
};
Monitoring.addStat(totalOnline);
// Sum of all users reported by domains.
// NOTE: this also sets the value for the anon user stat to save the extra DB query
const domainTotalUsers = new ValueStat('domainTotalUsers', 'metaverse', 'count', async (stat) => {
let totalUsers = 0;
let anonUsers = 0;
for await (const aDomain of Domains.enumerateAsync(new GenericFilter(
{ '$or': [ { 'numUsers': { '$gt': 0 } }, { 'anonUsers': { '$gt': 0 } } ]
}) ) ) {
totalUsers += aDomain.numUsers + aDomain.anonUsers;
anonUsers += aDomain.anonUsers;
};
stat.Event(totalUsers);
Monitoring.getStat('domainAnonUsers')?.Event(anonUsers);
});
if (Config.monitoring.history) {
domainTotalUsers.AddHistogram('perDay', new ValueHistogram(24*12, 5*60*1000));// one day's worth of 5 minute buckets
domainTotalUsers.AddHistogram('perWeek', new ValueHistogram(24*7, 60*60*1000));// one week per hour
};
Monitoring.addStat(domainTotalUsers);
// Sum of all anon users reported by domains.
// NOTE: value is set by domainTotalUsers
const domainAnonUsers = new ValueStat('domainAnonUsers', 'metaverse', 'count');
if (Config.monitoring.history) {
domainAnonUsers.AddHistogram('perDay', new ValueHistogram(24*12, 5*60*1000));// one day's worth of 5 minute buckets
domainAnonUsers.AddHistogram('perWeek', new ValueHistogram(24*7, 60*60*1000));// one week per hour
};
Monitoring.addStat(domainAnonUsers);
// Number of domains that are sending heartbeats
const activeDomains = new ValueStat('activeDomains', 'metaverse', 'count', async (stat) => {
const numDomains = await Domains.domainCount(
new GenericFilter( { 'timeOfLastHeartbeat': { '$gte': Domains.dateWhenNotActive }})
);
stat.Event(numDomains);
});
if (Config.monitoring.history) {
activeDomains.AddHistogram('perDay', new ValueHistogram(24*12, 5*60*1000));// one day's worth of 5 minute buckets
activeDomains.AddHistogram('perWeek', new ValueHistogram(24*7, 60*60*1000));// one week per hour
};
Monitoring.addStat(activeDomains);
};
Event(pCount: number): void {
throw new Error('Method not implemented.');
}
async Gather(): Promise<void> {
return;
}
Report(pReturnHistogram: boolean = true): any {
return {
'totalOnline': Monitoring.getStat('totalOnline')?.Report(pReturnHistogram),
'domainTotalUsers': Monitoring.getStat('domainTotalUsers')?.Report(pReturnHistogram),
'domainAnonUsers': Monitoring.getStat('domainAnonUsers')?.Report(pReturnHistogram),
'activeDomains': Monitoring.getStat('activeDomains')?.Report(pReturnHistogram),
};
}
};