Skip to content

Commit a36135e

Browse files
chore: wip
1 parent 52ad900 commit a36135e

File tree

3 files changed

+77
-163
lines changed

3 files changed

+77
-163
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Action } from '@stacksjs/actions'
2+
import { response } from '@stacksjs/router'
3+
import { Log } from '@stacksjs/orm'
4+
5+
export default new Action({
6+
name: 'Log Stats',
7+
description: 'Log Stats ORM Action',
8+
method: 'GET',
9+
async handle() {
10+
// Get current timestamp and 24 hours ago timestamp
11+
const now = Date.now()
12+
const twentyFourHoursAgo = now - (24 * 60 * 60 * 1000)
13+
14+
// Fetch logs within the last 24 hours
15+
const logs = await Log.where('timestamp', '>=', twentyFourHoursAgo)
16+
.orderBy('timestamp', 'asc')
17+
.get()
18+
19+
// Group logs by hour and count them
20+
const hourlyStats = logs.reduce((acc: { [key: string]: number }, log) => {
21+
const date = new Date(log.timestamp)
22+
const hour = date.getHours()
23+
const day = date.getDate()
24+
const month = date.getMonth() + 1
25+
const year = date.getFullYear()
26+
const key = `${year}-${month}-${day} ${hour}:00`
27+
28+
if (!acc[key]) {
29+
acc[key] = 0
30+
}
31+
acc[key]++
32+
33+
return acc
34+
}, {})
35+
36+
// Format data for line graph
37+
const data = Object.entries(hourlyStats).map(([time, count]) => ({
38+
time,
39+
count
40+
}))
41+
42+
return response.json({
43+
data,
44+
timeRange: {
45+
start: twentyFourHoursAgo,
46+
end: now
47+
}
48+
})
49+
},
50+
})

storage/framework/api/openapi.json

Lines changed: 1 addition & 137 deletions
Large diffs are not rendered by default.

storage/framework/core/router/src/server.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Model, Options, Route, RouteParam, ServeOptions } from '@stacksjs/types'
2-
import type { RateLimitResult } from 'ts-rate-limiter'
2+
// import type { RateLimitResult } from 'ts-rate-limiter'
33

44
import process from 'node:process'
55
import { handleError } from '@stacksjs/error-handling'
@@ -8,30 +8,30 @@ import { getModelName } from '@stacksjs/orm'
88
import { extname, path } from '@stacksjs/path'
99
import { fs, globSync } from '@stacksjs/storage'
1010
import { isNumber } from '@stacksjs/validation'
11-
import { RateLimiter } from 'ts-rate-limiter'
11+
// import { RateLimiter } from 'ts-rate-limiter'
1212
import { route, staticRoute } from '.'
1313

1414
import { middlewares } from './middleware'
1515

1616
import { request as RequestParam } from './request'
1717

18-
const limiter = new RateLimiter({
19-
windowMs: 15 * 60 * 1000, // 15 minutes
20-
maxRequests: 100,
21-
algorithm: 'sliding-window',
22-
handler: (req: Request, result: RateLimitResult) => {
23-
return new Response(JSON.stringify({
24-
error: 'Too many requests',
25-
retryAfter: Math.ceil(result.remaining / 1000),
26-
}), {
27-
status: 429,
28-
headers: {
29-
'Content-Type': 'application/json',
30-
'Retry-After': Math.ceil(result.remaining / 1000).toString(),
31-
},
32-
})
33-
},
34-
})
18+
// const limiter = new RateLimiter({
19+
// windowMs: 15 * 60 * 1000, // 15 minutes
20+
// maxRequests: 100,
21+
// algorithm: 'sliding-window',
22+
// handler: (req: Request, result: RateLimitResult) => {
23+
// return new Response(JSON.stringify({
24+
// error: 'Too many requests',
25+
// retryAfter: Math.ceil(result.remaining / 1000),
26+
// }), {
27+
// status: 429,
28+
// headers: {
29+
// 'Content-Type': 'application/json',
30+
// 'Retry-After': Math.ceil(result.remaining / 1000).toString(),
31+
// },
32+
// })
33+
// },
34+
// })
3535

3636
export async function serve(options: ServeOptions = {}): Promise<void> {
3737
const hostname = options.host || 'localhost'
@@ -61,15 +61,15 @@ export async function serverResponse(req: Request, body: string): Promise<Respon
6161
log.debug(`Headers: ${JSON.stringify(req.headers)}`)
6262
log.debug(`Body: ${JSON.stringify(req.body)}`)
6363

64-
const result = await limiter.check(req)
64+
// const result = await limiter.check(req)
6565

66-
if (!result.allowed) {
67-
log.info(`Rate limit exceeded: ${result.current}/${result.limit}`)
68-
log.info(`Reset in ${Math.ceil(result.remaining / 1000)} seconds`)
66+
// if (!result.allowed) {
67+
// log.info(`Rate limit exceeded: ${result.current}/${result.limit}`)
68+
// log.info(`Reset in ${Math.ceil(result.remaining / 1000)} seconds`)
6969

70-
// Handle rate limiting in your own way
71-
return new Response('Too many requests', { status: 429 })
72-
}
70+
// // Handle rate limiting in your own way
71+
// return new Response('Too many requests', { status: 429 })
72+
// }
7373

7474
const trimmedUrl = req.url.endsWith('/') && req.url.length > 1 ? req.url.slice(0, -1) : req.url
7575
const url: URL = new URL(trimmedUrl) as URL

0 commit comments

Comments
 (0)