alan-turing-institute / azure_usage_v1

Web app for the Turing research community to analyse Azure usage at a subscription level.
MIT License
3 stars 0 forks source link

Gain access to usage data as csv #22

Open DavidBeavan opened 3 years ago

DavidBeavan commented 3 years ago

This is mainly academic, as @OscartGiles kindly gave me a data dump...

It would be useful to get access to the raw cost data i.e. date and spend that underpins the top chart. My particular use case is to use it for forecasting spend.

DavidBeavan commented 3 years ago

With the azure web gui up, side-load the following code by running it in the JavaScript console and out pops a csv. Tested on Firefox, and intended as hack quality.

Posting here for interested parties:

// Download the Azure usage costs per day
// Liberates the data underpinning the top chart
// Run in console and save

// Find all Bokeh models
var founddata = [];
var all_models = Bokeh.documents[0]._all_models;

// Capture those with data
for (let id of all_models.keys()) {
  let modelinstance = Bokeh.documents[0].get_model_by_id(id);
    if (typeof(modelinstance.data) !== 'undefined') {
      founddata.push(modelinstance.data);
    }
}

// The usage data is the first in the array
var bdata = founddata[0]

// Restructure data
var data = [
  bdata['Date_str'],
  bdata['Cost']
];

// Add headers
var csvContent = 'Date,Cost' + '\n';

// Save each data and cost pair
for(let i = 0; i < data[0].length; i++){
    csvContent += data[0][i] + ',' + data[1][i] + '\n';
}

// The download function takes a CSV string, the filename and mimeType as parameters
// Scroll/look down at the bottom of this snippet to see how download is called
var download = function(content, fileName, mimeType) {
  var a = document.createElement('a');
  mimeType = mimeType || 'application/octet-stream';

  if (navigator.msSaveBlob) { // IE10
    navigator.msSaveBlob(new Blob([content], {
      type: mimeType
    }), fileName);
  } else if (URL && 'download' in a) { //html5 A[download]
    a.href = URL.createObjectURL(new Blob([content], {
      type: mimeType
    }));
    a.setAttribute('download', fileName);
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  } else {
    location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
  }
}

download(csvContent, 'azure_usage.csv', 'text/csv;encoding:utf-8');