Skip to content

Commit 340a986

Browse files
johnsimonsjasontaylordev
authored andcommitted
Replace moment with dayjs
This should save ~286KB gzipped
1 parent 262c1e2 commit 340a986

File tree

3 files changed

+116
-10
lines changed

3 files changed

+116
-10
lines changed

src/Frontend/src/components/failedmessages/DeletedMessages.vue

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import ServiceControlAvailable from "../ServiceControlAvailable.vue";
77
import MessageList, { IMessageList } from "./MessageList.vue";
88
import ConfirmDialog from "../ConfirmDialog.vue";
99
import PaginationStrip from "../../components/PaginationStrip.vue";
10-
import { FailedMessageStatus } from "@/resources/FailedMessage";
10+
import dayjs from "@/utils/dayjs";
11+
import { ExtendedFailedMessage } from "@/resources/FailedMessage";
1112
import { TYPE } from "vue-toastification";
1213
import FAIcon from "@/components/FAIcon.vue";
1314
import { faArrowRotateRight } from "@fortawesome/free-solid-svg-icons";
@@ -26,6 +27,102 @@ const { messages, groupId, groupName, totalCount, pageNumber, selectedPeriod } =
2627
2728
const showConfirmRestore = ref(false);
2829
const messageList = ref<IMessageList | undefined>();
30+
const messages = ref<ExtendedFailedMessage[]>([]);
31+
32+
watch(pageNumber, () => loadMessages());
33+
34+
const configurationStore = useConfigurationStore();
35+
const { configuration } = storeToRefs(configurationStore);
36+
const serviceControlStore = useServiceControlStore();
37+
38+
function loadMessages() {
39+
let startDate = new Date(0);
40+
const endDate = new Date();
41+
42+
switch (selectedPeriod.value) {
43+
case "All Deleted":
44+
startDate = new Date();
45+
startDate.setHours(startDate.getHours() - 24 * 365);
46+
break;
47+
case "Deleted in the last 2 Hours":
48+
startDate = new Date();
49+
startDate.setHours(startDate.getHours() - 2);
50+
break;
51+
case "Deleted in the last 1 Day":
52+
startDate = new Date();
53+
startDate.setHours(startDate.getHours() - 24);
54+
break;
55+
case "Deleted in the last 7 days":
56+
startDate = new Date();
57+
startDate.setHours(startDate.getHours() - 24 * 7);
58+
break;
59+
}
60+
return loadPagedMessages(groupId.value, pageNumber.value, "", "", startDate.toISOString(), endDate.toISOString());
61+
}
62+
63+
async function loadGroupDetails(groupId: string) {
64+
const [, data] = await serviceControlStore.fetchTypedFromServiceControl<FailureGroup>(`archive/groups/id/${groupId}`);
65+
groupName.value = data.title;
66+
}
67+
68+
function loadPagedMessages(groupId?: string, page: number = 1, sortBy: string = "modified", direction: string = "desc", startDate: string = new Date(0).toISOString(), endDate: string = new Date().toISOString()) {
69+
const dateRange = startDate + "..." + endDate;
70+
let loadGroupDetailsPromise;
71+
if (groupId && !groupName.value) {
72+
loadGroupDetailsPromise = loadGroupDetails(groupId);
73+
}
74+
75+
async function loadDelMessages() {
76+
try {
77+
const [response, data] = await serviceControlStore.fetchTypedFromServiceControl<ExtendedFailedMessage[]>(
78+
`${groupId ? `recoverability/groups/${groupId}/` : ""}errors?status=archived&page=${page}&per_page=${perPage}&sort=${sortBy}&direction=${direction}&modified=${dateRange}`
79+
);
80+
81+
totalCount.value = parseInt(response.headers.get("Total-Count") ?? "0");
82+
83+
if (messages.value.length && data.length) {
84+
// merge the previously selected messages into the new list so we can replace them
85+
messages.value.forEach((previousMessage) => {
86+
const receivedMessage = data.find((m) => m.id === previousMessage.id);
87+
if (receivedMessage) {
88+
if (previousMessage.last_modified === receivedMessage.last_modified) {
89+
receivedMessage.retryInProgress = previousMessage.retryInProgress;
90+
receivedMessage.deleteInProgress = previousMessage.deleteInProgress;
91+
}
92+
93+
receivedMessage.selected = previousMessage.selected;
94+
}
95+
});
96+
}
97+
messages.value = updateMessagesScheduledDeletionDate(data);
98+
} catch (err) {
99+
console.log(err);
100+
const result = {
101+
message: "error",
102+
};
103+
return result;
104+
}
105+
}
106+
107+
const loadDelMessagesPromise = loadDelMessages();
108+
109+
if (loadGroupDetailsPromise) {
110+
return Promise.all([loadGroupDetailsPromise, loadDelMessagesPromise]);
111+
}
112+
113+
return loadDelMessagesPromise;
114+
}
115+
116+
function updateMessagesScheduledDeletionDate(messages: ExtendedFailedMessage[]) {
117+
//check deletion time
118+
messages.forEach((message) => {
119+
message.error_retention_period = dayjs.duration(configuration.value?.data_retention.error_retention_period ?? "PT0S").asHours();
120+
const countdown = dayjs(message.last_modified).add(message.error_retention_period, "hours");
121+
message.delete_soon = countdown < dayjs();
122+
message.deleted_in = countdown.format();
123+
});
124+
return messages;
125+
}
29126
30127
function numberSelected() {
31128
return messageList.value?.getSelectedMessages()?.length ?? 0;

src/Frontend/src/composables/formatter.spec.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "vitest";
2-
import { useFormatTime, useGetDayDiffFromToday, useFormatLargeNumber, createDateWithDayOffset } from "./formatter";
2+
import { useFormatTime, useGetDayDiffFromToday, useFormatLargeNumber } from "./formatter";
33

44
describe("useFormatTime", () => {
55
describe("milliseconds formatting", () => {
@@ -100,37 +100,47 @@ describe("useFormatTime", () => {
100100

101101
describe("useGetDayDiffFromToday", () => {
102102
test("returns 0 for today's date", () => {
103-
const today = createDateWithDayOffset();
103+
const today = new Date();
104+
today.setHours(12, 0, 0, 0);
104105
const result = useGetDayDiffFromToday(today.toISOString());
105106
expect(result).toBe(0);
106107
});
107108

108109
test("returns positive number for future dates", () => {
109-
const tomorrow = createDateWithDayOffset(1);
110+
const tomorrow = new Date();
111+
tomorrow.setDate(tomorrow.getDate() + 1);
112+
tomorrow.setHours(12, 0, 0, 0);
110113
const result = useGetDayDiffFromToday(tomorrow.toISOString());
111114
expect(result).toBe(1);
112115
});
113116

114117
test("returns negative number for past dates", () => {
115-
const yesterday = createDateWithDayOffset(-1);
118+
const yesterday = new Date();
119+
yesterday.setDate(yesterday.getDate() - 1);
120+
yesterday.setHours(12, 0, 0, 0);
116121
const result = useGetDayDiffFromToday(yesterday.toISOString());
117122
expect(result).toBe(-1);
118123
});
119124

120125
test("returns 7 for date 7 days in the future", () => {
121-
const futureDate = createDateWithDayOffset(7);
126+
const futureDate = new Date();
127+
futureDate.setDate(futureDate.getDate() + 7);
128+
futureDate.setHours(12, 0, 0, 0);
122129
const result = useGetDayDiffFromToday(futureDate.toISOString());
123130
expect(result).toBe(7);
124131
});
125132

126133
test("returns -30 for date 30 days in the past", () => {
127-
const pastDate = createDateWithDayOffset(-30);
134+
const pastDate = new Date();
135+
pastDate.setDate(pastDate.getDate() - 30);
136+
pastDate.setHours(12, 0, 0, 0);
128137
const result = useGetDayDiffFromToday(pastDate.toISOString());
129138
expect(result).toBe(-30);
130139
});
131140

132141
test("handles dates without Z suffix", () => {
133-
const date = createDateWithDayOffset();
142+
const date = new Date();
143+
date.setHours(12, 0, 0, 0);
134144
const isoString = date.toISOString().replace("Z", "");
135145
const result = useGetDayDiffFromToday(isoString);
136146
expect(result).toBe(0);

src/Frontend/src/stores/MessageStore.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { EditAndRetryConfig } from "@/resources/Configuration";
1414
import EditRetryResponse from "@/resources/EditRetryResponse";
1515
import { EditedMessage } from "@/resources/EditMessage";
1616
import useEnvironmentAndVersionsAutoRefresh from "@/composables/useEnvironmentAndVersionsAutoRefresh";
17-
import { timeSpanToDuration } from "@/composables/formatter";
1817

1918
interface Model {
2019
id?: string;
@@ -78,7 +77,7 @@ export const useMessageStore = defineStore("MessageStore", () => {
7877
const areSimpleHeadersSupported = environmentStore.serviceControlIsGreaterThan("5.2.0");
7978

8079
const { configuration } = storeToRefs(configStore);
81-
const error_retention_period = computed(() => timeSpanToDuration(configuration.value?.data_retention?.error_retention_period).asHours());
80+
const error_retention_period = computed(() => dayjs.duration(configuration.value?.data_retention?.error_retention_period ?? "PT0S").asHours());
8281

8382
async function loadEditAndRetryConfiguration() {
8483
try {

0 commit comments

Comments
 (0)