All files / app/assets/javascripts/pages/projects/graphs/charts index.js

0% Statements 0/47
0% Branches 0/7
0% Functions 0/19
0% Lines 0/44

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { GlColumnChart } from '@gitlab/ui/dist/charts';
import Vue from 'vue';
import { __ } from '~/locale';
import { visitUrl } from '~/lib/utils/url_utility';
import { REF_TYPE_BRANCHES, REF_TYPE_TAGS } from '~/ref/constants';
import RefSelector from '~/ref/components/ref_selector.vue';
import CodeCoverage from '../components/code_coverage.vue';
import SeriesDataMixin from './series_data_mixin';
 
const seriesDataToBarData = (raw) => Object.entries(raw).map(([name, data]) => ({ name, data }));
 
const languagesContainer = document.getElementById('js-languages-chart');
const codeCoverageContainer = document.getElementById('js-code-coverage-chart');
const monthContainer = document.getElementById('js-month-chart');
const weekdayContainer = document.getElementById('js-weekday-chart');
const hourContainer = document.getElementById('js-hour-chart');
const branchSelector = document.getElementById('js-project-graph-ref-switcher');
const LANGUAGE_CHART_HEIGHT = 300;
const reorderWeekDays = (weekDays, firstDayOfWeek = 0) => {
  if (firstDayOfWeek === 0) {
    return weekDays;
  }
 
  return Object.keys(weekDays).reduce((acc, dayName, idx, arr) => {
    const reorderedDayName = arr[(idx + firstDayOfWeek) % arr.length];
 
    return {
      ...acc,
      [reorderedDayName]: weekDays[reorderedDayName],
    };
  }, {});
};
 
// eslint-disable-next-line no-new
new Vue({
  el: languagesContainer,
  components: {
    GlColumnChart,
  },
  data() {
    return {
      chartData: JSON.parse(languagesContainer.dataset.chartData),
    };
  },
  computed: {
    seriesData() {
      return [{ name: 'full', data: this.chartData.map((d) => [d.label, d.value]) }];
    },
  },
  render(h) {
    return h(GlColumnChart, {
      props: {
        bars: this.seriesData,
        xAxisTitle: __('Used programming language'),
        yAxisTitle: __('Percentage'),
        xAxisType: 'category',
      },
      attrs: {
        height: LANGUAGE_CHART_HEIGHT,
        responsive: true,
      },
    });
  },
});
 
if (codeCoverageContainer?.dataset) {
  const {
    graphEndpoint,
    graphEndDate,
    graphStartDate,
    graphRef,
    graphCsvPath,
  } = codeCoverageContainer.dataset;
  // eslint-disable-next-line no-new
  new Vue({
    el: codeCoverageContainer,
    render(h) {
      return h(CodeCoverage, {
        props: {
          graphEndpoint,
          graphEndDate,
          graphStartDate,
          graphRef,
          graphCsvPath,
        },
      });
    },
  });
}
 
// eslint-disable-next-line no-new
new Vue({
  el: monthContainer,
  components: {
    GlColumnChart,
  },
  mixins: [SeriesDataMixin],
  data() {
    return {
      chartData: JSON.parse(monthContainer.dataset.chartData),
    };
  },
  render(h) {
    return h(GlColumnChart, {
      props: {
        bars: seriesDataToBarData(this.seriesData),
        xAxisTitle: __('Day of month'),
        yAxisTitle: __('No. of commits'),
        xAxisType: 'category',
      },
      attrs: {
        responsive: true,
      },
    });
  },
});
 
// eslint-disable-next-line no-new
new Vue({
  el: weekdayContainer,
  components: {
    GlColumnChart,
  },
  data() {
    return {
      chartData: JSON.parse(weekdayContainer.dataset.chartData),
    };
  },
  computed: {
    seriesData() {
      const weekDays = reorderWeekDays(this.chartData, gon.first_day_of_week);
      const data = Object.keys(weekDays).reduce((acc, key) => {
        acc.push([key, weekDays[key]]);
        return acc;
      }, []);
      return [{ name: 'full', data }];
    },
  },
  render(h) {
    return h(GlColumnChart, {
      props: {
        bars: this.seriesData,
        xAxisTitle: __('Weekday'),
        yAxisTitle: __('No. of commits'),
        xAxisType: 'category',
      },
      attrs: {
        responsive: true,
      },
    });
  },
});
 
// eslint-disable-next-line no-new
new Vue({
  el: hourContainer,
  components: {
    GlColumnChart,
  },
  mixins: [SeriesDataMixin],
  data() {
    return {
      chartData: JSON.parse(hourContainer.dataset.chartData),
    };
  },
  render(h) {
    return h(GlColumnChart, {
      props: {
        bars: seriesDataToBarData(this.seriesData),
        xAxisTitle: __('Hour (UTC)'),
        yAxisTitle: __('No. of commits'),
        xAxisType: 'category',
      },
      attrs: {
        responsive: true,
      },
    });
  },
});
 
const { projectId, projectBranch, graphPath } = branchSelector.dataset;
 
const GRAPHS_PATH_REGEX = /^(.*?)\/-\/graphs/g;
const graphsPathPrefix = graphPath.match(GRAPHS_PATH_REGEX)?.[0];
if (!graphsPathPrefix) {
  // eslint-disable-next-line @gitlab/require-i18n-strings
  throw new Error('Path is not correct');
}
 
// eslint-disable-next-line no-new
new Vue({
  el: branchSelector,
  name: 'RefSelector',
  render(createComponent) {
    return createComponent(RefSelector, {
      props: {
        enabledRefTypes: [REF_TYPE_BRANCHES, REF_TYPE_TAGS],
        value: projectBranch,
        translations: {
          dropdownHeader: __('Switch branch/tag'),
          searchPlaceholder: __('Search branches and tags'),
        },
        projectId,
      },
      class: 'gl-w-20',
      on: {
        input(selected) {
          visitUrl(`${graphsPathPrefix}/${encodeURIComponent(selected)}/charts`);
        },
      },
    });
  },
});