Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timeline_chart.vue « components « error_tracking « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 907a6c8557f6a51c5c4d0c680f6d90675ba804b2 (plain)
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
<script>
import { GlChart } from '@gitlab/ui/dist/charts';
import { DATA_VIZ_BLUE_500 } from '@gitlab/ui/dist/tokens/js/tokens';
import { hexToRgba } from '@gitlab/ui/dist/utils/utils';
import { isNumber } from 'lodash';
import { formatDate } from '~/lib/utils/datetime/date_format_utility';
import { logError } from '~/lib/logger';

function parseTimelineData(timelineData) {
  const xData = [];
  const yData = [];
  const invalidDataPoints = [];
  timelineData.forEach((f) => {
    let rawDate;
    let count;

    if (Array.isArray(f)) {
      [rawDate, count] = f;
    } else if (f.count !== undefined && f.time !== undefined) {
      rawDate = f.time;
      count = f.count;
    }
    if (rawDate !== undefined && count !== undefined) {
      // dates/timestamps are in seconds
      const date = isNumber(rawDate) ? rawDate * 1000 : rawDate;
      xData.push(formatDate(date));
      yData.push(count);
    } else {
      invalidDataPoints.push(f);
    }
  });
  if (invalidDataPoints.length > 0) {
    // only log up to 5 invalid data points to reduce log size
    logError(`Found invalid data points ${invalidDataPoints.slice(0, 5)}`);
  }
  return { xData, yData };
}

export default {
  components: {
    GlChart,
  },
  props: {
    timelineData: {
      /**
       * Array items can be:
       *   touples: [a_date: string | number, a_count: number]
       *   objects: {time: a_date, count: a_count}: {time: string | number, count: number}
       *
       * Dates can either be string or number/timestamp.
       * When dates are timestamps, they are expected in seconds.
       *
       */
      type: Array,
      required: true,
      validator(value) {
        for (const item of value) {
          if (Array.isArray(item)) {
            if (item.length !== 2 || !isNumber(item[1])) {
              return false;
            }
          } else if (typeof item === 'object') {
            if (!('time' in item) || !('count' in item)) {
              return false;
            }
          } else {
            return false;
          }
        }
        return true;
      },
    },
    height: {
      type: Number,
      required: true,
    },
  },
  computed: {
    chartOptions() {
      if (!this.timelineData) {
        return {};
      }
      const { xData, yData } = parseTimelineData(this.timelineData);

      return {
        xAxis: {
          type: 'category',
          data: xData,
          show: true,
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              width: 1,
              color: '#ececec',
            },
          },
        },
        yAxis: {
          type: 'value',
          show: false,
        },
        series: [
          {
            data: yData,
            type: 'bar',
            itemStyle: { color: hexToRgba(DATA_VIZ_BLUE_500, 0.5) },
          },
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
      };
    },
  },
};
</script>

<template>
  <gl-chart v-if="timelineData" :options="chartOptions" :height="height" />
</template>