tango-app-api-infra 3.1.16 → 3.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,7 +62,12 @@ export async function storeTicketList( req, res ) {
62
62
  $filter: {
63
63
  input: '$ticketActivity',
64
64
  as: 'item',
65
- cond: { $ne: [ '$$item.actionType', 'statusChange' ] },
65
+ cond: {
66
+ $and: [
67
+ { $ne: [ '$$item.actionType', 'statusChange' ] },
68
+ { $ne: [ '$$item.actionType', 'statusCheck' ] },
69
+ ],
70
+ },
66
71
  },
67
72
  },
68
73
  primaryIssue: {
@@ -284,173 +289,141 @@ export async function edgeAppLogTable( req, res ) {
284
289
  try {
285
290
  const store = await findOneStore( { storeId: req.body.storeId } );
286
291
  if ( !store ) {
287
- return res.sendError( 'Stores Not fond', 204 );
292
+ return res.sendError( 'Store Not found', 204 );
288
293
  }
289
- let startHour = store.storeProfile.open.split( ':' )[0];
290
- let endHour = store.storeProfile.close.split( ':' )[0];
294
+
295
+ let startHour = parseInt( store.storeProfile.open.split( ':' )[0], 10 );
296
+ let endHour = parseInt( store.storeProfile.close.split( ':' )[0], 10 );
291
297
  const interval = 60; // 1 hour in minutes
292
298
  const timeSlots = generateTimeSlots( startHour, endHour, interval, req );
299
+
300
+ const date = dayjs( timeSlots[0].from ).format( 'DD-MM-YYYY' );
301
+ const storeId = req.body.storeId;
302
+
303
+ const internetSpeedQuery = {
304
+ size: 100,
305
+ query: {
306
+ bool: {
307
+ must: [
308
+ { term: { 'log_type.keyword': 'Application' } },
309
+ { term: { 'store_date.keyword': date } },
310
+ { term: { 'storeId.keyword': storeId } },
311
+ { term: { 'log_subtype.keyword': 'Speed_Test' } },
312
+ ],
313
+ },
314
+ },
315
+ sort: [ { timestamp: { order: 'desc' } } ],
316
+ _source: [ 'data.upload_Speed', 'data.occuringTime' ],
317
+ };
318
+
319
+ const fileCountQuery = {
320
+ size: 100,
321
+ query: {
322
+ bool: {
323
+ must: [
324
+ { term: { 'log_type.keyword': 'Application' } },
325
+ { term: { 'store_date.keyword': date } },
326
+ { term: { 'storeId.keyword': storeId } },
327
+ { term: { 'log_subtype.keyword': 'Zip_File_Count' } },
328
+ ],
329
+ },
330
+ },
331
+ sort: [ { timestamp: { order: 'desc' } } ],
332
+ _source: [ 'data.files_pushed', 'data.files_generated', 'data.occuringTime' ],
333
+ };
334
+
335
+ const downTimeQuery = {
336
+ size: 100,
337
+ query: {
338
+ bool: {
339
+ must: [
340
+ { term: { 'doc.date.keyword': date } },
341
+ { term: { 'doc.store_id.keyword': storeId } },
342
+ ],
343
+ },
344
+ },
345
+ _source: [ 'doc.streamwise_downtime', 'doc.hour' ],
346
+ };
347
+
348
+ // Execute all queries in parallel
349
+ const [ speedTestResult, fileCountResult, downtimeResult ] = await Promise.all( [
350
+ getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, internetSpeedQuery ),
351
+ getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, fileCountQuery ),
352
+ getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).downTimeHourly, downTimeQuery ),
353
+ ] );
354
+ // Process results
355
+ const speedTestData = speedTestResult?.body?.hits?.hits ?? [];
356
+ const fileCountData = fileCountResult?.body?.hits?.hits ?? [];
357
+ const downtimeData = downtimeResult?.body?.hits?.hits ?? [];
358
+
293
359
  for ( const obj of timeSlots ) {
360
+ const hour = obj.hour;
294
361
  obj.startTime = dayjs( obj.from ).format( 'hh:mm A' );
295
362
  obj.endTime = dayjs( obj.to ).format( 'hh:mm A' );
296
- let internetSpeedQuery = {
297
- 'query': {
298
- 'bool': {
299
- 'must': [
300
- {
301
- 'term': {
302
- 'log_type.keyword': 'Application',
303
- },
304
- },
305
- {
306
- 'term': {
307
- 'store_date.keyword': dayjs( obj.from ).format( 'DD-MM-YYYY' ),
308
- },
309
- },
310
- {
311
- 'term': {
312
- 'storeId.keyword': req.body.storeId,
313
- },
314
- },
315
- {
316
- 'term': {
317
- 'log_subtype.keyword': 'Speed_Test',
318
- },
319
- },
320
- ],
321
- },
322
- },
323
- 'sort': [
324
- { 'timestamp': { 'order': 'desc' } },
325
- ],
326
- };
327
- let speedTest = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, internetSpeedQuery );
328
- if ( speedTest&& speedTest.body.hits && speedTest.body.hits.hits.length > 0 ) {
329
- for ( const sourcedata of speedTest.body.hits.hits ) {
330
- if ( sourcedata._source ) {
331
- if ( Number( sourcedata._source.data.occuringTime.split( ':' )[0] )==obj.hour ) {
332
- const megabytes = bytesToMB( sourcedata._source.data.upload_Speed.split( '.' )[0] ).toFixed( 2 );
333
- obj.Internetspeed = megabytes+ ' MB/sec';
334
- }
335
- }
336
- }
337
- } else {
338
- obj.Internetspeed = '';
339
- }
340
- let FileCountQuery = {
341
- 'size': 1000,
342
- 'query': {
343
- 'bool': {
344
- 'must': [
345
- {
346
- 'term': {
347
- 'log_type.keyword': 'Application',
348
- },
349
- },
350
- {
351
- 'term': {
352
- 'store_date.keyword': dayjs( obj.from ).format( 'DD-MM-YYYY' ),
353
- },
354
- },
355
- {
356
- 'term': {
357
- 'storeId.keyword': req.body.storeId,
358
- },
359
- },
360
- {
361
- 'term': {
362
- 'log_subtype.keyword': 'Zip_File_Count',
363
- },
364
- },
365
- ],
366
363
 
367
- },
368
- },
364
+ // Internet speed
369
365
 
370
- 'sort': [
371
- { 'timestamp': { 'order': 'desc' } },
372
- ],
373
- };
374
- let newFilesCount = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, FileCountQuery );
375
- if ( newFilesCount&& newFilesCount.body.hits && newFilesCount.body.hits.hits.length > 0 ) {
376
- obj.files_pushed = 0;
377
- obj.files_generated = 0;
378
- for ( let sourcedata of newFilesCount.body.hits.hits ) {
379
- if ( sourcedata._source ) {
380
- if ( Number( sourcedata._source.data.occuringTime.split( ':' )[0] )==obj.hour ) {
381
- obj.files_pushed = obj.files_pushed+Number( sourcedata._source.data.files_pushed );
382
- obj.files_generated = obj.files_generated+Number( sourcedata._source.data.files_generated );
383
- }
384
- }
385
- }
366
+ const speedTest = speedTestData.find(
367
+ ( item ) => Number( item._source.data.occuringTime.split( ':' )[0] ) === hour,
368
+ );
369
+ if ( speedTest&&speedTest._source.data.upload_Speed =='0 bytes' ) {
370
+ obj.Internetspeed = '0 MB/sec';
386
371
  } else {
387
- obj.files_pushed = '';
388
- obj.files_generated = '';
372
+ obj.Internetspeed = speedTest ? `${bytesToMB( speedTest._source.data.upload_Speed.split( '.' )[0] ).toFixed( 2 )} MB/sec` : '';
389
373
  }
390
- let downTimeQuery = {
391
- 'size': 1,
392
- 'query': {
393
- 'bool': {
394
- 'must': [
395
- {
396
- 'term': {
397
- 'doc.date.keyword': dayjs( obj.from ).format( 'DD-MM-YYYY' ),
398
- },
399
- },
400
- {
401
- 'term': {
402
- 'doc.store_id.keyword': req.body.storeId,
403
- },
404
- },
405
- {
406
- 'terms': {
407
- 'doc.hour.keyword': [ obj.hour ],
408
- },
409
- },
410
- ],
411
374
 
412
- },
413
- },
414
- };
415
- const downtime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).downTimeHourly, downTimeQuery );
416
- let streamwiseDowntime = downtime.body.hits.hits.length > 0 ? downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
417
- if ( streamwiseDowntime.length > 0 ) {
418
- const sum = streamwiseDowntime.reduce( ( accumulator, currentValue ) => {
419
- return accumulator + currentValue.down_time;
420
- }, 0 );
421
- const average = sum / streamwiseDowntime.length;
422
- obj.downtime = Math.round( average );
375
+
376
+ // File counts
377
+ const fileCounts = fileCountData.filter(
378
+ ( item ) => Number( item._source.data.occuringTime.split( ':' )[0] ) === hour,
379
+ );
380
+ obj.files_pushed = fileCounts.reduce( ( sum, item ) => sum + Number( item._source.data.files_pushed ), 0 ) || '';
381
+ obj.files_generated = fileCounts.reduce( ( sum, item ) => sum + Number( item._source.data.files_generated ), 0 ) || '';
382
+
383
+ // Downtime
384
+
385
+ const downtime = downtimeData.find( ( item ) => Number( item._source.doc.hour ) === hour );
386
+
387
+ if ( downtime ) {
388
+ const streamwiseDowntime = downtime._source.doc.streamwise_downtime || [];
389
+ if ( streamwiseDowntime.length > 0 ) {
390
+ const sum = streamwiseDowntime.reduce( ( acc, cur ) => acc + cur.down_time, 0 );
391
+ const average = sum / streamwiseDowntime.length;
392
+ obj.downtime = Math.round( average );
393
+ } else {
394
+ obj.downtime = '';
395
+ }
423
396
  } else {
424
397
  obj.downtime = '';
425
398
  }
426
399
  }
