dataset-stats.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Backbone model denoting statistics on a dataset
  3. */
  4. define(
  5. function( require ) {
  6. "use strict";
  7. var Marionette = require( "marionette" ),
  8. Backbone = require( "backbone" ),
  9. _ = require( "underscore" ),
  10. fui = require( "app/fui" ),
  11. sprintf = require( "sprintf" );
  12. /**
  13. * This model represents the statistics available on a one or more datasets
  14. */
  15. var DatasetStats = Backbone.Model.extend( {
  16. initialize: function( dataset, stats ) {
  17. this.set( {dataset: dataset, stats: stats} );
  18. },
  19. /** Return the number of datasets we have statistics for */
  20. size: function() {
  21. return _.keys( datasets() ).length;
  22. },
  23. toJSON: function() {
  24. return this.asTable();
  25. },
  26. /** Return a table of the statistics we have, one row per dataset */
  27. asTable: function() {
  28. var ds = this.datasets();
  29. var endpoints = this.collectEndpoints( ds );
  30. var rows = [];
  31. _.each( ds, function( d, dsName ) {
  32. var row = [dsName, d.Requests, d.RequestsGood, d.RequestsBad];
  33. var es = d.endpoints;
  34. _.each( endpoints, function( e ) {
  35. if (es[e.key]) {
  36. var servStats = es[e.key];
  37. if (servStats.Requests === 0) {
  38. row.push( "0" );
  39. }
  40. else {
  41. row.push( sprintf( "%d (%d bad)", servStats.Requests, servStats.RequestsBad ))
  42. }
  43. }
  44. else {
  45. row.push( "–" );
  46. }
  47. } );
  48. rows.push( row );
  49. } );
  50. return {headings: this.columnHeadings( endpoints ), rows: rows};
  51. },
  52. stats: function() {
  53. return this.get( "stats" );
  54. },
  55. datasets: function() {
  56. return this.stats().datasets;
  57. },
  58. /** Reload the numbers from the server */
  59. refresh: function() {
  60. var self = this;
  61. this.get( "dataset" )
  62. .statistics()
  63. .done( function( data ) {
  64. self.set( "stats", data );
  65. } );
  66. },
  67. // internal methods
  68. collectEndpoints: function( ds ) {
  69. var endpoints = [];
  70. _.each( ds, function( d ) {
  71. var ep = _.each( d.endpoints, function( v, k ) {
  72. endpoints.push( {key: k, label: v.description} );
  73. } );
  74. } );
  75. return _.uniq( endpoints ).sort();
  76. },
  77. columnHeadings: function( services ) {
  78. return ["Name", "Overall", "Overall good", "Overall bad"].concat( _.pluck( services, 'label' ) );
  79. }
  80. } );
  81. return DatasetStats;
  82. }
  83. );