@@ -925,7 +925,6 @@ const startMigration = async (req: Request): Promise<any> => {
925925 }
926926} ;
927927
928-
929928const getLogs = async ( req : Request ) : Promise < any > => {
930929 const projectId = req ?. params ?. projectId ? path ?. basename ( req . params . projectId ) : "" ;
931930 const stackId = req ?. params ?. stackId ? path ?. basename ( req . params . stackId ) : "" ;
@@ -934,9 +933,7 @@ const getLogs = async (req: Request): Promise<any> => {
934933 const stopIndex = startIndex + limit ;
935934 const searchText = req ?. params ?. searchText ?? null ;
936935 const filter = req ?. params ?. filter ?? "all" ;
937-
938936 const srcFunc = "getLogs" ;
939-
940937 if (
941938 ! projectId ||
942939 ! stackId ||
@@ -945,40 +942,44 @@ const getLogs = async (req: Request): Promise<any> => {
945942 ) {
946943 throw new BadRequestError ( "Invalid projectId or stackId" ) ;
947944 }
948-
949945 try {
950946 const mainPath = process ?. cwd ( ) ?. split ( "migration-v2" ) ?. [ 0 ] ;
951947 if ( ! mainPath ) {
952948 throw new BadRequestError ( "Invalid application path" ) ;
953949 }
954-
955950 const logsDir = path ?. join ( mainPath , "migration-v2" , "api" , "logs" ) ;
956951 const loggerPath = path ?. join ( logsDir , projectId , `${ stackId } .log` ) ;
957952 const absolutePath = path ?. resolve ( loggerPath ) ;
958-
959953 if ( ! absolutePath ?. startsWith ( logsDir ) ) {
960954 throw new BadRequestError ( "Access to this file is not allowed." ) ;
961955 }
962-
963956 if ( fs . existsSync ( absolutePath ) ) {
957+ let index = 0 ;
964958 const logs = await fs . promises . readFile ( absolutePath , "utf8" ) ;
965959 let logEntries = logs
966960 ?. split ( "\n" )
967961 ?. map ( ( line ) => {
968962 try {
969- return line ? JSON ?. parse ( line ) : null ;
963+ const parsedLine = JSON ?. parse ( line )
964+ parsedLine [ 'id' ] = index ;
965+ ++ index ;
966+ return parsedLine ? parsedLine : null ;
970967 } catch ( error ) {
971968 return null ;
972969 }
973970 } )
974971 ?. filter ?.( ( entry ) => entry !== null ) ;
975-
976972 if ( ! logEntries ?. length ) {
977973 return { logs : [ ] , total : 0 } ;
978974 }
979-
975+ const filterOptions = Array . from ( new Set ( logEntries . map ( ( log ) => log . level ) ) ) ;
976+ const auditStartIndex = logEntries . findIndex ( log => log . message . includes ( "Starting audit process" ) ) ;
977+ const auditEndIndex = logEntries . findIndex ( log => log . message . includes ( "Audit process completed" ) ) ;
978+ logEntries = [
979+ ...logEntries . slice ( 0 , auditStartIndex ) ,
980+ ...logEntries . slice ( auditEndIndex + 1 )
981+ ]
980982 logEntries = logEntries ?. slice ?.( 1 , logEntries ?. length - 2 ) ;
981-
982983 if ( filter !== "all" ) {
983984 const filters = filter ?. split ( "-" ) ?? [ ] ;
984985 logEntries = logEntries ?. filter ( ( log ) => {
@@ -989,17 +990,16 @@ const getLogs = async (req: Request): Promise<any> => {
989990 } ) ;
990991 } ) ;
991992 }
992-
993993 if ( searchText && searchText !== "null" ) {
994994 logEntries = logEntries ?. filter ?.( ( log ) =>
995995 matchesSearchText ( log , searchText )
996996 ) ;
997997 }
998-
999998 const paginatedLogs = logEntries ?. slice ?.( startIndex , stopIndex ) ?? [ ] ;
1000999 return {
10011000 logs : paginatedLogs ,
10021001 total : logEntries ?. length ?? 0 ,
1002+ filterOptions : filterOptions ,
10031003 } ;
10041004 } else {
10051005 logger . error ( getLogMessage ( srcFunc , HTTP_TEXTS . LOGS_NOT_FOUND ) ) ;
0 commit comments