All files / ee/app/assets/javascripts/analytics/contribution_analytics contribution_analytics_bundle.js

0% Statements 0/33
0% Branches 0/10
0% Functions 0/11
0% Lines 0/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                                                                                                                                                                                                     
import { sortBy } from 'lodash';
import Vue from 'vue';
import { __ } from '~/locale';
import ColumnChart from './components/column_chart.vue';
import DateSelector from './components/date_selector.vue';
 
const sortByValue = (data) => sortBy(data, (item) => item[1]).reverse();
 
const allValuesEmpty = (graphData) =>
  graphData.reduce((acc, data) => acc + Math.min(0, data[1]), 0) === 0;
 
export default (dataEl) => {
  if (!dataEl) return;
 
  const data = JSON.parse(dataEl.innerHTML);
  const outputElIds = ['push', 'issues_closed', 'merge_requests_created'];
 
  const xAxisType = 'category';
  const xAxisTitle = __('User');
 
  const formattedData = {
    push: [],
    issues_closed: [],
    merge_requests_created: [],
  };
 
  outputElIds.forEach((id) => {
    data[id].data.forEach((d, index) => {
      formattedData[id].push([data.labels[index], d]);
    });
  });
 
  const dateSelectorEl = document.querySelector('.js-contribution-analytics-date-selector');
 
  if (dateSelectorEl) {
    const { path } = dateSelectorEl.dataset;
    // eslint-disable-next-line no-new
    new Vue({
      el: dateSelectorEl,
      components: {
        DateSelector,
      },
      provide: {
        path,
      },
      render(h) {
        return h(DateSelector);
      },
    });
  }
 
  const pushesEl = document.getElementById('js_pushes_chart_vue');
  if (allValuesEmpty(formattedData.push)) {
    // eslint-disable-next-line no-new
    new Vue({
      el: pushesEl,
      components: {
        ColumnChart,
      },
      render(h) {
        return h(ColumnChart, {
          props: {
            chartData: sortByValue(formattedData.push),
            xAxisTitle,
            yAxisTitle: __('Pushes'),
            xAxisType,
          },
        });
      },
    });
  }
 
  const mergeRequestEl = document.getElementById('js_merge_requests_chart_vue');
  if (allValuesEmpty(formattedData.merge_requests_created)) {
    // eslint-disable-next-line no-new
    new Vue({
      el: mergeRequestEl,
      components: {
        ColumnChart,
      },
      render(h) {
        return h(ColumnChart, {
          props: {
            chartData: sortByValue(formattedData.merge_requests_created),
            xAxisTitle,
            yAxisTitle: __('Merge Requests created'),
            xAxisType,
          },
        });
      },
    });
  }
 
  const issueEl = document.getElementById('js_issues_chart_vue');
  if (allValuesEmpty(formattedData.issues_closed)) {
    // eslint-disable-next-line no-new
    new Vue({
      el: issueEl,
      components: {
        ColumnChart,
      },
      render(h) {
        return h(ColumnChart, {
          props: {
            chartData: sortByValue(formattedData.issues_closed),
            xAxisTitle,
            yAxisTitle: __('Issues closed'),
            xAxisType,
          },
        });
      },
    });
  }
};