CloudLogList: group by calendar days

This commit is contained in:
Sidney Alcantara
2021-11-09 23:27:04 +11:00
parent 472cb2dcc6
commit ea74a17404

View File

@@ -1,4 +1,5 @@
import _get from "lodash/get";
import { differenceInCalendarDays } from "date-fns";
import { List, ListProps } from "@mui/material";
import CloudLogSubheader from "./CloudLogSubheader";
@@ -15,16 +16,43 @@ export default function CloudLogList({ items, ...props }: ICloudLogListProps) {
const item = items[i];
const prevItem = items[i - 1];
if (
_get(item, "labels.execution_id") !==
_get(prevItem, "labels.execution_id")
) {
renderedLogItems.push(
<CloudLogSubheader key={_get(item, "labels.execution_id")}>
Function <code>{_get(item, "resource.labels.function_name")}</code>{" "}
execution <code>{_get(item, "labels.execution_id")}</code>
</CloudLogSubheader>
// Group by function execution ID if available
if (item.labels.execution_id) {
if (
_get(item, "labels.execution_id") !==
_get(prevItem, "labels.execution_id")
) {
renderedLogItems.push(
<CloudLogSubheader key={_get(item, "labels.execution_id")}>
Function{" "}
<code>{_get(item, "resource.labels.function_name")}</code>{" "}
execution <code>{_get(item, "labels.execution_id")}</code>
</CloudLogSubheader>
);
}
}
// Otherwise, group by day
else {
const diff = differenceInCalendarDays(
Date.now(),
(_get(item, "timestamp.seconds") ?? 0) * 1000
);
const prevDiff = differenceInCalendarDays(
Date.now(),
(_get(prevItem, "timestamp.seconds") ?? 0) * 1000
);
if (diff !== prevDiff) {
renderedLogItems.push(
<CloudLogSubheader key={`${diff} days ago`}>
{diff === 0
? "Today"
: diff === 1
? "Yesterday"
: `${diff} days ago`}
</CloudLogSubheader>
);
}
}
renderedLogItems.push(