// ==UserScript== // @name AutoPerfReports // @namespace http://tampermonkey.net/ // @version 0.1 // @description Automatically pull data out of Riverbed for the previous day. // @author joe.tretter@kcc.com // @match https://ttw.gws.kcc.com/* // @grant window.close // ==/UserScript== (function() { 'use strict'; console.log("Start"); let today = new Date(); let simulateDate = new Date(); // simulation is using yesterday's date. simulateDate.setDate(today.getDate() - 1); simulateDate = new Date(simulateDate.getFullYear(), simulateDate.getMonth(), simulateDate.getDate()); let timestamp = simulateDate.getTime() / 1000; let rivUrl = "https://ttw.gws.kcc.com/#search:time=" + timestamp + "+1435&search=Hello&server=ustca089.kcc.com&serverTableKey=ustca089.kcc.com"; window.location.href = rivUrl; // no cleartext password function encryptXor(text, key) { return Array.from( text, (c, i) => String.fromCharCode(c.charCodeAt() ^ key.charCodeAt(i % key.length)) ).join(''); } // if the login dialog pops up -> log in. function loginThread(credentials) { let myInterval; myInterval= window.setInterval(()=>{ if (document.getElementById("usernameField") !== null) { clearInterval(myInterval); document.getElementById("usernameField").value=credentials.username; document.getElementById("passwordField").value=encryptXor(credentials.passwordCrypt, '5346245634634765377'); document.getElementById("loginSubmitBtn").click(); } },1000); } // I have not found a reliable way to see if the page is ready than polling... function execQuery(theQuery) { let myInterval; return new Promise((resolve, reject) => { myInterval = window.setInterval(() => { if (document.getElementsByClassName("gwt-SuggestBox EKM0XQC-tb-f").length === 1) { clearInterval(myInterval); // even if the screen is loaded, if we are too fast it will be too fast for the javascript of the page... window.setTimeout(function(){ console.log("ExecQuery ",theQuery); let theSearchField = document.getElementsByClassName("gwt-SuggestBox EKM0XQC-tb-f")[0]; theSearchField.value = theQuery; let theSearchButton = document.getElementsByClassName("gwt-Button EKM0XQC-e-d")[0]; theSearchButton.click(); resolve(); },5000); } else { console.log("Waiting for Query field",theQuery); } }, 1000); }); }; // I have not found a reliable way to see if the data is ready than polling... function downloadResultsCsv(theName) { let myInterval; return new Promise((resolve, reject) => { myInterval = window.setInterval(() => { let pTag = document.getElementsByTagName("P"); if (pTag.length > 0) { console.log("Ready", theName, pTag); clearInterval(myInterval); let theCsvUrl = pTag[0].firstChild.href; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { console.log("Length of response ",this.responseText.length); // to be able to give the file a custom name I load it into a blob first, otherwise it will always save with the generic name. let blob = new Blob([this.responseText], { type: 'text/plain' }); let anchor = document.createElement('a'); anchor.download = theName + "-" + simulateDate.getFullYear() + "-" + (1+simulateDate.getMonth()) + "-" + simulateDate.getDate() + ".csv"; anchor.href = window.URL.createObjectURL(blob); anchor.dataset.downloadurl = ['text/plain', anchor.download, anchor.href].join(':'); anchor.click(); resolve(); } }; xhttp.open("GET", theCsvUrl, true); xhttp.send(); } else { console.log("Waiting for Results",theName); } }, 5000); }); } let allQueriesAndNames = [ { name: 'PrmMainBo.Save', query: " ! ( class.method = 'Cas.BusinessLogic.Clc.ClcCalculationEngineBo.Activate' ) and ! ( class.method = 'Cas.BusinessLogic.Prm.PrmMainBo.Copy' ) and ( class.method = 'Cas.BusinessLogic.Prm.PrmMainBo.Save' ) and ( instance = 'ACNCASWebSite#LAOProdServer' ) | calls -type class.method -class.method Cas.BusinessLogic.Prm.PrmMainBo.Save -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'PayMainBo.Save', query: " class.method = 'Cas.BusinessLogic.Pay.PayMainBo.Save' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Pay.PayMainBo.Save -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'ClcCalculationBo.Calculation', query: "class.method = 'Cas.BusinessLogic.Clc.ClcCalculationEngineBo.Calculation' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Clc.ClcCalculationEngineBo.Calculation -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'PrmTacticView.GetView', query: "class.method = 'Cas.BusinessLogic.Prm.PrmTacticView.GetView' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Prm.PrmTacticView.GetView -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'PrmTacticView.AddViewItem', query: "class.method = 'Cas.BusinessLogic.Prm.PrmTacticView.AddViewItem' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Prm.PrmTacticView.AddViewItem -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'PrmProductAllView.GetView', query: "class.method = 'Cas.BusinessLogic.Prm.PrmProductAllView.GetView' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Prm.PrmProductAllView.GetView -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'PrmProductAllView.AddViewItem', query: "class.method = 'Cas.BusinessLogic.Prm.PrmProductAllView.AddViewItem' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Prm.PrmProductAllView.AddViewItem -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" }, { name: 'BpaMyPaymentMassCreationView.GetView', query: "class.method = 'Cas.BusinessLogic.Bpa.BpaMyPaymentMassCreationView.GetView' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Bpa.BpaMyPaymentMassCreationView.GetView -include_call_count -visualization transactions -limit 10000 -sort_by total_duration" } ]; // Password is encrypted with XOR to preent "accidental" revelation let credentials={username:"U15020",passwordCrypt:"wYTFFUX"}; loginThread(credentials); let allDone = new Promise((allResolve,allReject)=>{ // Loop over all queries one by one considering the promise resolution: have to execute them in sequence! for (let i=0,p=Promise.resolve();i new Promise(resolve => {let theQueryAndName=allQueriesAndNames[i]; console.log("Executing", theQueryAndName); execQuery(theQueryAndName.query).then(() => { downloadResultsCsv(theQueryAndName.name).then(() => { console.log("Done ", theQueryAndName,i,allQueriesAndNames.length) resolve(); if (i===(allQueriesAndNames.length-1)) { console.log("All Done."); allResolve(); } }); }) })) };} ); // if we are all finished then we can close the window. allDone.then(()=>{ console.log("Closing window"); // this doesn't work, Chrome doesn't allow me to close the last window, worked around that by killing the task in the scheduling... window.close(); }); })();