Skip to content

Commit 462075d

Browse files
committed
Load CPU and memory usage
1 parent 91329be commit 462075d

File tree

11 files changed

+207
-24
lines changed

11 files changed

+207
-24
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/public/**

examples/test-status-monitor/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { AppModule } from './app.module';
33

44
async function bootstrap() {
55
const app = await NestFactory.create(AppModule);
6-
await app.listen(3000);
6+
await app.listen(3001);
77
}
88
bootstrap();

package-lock.json

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,22 @@
2727
"typescript": "^3.0.3"
2828
},
2929
"dependencies": {
30+
"@nestjs/websockets": "^5.4.0",
3031
"debug": "^2.6.8",
3132
"handlebars": "~4.0.12",
3233
"on-headers": "^1.0.1",
3334
"pidusage": "^1.1.6",
35+
"reflect-metadata": "^0.1.12",
3436
"request-promise-native": "^1.0.5",
35-
"socket.io": "^2.0.3",
36-
"reflect-metadata": "^0.1.12"
37+
"socket.io": "^2.0.3"
3738
},
3839
"scripts": {
3940
"test": "jest",
4041
"coverage": "jest --coverage",
41-
"coveralls": "yarn run coverage --coverageReporters=text-lcov | coveralls",
42+
"coveralls": "npm run coverage --coverageReporters=text-lcov | coveralls",
4243
"test:watch": "jest --watch",
4344
"build": "rm -rf ./dist && tsc",
44-
"format": "prettier **/**/*.ts --ignore-path ./.prettierignore --write && git status",
45+
"format": "prettier src/**/*.ts --ignore-path ./.prettierignore --write && git status",
4546
"prepublish": "npm run format && npm run build"
4647
},
4748
"jest": {

src/default.config.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const configuration = {
2+
title: 'Express Status',
3+
theme: 'default.css',
4+
path: '/status',
5+
spans: [
6+
{
7+
interval: 1,
8+
retention: 60,
9+
},
10+
{
11+
interval: 5,
12+
retention: 60,
13+
},
14+
{
15+
interval: 15,
16+
retention: 60,
17+
},
18+
],
19+
port: null,
20+
websocket: null,
21+
iframe: false,
22+
chartVisibility: {
23+
cpu: true,
24+
mem: true,
25+
load: true,
26+
responseTime: true,
27+
rps: true,
28+
statusCodes: true,
29+
},
30+
ignoreStartsWith: '/admin',
31+
healthChecks: [],
32+
};
33+
34+
export default configuration;

src/index.ts

Whitespace-only changes.

src/public/index.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.bundle.min.js"></script>
77
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"></script>
88
<style>
9-
{
10-
{
11-
{
12-
style
13-
}
14-
}
15-
}
9+
{{{style}}}
1610
</style>
1711
</head>
1812

@@ -98,7 +92,7 @@ <h1>{{status}}</h1>
9892
</div>
9993
<script>
10094
var port = '{{port}}';
101-
{ { { script } } }
95+
{{{script}}}
10296
</script>
10397
</body>
10498

src/status.monitor.controller.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@ const controllerPath = 'monitor';
77

88
@Controller(controllerPath)
99
export class StatusMonitorController {
10-
@Get()
11-
@HttpCode(200)
12-
root() {
13-
const data = {
10+
data;
11+
render;
12+
13+
constructor() {
14+
this.data = {
15+
title: 'Nest.js Status',
16+
port: 3001,
17+
bodyClasses: '',
1418
script: fs.readFileSync(
15-
path.join(__dirname, '/public/javascripts/app.js'),
19+
path.join(__dirname, '../src/public/javascripts/app.js'),
1620
),
1721
style: fs.readFileSync(
18-
path.join(__dirname, '/public/stylesheets/style.css'),
22+
path.join(__dirname, '../src/public/stylesheets/style.css'),
1923
),
2024
};
2125

2226
const htmlTmpl = fs
23-
.readFileSync(path.join(__dirname, '/public/index.html'))
27+
.readFileSync(path.join(__dirname, '../src/public/index.html'))
2428
.toString();
2529

26-
const render = Handlebars.compile(htmlTmpl);
27-
return render(data);
30+
this.render = Handlebars.compile(htmlTmpl, { strict: true });
31+
}
32+
33+
@Get()
34+
@HttpCode(200)
35+
root() {
36+
return this.render(this.data);
2837
}
2938
}

src/status.monitor.gateway.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
SubscribeMessage,
3+
WebSocketGateway,
4+
WebSocketServer,
5+
WsResponse,
6+
OnGatewayConnection,
7+
} from '@nestjs/websockets';
8+
import { StatusMonitoringService } from './status.monitoring.service';
9+
import { Inject, forwardRef } from '@nestjs/common';
10+
11+
@WebSocketGateway()
12+
export class StatusMonitorGateway implements OnGatewayConnection {
13+
@WebSocketServer()
14+
server;
15+
16+
constructor(
17+
@Inject(forwardRef(() => StatusMonitoringService))
18+
private readonly statusMonitoringService: StatusMonitoringService,
19+
) {}
20+
21+
@SubscribeMessage('esm_change')
22+
onEvent(client, data: any) {
23+
const event = 'esm_start';
24+
const spans = this.statusMonitoringService.getData();
25+
return { event, spans };
26+
}
27+
28+
handleConnection(client) {
29+
const spans = this.statusMonitoringService.getData();
30+
client.emit('esm_start', spans);
31+
}
32+
33+
sendMetrics(metrics) {
34+
const data = {
35+
os: metrics.os[metrics.os.length - 2],
36+
responses: metrics.responses[metrics.responses.length - 2],
37+
interval: metrics.interval,
38+
retention: metrics.retention,
39+
};
40+
this.server.emit('esm_stats', data);
41+
}
42+
}

src/status.monitor.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Module } from '@nestjs/common';
22
import { StatusMonitorController } from './status.monitor.controller';
3+
import { StatusMonitorGateway } from './status.monitor.gateway';
4+
import { StatusMonitoringService } from './status.monitoring.service';
35

46
@Module({
57
controllers: [StatusMonitorController],
8+
providers: [StatusMonitorGateway, StatusMonitoringService],
69
})
710
export class StatusMonitorModule {}

0 commit comments

Comments
 (0)