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

179 lines
4.2 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,
2020-11-24 15:15:51 +05:30
GlDropdown,
GlDropdownItem,
GlDropdownDivider,
2020-07-28 23:09:34 +05:30
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,
2020-11-24 15:15:51 +05:30
GlDropdown,
GlDropdownItem,
GlDropdownDivider,
2020-07-28 23:09:34 +05:30
},
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-11-24 15:15:51 +05:30
<gl-dropdown
2020-10-24 23:57:45 +05:30
v-if="!disableMetricDashboardRefreshRate"
v-gl-tooltip
:title="s__('Metrics|Set refresh rate')"
:text="dropdownText"
>
2020-11-24 15:15:51 +05:30
<gl-dropdown-item
2020-07-28 23:09:34 +05:30
:is-check-item="true"
:is-checked="refreshInterval === null"
@click="removeRefreshInterval()"
2020-11-24 15:15:51 +05:30
>{{ __('Off') }}</gl-dropdown-item
2020-07-28 23:09:34 +05:30
>
2020-11-24 15:15:51 +05:30
<gl-dropdown-divider />
<gl-dropdown-item
2020-07-28 23:09:34 +05:30
v-for="(option, i) in $options.refreshIntervals"
:key="i"
:is-check-item="true"
:is-checked="isChecked(option)"
@click="setRefreshInterval(option)"
2020-11-24 15:15:51 +05:30
>{{ option.label }}</gl-dropdown-item
2020-07-28 23:09:34 +05:30
>
2020-11-24 15:15:51 +05:30
</gl-dropdown>
2020-07-28 23:09:34 +05:30
</gl-button-group>
</template>