Files
RiverRobot/RivBedAuto.tempermonkey - 2018-12-27.js
2019-01-04 20:02:57 -06:00

207 lines
10 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ==UserScript==
// @name AutoPerfReports
// @namespace http://tampermonkey.net/
// @version 0.2
// @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);
// To overwrite the simulate date uncomment the below and change the date (remember that the month starts at 0!
//simulateDate = new Date(2018,11,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 the login dialog comes up again in less than 10 seconds we assume the password changed and don't retry automatically to prevent account locking.
if ((localStorage.getItem("LastLoginDate") !== null) && ((new Date().getTime() - localStorage.getItem("LastLoginDate")) / 1000 < 10 )) {
clearInterval(myInterval);
window.document.title="[RiverRobot] Login unsuccessful.";
console.log("Login unsuccessful.");
} else {
window.document.title="[RiverRobot] Logging In";
localStorage.setItem("LastLoginDate",new Date().getTime())
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 other 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(){
window.document.title="[RiverRobot] Executing Query " + theQuery;
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 {
window.document.title="[RiverRobot] Waiting for Query field " + theQuery;
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 clsErrorEle = document.getElementsByClassName("pluginErrorStringBlack");
if (clsErrorEle.length > 0) {
clearInterval(myInterval);
let errText=clsErrorEle[0].innerText ;
console.log("While waiting for results of " + theName + " Page shows error: " + errText + " -> Proceeding to next");
resolve(errText);
}
let pTag = document.getElementsByTagName("P");
if (pTag.length > 0) {
console.log("Ready", theName, pTag);
window.document.title="[RiverRobot] Downloading Results " + theName;
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 {
window.document.title="[RiverRobot] Waiting for Results " + theName;
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: 'ClcCalculationEngineBo.UpdateSummaryForPeriod',
query: "class.method = 'Cas.BusinessLogic.Clc.ClcCalculationEngineBo.UpdateSummaryForPeriod' and instance = 'ACNCASWebSite#LAOProdServer' | calls -type class.method -class.method Cas.BusinessLogic.Clc.ClcCalculationEngineBo.UpdateSummaryForPeriod -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<allQueriesAndNames.length; i++){
p = p.then(_ => 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(()=>{
window.document.title="[RiverRobot] DONE";
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();
});
})();