File tree Expand file tree Collapse file tree 5 files changed +55
-17
lines changed Expand file tree Collapse file tree 5 files changed +55
-17
lines changed Original file line number Diff line number Diff line change 1
- import { ServerResponse } from 'http' ;
2
-
3
1
import { PrismaClient } from '@prisma/client' ;
4
2
import { WebhookService } from 'app/services/WebhookService' ;
3
+ import { NextApiRequest , NextApiResponse } from 'next' ;
5
4
6
5
import { prisma } from '../lib/prisma/prisma' ;
7
6
import { EventService } from '../services/EventService' ;
8
7
import { TraceService } from '../services/TraceService' ;
9
8
import { UserService } from '../services/UserService' ;
10
9
import { WebsiteService } from '../services/WebsiteService' ;
11
- import type { NextIncomingMessage , RequestObjectHandler } from '../utils/types' ;
10
+ import type { RequestObjectHandler } from '../utils/types' ;
12
11
import { Auth , AuthInfo } from './auth' ;
13
12
14
13
export type Context = {
15
14
authInfo ?: AuthInfo ;
15
+ exemptAuth ?: boolean ;
16
16
prisma : PrismaClient ;
17
17
isLoggedIn : boolean ;
18
18
userService : UserService ;
19
19
eventService : EventService ;
20
20
websiteService : WebsiteService ;
21
21
traceService : TraceService ;
22
22
webhookService : WebhookService ;
23
- req : NextIncomingMessage ;
24
- res : ServerResponse ;
23
+ req : NextApiRequest ;
24
+ res : NextApiResponse ;
25
25
} ;
26
26
27
27
const auth = new Auth ( ) ;
Original file line number Diff line number Diff line change
1
+ import { createContext } from 'app/graphql/context' ;
2
+ import { RequestHandler } from 'app/utils/types' ;
3
+
4
+ import { TraceStatus } from '.prisma/client' ;
5
+
6
+ export default ( async ( req , res ) => {
7
+ const context = await createContext ( { req, res } ) ;
8
+ context . exemptAuth = true ;
9
+
10
+ const { id, rangeTime = '24h' } = req . query as { id : string ; rangeTime ?: string } ;
11
+
12
+ const result = await context . traceService . findTraces ( { websiteId : parseInt ( id ) , rangeTime } ) ;
13
+
14
+ const oks = result . results . filter ( ( r ) => r . status === TraceStatus . OK ) ;
15
+
16
+ const percent =
17
+ result . results . length === 0
18
+ ? 'unknown'
19
+ : ( Math . floor ( ( oks . length / result . results . length ) * 1000 ) / 1000 ) * 100 ;
20
+
21
+ const color =
22
+ percent === 'unknown'
23
+ ? 'inactive'
24
+ : percent >= 100
25
+ ? 'brightgreen'
26
+ : percent >= 90
27
+ ? 'important'
28
+ : 'critical' ;
29
+
30
+ res . redirect ( `https://img.shields.io/badge/uptime-${ encodeURIComponent ( percent + '%' ) } -${ color } ` ) ;
31
+ } ) as RequestHandler ;
Original file line number Diff line number Diff line change @@ -117,6 +117,20 @@ export default function Page() {
117
117
{ website . data ?. website . webhooks . length }
118
118
</ Descriptions . Item >
119
119
) }
120
+
121
+ < Descriptions . Item label = "Badge" >
122
+ < img
123
+ src = { `/api/badge/${ website . data ?. website ?. id } ` }
124
+ alt = "The shield"
125
+ className = "cursor-pointer"
126
+ onClick = { async ( ) => {
127
+ await navigator . clipboard . writeText (
128
+ `[](${ location . origin } /monitoring/websiteStatus/${ website . data ?. website ?. id } )` ,
129
+ ) ;
130
+ alert ( 'Markdown copied!' ) ;
131
+ } }
132
+ />
133
+ </ Descriptions . Item >
120
134
</ Descriptions >
121
135
122
136
< Link
Original file line number Diff line number Diff line change @@ -243,7 +243,7 @@ export class TraceService extends BaseService {
243
243
const whereWithId = {
244
244
...cursorWhere ,
245
245
...where ,
246
- userId : this . ctx . authInfo ! . id ,
246
+ userId : this . ctx . exemptAuth ? undefined : this . ctx . authInfo ! . id ,
247
247
} as const ;
248
248
249
249
const minId = (
Original file line number Diff line number Diff line change 1
- import { ServerResponse , IncomingMessage } from 'http' ;
2
-
3
1
import * as t from 'io-ts' ;
2
+ import type { NextApiRequest , NextApiResponse } from 'next' ;
4
3
5
4
import { NexusGenInputs } from '../../graphql/server/generated' ;
6
5
7
- export type NextIncomingMessage = IncomingMessage & {
8
- cookies ?: {
9
- [ key : string ] : any ;
10
- } ;
11
- } ;
12
-
13
- export type RequestHandler = ( req : NextIncomingMessage , res : ServerResponse ) => Promise < void > ;
6
+ export type RequestHandler = ( req : NextApiRequest , res : NextApiResponse ) => Promise < void > ;
14
7
15
8
export type RequestObjectHandler < T = undefined > = ( o : {
16
- req : NextIncomingMessage ;
17
- res : ServerResponse ;
9
+ req : NextApiRequest ;
10
+ res : NextApiResponse ;
18
11
} ) => Promise < T > ;
19
12
20
13
export type InputObjectNames = keyof NexusGenInputs ;
You can’t perform that action at this time.
0 commit comments