-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
192 lines (180 loc) · 6.17 KB
/
server.ts
File metadata and controls
192 lines (180 loc) · 6.17 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { Session, BodyPacket, Rapport } from './types'
const http = require('http')
const osu = require('node-os-utils')
/**
* when DEBUG is true
* - host is changed to localhost:35200
* - SESSION_EXPIRY_DELAY is 30 sec instead of 360
* - System informations are not displayed
*/
const DEBUG: boolean = false
const GUI: boolean = false
const SESSION_EXPIRY_DELAY = DEBUG ? 10 : 360
const DATA_UPDATE_DELAY = 2000
const hostname = DEBUG ? 'localhost' : 'noxunote.fr';
const port = 35200;
let activeSessions: Session[] = []
let inactiveSessions: Session[] = []
// updates every DATA_UPDATE_DELAY
let lastRapport: Rapport
/***************************************************************************************************
* HTTP SERVER *
***************************************************************************************************/
function paquetRecu(bodyString: string) {
let body
try {
body = JSON.parse(bodyString) as BodyPacket
} catch(e) {
console.warn("Can't parse body to JSON object")
return
}
if (!body)
return
let session: Session = Session.findInList(activeSessions, body.session)
if (!session) {
// Création d'un nouvelle session
let s: Session = new Session(body.os, body.version, body.session, SESSION_EXPIRY_DELAY)
// Ajout à la liste
activeSessions.push(s)
s.expiryEvent.on('expired', () => {
// Suppression de la liste en cas d'expiration
activeSessions.splice(activeSessions.findIndex(sess => sess.sessionId == s.sessionId), 1)
// Ajout a la liste des sessions inactives
inactiveSessions.push(s)
})
} else {
session.update()
}
}
const server = http.createServer((req: any, res: any) => {
if (req.method == "POST") {
var body = '';
req.on('data', (data: any) => {
body += data;
})
req.on('end', () => {
paquetRecu(body)
})
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('post received');
} else if (req.method == "GET") {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(lastRapport));
} else {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<meta charset="utf-8">Erreur de session, cet incident à été enregistré.\n');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
/***************************************************************************************************
* GUI *
***************************************************************************************************/
// Gui vars
let userCount: any
let sysInfo: any
let screen: any
if (GUI) {
const blessed = require('blessed')
const contrib = require('blessed-contrib')
const screen = blessed.screen()
const grid = new contrib.grid({ rows: 1, cols: 2, screen: screen })
const userCount = grid.set(0, 0, 1, 1, contrib.bar,
{
label: 'Users',
barWidth: 4,
barSpacing: 6,
xOffset: 0,
maxHeight: 9,
}
)
const sysInfo = grid.set(0, 1, 1, 1, contrib.bar,
{
label: 'System information',
barWidth: 4,
barSpacing: 6,
xOffset: 0,
maxHeight: 100,
}
)
}
setInterval(() => {
generateRapport().then((rapport: Rapport) => {
if (GUI) {
userCount.setData({
titles: ['online', 'total'],
data: [rapport.stats.online, rapport.stats.total]
})
sysInfo.setData({
titles: ['CPU (%)', `Memory (%)`, 'Bandwitch'],
data: [rapport.system.cpuUsage, rapport.system.memoryUsage, rapport.system.BandwitchUsage]
})
screen.render()
}
})
}, DATA_UPDATE_DELAY)
/***************************************************************************************************
* DATA GENERATION *
***************************************************************************************************/
async function generateRapport(): Promise<Rapport> {
let allSessions: Session[] = [...activeSessions, ...inactiveSessions]
// Count users by version
let allVersions: Set<string> = getVersions(allSessions)
let infos = await Promise.all([
osu.cpu.usage(),
osu.mem.info(),
osu.netstat.inOut()
])
let now = new Date()
lastRapport = {
"stats": {
"online": activeSessions.length,
"total": allSessions.length,
"bySystemActive": {
"darwin": activeSessions.filter(s=>s.os=="darwin").length,
"win32": activeSessions.filter(s=>s.os=="win32").length,
"linux": activeSessions.filter(s=>s.os=="linux").length,
"other": activeSessions.filter(s=>!["darwin", "win32", "linux"].includes(s.os)).length
},
"bySystemAll": {
"darwin": allSessions.filter(s=>s.os=="darwin").length,
"win32": allSessions.filter(s=>s.os=="win32").length,
"linux": allSessions.filter(s=>s.os=="linux").length,
"other": allSessions.filter(s=>!["darwin", "win32", "linux"].includes(s.os)).length
},
"byVersionActive": Array.from(allVersions).map((v: string) => {
return {
version: v,
count: activeSessions.filter(s=>s.version==v).length
}
}),
"byVersionAll": Array.from(allVersions).map((v: string) => {
return {
version: v,
count: allSessions.filter(s=>s.version==v).length
}
}),
"byPeriod": {
"today": allSessions.filter(s=> !s.isExpired || (now.getDate()==s.beginDate.getDate())).length,
"last24Hours": allSessions.filter(s=> !s.isExpired || (now.getTime()-s.beginDate.getTime())<(3600*1000*24)).length,
"last48Hours": allSessions.filter(s=> !s.isExpired || (now.getTime()-s.beginDate.getTime())<(3600*1000*48)).length,
"lastWeek": allSessions.filter(s=> !s.isExpired || (now.getTime()-s.beginDate.getTime())<(3600*1000*24*7)).length,
"lastHour": allSessions.filter(s=> !s.isExpired || (now.getTime()-s.beginDate.getTime())<(3600*1000*1)).length
}
},
"system": {
"cpuUsage": infos[0],
"memoryUsage": 100-infos[1].freeMemPercentage,
"BandwitchUsage": !DEBUG ? infos[2].total.outputMb : 0
}
}
return lastRapport
}
function getVersions(sessions: Session[]): Set<string> {
let versions: Set<string> = new Set()
sessions.forEach(s => versions.add(s.version));
return versions
}