export_metrics_mixin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('views/common/export_metrics_menu_view');
  20. var stringUtils = require('utils/string_utils');
  21. var fileUtils = require('utils/file_utils');
  22. App.ExportMetricsMixin = Em.Mixin.create({
  23. /**
  24. * Used as argument passed from template to indicate that resulting format is CSV instead of JSON
  25. */
  26. exportToCSVArgument: true,
  27. isExportMenuHidden: true,
  28. isExportButtonHidden: false,
  29. exportMetricsMenuView: App.ExportMetricsMenuView.extend(),
  30. targetView: function () {
  31. return this.get('exportTargetView') || this;
  32. }.property(),
  33. hideMenuForNoData: function () {
  34. if (this.get('isExportButtonHidden')) {
  35. this.set('isExportMenuHidden', true);
  36. }
  37. }.observes('isExportButtonHidden'),
  38. toggleFormatsList: function () {
  39. this.toggleProperty('isExportMenuHidden');
  40. },
  41. exportGraphData: function (event) {
  42. this.set('isExportMenuHidden', true);
  43. var ajaxIndex = this.get('targetView.ajaxIndex');
  44. App.ajax.send({
  45. name: ajaxIndex,
  46. data: $.extend(this.get('targetView').getDataForAjaxRequest(), {
  47. isCSV: !!event.context
  48. }),
  49. sender: this,
  50. success: 'exportGraphDataSuccessCallback',
  51. error: 'exportGraphDataErrorCallback'
  52. });
  53. },
  54. exportGraphDataSuccessCallback: function (response, request, params) {
  55. var seriesData = this.get('targetView').getData(response);
  56. if (!seriesData.length) {
  57. App.showAlertPopup(Em.I18n.t('graphs.noData.title'), Em.I18n.t('graphs.noData.tooltip.title'));
  58. } else {
  59. var fileType = params.isCSV ? 'csv' : 'json',
  60. fileName = 'data.' + fileType,
  61. data = params.isCSV ? this.prepareCSV(seriesData) : JSON.stringify(seriesData, this.jsonReplacer(), 4);
  62. fileUtils.downloadTextFile(data, fileType, fileName);
  63. }
  64. },
  65. exportGraphDataErrorCallback: function (jqXHR, ajaxOptions, error, opt) {
  66. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.type, jqXHR.status);
  67. },
  68. prepareCSV: function (data) {
  69. var displayUnit = this.get('targetView.displayUnit'),
  70. titles,
  71. ticksNumber,
  72. metricsNumber,
  73. metricsArray;
  74. titles = data.map(function (item) {
  75. return displayUnit ? item.name + ' (' + displayUnit + ')' : item.name;
  76. }, this);
  77. titles.unshift(Em.I18n.t('common.timestamp'));
  78. ticksNumber = data[0].data.length;
  79. metricsNumber = data.length;
  80. metricsArray = [titles];
  81. for (var i = 0; i < ticksNumber; i++) {
  82. metricsArray.push([data[0].data[i][1]]);
  83. for (var j = 0; j < metricsNumber; j++) {
  84. metricsArray[i + 1].push(data[j].data[i][0]);
  85. };
  86. }
  87. return stringUtils.arrayToCSV(metricsArray);
  88. },
  89. jsonReplacer: function () {
  90. var displayUnit = this.get('targetView.displayUnit');
  91. return function (key, value) {
  92. if (['name', 'data'].contains(key) || (!isNaN(key))) {
  93. return key == 'name' && displayUnit ? value + ' (' + displayUnit + ')' : value;
  94. }
  95. }
  96. }
  97. });