export_metrics_mixin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var stringUtils = require('utils/string_utils');
  20. var fileUtils = require('utils/file_utils');
  21. App.ExportMetricsMixin = Em.Mixin.create({
  22. /**
  23. * Used as argument passed from template to indicate that resulting format is CSV, not JSON
  24. */
  25. exportToCSVArgument: true,
  26. toggleFormatsList: function () {
  27. this.$('.export-graph-list').toggle();
  28. },
  29. exportGraphData: function () {
  30. this.toggleFormatsList();
  31. },
  32. exportGraphDataSuccessCallback: function (response, request, params) {
  33. var hasData = response && response.metrics && Em.keys(response.metrics).length;
  34. if (!hasData) {
  35. App.showAlertPopup(Em.I18n.t('graphs.noData.title'), Em.I18n.t('graphs.noData.tooltip.title'));
  36. } else {
  37. var fileType = params.isCSV ? 'csv' : 'json',
  38. fileName = 'data.' + fileType,
  39. data = params.isCSV ? this.prepareCSV(response) : this.prepareJSON(response);
  40. fileUtils.downloadTextFile(data, fileType, fileName);
  41. }
  42. },
  43. exportGraphDataErrorCallback: function (jqXHR, ajaxOptions, error, opt) {
  44. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  45. },
  46. /**
  47. * Take metrics from any depth level in JSON response
  48. * @method setMetricsArrays
  49. * @param data
  50. * @param metrics
  51. * @param titles
  52. */
  53. setMetricsArrays: function (data, metrics, titles) {
  54. Em.keys(data).forEach(function (key) {
  55. if (Em.isArray(data[key])) {
  56. titles.push(key);
  57. metrics.push(data[key]);
  58. } else {
  59. this.setMetricsArrays(data[key], metrics, titles);
  60. }
  61. }, this);
  62. },
  63. prepareCSV: function (data) {
  64. var metrics = [],
  65. getMetricsItem = function (i, j, k) {
  66. var item;
  67. if (data.metrics) {
  68. item = metrics[j][i][k];
  69. } else if (Em.isArray(data)) {
  70. item = data[j].data[i][k];
  71. }
  72. return item;
  73. },
  74. titles,
  75. ticksNumber,
  76. metricsNumber,
  77. metricsArray;
  78. if (data.metrics) {
  79. titles = [Em.I18n.t('common.timestamp')];
  80. this.setMetricsArrays(data.metrics, metrics, titles);
  81. ticksNumber = metrics[0].length;
  82. metricsNumber = metrics.length
  83. } else if (Em.isArray(data)) {
  84. titles = data.mapProperty('name');
  85. titles.unshift(Em.I18n.t('common.timestamp'));
  86. ticksNumber = data[0].data.length;
  87. metricsNumber = data.length;
  88. }
  89. metricsArray = [titles];
  90. for (var i = 0; i < ticksNumber; i++) {
  91. metricsArray.push([getMetricsItem(i, 0, 1)]);
  92. for (var j = 0; j < metricsNumber; j++) {
  93. metricsArray[i + 1].push(getMetricsItem(i, j, 0));
  94. };
  95. }
  96. return stringUtils.arrayToCSV(metricsArray);
  97. },
  98. prepareJSON: function (data) {
  99. var fileData;
  100. if (data.metrics) {
  101. fileData = JSON.stringify(data.metrics, null, 4);
  102. } else if (Em.isArray(data)) {
  103. fileData = JSON.stringify(data, ['name', 'data'], 4);
  104. }
  105. return fileData;
  106. }
  107. });