Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix charts in bouts page due to missing timestamp conversion #716

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions ui/src/components/Bouts/FeedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export default function FeedItem({
feed: Pick<Feed, "id" | "name" | "slug" | "online">;
onStatUpdate?: (feedId: string, stat: string, value: number) => void;
}) {
const categories: Array<DetectionCategory> = ["WHALE", "VESSEL", "OTHER"];
const categories: Array<DetectionCategory> = useMemo(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need useMemo, this can just be moved outside the component (and then you can remove it from the useEffect dependency array)

() => ["WHALE", "VESSEL", "OTHER"],
[],
);

const [showTable, setShowTable] = useState(false);
const [selectedCategory, setSelectedCategory] = useState<DetectionCategory>();
Expand Down Expand Up @@ -63,10 +66,12 @@ export default function FeedItem({

const detsCount = recentDetections.length;
const detsCount15MinAgo = recentDetections.filter(
({ timestamp }) => timestamp > new Date(now.valueOf() - 15 * 60 * 1000),
({ timestamp }) =>
new Date(timestamp) > new Date(now.valueOf() - 15 * 60 * 1000),
).length;
const detsCount5MinAgo = recentDetections.filter(
({ timestamp }) => timestamp > new Date(now.valueOf() - 5 * 60 * 1000),
({ timestamp }) =>
new Date(timestamp) > new Date(now.valueOf() - 5 * 60 * 1000),
Comment on lines +69 to +74
Copy link

@coderabbitai coderabbitai bot Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize timestamp comparisons

While the timestamp conversion fixes are correct, creating Date objects inside filter callbacks may impact performance. Consider pre-calculating the comparison timestamps:

+ const fifteenMinutesAgo = new Date(now.valueOf() - 15 * 60 * 1000);
+ const fiveMinutesAgo = new Date(now.valueOf() - 5 * 60 * 1000);

  const detsCount15MinAgo = recentDetections.filter(
-   ({ timestamp }) =>
-     new Date(timestamp) > new Date(now.valueOf() - 15 * 60 * 1000),
+   ({ timestamp }) => new Date(timestamp) > fifteenMinutesAgo,
  ).length;
  const detsCount5MinAgo = recentDetections.filter(
-   ({ timestamp }) =>
-     new Date(timestamp) > new Date(now.valueOf() - 5 * 60 * 1000),
+   ({ timestamp }) => new Date(timestamp) > fiveMinutesAgo,
  ).length;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
({ timestamp }) =>
new Date(timestamp) > new Date(now.valueOf() - 15 * 60 * 1000),
).length;
const detsCount5MinAgo = recentDetections.filter(
({ timestamp }) => timestamp > new Date(now.valueOf() - 5 * 60 * 1000),
({ timestamp }) =>
new Date(timestamp) > new Date(now.valueOf() - 5 * 60 * 1000),
const fifteenMinutesAgo = new Date(now.valueOf() - 15 * 60 * 1000);
const fiveMinutesAgo = new Date(now.valueOf() - 5 * 60 * 1000);
const detsCount15MinAgo = recentDetections.filter(
({ timestamp }) => new Date(timestamp) > fifteenMinutesAgo,
).length;
const detsCount5MinAgo = recentDetections.filter(
({ timestamp }) => new Date(timestamp) > fiveMinutesAgo,
).length;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a reasonable suggestion?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulcretu, yes, this is a reasonable suggestion. Pre-calculating the comparison timestamps can improve performance by avoiding the creation of new Date objects inside the filter callbacks. This optimization can make your code more efficient.

).length;

const detectionChartData = useMemo(
Expand All @@ -76,7 +81,7 @@ export default function FeedItem({
({
cat: category,
minutesAgo: Math.floor(
(now.valueOf() - timestamp.valueOf()) / (60 * 1000),
(now.valueOf() - new Date(timestamp).valueOf()) / (60 * 1000),
),
}),
),
Expand All @@ -91,17 +96,22 @@ export default function FeedItem({
if (typeof detsCount === "number") {
onStatUpdate(feed.id, "detections", detsCount);
}
["whale", "vessel", "other"].forEach((cat) => {
categories.forEach((cat) => {
onStatUpdate(
feed.id,
cat,
recentDetections.filter(
({ category }) => category?.toLocaleLowerCase() === cat,
).length,
recentDetections.filter(({ category }) => category === cat).length,
);
});
}
}, [feed.id, recentDetections, detsCount, onStatUpdate, listenerCount]);
}, [
feed.id,
recentDetections,
detsCount,
onStatUpdate,
listenerCount,
categories,
]);

return (
<Card sx={{ width: "100%", p: 2, overflowX: "auto" }} elevation={1}>
Expand Down
Loading