export_metrics_mixin.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. this.checkGraphDataForValidity(data);
  75. titles = data.map(function (item) {
  76. return displayUnit ? item.name + ' (' + displayUnit + ')' : item.name;
  77. }, this);
  78. titles.unshift(Em.I18n.t('common.timestamp'));
  79. ticksNumber = data[0].data.length;
  80. metricsNumber = data.length;
  81. metricsArray = [titles];
  82. for (var i = 0; i < ticksNumber; i++) {
  83. metricsArray.push([data[0].data[i][1]]);
  84. for (var j = 0; j < metricsNumber; j++) {
  85. metricsArray[i + 1].push(data[j].data[i][0]);
  86. };
  87. };
  88. return stringUtils.arrayToCSV(metricsArray);
  89. },
  90. checkGraphDataForValidity: function (data) {
  91. data.sort(function (a, b) {
  92. return b.data.length - a.data.length
  93. });
  94. var maxLength = data[0].data.length;
  95. for (var i = 1; i < data.length; i ++) {
  96. if (data[i].data.length !== maxLength) this.fillGraphDataArrayWithMockedData(data[i], maxLength);
  97. }
  98. },
  99. fillGraphDataArrayWithMockedData: function (dataArray, neededLength) {
  100. var startIndex = dataArray.data.length,
  101. timestampInterval = dataArray.data[2][1] - dataArray.data[1][1];
  102. for (var i = startIndex; i < neededLength; i++) {
  103. var previousTimestamp = dataArray.data[i - 1][1];
  104. dataArray.data.push([null, previousTimestamp + timestampInterval]);
  105. }
  106. },
  107. jsonReplacer: function () {
  108. var displayUnit = this.get('targetView.displayUnit');
  109. return function (key, value) {
  110. if (['name', 'data'].contains(key) || (!isNaN(key))) {
  111. return key == 'name' && displayUnit ? value + ' (' + displayUnit + ')' : value;
  112. }
  113. }
  114. }
  115. });