summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/clusters/agents/components/activity_events_list.vue
blob: 18c6503bfb2a5254b59911d8013ff8f185453b72 (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
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
<script>
import {
  GlLoadingIcon,
  GlEmptyState,
  GlLink,
  GlIcon,
  GlAlert,
  GlTooltipDirective,
} from '@gitlab/ui';
import { helpPagePath } from '~/helpers/help_page_helper';
import { n__, s__, __ } from '~/locale';
import { formatDate, getDayDifference, isToday } from '~/lib/utils/datetime_utility';
import { EVENTS_STORED_DAYS } from '../constants';
import getAgentActivityEventsQuery from '../graphql/queries/get_agent_activity_events.query.graphql';
import ActivityHistoryItem from './activity_history_item.vue';

export default {
  components: {
    GlLoadingIcon,
    GlEmptyState,
    GlAlert,
    GlLink,
    GlIcon,
    ActivityHistoryItem,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  i18n: {
    emptyText: s__(
      'ClusterAgents|See agent activity updates, like tokens created or revoked and clusters connected or not connected.',
    ),
    emptyTooltip: s__('ClusterAgents|What is agent activity?'),
    error: s__(
      'ClusterAgents|An error occurred while retrieving agent activity. Reload the page to try again.',
    ),
    today: __('Today'),
    yesterday: __('Yesterday'),
  },
  emptyHelpLink: helpPagePath('user/clusters/agent/work_with_agent', {
    anchor: 'view-an-agents-activity-information',
  }),
  borderClasses: 'gl-border-b-1 gl-border-b-solid gl-border-b-gray-100',
  apollo: {
    agentEvents: {
      query: getAgentActivityEventsQuery,
      variables() {
        return {
          agentName: this.agentName,
          projectPath: this.projectPath,
        };
      },
      update: (data) => data?.project?.clusterAgent?.activityEvents?.nodes,
      error() {
        this.isError = true;
      },
    },
  },
  inject: ['agentName', 'projectPath', 'activityEmptyStateImage'],
  data() {
    return {
      isError: false,
    };
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.agentEvents?.loading;
    },
    emptyStateTitle() {
      return n__(
        'ClusterAgents|No activity occurred in the past day',
        'ClusterAgents|No activity occurred in the past %d days',
        EVENTS_STORED_DAYS,
      );
    },
    eventsList() {
      const list = this.agentEvents;
      const listByDates = {};

      if (!list?.length) {
        return listByDates;
      }

      list.forEach((event) => {
        const dateName = this.getFormattedDate(event.recordedAt);
        if (!listByDates[dateName]) {
          listByDates[dateName] = [];
        }
        listByDates[dateName].push(event);
      });

      return listByDates;
    },
    hasEvents() {
      return Object.keys(this.eventsList).length;
    },
  },
  methods: {
    isYesterday(date) {
      const today = new Date();
      return getDayDifference(today, date) === -1;
    },
    getFormattedDate(dateString) {
      const date = new Date(dateString);
      let dateName;
      if (isToday(date)) {
        dateName = this.$options.i18n.today;
      } else if (this.isYesterday(date)) {
        dateName = this.$options.i18n.yesterday;
      } else {
        dateName = formatDate(date, 'yyyy-mm-dd');
      }
      return dateName;
    },
    isLast(dateEvents, idx) {
      return idx === dateEvents.length - 1;
    },
    getBodyClasses(dateEvents, idx) {
      return !this.isLast(dateEvents, idx) ? this.$options.borderClasses : '';
    },
  },
};
</script>

<template>
  <div>
    <gl-loading-icon v-if="isLoading" size="lg" />

    <div v-else-if="hasEvents">
      <div
        v-for="(dateEvents, key) in eventsList"
        :key="key"
        class="agent-activity-list issuable-discussion"
      >
        <h4
          class="gl-pb-4 gl-ml-5"
          :class="$options.borderClasses"
          data-testid="activity-section-title"
        >
          {{ key }}
        </h4>

        <ul class="notes main-notes-list timeline">
          <activity-history-item
            v-for="(event, idx) in dateEvents"
            :key="idx"
            :event="event"
            :body-class="getBodyClasses(dateEvents, idx)"
          />
        </ul>
      </div>
    </div>

    <gl-alert v-else-if="isError" variant="danger" :dismissible="false" class="gl-mt-3">
      {{ $options.i18n.error }}
    </gl-alert>

    <gl-empty-state
      v-else
      :title="emptyStateTitle"
      :svg-path="activityEmptyStateImage"
      :svg-height="150"
    >
      <template #description
        >{{ $options.i18n.emptyText }}
        <gl-link
          v-gl-tooltip
          :href="$options.emptyHelpLink"
          :title="$options.i18n.emptyTooltip"
          :aria-label="$options.i18n.emptyTooltip"
          ><gl-icon name="question" :size="14"
        /></gl-link>
      </template>
    </gl-empty-state>
  </div>
</template>