adonisjs-server-stats 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +39 -12
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -89,6 +89,23 @@ server.use([
89
89
  ```ts
90
90
  // config/server_stats.ts
91
91
  import { defineConfig } from 'adonisjs-server-stats'
92
+ import { processCollector, systemCollector, httpCollector } from 'adonisjs-server-stats/collectors'
93
+
94
+ export default defineConfig({
95
+ collectors: [
96
+ processCollector(),
97
+ systemCollector(),
98
+ httpCollector(),
99
+ ],
100
+ })
101
+ ```
102
+
103
+ That's it -- this gives you CPU, memory, event loop lag, and HTTP throughput out of the box. All other options have sensible defaults. Add more collectors as needed:
104
+
105
+ ```ts
106
+ // config/server_stats.ts
107
+ import env from '#start/env'
108
+ import { defineConfig } from 'adonisjs-server-stats'
92
109
  import {
93
110
  processCollector,
94
111
  systemCollector,
@@ -99,14 +116,12 @@ import {
99
116
  logCollector,
100
117
  appCollector,
101
118
  } from 'adonisjs-server-stats/collectors'
102
- import env from '#start/env'
103
119
 
104
120
  export default defineConfig({
105
121
  intervalMs: 3000,
106
122
  transport: 'transmit',
107
123
  channelName: 'admin/server-stats',
108
124
  endpoint: '/admin/api/server-stats',
109
- shouldShow: (ctx) => !!ctx.auth?.user?.isAdmin,
110
125
  collectors: [
111
126
  processCollector(),
112
127
  systemCollector(),
@@ -133,7 +148,7 @@ export default defineConfig({
133
148
  // start/routes.ts
134
149
  router
135
150
  .get('/admin/api/server-stats', '#controllers/admin/server_stats_controller.index')
136
- .use(middleware.superadmin())
151
+ .use(middleware.superadmin()) // Replace with your own middleware
137
152
  ```
138
153
 
139
154
  ### 5. Create the controller
@@ -278,23 +293,35 @@ interface MetricCollector {
278
293
 
279
294
  ## Visibility Control (`shouldShow`)
280
295
 
281
- Control who sees the stats bar via the `shouldShow` config callback. It receives the AdonisJS `HttpContext` and returns `true` to render.
296
+ By default the stats bar renders for every request. Use `shouldShow` to restrict it. The callback receives the AdonisJS `HttpContext` and should return `true` to show the bar, `false` to hide it.
297
+
298
+ Because `shouldShow` runs **after** middleware (including auth), you have full access to `ctx.auth`.
282
299
 
283
300
  ```ts
284
301
  export default defineConfig({
285
- // Only admin users
286
- shouldShow: (ctx) => !!ctx.auth?.user?.isAdmin,
302
+ // Only show in development
303
+ shouldShow: () => env.get('NODE_ENV'),
304
+ })
305
+ ```
287
306
 
288
- // Only in development
289
- shouldShow: () => process.env.NODE_ENV === 'development',
307
+ ```ts
308
+ export default defineConfig({
309
+ // Only show for logged-in admin users
310
+ shouldShow: (ctx) => ctx.auth?.user?.isAdmin === true,
311
+ })
312
+ ```
290
313
 
291
- // Multiple roles
292
- shouldShow: (ctx) =>
293
- ctx.auth?.user?.role === 'admin' || ctx.auth?.user?.role === 'superadmin',
314
+ ```ts
315
+ export default defineConfig({
316
+ // Only show for specific roles
317
+ shouldShow: (ctx) => {
318
+ const role = ctx.auth?.user?.role
319
+ return role === 'admin' || role === 'superadmin'
320
+ },
294
321
  })
295
322
  ```
296
323
 
297
- When `shouldShow` is not set, the stats bar always renders.
324
+ > **Tip:** When `shouldShow` is not set, the bar renders for everyone. In production you almost always want to set this.
298
325
 
299
326
  ---
300
327
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adonisjs-server-stats",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Real-time server monitoring for AdonisJS v6 applications",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",