400
+
427
401
  if ( req.body.export ) {
428
- const exportdata = [];
429
- timeSlots.forEach( ( element ) => {
430
- exportdata.push( {
431
- 'Time Stamp': element.startTime+ '-'+ element.endTime,
432
- 'Downtime': element.ticketId,
433
- 'Avg Internet Speed': element.Date,
434
- 'Files Genarated': element.issueClosedDate,
435
- 'Files Pushed': element.primaryIssue,
436
- } );
437
- } );
438
- await download( exportdata, res );
402
+ const exportData = timeSlots.map( ( element ) => ( {
403
+ 'Time Stamp': `${element.startTime}-${element.endTime}`,
404
+ 'Downtime': element.downtime?element.downtime +' Mins':'--',
405
+ 'Avg Internet Speed': element.Internetspeed,
406
+ 'Files Generated': element.files_generated,
407
+ 'Files Pushed': element.files_pushed,
408
+ } ) );
409
+ await download( exportData, res );
439
410
  return;
440
411
  }
412
+
441
413
  res.sendSuccess( timeSlots );
442
414
  } catch ( error ) {
443
- logger.error( { error: error, function: 'edgeAppLog' } );
415
+ logger.error( { error, function: 'edgeAppLog' } );
444
416
  return res.sendError( error, 500 );
445
417
  }
