@@ -4,7 +4,8 @@ var exported = {},
44 automaticStateManager = require ( './helpers/automaticStateManager' ) ,
55 log = require ( '../../../api/utils/log.js' ) ( 'logger:api' ) ,
66 { validateRead } = require ( '../../../api/utils/rights.js' ) ;
7-
7+ const JOB = require ( '../../../api/parts/jobs' ) ;
8+ const MAX_NUMBER_OF_LOG_ENTRIES = 1000 ;
89const FEATURE_NAME = 'logger' ;
910
1011var RequestLoggerStateEnum = {
@@ -20,6 +21,13 @@ plugins.setConfigs("logger", {
2021} ) ;
2122
2223( function ( ) {
24+ plugins . register ( "/master" , function ( ) {
25+ setTimeout ( ( ) => {
26+ JOB . job ( 'logger:clear' , { max : MAX_NUMBER_OF_LOG_ENTRIES } )
27+ . replace ( )
28+ . schedule ( "every 5 minutes" ) ;
29+ } , 10000 ) ;
30+ } ) ;
2331
2432 plugins . register ( "/permissions/features" , function ( ob ) {
2533 ob . features . push ( FEATURE_NAME ) ;
@@ -345,46 +353,32 @@ plugins.setConfigs("logger", {
345353 return true ;
346354 }
347355 if ( params . qstring . method === 'collection_info' ) {
348- validateRead ( params , FEATURE_NAME , async function ( parameters ) {
349- try {
350- var stats = await common . db . collection ( 'logs' + parameters . app_id ) . aggregate ( [ { $collStats : { storageStats : { } } } ] ) . toArray ( ) ;
351- common . returnOutput ( parameters , { capped : stats ?. [ 0 ] ?. storageStats ?. capped , max : stats ?. [ 0 ] ?. storageStats ?. max } ) ;
352- }
353- catch ( ex ) {
354- console . log ( "Failed fetching logs collection info: " , ex ) ;
355- common . returnMessage ( parameters , 400 , 'Error fetching collection info' ) ;
356- }
356+ validateRead ( params , FEATURE_NAME , function ( parameters ) {
357+ common . db . collection ( 'logs' + parameters . app_id ) . stats ( function ( err , stats ) {
358+ if ( err ) {
359+ console . log ( "Failed fetching logs collection info" , err ) ;
360+ return common . returnMessage ( parameters , 400 , 'Error fetching collection info' ) ;
361+ }
362+ common . returnOutput ( parameters , stats && { size : stats . size , count : stats . count , max : MAX_NUMBER_OF_LOG_ENTRIES } || { } ) ;
363+ } ) ;
357364 } ) ;
358365 return true ;
359366 }
360367 } ) ;
361368
362- plugins . register ( "/i/apps/create" , function ( ob ) {
363- var appId = ob . appId ;
364- common . db . command ( { "convertToCapped" : 'logs' + appId , size : 10000000 , max : 1000 } , function ( err ) {
365- if ( err ) {
366- common . db . createCollection ( 'logs' + appId , { capped : true , size : 10000000 , max : 1000 } , function ( ) { } ) ;
367- }
368- } ) ;
369- } ) ;
370-
371369 plugins . register ( "/i/apps/delete" , function ( ob ) {
372370 var appId = ob . appId ;
373371 common . db . collection ( 'logs' + appId ) . drop ( function ( ) { } ) ;
374372 } ) ;
375373
376374 plugins . register ( "/i/apps/reset" , function ( ob ) {
377375 var appId = ob . appId ;
378- common . db . collection ( 'logs' + appId ) . drop ( function ( ) {
379- common . db . createCollection ( 'logs' + appId , { capped : true , size : 10000000 , max : 1000 } , function ( ) { } ) ;
380- } ) ;
376+ common . db . collection ( 'logs' + appId ) . drop ( function ( ) { } ) ;
381377 } ) ;
382378
383379 plugins . register ( "/i/apps/clear_all" , function ( ob ) {
384380 var appId = ob . appId ;
385- common . db . collection ( 'logs' + appId ) . drop ( function ( ) {
386- common . db . createCollection ( 'logs' + appId , { capped : true , size : 10000000 , max : 1000 } , function ( ) { } ) ;
387- } ) ;
381+ common . db . collection ( 'logs' + appId ) . drop ( function ( ) { } ) ;
388382 } ) ;
389383} ( exported ) ) ;
390384
0 commit comments