-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
1 lines (1 loc) · 11.4 KB
/
main.js
1
function e(){$=[],U=[],clearInterval(Y),Y=null}function n(n,s,r,a){if(null!=n&&null!=s){if(G=a,w(),e(),-1!=a.indexOf(q))for(var i=0;i<Z.length;i++)o(Z[i],n,s,r,!1),o(Z[i],n,s,r,!0);else o(G,n,s,r,!1),o(G,n,s,r,!0);Y=setInterval(t,1e3)}}function t(){-1!=G.indexOf(q)?$.length==Z.length&&U.length==Z.length&&(clearInterval(Y),g(r($,U))):$.length>0&&U.length>0&&(clearInterval(Y),g(r($,U)))}function s(e){for(var n=e.length,t=[],s=0;n>s;s++)Array.prototype.push.apply(t,e[s].data);return t}function r(e,n){for(var t=s(e),r=s(n),o=r.length,a=[],i=0,c={},l=[],u=0;o>u;u++){var d=JSON.parse(JSON.stringify(r[u])),f=d.type+d.az+d.windows+d.vpc;if(!l.contains(f)){void 0===c[f]&&(c[f]=i,l.push(f),a[i]=JSON.parse(JSON.stringify(d)),i++);for(var v=u+1;o>v;v++){var p=JSON.parse(JSON.stringify(r[v]));d.type===p.type&&d.az===p.az&&d.windows===p.windows&&d.vpc===p.vpc&&u!==v&&p!==d&&(a[c[f]].count+=p.count,Array.prototype.push.apply(a[c[f]].resIds,p.resIds))}}}for(var g=t.length,h=0;g>h;h++){for(var m=a.length,w=t[h],y=!1,I=0;m>I;I++){null==a[I].running&&(a[I].running=0,a[I].running_ids=[],a[I].running_names=[],a[I].diff=a[I].count);var b=a[I];if(w.type==b.type&&w.az==b.az&&w.windows==b.windows&&w.vpc==b.vpc){a[I].running+=1,a[I].diff-=1,a[I].running_ids.push(w.id),void 0!=w.name&&null!=w.name&&""!=w.name&&a[I].running_names.push(w.name),y=!0;break}}if(y===!1){var E={};E.running=1,E.running_ids=[w.id],E.running_names=[],void 0!=w.name&&null!=w.name&&""!=w.name&&E.running_names.push(w.name),E.diff=-1,E.resIds=[],E.type=w.type,E.count=0,E.az=w.az,E.cost=0,E.windows=w.windows,E.vpc=w.vpc,a.push(E)}}return a}function o(e,n,t,s,r){var o=new AWS.EC2({accessKeyId:n,secretAccessKey:t,sessionToken:s,region:e,maxRetries:5,sslEnabled:!0});if(r){var i={Filters:[{Name:"state",Values:["active"]}]};o.describeReservedInstances(i,function(n,t){a(n,t,e,r)})}else{var i={Filters:[{Name:"instance-state-name",Values:["running"]}],MaxResults:1e3};o.describeInstances(i,function(n,t){a(n,t,e,r)})}}function a(e,n,t,s){e?(console.log(e,e.stack),y("<b><font color='red'>The following error has occured: "+e+"; see the javascript console for more details.</font></b>")):i(n,t,s)}function i(e,n,t){t?U.push({region:n,data:c(e)}):$.push({region:n,data:l(e)})}function c(e){var n=[];if(!e||!e.ReservedInstances||0==e.ReservedInstances.length)return n;for(var t=e.ReservedInstances.length,s=0;t>s;s++)n[s]={},n[s].resIds=[e.ReservedInstances[s].ReservedInstancesId],n[s].type=e.ReservedInstances[s].InstanceType,n[s].count=e.ReservedInstances[s].InstanceCount,n[s].az=e.ReservedInstances[s].AvailabilityZone,e.ReservedInstances[s].RecurringCharges[0]&&(n[s].cost=e.ReservedInstances[s].RecurringCharges[0].Amount),-1!=e.ReservedInstances[s].ProductDescription.toLowerCase().indexOf("windows")?n[s].windows=!0:n[s].windows=!1,-1!=e.ReservedInstances[s].ProductDescription.toLowerCase().indexOf("vpc")?n[s].vpc=!0:n[s].vpc=!1;return n}function l(e){var n=[];if(!e||!e.Reservations||0==e.Reservations.length)return n;for(var t=e.Reservations.length,s=0,r=0;t>r;r++)for(var o=0;o<e.Reservations[r].Instances.length;o++){var a=e.Reservations[r].Instances[o].Tags,i="";if(a.length>1)for(var c=a.length,l=0;c>l;l++)try{if(null!=a[l].Key)a[l].Key.toLowerCase()=="aws:autoscaling:groupName".toLowerCase()&&""==i&&(i=a[l].Value),"name"==a[l].Key.toLowerCase()&&(i=a[l].Value);else try{console.log("Tags["+l+"] empty: "+a.toString())}catch(e){console.log("Exception Id 00x1")}}catch(e){console.log("Exception Id 00x3")}else if(1==a.length)i=a[0].Value;else try{console.log("No name tag found for instance with id: "+e.Reservations[r].Instances[o].InstanceId)}catch(e){console.log("Exception Id 00x2")}n[s]={},n[s].name=i,n[s].id=e.Reservations[r].Instances[o].InstanceId,n[s].type=e.Reservations[r].Instances[o].InstanceType,n[s].az=e.Reservations[r].Instances[o].Placement.AvailabilityZone;try{void 0!=e.Reservations[r].Instances[o].Platform&&null!=e.Reservations[r].Instances[0].Platform&&"windows"==e.Reservations[r].Instances[o].Platform.toLowerCase()?n[s].windows=!0:n[s].windows=!1}catch(e){console.log("Exception Id 00x4")}null!=e.Reservations[r].Instances[o].VpcId&&""!=e.Reservations[r].Instances[o].VpcId?n[s].vpc=!0:n[s].vpc=!1,s++}return n}function u(e,n,t,s){d(),"all"==s.toLowerCase()&&(s="us-east-1"),v();var r=new AWS.EC2({accessKeyId:e,secretAccessKey:n,sessionToken:t,region:s,maxRetries:5,sslEnabled:!0});try{r.describeRegions({},function(e,n){e?(console.log("BAD CREDENTIALS"),console.log(e,e.stack),X=!1,ee=!0):(X=!0,ee=!0)})}catch(e){return console.log("BAD CREDENTIALS"),console.log(e),!1}}function d(){X=!1,ee=!1}function f(){ne=setInterval(p,1e3)}function v(){clearInterval(ne)}function p(){ee&&(clearInterval(p),w(),X?(n(P(),J(),W(),j()),d()):(B(),d()))}function g(e){O().innerHTML="",O().innerHTML+=I(e),x().innerHTML="<b>Total Reservations: "+se+" --- Total Running Instances: "+re+" --- <button id='downloadAsCSV' class='btn btn-primary'> Download Data as CSV</button><br><hr>",new Tablesort(document.getElementById("resCoTable")),document.getElementById("differentialHeader").click(),T(),h(),F()}function h(){document.getElementById("downloadAsCSV").addEventListener("click",function(){D()})}function m(){for(var e="<form class='form-inline' id='zoneForm'>",n=0;n<oe.length;n++)e+="<div class='checkbox'><label><input type='checkbox' name='"+oe[n]+"'>"+oe[n]+"</input></label></div> ";return e+="<button id='zoneSelectButton' type='submit' class='btn btn-success'>Filter</button></form>"}function w(){M(),O().innerHTML="",x().innerHTML="",se=0,re=0}function y(e){O().innerHTML=e}function I(e){for(var n="",t=e.length,s=0;t>s;s++)n+="<tr><th scope='row'>"+e[s].count+"</th><td>"+e[s].running+"</th><td>"+e[s].diff+"</th><td>"+e[s].type+"</td><td>"+e[s].az+"</td><td>"+e[s].windows.toString()+"</td><td>"+e[s].vpc.toString()+"</td><td>"+e[s].running_ids.join(",<br/>")+"</td><td>"+e[s].running_names.join(",<br/>")+"</td></tr>",se+=e[s].count,re+=e[s].running,oe.contains(e[s].az)||oe.push(e[s].az);return n}function b(){te=setInterval(E,1e3)}function E(){-1==L().innerHTML.toLowerCase().indexOf("please wait.....")?L().innerHTML+=".":L().innerHTML="Please Wait"}function x(){return document.getElementById("totalData")}function L(){return document.getElementById("pleaseWait")}function R(){b(),L().style.display="block"}function T(){C(),L().style.display="none"}function C(){clearInterval(te)}function A(){return document.getElementById("errorCredentials")}function B(){T(),A().style.display="block"}function S(){A().style.display="none"}function k(){return document.getElementById("errorAccessSecretKey")}function N(){T(),k().style.display="block"}function H(){k().style.display="none"}function O(){return document.getElementById("ec2DataTableBody")}function z(){return document.getElementById("awsQueryResults")}function F(){z().style.display="block"}function M(){z().style.display="none"}function D(){for(var e,n=document.getElementById("resCoTable"),t="data:text/csv;charset=utf-8,",s=0;e=n.rows[s];s++){for(var r,o=0;r=e.cells[o];o++){o>0&&(t+=",");var a=r.innerHTML.toString().trim();(a.indexOf(",")>=0||a.indexOf('"')>=0||a.indexOf("\n")>=0)&&(a='"'+a.replace(/\"/g,'""')+'"'),a.indexOf("<br>")>=0&&(a=a.replace(/<br>/g,"")),t+=a}t+="\n"}var i=document.createElement("a");i.setAttribute("href",encodeURI(t)),i.setAttribute("download",_()),i.click()}function _(){var e="awsresco-export_",n=new Date;return e+=n.getFullYear()+"-"+(n.getMonth()+1)+"-"+n.getDate()+"_"+n.getHours()+":"+n.getMinutes()+":"+n.getSeconds()}function K(){document.getElementById("awsQueryButton").addEventListener("click",function(){Q()})}function V(e){return void 0==e||null==e||""==e?(N(),null):e}function P(){return V(document.getElementById("awsAccessKey").value)}function J(){return V(document.getElementById("awsSecretKey").value)}function W(){return document.getElementById("awsToken").value}function j(){return document.getElementById("regionSelect").value}function Q(){return d(),R(),H(),S(),u(P(),J(),W(),j()),f(),!1}var Z=["us-east-1","us-west-1","us-west-2","eu-west-1","eu-central-1","ap-southeast-1","ap-southeast-2","ap-northeast-1","sa-east-1"],$=[],U=[],Y=null,q="ALL",G="ALL",X=!1,ee=!1,ne=null,te=null,se=0,re=0,oe=[];String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")}),window.onload=function(){K()},Array.prototype.contains=function(e){for(var n=this.length;n--;)if(this[n]===e)return!0;return!1},!function(){function e(n,t){if(!(this instanceof e))return new e(n,t);if(!n||"TABLE"!==n.tagName)throw new Error("Element must be a table");this.init(n,t||{})}var n=[],t=function(e){var n;return window.CustomEvent&&"function"==typeof window.CustomEvent?n=new CustomEvent(e):(n=document.createEvent("CustomEvent"),n.initCustomEvent(e,!1,!1,void 0)),n},s=function(e){return e.getAttribute("data-sort")||e.textContent||e.innerText||""},r=function(e,n){return e=e.toLowerCase(),n=n.toLowerCase(),e===n?0:n>e?1:-1},o=function(e,n){return function(t,s){var r=e(t.td,s.td);return 0===r?n?s.index-t.index:t.index-s.index:r}};e.extend=function(e,t,s){if("function"!=typeof t||"function"!=typeof s)throw new Error("Pattern and sort must be a function");n.push({name:e,pattern:t,sort:s})},e.prototype={init:function(e,n){var t,s,r,o,a=this;if(a.table=e,a.thead=!1,a.options=n,e.rows&&e.rows.length>0&&(e.tHead&&e.tHead.rows.length>0?(t=e.tHead.rows[e.tHead.rows.length-1],a.thead=!0):t=e.rows[0]),t){var i=function(){a.current&&a.current!==this&&(a.current.classList.remove("sort-up"),a.current.classList.remove("sort-down")),a.current=this,a.sortTable(this)};for(r=0;r<t.cells.length;r++)o=t.cells[r],o.classList.contains("no-sort")||(o.classList.add("sort-header"),o.tabindex=0,o.addEventListener("click",i,!1),o.classList.contains("sort-default")&&(s=o));s&&(a.current=s,a.sortTable(s))}},sortTable:function(e,a){var i,c=this,l=e.cellIndex,u=r,d="",f=[],v=c.thead?0:1,p=e.getAttribute("data-sort-method"),g=e.getAttribute("data-sort-order");if(c.table.dispatchEvent(t("beforeSort")),a?i=e.classList.contains("sort-up")?"sort-up":"sort-down":(i=e.classList.contains("sort-up")?"sort-down":e.classList.contains("sort-down")?"sort-up":"asc"===g?"sort-down":"desc"===g?"sort-up":c.options.descending?"sort-up":"sort-down",e.classList.remove("sort-down"===i?"sort-up":"sort-down"),e.classList.add(i)),!(c.table.rows.length<2)){if(!p){for(;f.length<3&&v<c.table.tBodies[0].rows.length;)d=s(c.table.tBodies[0].rows[v].cells[l]),d=d.trim(),d.length>0&&f.push(d),v++;if(!f)return}for(v=0;v<n.length;v++)if(d=n[v],p){if(d.name===p){u=d.sort;break}}else if(f.every(d.pattern)){u=d.sort;break}c.col=l;var h,m=[],w={},y=0,I=0;for(v=0;v<c.table.tBodies.length;v++)for(h=0;h<c.table.tBodies[v].rows.length;h++)d=c.table.tBodies[v].rows[h],d.classList.contains("no-sort")?w[y]=d:m.push({tr:d,td:s(d.cells[c.col]),index:y}),y++;for("sort-down"===i?(m.sort(o(u,!0)),m.reverse()):m.sort(o(u,!1)),v=0;y>v;v++)w[v]?(d=w[v],I++):d=m[v-I].tr,c.table.tBodies[0].appendChild(d);c.table.dispatchEvent(t("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=e:window.Tablesort=e}(),function(){var e=function(e){return e.replace(/[^\-?0-9.]/g,"")},n=function(e,n){return e=parseFloat(e),n=parseFloat(n),e=isNaN(e)?0:e,n=isNaN(n)?0:n,e-n};Tablesort.extend("number",function(e){return e.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/)||e.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/)||e.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/)},function(t,s){return t=e(t),s=e(s),n(s,t)})}();