1+ import type { AlertsApiResponse } from '@hyperdx/common-utils/dist/types' ;
12import express from 'express' ;
2- import _ from 'lodash' ;
33import { ObjectId } from 'mongodb' ;
44import { z } from 'zod' ;
55import { processRequest , validateRequest } from 'zod-express-middleware' ;
@@ -17,7 +17,8 @@ import { alertSchema, objectIdSchema } from '@/utils/zod';
1717
1818const router = express . Router ( ) ;
1919
20- router . get ( '/' , async ( req , res , next ) => {
20+ type AlertsExpRes = express . Response < AlertsApiResponse > ;
21+ router . get ( '/' , async ( req , res : AlertsExpRes , next ) => {
2122 try {
2223 const teamId = req . user ?. team ;
2324 if ( teamId == null ) {
@@ -26,58 +27,87 @@ router.get('/', async (req, res, next) => {
2627
2728 const alerts = await getAlertsEnhanced ( teamId ) ;
2829
29- const data = await Promise . all (
30+ const data : AlertsApiResponse [ 'data' ] = await Promise . all (
3031 alerts . map ( async alert => {
3132 const history = await getRecentAlertHistories ( {
3233 alertId : new ObjectId ( alert . _id ) ,
3334 limit : 20 ,
3435 } ) ;
3536
3637 return {
37- history,
38+ _id : alert . _id . toString ( ) ,
39+ interval : alert . interval ,
40+ scheduleOffsetMinutes : alert . scheduleOffsetMinutes ,
41+ scheduleStartAt : alert . scheduleStartAt ?. toISOString ( ) ?? undefined ,
42+ threshold : alert . threshold ,
43+ thresholdType : alert . thresholdType ,
44+ channel : { type : alert . channel . type ?? undefined } ,
45+ state : alert . state ,
46+ source : alert . source ,
47+ tileId : alert . tileId ,
48+ name : alert . name ,
49+ message : alert . message ,
50+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
51+ createdAt : ( alert as any ) . createdAt ?. toISOString ?.( ) ?? '' ,
52+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
53+ updatedAt : ( alert as any ) . updatedAt ?. toISOString ?.( ) ?? '' ,
54+ history : history . map ( h => ( {
55+ counts : h . counts ,
56+ createdAt : h . createdAt . toISOString ( ) ,
57+ state : h . state ,
58+ lastValues : h . lastValues . map ( lv => ( {
59+ startTime : lv . startTime . toISOString ( ) ,
60+ count : lv . count ,
61+ } ) ) ,
62+ } ) ) ,
3863 silenced : alert . silenced
3964 ? {
40- by : alert . silenced . by ?. email ,
41- at : alert . silenced . at ,
42- until : alert . silenced . until ,
65+ by : alert . silenced . by ?. email ?? '' ,
66+ at : alert . silenced . at . toISOString ( ) ,
67+ until : alert . silenced . until . toISOString ( ) ,
4368 }
4469 : undefined ,
4570 createdBy : alert . createdBy
46- ? _ . pick ( alert . createdBy , [ 'email' , 'name' ] )
71+ ? {
72+ email : alert . createdBy . email ,
73+ name : alert . createdBy . name ,
74+ }
75+ : undefined ,
76+ dashboardId : alert . dashboard
77+ ? alert . dashboard . _id . toString ( )
78+ : undefined ,
79+ savedSearchId : alert . savedSearch
80+ ? alert . savedSearch . _id . toString ( )
81+ : undefined ,
82+ dashboard : alert . dashboard
83+ ? {
84+ _id : alert . dashboard . _id . toString ( ) ,
85+ name : alert . dashboard . name ,
86+ updatedAt :
87+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
88+ ( alert as any ) . dashboard ?. updatedAt ?. toISOString ?.( ) ?? '' ,
89+ tags : alert . dashboard . tags ,
90+ tiles : alert . dashboard . tiles
91+ . filter ( tile => tile . id === alert . tileId )
92+ . map ( tile => ( {
93+ id : tile . id ,
94+ config : { name : tile . config . name } ,
95+ } ) ) ,
96+ }
97+ : undefined ,
98+ savedSearch : alert . savedSearch
99+ ? {
100+ _id : alert . savedSearch . _id . toString ( ) ,
101+ createdAt :
102+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
103+ ( alert as any ) . savedSearch ?. createdAt ?. toISOString ?.( ) ?? '' ,
104+ name : alert . savedSearch . name ,
105+ updatedAt :
106+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
107+ ( alert as any ) . savedSearch ?. updatedAt ?. toISOString ?.( ) ?? '' ,
108+ tags : alert . savedSearch . tags ,
109+ }
47110 : undefined ,
48- channel : _ . pick ( alert . channel , [ 'type' ] ) ,
49- ...( alert . dashboard && {
50- dashboardId : alert . dashboard . _id ,
51- dashboard : {
52- tiles : alert . dashboard . tiles
53- . filter ( tile => tile . id === alert . tileId )
54- . map ( tile => _ . pick ( tile , [ 'id' , 'config.name' ] ) ) ,
55- ..._ . pick ( alert . dashboard , [ '_id' , 'name' , 'updatedAt' , 'tags' ] ) ,
56- } ,
57- } ) ,
58- ...( alert . savedSearch && {
59- savedSearchId : alert . savedSearch . _id ,
60- savedSearch : _ . pick ( alert . savedSearch , [
61- '_id' ,
62- 'createdAt' ,
63- 'name' ,
64- 'updatedAt' ,
65- 'tags' ,
66- ] ) ,
67- } ) ,
68- ..._ . pick ( alert , [
69- '_id' ,
70- 'interval' ,
71- 'scheduleOffsetMinutes' ,
72- 'scheduleStartAt' ,
73- 'threshold' ,
74- 'thresholdType' ,
75- 'state' ,
76- 'source' ,
77- 'tileId' ,
78- 'createdAt' ,
79- 'updatedAt' ,
80- ] ) ,
81111 } ;
82112 } ) ,
83113 ) ;
0 commit comments