<commit_before>'use strict';

(function() {
  angular.module('ncsaas').service('DashboardChartService', DashboardChartService);

  DashboardChartService.$inject = ['$q', 'priceEstimationService', 'quotasService', '$filter'];
  function DashboardChartService($q, priceEstimationService, quotasService, $filter) {
    var vm = this;

    this.getAllCharts = function(customer) {
      return $q.all([
        vm.getResourceHistoryCharts(customer),
        vm.getCostChart(customer)
      ]).then(function(result) {
        return result[0].concat([result[1]]);
      });
    };

    this.getResourceHistoryCharts = function(customer) {
      var charts = [
        {
          quota: 'nc_app_count',
          title: 'Applications'
        },
        {
          quota: 'nc_vm_count',
          title: 'Virtual machines'
        },
        {
          quota: 'nc_private_cloud_count',
          title: 'Private clouds'
        }
      ];

      var promises = charts.map(function(chart) {
        var matches = customer.quotas.filter(function(quota) {
          return quota.name === chart.quota;
        });
        if (matches) {
          var quota = matches[0];
          chart.quota = quota;
          chart.current = quota.usage;
          return vm.getQuotaHistory(quota.url).then(function(data) {
            chart.data = data;
          });
        }
      });
      return $q.all(promises).then(function() {
        angular.forEach(charts, function(chart) {
          if (chart.data.length > 1) {
            chart.change = vm.getRelativeChange([
              {value: chart.data[chart.data.length - 1].value},
              {value: chart.data[0].value}
            ]);
          }
        });
        return charts;
      });
    };
    this.getQuotaHistory = function(url) {
      var end = moment.utc().unix();
      var count = 30;
      var start = moment.utc().subtract(count, 'days').unix();

      return quotasService.getHistory(url, start, end, count).then(function(items) {
        return items.filter(function(item) {
          return !!item.object;
        }).map(function(item) {
          return {
            date: moment.unix(item.point).toDate(),
            value: item.object.usage
          };
        });
      });
    };
    this.getCostChart = function(customer) {
      return priceEstimationService.getList({
        scope: customer.url
      }).then(function(estimates) {
        estimates = estimates.map(function(estimate) {
          return {<commit_msg>Make dashboard chart code more generic (SAAS-1421)<commit_after>'use strict';

(function() {
  angular.module('ncsaas').service('DashboardChartService', DashboardChartService);

  DashboardChartService.$inject = ['$q', 'priceEstimationService', 'quotasService', '$filter'];
  function DashboardChartService($q, priceEstimationService, quotasService, $filter) {
    var vm = this;

    this.getAllCharts = function(scope) {
      return $q.all([
        vm.getResourceHistoryCharts(scope),
        vm.getCostChart(scope)
      ]).then(function(result) {
        return result[0].concat([result[1]]);
      });
    };

    this.getResourceHistoryCharts = function(scope) {
      var charts = [
        {
          quota: 'nc_app_count',
          title: 'Applications'
        },
        {
          quota: 'nc_vm_count',
          title: 'Virtual machines'
        },
        {
          quota: 'nc_private_cloud_count',
          title: 'Private clouds'
        }
      ];

      var promises = charts.map(function(chart) {
        var matches = scope.quotas.filter(function(quota) {
          return quota.name === chart.quota;
        });
        if (matches) {
          var quota = matches[0];
          chart.quota = quota;
          chart.current = quota.usage;
          return vm.getQuotaHistory(quota.url).then(function(data) {
            chart.data = data;
          });
        }
      });
      return $q.all(promises).then(function() {
        angular.forEach(charts, function(chart) {
          if (chart.data.length > 1) {
            chart.change = vm.getRelativeChange([
              {value: chart.data[chart.data.length - 1].value},
              {value: chart.data[0].value}
            ]);
          }
        });
        return charts;
      });
    };
    this.getQuotaHistory = function(url) {
      var end = moment.utc().unix();
      var count = 30;
      var start = moment.utc().subtract(count, 'days').unix();

      return quotasService.getHistory(url, start, end, count).then(function(items) {
        return items.filter(function(item) {
          return !!item.object;
        }).map(function(item) {
          return {
            date: moment.unix(item.point).toDate(),
            value: item.object.usage
          };
        });
      });
    };
    this.getCostChart = function(scope) {
      return priceEstimationService.getList({
        scope: scope.url
      }).then(function(estimates) {
        estimates = estimates.map(function(estimate) {
          return {
{'fraction_non_alphanumeric': 0.1374, 'fraction_numerical': 0.004, 'mean_word_length': 2.045676004872107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '127433', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1441, 'n_words': 246}