summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/analytics/usage_trends/components/users_chart.vue
blob: 09dfcddcb73283f03d1f8ab08fcd98f2c3a68656 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<script>
import { GlAlert } from '@gitlab/ui';
import { GlAreaChart } from '@gitlab/ui/dist/charts';
import * as Sentry from '@sentry/browser';
import produce from 'immer';
import { sortBy } from 'lodash';
import { formatDateAsMonth } from '~/lib/utils/datetime_utility';
import { __ } from '~/locale';
import ChartSkeletonLoader from '~/vue_shared/components/resizable_chart/skeleton_loader.vue';
import usersQuery from '../graphql/queries/users.query.graphql';
import { getAverageByMonth } from '../utils';

const sortByDate = (data) => sortBy(data, (item) => new Date(item[0]).getTime());

export default {
  name: 'UsersChart',
  components: { GlAlert, GlAreaChart, ChartSkeletonLoader },
  props: {
    startDate: {
      type: Date,
      required: true,
    },
    endDate: {
      type: Date,
      required: true,
    },
    totalDataPoints: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      loadingError: null,
      users: [],
      pageInfo: null,
    };
  },
  apollo: {
    users: {
      query: usersQuery,
      variables() {
        return {
          first: this.totalDataPoints,
          after: null,
        };
      },
      update(data) {
        return data.users?.nodes || [];
      },
      result({ data }) {
        const {
          users: { pageInfo },
        } = data;
        this.pageInfo = pageInfo;
        this.fetchNextPage();
      },
      error(error) {
        this.handleError(error);
      },
    },
  },
  i18n: {
    yAxisTitle: __('Total users'),
    xAxisTitle: __('Month'),
    loadUserChartError: __('Could not load the user chart. Please refresh the page to try again.'),
    noDataMessage: __('There is no data available.'),
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.users.loading || this.pageInfo?.hasNextPage;
    },
    chartUserData() {
      const averaged = getAverageByMonth(
        this.users.length > this.totalDataPoints
          ? this.users.slice(0, this.totalDataPoints)
          : this.users,
        { shouldRound: true },
      );
      return sortByDate(averaged);
    },
    options() {
      return {
        xAxis: {
          name: this.$options.i18n.xAxisTitle,
          type: 'category',
          axisLabel: {
            formatter: formatDateAsMonth,
          },
        },
        yAxis: {
          name: this.$options.i18n.yAxisTitle,
        },
      };
    },
  },
  methods: {
    handleError(error) {
      this.loadingError = true;
      this.users = [];
      Sentry.captureException(error);
    },
    fetchNextPage() {
      if (this.pageInfo?.hasNextPage) {
        this.$apollo.queries.users
          .fetchMore({
            variables: { first: this.totalDataPoints, after: this.pageInfo.endCursor },
            updateQuery: (previousResult, { fetchMoreResult }) => {
              return produce(fetchMoreResult, (newUsers) => {
                // eslint-disable-next-line no-param-reassign
                newUsers.users.nodes = [...previousResult.users.nodes, ...newUsers.users.nodes];
              });
            },
          })
          .catch(this.handleError);
      }
    },
  },
};
</script>
<template>
  <div>
    <h3>{{ $options.i18n.yAxisTitle }}</h3>
    <gl-alert v-if="loadingError" variant="danger" :dismissible="false" class="gl-mt-3">
      {{ this.$options.i18n.loadUserChartError }}
    </gl-alert>
    <chart-skeleton-loader v-else-if="isLoading" />
    <gl-alert v-else-if="!chartUserData.length" variant="info" :dismissible="false" class="gl-mt-3">
      {{ $options.i18n.noDataMessage }}
    </gl-alert>
    <gl-area-chart
      v-else
      :option="options"
      :include-legend-avg-max="true"
      :data="[
        {
          name: $options.i18n.yAxisTitle,
          data: chartUserData,
        },
      ]"
    />
  </div>
</template>