debian-mirror-gitlab/app/assets/javascripts/monitoring/components/refresh_button.vue

179 lines
4.3 KiB
Vue
Raw Normal View History

2020-07-28 23:09:34 +05:30
<script>
2020-10-24 23:57:45 +05:30
import Visibility from 'visibilityjs';
2020-07-28 23:09:34 +05:30
import { mapActions } from 'vuex';
import {
GlButtonGroup,
GlButton,
GlNewDropdown,
GlNewDropdownItem,
GlNewDropdownDivider,
GlTooltipDirective,
} from '@gitlab/ui';
2020-10-24 23:57:45 +05:30
import { n__, __ } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
2020-07-28 23:09:34 +05:30
const makeInterval = (length = 0, unit = 's') => {
const shortLabel = `${length}${unit}`;
switch (unit) {
case 'd':
return {
interval: length * 24 * 60 * 60 * 1000,
shortLabel,
label: n__('%d day', '%d days', length),
};
case 'h':
return {
interval: length * 60 * 60 * 1000,
shortLabel,
label: n__('%d hour', '%d hours', length),
};
case 'm':
return {
interval: length * 60 * 1000,
shortLabel,
label: n__('%d minute', '%d minutes', length),
};
case 's':
default:
return {
interval: length * 1000,
shortLabel,
label: n__('%d second', '%d seconds', length),
};
}
};
export default {
components: {
GlButtonGroup,
GlButton,
GlNewDropdown,
GlNewDropdownItem,
GlNewDropdownDivider,
},
directives: {
GlTooltip: GlTooltipDirective,
},
2020-10-24 23:57:45 +05:30
mixins: [glFeatureFlagsMixin()],
2020-07-28 23:09:34 +05:30
data() {
return {
refreshInterval: null,
timeoutId: null,
};
},
computed: {
2020-10-24 23:57:45 +05:30
disableMetricDashboardRefreshRate() {
// Can refresh rates impact performance?
// Add "negative" feature flag called `disable_metric_dashboard_refresh_rate`
// See more at: https://gitlab.com/gitlab-org/gitlab/-/issues/229831
return this.glFeatures.disableMetricDashboardRefreshRate;
},
2020-07-28 23:09:34 +05:30
dropdownText() {
return this.refreshInterval?.shortLabel ?? __('Off');
},
},
watch: {
refreshInterval() {
if (this.refreshInterval !== null) {
this.startAutoRefresh();
} else {
this.stopAutoRefresh();
}
},
},
destroyed() {
this.stopAutoRefresh();
},
methods: {
...mapActions('monitoringDashboard', ['fetchDashboardData']),
refresh() {
this.fetchDashboardData();
},
startAutoRefresh() {
const schedule = () => {
if (this.refreshInterval) {
this.timeoutId = setTimeout(this.startAutoRefresh, this.refreshInterval.interval);
}
};
this.stopAutoRefresh();
2020-10-24 23:57:45 +05:30
if (Visibility.hidden()) {
2020-07-28 23:09:34 +05:30
// Inactive tab? Skip fetch and schedule again
schedule();
} else {
// Active tab! Fetch data and then schedule when settled
// eslint-disable-next-line promise/catch-or-return
this.fetchDashboardData().finally(schedule);
}
},
stopAutoRefresh() {
clearTimeout(this.timeoutId);
this.timeoutId = null;
},
setRefreshInterval(option) {
this.refreshInterval = option;
},
removeRefreshInterval() {
this.refreshInterval = null;
},
isChecked(option) {
if (this.refreshInterval) {
return option.interval === this.refreshInterval.interval;
}
return false;
},
},
refreshIntervals: [
makeInterval(5),
makeInterval(10),
makeInterval(30),
makeInterval(5, 'm'),
makeInterval(30, 'm'),
makeInterval(1, 'h'),
makeInterval(2, 'h'),
makeInterval(12, 'h'),
makeInterval(1, 'd'),
],
};
</script>
<template>
<gl-button-group>
<gl-button
v-gl-tooltip
class="gl-flex-grow-1"
variant="default"
:title="s__('Metrics|Refresh dashboard')"
icon="retry"
@click="refresh"
/>
2020-10-24 23:57:45 +05:30
<gl-new-dropdown
v-if="!disableMetricDashboardRefreshRate"
v-gl-tooltip
:title="s__('Metrics|Set refresh rate')"
:text="dropdownText"
>
2020-07-28 23:09:34 +05:30
<gl-new-dropdown-item
:is-check-item="true"
:is-checked="refreshInterval === null"
@click="removeRefreshInterval()"
>{{ __('Off') }}</gl-new-dropdown-item
>
<gl-new-dropdown-divider />
<gl-new-dropdown-item
v-for="(option, i) in $options.refreshIntervals"
:key="i"
:is-check-item="true"
:is-checked="isChecked(option)"
@click="setRefreshInterval(option)"
>{{ option.label }}</gl-new-dropdown-item
>
</gl-new-dropdown>
</gl-button-group>
</template>