446
418
  }
419
+
447
420
  function bytesToMB( bytes ) {
448
421
  return bytes / ( 1024 * 1024 );
449
422
  }
450
423
  function generateTimeSlots( startHour, endHour, interval, req ) {
451
424
  try {
452
425
  const timeSlots = [];
453
- for ( let hour = startHour; hour <= endHour; hour++ ) {
426
+ for ( let hour = startHour; hour < endHour; hour++ ) {
454
427
  for ( let minute = 0; minute < 60; minute += interval ) {
455
428
  let isoDate = new Date();
456
429
 
@@ -487,172 +460,154 @@ export async function viewedgeAppLog( req, res ) {
487
460
  try {
488
461
  const store = await findOneStore( { storeId: req.body.storeId } );
489
462
  if ( !store ) {
490
- return res.sendError( 'Stores Not fond', 204 );
463
+ return res.sendError( 'Stores Not found', 204 );
491
464
  }
492
- const inputDate = dayjs( req.body.Date ).format( 'YYYY-MM-DD' ); // Specify the date you want to add the times to
465
+ const inputDate = dayjs( req.body.Date ).format( 'DD-MM-YYYY' );
493
466
  const fromTime = req.body.from;
494
467
  const toTime = req.body.to;
495
468
  let response = {};
496
469
 
497
-
498
- let appStartTimeQuery = {
499
- 'size': 1000,
500
- 'query': {
501
- 'bool': {
502
- 'must': [
503
- {
504
- 'term': {
505
- 'log_type.keyword': 'Application',
506
- },
507
- },
508
- {
509
- 'term': {
510
- 'store_date.keyword': dayjs( inputDate ).format( 'DD-MM-YYYY' ),
511
- },
512
- },
513
- {
514
- 'term': {
515
- 'store_id.keyword': req.body.storeId,
516
- },
517
- },
518
- {
519
- 'term': {
520
- 'log_subtype.keyword': 'AppStart_Time',
521
- },
470
+ const queries = [
471
+ {
472
+ queryName: 'appStartTime',
473
+ query: {
474
+ size: 1,
475
+ query: {
476
+ bool: {
477
+ must: [
478
+ { term: { 'log_type.keyword': 'Application' } },
479
+ { term: { 'store_date.keyword': inputDate } },
480
+ { term: { 'storeId.keyword': req.body.storeId } },
481
+ { term: { 'log_subtype.keyword': 'AppStart_Time' } },
482
+ { term: { 'data.message.keyword': 'Login Success Event' } },
483
+ ],
522
484
  },
523
- ],
524
-
485
+ },
525
486
  },
526
487
  },
527
- };
528
- const appStartTime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, appStartTimeQuery );
529
-
530
- response.appStartTime = appStartTime.body.hits.hits.length > 0 ? appStartTime.body.hits.hits[0]._source.data.occuringTime : '';
531
-
532
- let appQuitTimeQuery = {
533
- 'size': 1,
534
- 'query': {
535
- 'bool': {
536
- 'must': [
537
- {
538
- 'term': {
539
- 'log_type.keyword': 'Application',
540
- },
541
- },
542
- {
543
- 'term': {
544
- 'store_date.keyword': dayjs( inputDate ).format( 'DD-MM-YYYY' ),
545
- },
546
- },
547
- {
548
- 'term': {
549
- 'store_id.keyword': req.body.storeId,
550
- },
551
- },
552
- {
553
- 'term': {
554
- 'log_subtype.keyword': 'App_Quit',
555
- },
488
+ {
489
+ queryName: 'appQuitTime',
490
+ query: {
491
+ size: 1,
492
+ query: {
493
+ bool: {
494
+ must: [
495
+ { term: { 'log_type.keyword': 'Application' } },
496
+ { term: { 'store_date.keyword': inputDate } },
497
+ { term: { 'storeId.keyword': req.body.storeId } },
498
+ { term: { 'log_subtype.keyword': 'App_Quit_Time' } },
499
+ { term: { 'data.message.keyword': 'App Quit With password' } },
500
+ ],
556
501
  },
557
- ],
558
-
502
+ },
503
+ sort: [ { timestamp: { order: 'desc' } } ],
559
504
  },
560
505
  },
561
- 'sort': [
562
- { 'timestamp': { 'order': 'desc' } },
563
- ],
564
- };
565
- const appQuitTime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, appQuitTimeQuery );
566
- if ( appQuitTime.body.hits.hits&&appQuitTime.body.hits.hits.length > 0 ) {
567
- for ( const sourceData of appQuitTime.body.hits.hits ) {
568
- if ( Number( sourceData._source.data.occuringTime.split( ':' )[0] )== Number( fromTime.split( ':' )[0] ) ) {
569
- response.appQuitTime = appQuitTime.body.hits.hits.length > 0 ? sourceData._source.data.occuringTime : '';
570
- }
571
- }
572
- }
573
- let appCrashTimeQuery = {
574
- 'size': 1,
575
- 'query': {
576
- 'bool': {
577
- 'must': [
578
- {
579
- 'term': {
580
- 'log_type.keyword': 'Application',
581
- },
506
+ {
507
+ queryName: 'appCrashTime',
508
+ query: {
509
+ size: 1,
510
+ query: {
511
+ bool: {
512
+ must: [
513
+ { term: { 'log_type.keyword': 'Application' } },
514
+ { term: { 'store_date.keyword': inputDate } },
515
+ { term: { 'storeId.keyword': req.body.storeId } },
516
+ { term: { 'log_subtype.keyword': 'App_Close_Event' } },
517
+ ],
582
518
  },
583
- {
584
- 'term': {
585
- 'store_date.keyword': dayjs( inputDate ).format( 'DD-MM-YYYY' ),
586
- },
519
+ },
520
+ sort: [ { timestamp: { order: 'desc' } } ],
521
+ },
522
+ },
523
+ {
524
+ queryName: 'screenStatus',
525
+ query: {
526
+ size: 100,
527
+ query: {
528
+ bool: {
529
+ must: [
530
+ { term: { 'log_type.keyword': 'Application' } },
531
+ { term: { 'store_date.keyword': inputDate } },
532
+ { term: { 'storeId.keyword': req.body.storeId } },
533
+ { term: { 'log_subtype.keyword': 'System_Status' } },
534
+ ],
587
535
  },
588
- {
589
- 'term': {
590
- 'store_id.keyword': req.body.storeId,
591
- },
536
+ },
537
+ sort: [ { timestamp: { order: 'asc' } } ],
538
+ },
539
+ },
540
+ {
541
+ queryName: 'fileCount',
542
+ query: {
543
+ size: 100,
544
+ query: {
545
+ bool: {
546
+ must: [
547
+ { term: { 'log_type.keyword': 'Application' } },
548
+ { term: { 'store_date.keyword': inputDate } },
549
+ { term: { 'storeId.keyword': req.body.storeId } },
550
+ { term: { 'log_subtype.keyword': 'Zip_File_Count' } },
551
+ ],
592
552
  },
593
- {
594
- 'term': {
595
- 'log_subtype.keyword': 'App_Crash',
596
- },
553
+ },
554
+ sort: [ { timestamp: { order: 'desc' } } ],
555
+ },
556
+ },
557
+ {
558
+ queryName: 'antiVirus',
559
+ query: {
560
+ size: 100,
561
+ query: {
562
+ bool: {
563
+ must: [
564
+ { term: { 'log_type.keyword': 'Application' } },
565
+ { term: { 'store_date.keyword': inputDate } },
566
+ { term: { 'storeId.keyword': req.body.storeId } },
567
+ { term: { 'log_subtype.keyword': 'Anti_Virus' } },
568
+ ],
597
569
  },
598
- ],
599
-
570
+ },
571
+ sort: [ { timestamp: { order: 'desc' } } ],
600
572
  },
601
573
  },
602
- 'sort': [
603
- { 'timestamp': { 'order': 'desc' } },
604
- ],
605
- };
574
+ ];
606
575
 
607
- const appCrashTime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, appCrashTimeQuery );
608
- if ( appCrashTime.body.hits.hits&&appCrashTime.body.hits.hits.length > 0 ) {
609
- for ( const sourceData of appCrashTime.body.hits.hits ) {
610
- if ( Number( sourceData._source.data.occuringTime.split( ':' )[0] )== Number( fromTime.split( ':' )[0] ) ) {
611
- response.AppCrashtime = appCrashTime.body.hits.hits.length > 0 ? sourceData._source.data.occuringTime : '';
612
- }
613
- }
576
+ const fetchLogs = queries.map( ( q ) =>
577
+ getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, q.query ),
578
+ );
579
+
580
+ const results = await Promise.all( fetchLogs );
581
+
582
+ const appStartTimeResult = results[0];
583
+ const appQuitTimeResult = results[1];
584
+ const appCrashTimeResult = results[2];
585
+ const screenStatusResult = results[3];
586
+ const fileCountResult = results[4];
587
+ const antiVirusResult = results[5];
588
+
589
+ response.appStartTime = appStartTimeResult.body.hits.hits.length > 0 ? appStartTimeResult.body.hits.hits[0]._source.data.occuringTime : '';
590
+
591
+ if ( appQuitTimeResult.body.hits.hits && appQuitTimeResult.body.hits.hits.length > 0 ) {
592
+ const quitTime = appQuitTimeResult.body.hits.hits.find( ( sourceData ) =>
593
+ Number( sourceData._source.data.occuringTime.split( ':' )[0] ) === Number( fromTime.split( ':' )[0] ),
594
+ );
595
+ response.appQuitTime = quitTime ? quitTime._source.data.occuringTime : '';
614
596
  }
615
- const screenStatus = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, {
616
- 'size': 1000,
617
- 'query': {
618
- 'bool': {
619
- 'must': [
620
- {
621
- 'term': {
622
- 'log_type.keyword': 'Application',
623
- },
624
- },
625
- {
626
- 'term': {
627
- 'store_date.keyword': dayjs( inputDate ).format( 'DD-MM-YYYY' ),
628
- },
629
- },
630
- {
631
- 'term': {
632
- 'store_id.keyword': req.body.storeId,
633
- },
634
- },
635
- {
636
- 'term': {
637
- 'log_subtype.keyword': 'System_Status',
638
- },
639
- },
640
- ],
641
597
 
642
- },
643
- },
644
- 'sort': [
645
- { 'timestamp': { 'order': 'asc' } },
646
- ],
598
+ if ( appCrashTimeResult.body.hits.hits && appCrashTimeResult.body.hits.hits.length > 0 ) {
599
+ const crashTime = appCrashTimeResult.body.hits.hits.find( ( sourceData ) =>
600
+ Number( sourceData._source.data.occuringTime.split( ':' )[0] ) === Number( fromTime.split( ':' )[0] ),
601
+ );
602
+ response.AppCrashtime = crashTime ? crashTime._source.data.occuringTime : '';
603
+ }
647
604
 
648
- } );
649
- if ( screenStatus&& screenStatus.body.hits.hits&&screenStatus.body.hits.hits.length > 0 ) {
605
+ if ( screenStatusResult.body.hits.hits && screenStatusResult.body.hits.hits.length > 0 ) {
650
606
  let suspendedTime;
651
607
  let resumedTime;
652
608
  const differences = [];
653
-
654
- for ( const sourceData of screenStatus.body.hits.hits ) {
655
- if ( Number( sourceData._source.data.occuringTime.split( ':' )[0] )== Number( fromTime.split( ':' )[0] ) ) {
609
+ for ( const sourceData of screenStatusResult.body.hits.hits ) {
610
+ if ( Number( sourceData._source.data.occuringTime.split( ':' )[0] ) === Number( fromTime.split( ':' )[0] ) ) {
656
611
  if ( sourceData._source.data.message.trim() === 'SYSTEM SUSPENDED' ) {
657
612
  suspendedTime = sourceData._source.data.occuringTime;
658
613
  } else if ( sourceData._source.data.message.trim() === 'SYSTEM RESUMED' ) {
@@ -671,96 +626,30 @@ export async function viewedgeAppLog( req, res ) {
671
626
  }
672
627
  }
673
628
  }
674
- response.screenStatus = differences.length>0?`${differences[0].minutes}Mins ${differences[0].seconds}Sec`:'';
629
+ response.screenStatus = differences.length > 0 ? `${differences[0].minutes}Mins ${differences[0].seconds}Sec` : '';
675
630
  }
676
- const FileCountQuery = {
677
- 'size': 1000,
678
- 'query': {
679
- 'bool': {
680
- 'must': [
681
- {
682
- 'term': {
683
- 'log_type.keyword': 'Application',
684
- },
685
- },
686
- {
687
- 'term': {
688
- 'store_date.keyword': dayjs( inputDate ).format( 'DD-MM-YYYY' ),
689
- },
690
- },
691
- {
692
- 'term': {
693
- 'storeId.keyword': req.body.storeId,
694
- },
695
- },
696
- {
697
- 'term': {
698
- 'log_subtype.keyword': 'Zip_File_Count',
699
- },
700
- },
701
- ],
702
-
703
- },
704
- },
705
631
 
706
- 'sort': [
707
- { 'timestamp': { 'order': 'desc' } },
708
- ],
709
- };
710
- const newFilesCount = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, FileCountQuery );
711
- if ( newFilesCount&& newFilesCount.body.hits && newFilesCount.body.hits.hits.length > 0 ) {
632
+ if ( fileCountResult.body.hits.hits && fileCountResult.body.hits.hits.length > 0 ) {
712
633
  response.filesPushed = 0;
713
- response.files_genrated = 0;
714
- for ( const sourcedata of newFilesCount.body.hits.hits ) {
715
- if ( sourcedata._source ) {
716
- if ( Number( sourcedata._source.data.occuringTime.split( ':' )[0] )==Number( fromTime.split( ':' )[0] ) ) {
717
- response.filesPushed = response.filesPushed+Number( sourcedata._source.data.files_pushed );
718
- response.files_genrated = response.files_generated+Number( sourcedata._source.data.files_generated );
719
- }
634
+ response.files_generated = 0;
635
+ for ( const sourceData of fileCountResult.body.hits.hits ) {
636
+ if ( sourceData._source && Number( sourceData._source.data.occuringTime.split( ':' )[0] ) === Number( fromTime.split( ':' )[0] ) ) {
637
+ response.filesPushed += Number( sourceData._source.data.files_pushed );
638
+ response.files_generated += Number( sourceData._source.data.files_generated );
720
639
  }
721
640
  }
722
641
  } else {
723
642
  response.filesPushed = '';
724
- response.files_genrated = '';
643
+ response.files_generated = '';
725
644
  }
726
645
 
727
-
728
- const antiVirus = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).edgeAppSystemLogs, {
729
- 'size': 1,
730
- 'query': {
731
- 'bool': {
732
- 'must': [
733
- {
734
- 'term': {
735
- 'log_type.keyword': 'Application',
736
- },
737
- },
738
- {
739
- 'term': {
740
- 'store_id.keyword': req.body.storeId,
741
- },
742
- },
743
- {
744
- 'term': {
745
- 'store_date.keyword': dayjs( inputDate ).format( 'DD-MM-YYYY' ),
746
- },
747
- },
748
- {
749
- 'term': {
750
- 'log_subtype.keyword': 'Anti_Virus',
751
- },
752
- },
753
- ],
754
-
755
- },
756
- },
757
- 'sort': [
758
- { 'timestamp': { 'order': 'desc' } },
759
- ],
760
- },
761
- );
762
- response.antiVirus = antiVirus.body.hits.hits.length > 0 && antiVirus.body.hits.hits[0]._source.data.message == 'st-launch-1.0.exe is deleted' ? antiVirus.body.hits.hits[0]._source.data.occuringTime : '';
763
-
646
+ if ( antiVirusResult.body.hits.hits && antiVirusResult.body.hits.hits.length > 0 ) {
647
+ const antivirusEvent = antiVirusResult.body.hits.hits.find( ( sourceData ) =>
648
+ Number( sourceData._source.data.occuringTime.split( ':' )[0] ) === Number( fromTime.split( ':' )[0] ) &&
649
+ sourceData._source.data.message === 'st-launch-1.0.exe is deleted',
650
+ );
651
+ response.antiVirus = antivirusEvent ? antivirusEvent._source.data.occuringTime : '';
652
+ }
764
653
 
765
654
  res.sendSuccess( response );
766
655
  } catch ( error ) {
@@ -769,6 +658,7 @@ export async function viewedgeAppLog( req, res ) {
769
658
  }
770
659
  }
771
660
 
661
+
772
662
  export async function cameraAngleChange( req, res ) {
773
663
  try {
774
664
  const angleChange = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).cameraAngleChange,
@@ -849,27 +739,30 @@ export async function datewiseDowntime( req, res ) {
849
739
  try {
850
740
  const store = await findOneStore( { storeId: req.body.storeId } );
851
741
  if ( !store ) {
852
- return res.sendError( 'Stores Not fond', 204 );
742
+ return res.sendError( 'Store Not Found', 204 );
853
743
  }
854
- let startHour = store.storeProfile.open.split( ':' )[0];
855
- let endHour = store.storeProfile.close.split( ':' )[0];
856
-
857
- let datesArray = getDatesArray( req.body.fromDate, req.body.toDate );
858
- let paginatedDates = paginateArray( datesArray, req.body.offset, req.body.limit );
859
- let result = [];
860
- for ( const singleDate of paginatedDates ) {
861
- let inputData = {
744
+
745
+ const startHour = parseInt( store.storeProfile.open.split( ':' )[0], 10 );
746
+ const endHour = parseInt( store.storeProfile.close.split( ':' )[0], 10 );
747
+
748
+ const datesArray = getDatesArray( req.body.fromDate, req.body.toDate );
749
+ const paginatedDates = paginateArray( datesArray, req.body.offset, req.body.limit );
750
+
751
+ const resultPromises = paginatedDates.map( ( singleDate ) => {
752
+ const inputData = {
862
753
  start: startHour,
863
754
  end: endHour,
864
755
  interval: 60,
865
756
  };
866
757
  req.body.Date = singleDate;
867
- let storedata = await livecountCheck( inputData, req );
868
- result.push( storedata[0] );
869
- }
758
+ return livecountCheck( inputData, singleDate, req );
759
+ } );
760
+
761
+ const result = await Promise.all( resultPromises );
762
+
870
763
  res.sendSuccess( {
871
764
  count: datesArray.length,
872
- data: result,
765
+ data: result.flat(), // Flatten the array of arrays
873
766
  } );
874
767
  } catch ( error ) {
875
768
  logger.error( { error: error, function: 'datewiseDowntime' } );
@@ -880,55 +773,47 @@ export async function streamwiseDowntime( req, res ) {
880
773
  try {
881
774
  const store = await findOneStore( { storeId: req.body.storeId } );
882
775
  if ( !store ) {
883
- return res.sendError( 'Stores Not fond', 204 );
776
+ return res.sendError( 'Store not found', 204 );
884
777
  }
885
- let startHour = store.storeProfile.open.split( ':' )[0];
886
- let endHour = store.storeProfile.close.split( ':' )[0];
778
+
779
+ const startHour = store.storeProfile.open.split( ':' )[0];
780
+ const endHour = store.storeProfile.close.split( ':' )[0];
887
781
  const interval = 60; // 1 hour in minutes
888
782
  const TimeSlots = generateTimeSlots( startHour, endHour, interval, req );
889
- let timewise = [];
890
- for ( const obj of TimeSlots ) {
891
- obj.startTime = dayjs( obj.from ).format( 'hh:mm A' );
892
- obj.endTime = dayjs( obj.to ).format( 'hh:mm A' );
893
- let downTimeQuery = {
894
- 'size': 1,
895
- 'query': {
896
- 'bool': {
897
- 'must': [
898
- {
899
- 'term': {
900
- 'doc.date.keyword': dayjs( obj.from ).format( 'DD-MM-YYYY' ),
901
- },
902
- },
903
- {
904
- 'term': {
905
- 'doc.store_id.keyword': req.body.storeId,
906
- },
907
- },
908
- {
909
- 'term': {
910
- 'doc.hour.keyword': obj.hour,
911
- },
912
- },
913
- ],
914
783
 
784
+ const parsedOpenSearch = JSON.parse( process.env.OPENSEARCH );
785
+ const downTimeIndex = parsedOpenSearch.downTimeHourly;
786
+
787
+ const batchRequests = TimeSlots.map( async ( obj ) => {
788
+ const formattedDate = dayjs( obj.from ).format( 'DD-MM-YYYY' );
789
+ const downTimeQuery = {
790
+ size: 1,
791
+ query: {
792
+ bool: {
793
+ must: [
794
+ { term: { 'doc.date.keyword': formattedDate } },
795
+ { term: { 'doc.store_id.keyword': req.body.storeId } },
796
+ { term: { 'doc.hour.keyword': obj.hour } },
797
+ ],
915
798
  },
916
799
  },
917
800
  };
918
- const downtime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).downTimeHourly, downTimeQuery );
919
801
 
920
- let streamwiseDowntime = downtime.body.hits.hits.length > 0 ? downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
921
- if ( streamwiseDowntime.length > 0 ) {
922
- for ( let stream of streamwiseDowntime ) {
923
- if ( stream.stream === req.body.stream ) {
924
- obj.downTime= stream.down_time;
925
- }
926
- }
927
- } else {
928
- obj.downTime= '';
929
- }
930
- timewise.push( obj );
931
- }
802
+ const downtime = await getOpenSearchData( downTimeIndex, downTimeQuery );
803
+ const streamwiseDowntime = downtime.body.hits.hits.length > 0 ?
804
+ downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
805
+
806
+ const foundStream = streamwiseDowntime.find( ( stream ) => stream.stream === req.body.stream );
807
+ return {
808
+ startTime: dayjs( obj.from ).format( 'hh:mm A' ),
809
+ endTime: dayjs( obj.to ).format( 'hh:mm A' ),
810
+ downTime: foundStream ? foundStream.down_time : '',
811
+ hour: obj.hour,
812
+
813
+ };
814
+ } );
815
+
816
+ const timewise = await Promise.all( batchRequests );
932
817
  res.sendSuccess( timewise );
933
818
  } catch ( error ) {
934
819
  logger.error( { error: error, function: 'streamwiseDowntime' } );
@@ -936,73 +821,61 @@ export async function streamwiseDowntime( req, res ) {
936
821
  }
937
822
  }
938
823
 
939
- export async function livecountCheck( inputData, req ) {
940
- return new Promise( async ( Resolve, Reject ) => {
941
- try {
942
- let TimeSlots = generateTimeSlots( inputData.start, inputData.end, inputData.interval, req );
943
- let timewise = [];
944
- for ( const obj of TimeSlots ) {
945
- obj.startTime = dayjs( obj.from ).format( 'hh:mm A' );
946
- obj.endTime = dayjs( obj.to ).format( 'hh:mm A' );
947
- let downTimeQuery = {
948
- 'size': 1,
949
- 'query': {
950
- 'bool': {
951
- 'must': [
952
- {
953
- 'term': {
954
- 'doc.date.keyword': dayjs( obj.from ).format( 'DD-MM-YYYY' ),
955
- },
956
- },
957
- {
958
- 'term': {
959
- 'doc.store_id.keyword': req.body.storeId,
960
- },
961
- },
962
- {
963
- 'terms': {
964
- 'doc.hour.keyword': [ obj.hour ],
965
- },
966
- },
967
- ],
968
824
 
969
- },
825
+ export async function livecountCheck( inputData, singleDate, req ) {
826
+ try {
827
+ const TimeSlots = generateTimeSlots( inputData.start, inputData.end, inputData.interval, req );
828
+
829
+ const queryPromises = TimeSlots.map( async ( obj ) => {
830
+ obj.startTime = dayjs( obj.from ).format( 'hh:mm A' );
831
+ obj.endTime = dayjs( obj.to ).format( 'hh:mm A' );
832
+
833
+ const downTimeQuery = {
834
+ size: 1,
835
+ query: {
836
+ bool: {
837
+ must: [
838
+ { term: { 'doc.date.keyword': dayjs( obj.from ).format( 'DD-MM-YYYY' ) } },
839
+ { term: { 'doc.store_id.keyword': req.body.storeId } },
840
+ { terms: { 'doc.hour.keyword': [ obj.hour ] } },
841
+ ],
970
842
  },
971
- };
972
- const downtime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).downTimeHourly, downTimeQuery );
973
- let streamwiseDowntime = downtime.body.hits.hits.length > 0 ? downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
974
- if ( streamwiseDowntime.length > 0 ) {
975
- const sum = streamwiseDowntime.reduce( ( accumulator, currentValue ) => {
976
- return accumulator + currentValue.down_time;
977
- }, 0 );
978
- const average = sum / streamwiseDowntime.length;
979
- obj[obj.startTime + '-' + obj.endTime] = Math.round( average );
980
- } else {
981
- obj[obj.startTime + '-' + obj.endTime] = '';
982
- }
843
+ },
844
+ };
983
845
 
984
- timewise.push( obj );
846
+ const downtime = await getOpenSearchData( JSON.parse( process.env.OPENSEARCH ).downTimeHourly, downTimeQuery );
847
+ const streamwiseDowntime = downtime.body.hits.hits.length > 0 ? downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
848
+ if ( streamwiseDowntime.length > 0 ) {
849
+ const sum = streamwiseDowntime.reduce( ( accumulator, currentValue ) => accumulator + currentValue.down_time, 0 );
850
+ const average = sum / streamwiseDowntime.length;
851
+ obj[obj.startTime + '-' + obj.endTime] = Math.round( average );
852
+ } else {
853
+ obj[obj.startTime + '-' + obj.endTime] = '';
985
854
  }
986
- const mergedData = {
987
- Date: dayjs( req.body.Date ).format( 'YYYY-MM-DD' ),
988
- };
989
- timewise.forEach( ( obj ) => {
990
- for ( const key in obj ) {
991
- if ( key !== 'hour' && key !== 'from' && key !== 'to' && key !== 'startTime' && key !== 'endTime' && key !== 'clientId' ) {
992
- if ( mergedData[key] ) {
993
- mergedData[key] += obj[key];
994
- } else {
995
- mergedData[key] = obj[key];
996
- }
855
+ return obj;
856
+ } );
857
+
858
+ const timewise = await Promise.all( queryPromises );
859
+
860
+
861
+ const mergedData = {
862
+ Date: dayjs( singleDate ).format( 'YYYY-MM-DD' ),
863
+ };
864
+
865
+ timewise.forEach( ( obj ) => {
866
+ for ( const key in obj ) {
867
+ if ( key !== 'hour' && key !== 'from' && key !== 'to' && key !== 'startTime' && key !== 'endTime' && key !== 'clientId' ) {
868
+ if ( mergedData[key] ) {
869
+ mergedData[key] += obj[key];
870
+ } else {
871
+ mergedData[key] = obj[key];
997
872
  }
998
873
  }
999
- } );
874
+ }
875
+ } );
1000
876
 
1001
- // Create an array with the merged data.
1002
- const mergedArray = [ mergedData ];
1003
- Resolve( mergedArray );
1004
- } catch ( err ) {
1005
- Reject( err );
1006
- }
1007
- } );
877
+ return [ mergedData ];
878
+ } catch ( err ) {
879
+ throw err;
880
+ }
1008
881
  }