Skip to content

Instantly share code, notes, and snippets.

@CiscoKidxx
Last active May 15, 2017 19:09
Show Gist options
  • Save CiscoKidxx/13258c2d4e6995794a75fff285a08d88 to your computer and use it in GitHub Desktop.
Save CiscoKidxx/13258c2d4e6995794a75fff285a08d88 to your computer and use it in GitHub Desktop.
const mysql = require( 'mysql' ),
moment = require( 'moment' ),
csvWriter = require( 'csv-write-stream' ),
fs = require( 'fs' ),
async = require( 'async' );
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())";
let goodCalls = [],
badCalls = [],
totalCalls = [];
const badCallThreshold = 6;
const connection = mysql.createConnection( {
host: '10.20.28.26',
port: '4310',
user: 'admin',
password: 'changeme',
database: 'shorewaremonitoring'
} );
// Begin async execution
async.waterfall( [
getCallData,
isCallGoodOrBad,
writeToFile,
killProc
], function( err, result ) {
if ( err ) {
console.log( err );
} else {
// console.log(result);
}
} );
// Define functions
function getCallData( callback ) {
connection.query( query, function( error, calls, fields ) {
if ( error ) {
console.log( error );
}
callback( null, calls );
} );
}
function isCallGoodOrBad( calls, callback ) {
calls.map( function( obj ) {
if ( obj.WorstQuality < badCallThreshold ) {
badCalls.push( obj );
totalCalls.push( obj );
} else {
goodCalls.push( obj );
totalCalls.push( obj );
}
} );
callback( null );
}
function writeToFile( callback ) {
var filename = moment.unix( goodCalls[ 0 ].StartTime ).format( 'MM_DD_YY' );
// Write the results to a .csv
var writer = csvWriter( {
headers: [ "Date", "Bad Calls", "Good Calls", "Total Calls", "% Good" ]
} );
writer.pipe( fs.createWriteStream( 'dailyReport_' + filename + '.csv' ) );
let report = {
"Date": JSON.stringify( moment.unix( goodCalls[ 0 ].StartTime ).format( "L" ) ),
"Bad Calls": badCalls.length,
"Good Calls": goodCalls.length,
"Total Calls": totalCalls.length,
"% Good": Math.abs( ( ( badCalls.length / goodCalls.length ) * 100 ) - 100 ).toFixed( 4 ).toString(),
}
writer.write( report );
writer.end();
callback( null );
}
function killProc() {
setTimeout( function() {
return process.exit();
}, 3000 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment