Advertisement
Guest User

Untitled

a guest
May 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. const mysql = require( 'mysql' ),
  2. moment = require( 'moment' ),
  3. csvWriter = require( 'csv-write-stream' ),
  4. fs = require( 'fs' ),
  5. async = require( 'async' );
  6.  
  7. const query = "SELECT streammetrics.CallQuality AS WorstQuality, streammetrics.CallGUID, streammetrics.PairID, streammetrics.StartTime, streammetrics.EndTime, streammetrics.Codec, streammetrics.SourceSiteName, streammetrics.DestSiteName, streammetrics.SourceSwitchName, streammetrics.DestSwitchName, streammetrics.SourceEndpointID, streammetrics.SourceEndpointName, streammetrics.DestEndpointID, streammetrics.DestEndpointName, streammetrics.SourceIPAddress, streammetrics.DestIPAddress FROM streammetrics WHERE streammetrics.EndTime > 0 && streammetrics.StartTime BETWEEN UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY) AND UNIX_TIMESTAMP(CURDATE())";
  8.  
  9. let goodCalls = [],
  10. badCalls = [],
  11. totalCalls = [];
  12.  
  13. const badCallThreshold = 6;
  14.  
  15. const connection = mysql.createConnection( {
  16. host: '10.20.28.26',
  17. port: '4310',
  18. user: 'admin',
  19. password: 'changeme',
  20. database: 'shorewaremonitoring'
  21. } );
  22.  
  23. // Begin async execution
  24. async.waterfall( [
  25. getCallData,
  26. isCallGoodOrBad,
  27. writeToFile,
  28. killProc
  29. ], function( err, result ) {
  30. if ( err ) {
  31. console.log( err );
  32. } else {
  33. // console.log(result);
  34. }
  35. } );
  36.  
  37. // Define functions
  38. function getCallData( callback ) {
  39.  
  40. connection.query( query, function( error, calls, fields ) {
  41. if ( error ) {
  42. console.log( error );
  43. }
  44.  
  45. callback( null, calls );
  46.  
  47. } );
  48.  
  49. }
  50.  
  51. function isCallGoodOrBad( calls, callback ) {
  52.  
  53. calls.map( function( obj ) {
  54.  
  55. if ( obj.WorstQuality < badCallThreshold ) {
  56. badCalls.push( obj );
  57. totalCalls.push( obj );
  58. } else {
  59. goodCalls.push( obj );
  60. totalCalls.push( obj );
  61. }
  62.  
  63. } );
  64.  
  65. callback( null );
  66.  
  67. }
  68.  
  69. function writeToFile( callback ) {
  70.  
  71. var filename = moment.unix( goodCalls[ 0 ].StartTime ).format( 'MM_DD_YY' );
  72.  
  73. // Write the results to a .csv
  74. var writer = csvWriter( {
  75. headers: [ "Date", "Bad Calls", "Good Calls", "Total Calls", "% Good" ]
  76. } );
  77.  
  78. writer.pipe( fs.createWriteStream( 'dailyReport_' + filename + '.csv' ) );
  79.  
  80. let report = {
  81. "Date": JSON.stringify( moment.unix( goodCalls[ 0 ].StartTime ).format( "L" ) ),
  82. "Bad Calls": badCalls.length,
  83. "Good Calls": goodCalls.length,
  84. "Total Calls": totalCalls.length,
  85. "% Good": Math.abs( ( ( badCalls.length / goodCalls.length ) * 100 ) - 100 ).toFixed( 4 ).toString(),
  86. }
  87.  
  88. writer.write( report );
  89. writer.end();
  90.  
  91. callback( null );
  92.  
  93. }
  94.  
  95. function killProc() {
  96. setTimeout( function() {
  97. return process.exit();
  98. }, 3000 );
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement