Feat: Handle monitor filter state in URL

This commit is contained in:
NihadBadalov 2024-02-17 18:30:39 +01:00
parent d3a5b224cc
commit 3d4fb163d5
2 changed files with 120 additions and 90 deletions

View file

@ -26,7 +26,7 @@
</div> </div>
</div> </div>
<div class="header-filter"> <div class="header-filter">
<MonitorListFilter :filterState="filterState" @update-filter="updateFilter" /> <MonitorListFilter @update-filter="updateFilter" />
</div> </div>
<!-- Selection Controls --> <!-- Selection Controls -->
@ -95,11 +95,22 @@ export default {
disableSelectAllWatcher: false, disableSelectAllWatcher: false,
selectedMonitors: {}, selectedMonitors: {},
windowTop: 0, windowTop: 0,
filterState: { statusStates: {
status: null, up: 1,
active: null, down: 0,
tags: null, pending: 2,
} maintenance: 3,
1: 'up',
0: 'down',
2: 'pending',
3: 'maintenance',
},
activeStates: {
running: true,
paused: false,
true: 'running',
false: 'paused',
},
}; };
}, },
computed: { computed: {
@ -169,7 +180,7 @@ export default {
* @returns {boolean} True if any filter is active, false otherwise. * @returns {boolean} True if any filter is active, false otherwise.
*/ */
filtersActive() { filtersActive() {
return this.filterState.status != null || this.filterState.active != null || this.filterState.tags != null || this.searchText !== ""; return this.$router.currentRoute.value.query?.status != null || this.$router.currentRoute.value.query?.active != null || this.$router.currentRoute.value.query?.tags != null || this.searchText !== "";
} }
}, },
watch: { watch: {
@ -207,21 +218,10 @@ export default {
async mounted() { async mounted() {
window.addEventListener("scroll", this.onScroll); window.addEventListener("scroll", this.onScroll);
const statusParams = this.$router.currentRoute.value.query["status"]; const queryParams = this.$router.currentRoute.value.query;
const activeParams = this.$router.currentRoute.value.query["active"]; const statusParams = queryParams?.["status"];
const tagParams = this.$router.currentRoute.value.query["tags"]; const activeParams = queryParams?.["active"];
const tagParams = queryParams?.["tags"];
const statusStates = {
up: 1,
down: 0,
pending: 2,
maintenance: 3,
};
const activeStates = {
running: true,
paused: false,
};
const tags = await (() => { const tags = await (() => {
return new Promise((resolve) => { return new Promise((resolve) => {
@ -233,24 +233,26 @@ export default {
}); });
})(); })();
const fetchedTagIDs = tagParams const fetchedTagNames = tagParams
? tagParams
.split(",") .split(",")
.map(identifier => { .map(identifier => {
const tagID = parseInt(identifier, 10); const tagID = parseInt(identifier, 10);
return tags return tags
.find(t => t.name === identifier || t.id === tagID) .find(t => t.name === identifier || t.id === tagID)
?.id ?? 0; ?.name ?? 0;
}); })
.filter(tagID => tagID !== 0)
: undefined;
this.updateFilter({ this.updateFilter({
...this.filterState,
status: statusParams ? statusParams.split(",").map( status: statusParams ? statusParams.split(",").map(
status => statusStates[status.trim()] status => this.statusStates[this.statusStates[status.trim()]]
) : this.filterState["status"], ) : queryParams?.["status"],
active: activeParams ? activeParams.split(",").map( active: activeParams ? activeParams.split(",").map(
active => activeStates[active.trim()] active => this.activeStates[this.activeStates[active.trim()]]
) : this.filterState["active"], ) : queryParams?.["active"],
tags: tagParams ? fetchedTagIDs : this.filterState["tags"], tags: tagParams ? fetchedTagNames : queryParams?.["tags"],
}); });
}, },
beforeUnmount() { beforeUnmount() {
@ -289,7 +291,20 @@ export default {
* @returns {void} * @returns {void}
*/ */
updateFilter(newFilter) { updateFilter(newFilter) {
this.filterState = newFilter; const newQuery = { ...this.$router.currentRoute.value.query };
for (const [key, value] of Object.entries(newFilter)) {
if (!value
|| (value instanceof Array && value.length === 0)) {
delete newQuery[key];
continue
}
newQuery[key] = value instanceof Array
? value.length > 0 ? value.join(",") : null
: value;
}
this.$router.push({ query: newQuery });
}, },
/** /**
* Deselect a monitor * Deselect a monitor
@ -379,24 +394,25 @@ export default {
// filter by status // filter by status
let statusMatch = true; let statusMatch = true;
if (this.filterState.status != null && this.filterState.status.length > 0) { if (this.$router.currentRoute.value.query?.status != null && this.$router.currentRoute.value.query?.status.length > 0) {
if (monitor.id in this.$root.lastHeartbeatList && this.$root.lastHeartbeatList[monitor.id]) { if (monitor.id in this.$root.lastHeartbeatList && this.$root.lastHeartbeatList[monitor.id]) {
monitor.status = this.$root.lastHeartbeatList[monitor.id].status; monitor.status = this.$root.lastHeartbeatList[monitor.id].status;
} }
statusMatch = this.filterState.status.includes(monitor.status); statusMatch = this.$router.currentRoute.value.query?.status.includes(this.statusStates[monitor.status]);
} }
// filter by active // filter by active
let activeMatch = true; let activeMatch = true;
if (this.filterState.active != null && this.filterState.active.length > 0) { if (this.$router.currentRoute.value.query?.active != null && this.$router.currentRoute.value.query?.active.length > 0) {
activeMatch = this.filterState.active.includes(monitor.active); activeMatch = this.$router.currentRoute.value.query?.active.includes(monitor.active);
} }
// filter by tags // filter by tags
let tagsMatch = true; let tagsMatch = true;
if (this.filterState.tags != null && this.filterState.tags.length > 0) { const tagsInURL = this.$router.currentRoute.value.query?.tags?.split(",") || [];
tagsMatch = monitor.tags.map(tag => tag.tag_id) // convert to array of tag IDs if (this.$router.currentRoute.value.query?.tags != null && this.$router.currentRoute.value.query?.tags.length > 0) {
.filter(monitorTagId => this.filterState.tags.includes(monitorTagId)) // perform Array Intersaction between filter and monitor's tags tagsMatch = monitor.tags.map(tag => tag.name) // convert to array of tag names
.filter(monitorTagId => tagsInURL.includes(monitorTagId)) // perform Array Intersaction between filter and monitor's tags
.length > 0; .length > 0;
} }

View file

@ -14,10 +14,10 @@
<font-awesome-icon v-if="numFiltersActive > 0" icon="times" /> <font-awesome-icon v-if="numFiltersActive > 0" icon="times" />
</button> </button>
<MonitorListFilterDropdown <MonitorListFilterDropdown
:filterActive="filterState.status?.length > 0" :filterActive="this.$router.currentRoute.value.query?.status?.length > 0"
> >
<template #status> <template #status>
<Status v-if="filterState.status?.length === 1" :status="filterState.status[0]" /> <Status v-if="this.$router.currentRoute.value.query?.status?.length === 1" :status="this.$router.currentRoute.value.query?.status[0]" />
<span v-else> <span v-else>
{{ $t('Status') }} {{ $t('Status') }}
</span> </span>
@ -29,7 +29,7 @@
<Status :status="1" /> <Status :status="1" />
<span class="ps-3"> <span class="ps-3">
{{ $root.stats.up }} {{ $root.stats.up }}
<span v-if="filterState.status?.includes(1)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.status?.includes('up')" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -42,7 +42,7 @@
<Status :status="0" /> <Status :status="0" />
<span class="ps-3"> <span class="ps-3">
{{ $root.stats.down }} {{ $root.stats.down }}
<span v-if="filterState.status?.includes(0)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.status?.includes('down')" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -55,7 +55,7 @@
<Status :status="2" /> <Status :status="2" />
<span class="ps-3"> <span class="ps-3">
{{ $root.stats.pending }} {{ $root.stats.pending }}
<span v-if="filterState.status?.includes(2)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.status?.includes('pending')" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -68,7 +68,7 @@
<Status :status="3" /> <Status :status="3" />
<span class="ps-3"> <span class="ps-3">
{{ $root.stats.maintenance }} {{ $root.stats.maintenance }}
<span v-if="filterState.status?.includes(3)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.status?.includes('maintenance')" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -77,10 +77,10 @@
</li> </li>
</template> </template>
</MonitorListFilterDropdown> </MonitorListFilterDropdown>
<MonitorListFilterDropdown :filterActive="filterState.active?.length > 0"> <MonitorListFilterDropdown :filterActive="this.$router.currentRoute.value.query?.active?.length > 0">
<template #status> <template #status>
<span v-if="filterState.active?.length === 1"> <span v-if="this.$router.currentRoute.value.query?.active?.length === 1">
<span v-if="filterState.active[0]">{{ $t("Running") }}</span> <span v-if="this.$router.currentRoute.value.query?.active[0]">{{ $t("Running") }}</span>
<span v-else>{{ $t("filterActivePaused") }}</span> <span v-else>{{ $t("filterActivePaused") }}</span>
</span> </span>
<span v-else> <span v-else>
@ -94,7 +94,7 @@
<span>{{ $t("Running") }}</span> <span>{{ $t("Running") }}</span>
<span class="ps-3"> <span class="ps-3">
{{ $root.stats.active }} {{ $root.stats.active }}
<span v-if="filterState.active?.includes(true)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.active?.includes(true)" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -107,7 +107,7 @@
<span>{{ $t("filterActivePaused") }}</span> <span>{{ $t("filterActivePaused") }}</span>
<span class="ps-3"> <span class="ps-3">
{{ $root.stats.pause }} {{ $root.stats.pause }}
<span v-if="filterState.active?.includes(false)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.active?.includes(false)" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -116,11 +116,11 @@
</li> </li>
</template> </template>
</MonitorListFilterDropdown> </MonitorListFilterDropdown>
<MonitorListFilterDropdown :filterActive="filterState.tags?.length > 0"> <MonitorListFilterDropdown :filterActive="this.$router.currentRoute.value.query?.tags?.length > 0">
<template #status> <template #status>
<Tag <Tag
v-if="filterState.tags?.length === 1" v-if="this.$router.currentRoute.value.query?.tags?.length === 1"
:item="tagsList.find(tag => tag.id === filterState.tags[0])" :item="tagsList.find(tag => tag.id === this.$router.currentRoute.value.query?.tags[0])"
:size="'sm'" :size="'sm'"
/> />
<span v-else> <span v-else>
@ -134,7 +134,7 @@
<span><Tag :item="tag" :size="'sm'" /></span> <span><Tag :item="tag" :size="'sm'" /></span>
<span class="ps-3"> <span class="ps-3">
{{ getTaggedMonitorCount(tag) }} {{ getTaggedMonitorCount(tag) }}
<span v-if="filterState.tags?.includes(tag.id)" class="px-1 filter-active"> <span v-if="this.$router.currentRoute.value.query?.tags?.split(',').includes(tag.name)" class="px-1 filter-active">
<font-awesome-icon icon="check" /> <font-awesome-icon icon="check" />
</span> </span>
</span> </span>
@ -162,23 +162,26 @@ export default {
Status, Status,
Tag, Tag,
}, },
props: {
filterState: {
type: Object,
required: true,
}
},
emits: [ "updateFilter" ], emits: [ "updateFilter" ],
data() { data() {
return { return {
tagsList: [], tagsList: [],
filterNames: [
'status',
'active',
'tags',
],
}; };
}, },
computed: { computed: {
numFiltersActive() { numFiltersActive() {
let num = 0; let num = 0;
Object.values(this.filterState).forEach(item => { Object.values(
Array.from(Object.entries(this.$router.currentRoute.value.query)).filter(
e => this.filterNames.includes(e[0])
)
).forEach(item => {
if (item != null && item.length > 0) { if (item != null && item.length > 0) {
num += 1; num += 1;
} }
@ -191,52 +194,63 @@ export default {
this.getExistingTags(); this.getExistingTags();
}, },
methods: { methods: {
getActiveFilters: function() {
const filters = Object.fromEntries(
Array.from(Object.entries(this.$router.currentRoute.value.query ?? {}))
);
return {
status: filters['status'] ? filters['status'].split(',') : [],
active: filters['active'] ? filters['active'].split(',') : [],
tags: filters['tags'] ? filters['tags'].split(',') : [],
};
},
toggleStatusFilter(status) { toggleStatusFilter(status) {
let newFilter = { let newFilter = {
...this.filterState ...this.getActiveFilters(),
}; };
if (newFilter.status == null) { const statusStates = {
newFilter.status = [ status ]; 1: 'up',
0: 'down',
2: 'pending',
3: 'maintenance',
};
const finalStatus = statusStates[status];
if (newFilter.status.includes(''+finalStatus)) {
newFilter.status = newFilter.status.filter(item => item !== ''+finalStatus);
} else { } else {
if (newFilter.status.includes(status)) { newFilter.status.push(finalStatus);
newFilter.status = newFilter.status.filter(item => item !== status);
} else {
newFilter.status.push(status);
}
} }
this.$emit("updateFilter", newFilter); this.$emit("updateFilter", newFilter);
}, },
toggleActiveFilter(active) { toggleActiveFilter(active) {
let newFilter = { let newFilter = {
...this.filterState ...this.getActiveFilters(),
}; };
if (newFilter.active == null) { if (newFilter.active.includes(''+active)) {
newFilter.active = [ active ]; newFilter.active = newFilter.active.filter(item => item !== ''+active);
} else {
if (newFilter.active.includes(active)) {
newFilter.active = newFilter.active.filter(item => item !== active);
} else { } else {
newFilter.active.push(active); newFilter.active.push(active);
} }
}
this.$emit("updateFilter", newFilter); this.$emit("updateFilter", newFilter);
}, },
toggleTagFilter(tag) { toggleTagFilter(tag) {
let newFilter = { let newFilter = {
...this.filterState ...this.getActiveFilters(),
}; };
if (newFilter.tags == null) { if (newFilter.tags.includes(''+tag.name)) {
newFilter.tags = [ tag.id ]; newFilter.tags = newFilter.tags.filter(item => item !== ''+tag.name);
} else { } else {
if (newFilter.tags.includes(tag.id)) { newFilter.tags.push(tag.name);
newFilter.tags = newFilter.tags.filter(item => item !== tag.id);
} else {
newFilter.tags.push(tag.id);
}
} }
this.$emit("updateFilter", newFilter); this.$emit("updateFilter", newFilter);
}, },
clearFilters() { clearFilters() {