<template name="BGP Accounting" category="accounting">
  <description>WAN link traffic reports for BGP</description>
  <section name="Link Total by AS">
    <description>Accumulate total bytes to or through each active AS, as seen on the given link.</description>
    <usage></usage>
    <input name="port" label="interface" type="interface" required="true" />
    <input name="interval" type="interval.realtime" value="last60minutes" />
    <input name="where" type="filter.rttraffic" required="false" />
    <script>

var report = new Report();

var filt = "path = " + port;
if(typeof(where) != 'undefined') filt = "(" + where + ") &amp; (" + filt + ")";

// first make the query to get the totals for each as path
var view     = "rttraffic";
var select   = "bgpdestinationaspath,bytes";
var sort     = "bytes";
var query = Query.topN(view, select, filt, interval, sort, null);
var table = query.run();

// now accumulate the total against each individual ASN
var totals = new Array();
for(var i=0; i&lt;table.nrows; i++) {
  var path = table.cell(i, 0);
  var bytes = table.cell(i, 1);
  if(path) {
    var asns = path.split("-");
    for each (var asn in asns) {
       if(!totals[asn]) totals[asn] = 0;
       totals[asn] += bytes;
    }
  }
}  

// create a two-column table to hold the results
var results = Table.create(["AS", "MBytes"],["as","double"]);
results.start = table.start;
results.end = table.end;

// add the rows to the table one at a time
for (var asn in totals) {
    var row = new Array(asn, totals[asn]);
    results.addRow(row);
}

// sort by bytes
results.sort(1, true);

// add another column to represent Mbits/sec
var seconds = (table.end.getTime() - table.start.getTime()) / 1000;
var rates = new Array();
var scale = (8 / (seconds * 1000000));
for(var i=0; i&lt; results.nrows; i++) {
  rates[i] = results.cell(i,1) * scale;
}
results.addColumn("Mbits/sec", "double", rates);

// scale the bytes column to make it just show MBytes instead of bytes
results.scaleColumn(1, (1 / 1000000));

// insert the AS names as another column
var names = (new Network()).asMap(results.column(0));
results.insertColumn("AS Name", "string", names, 1);

//add to report
report.table(results);

    </script>
  </section>
  <section name="Link Total by Source AS">
    <description>Accumulate total bytes from each Source AS, as seen on the given link.</description>
    <usage></usage>
    <input name="port" label="interface" type="interface" required="true" />
    <input name="interval" type="interval.realtime" value="last60minutes" />
    <input name="where" type="filter.rttraffic" required="false" />
    <script>

var report = new Report();

var filt = "path = " + port;
if(typeof(where) != 'undefined') filt = "(" + where + ") &amp; (" + filt + ")";

var view     = "rttraffic";
var select   = "bgpsourceas,bytes";
var sort     = "bytes";
var query = Query.topN(view, select, filt, interval, sort, null);
var table = query.run();

// add another column to represent Mbits/sec
var seconds = (table.end.getTime() - table.start.getTime()) / 1000;
var rates = new Array();
var scale = (8 / (seconds * 1000000));
for(var i=0; i&lt; table.nrows; i++) {
  rates[i] = table.cell(i,1) * scale;
}
table.addColumn("Mbits/sec", "double", rates);

// scale the bytes column to make it just show MBytes instead of bytes
table.scaleColumn(1, (1 / 1000000));

// insert the AS names as another column
var names = (new Network()).asMap(table.column(0));
table.insertColumn("AS Name", "string", names, 1);

//add to report
report.table(table);

    </script>
  </section>

</template>
