Skip to content

Commit

Permalink
feat #309: migrate getTopVesselsInActivity to getTopVesselsInMpas (da…
Browse files Browse the repository at this point in the history
…shboard)
  • Loading branch information
marthevienne committed Dec 13, 2024
1 parent 2eb8cd3 commit 2dcfa2a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
26 changes: 16 additions & 10 deletions frontend/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
"use client"
"use client";

import { useMemo, useState } from "react";
import { useDashboardData } from "@/services/dashboard.service";



import { getDateRange } from "@/libs/dateUtils";
import DashboardHeader from "@/components/dashboard/dashboard-header";
import DashboardOverview from "@/components/dashboard/dashboard-overview";



import { useMemo, useState } from "react"
import { useDashboardData } from "@/services/dashboard.service"

import { getDateRange } from "@/libs/dateUtils"
import DashboardHeader from "@/components/dashboard/dashboard-header"
import DashboardOverview from "@/components/dashboard/dashboard-overview"

export default function DashboardPage() {
const [selectedDays, setSelectedDays] = useState(7)
Expand All @@ -14,7 +20,7 @@ export default function DashboardPage() {
}, [selectedDays])

const {
topVesselsInActivity,
topVesselsInMpas,
topAmpsVisited,
totalVesselsInActivity,
totalAmpsVisited,
Expand All @@ -31,15 +37,15 @@ export default function DashboardPage() {

<div className="size-full">
<DashboardOverview
topVesselsInActivity={topVesselsInActivity}
topVesselsInMpas={topVesselsInMpas}
topAmpsVisited={topAmpsVisited}
totalVesselsActive={totalVesselsInActivity}
totalAmpsVisited={totalAmpsVisited}
totalVesselsTracked={totalVesselsTracked}
onDateRangeChange={(value) => {
setSelectedDays(Number(value))
}}
topVesselsInActivityLoading={isLoading.topVesselsInActivity}
topVesselsInMpasLoading={isLoading.topVesselsInMpas}
topAmpsVisitedLoading={isLoading.topAmpsVisited}
totalVesselsActiveLoading={isLoading.totalVesselsInActivity}
totalAmpsVisitedLoading={isLoading.totalAmpsVisited}
Expand All @@ -49,4 +55,4 @@ export default function DashboardPage() {
</div>
</section>
)
}
}
31 changes: 18 additions & 13 deletions frontend/components/dashboard/dashboard-overview.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"use client"
"use client";

import { TOTAL_AMPS, TOTAL_VESSELS } from "@/constants/totals.constants"
import { TOTAL_AMPS, TOTAL_VESSELS } from "@/constants/totals.constants";

import { Item } from "@/types/item"
import ListCard from "@/components/ui/list-card"
import KPICard from "@/components/dashboard/kpi-card"

import { DateRangeSelector } from "../ui/date-range-selector"

import { Item } from "@/types/item";
import ListCard from "@/components/ui/list-card";
import KPICard from "@/components/dashboard/kpi-card";



import { DateRangeSelector } from "../ui/date-range-selector";


type Props = {
topVesselsInActivity: Item[]
topVesselsInActivityLoading: boolean
topVesselsInMpas: Item[]
topVesselsInMpasLoading: boolean
topAmpsVisited: Item[]
topAmpsVisitedLoading: boolean
totalVesselsActive: number
Expand All @@ -23,8 +28,8 @@ type Props = {
}

export default function DashboardOverview({
topVesselsInActivity,
topVesselsInActivityLoading,
topVesselsInMpas,
topVesselsInMpasLoading,
topAmpsVisited,
topAmpsVisitedLoading,
totalVesselsActive,
Expand Down Expand Up @@ -74,14 +79,14 @@ export default function DashboardOverview({
<ListCard
key="top-vessels-in-activity"
title="Top Vessels visiting MPAs"
items={topVesselsInActivity ?? []}
items={topVesselsInMpas ?? []}
enableViewDetails
loading={topVesselsInActivityLoading}
loading={topVesselsInMpasLoading}
titleClassName="absolute -top-8 xl:-top-10"
/>
</div>
</div>
</div>
</section>
)
}
}
2 changes: 1 addition & 1 deletion frontend/libs/mapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function convertVesselDtoToItem(metrics: VesselMetrics[]): Item[] {
id: `${vessel.id}`,
title: vessel.ship_name,
description: `IMO ${vessel.imo} / MMSI ${vessel.mmsi} / ${vessel.length} m`,
value: convertDurationToString(vesselMetrics.total_time_at_sea),
value: convertDurationToString(vesselMetrics.total_time_in_mpas),
type: "vessel",
countryIso3: vessel.country_iso3,
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/services/backend-rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ export async function getVesselFirstExcursionSegments(vesselId: number) {
}
}

export function getTopVesselsInActivity(
export function getTopVesselsInMpas(
startAt: string,
endAt: string,
topVesselsLimit: number
) {
const url = `${BASE_URL}/metrics/vessels-in-activity?start_at=${startAt}&end_at=${endAt}&limit=${topVesselsLimit}&order=DESC`
const url = `${BASE_URL}/metrics/vessels/activity-in-mpas?start_at=${startAt}&end_at=${endAt}&limit=${topVesselsLimit}&order=DESC`
console.log(`GET ${url}`)
return axios.get<VesselMetrics[]>(url)
}
Expand Down
20 changes: 10 additions & 10 deletions frontend/services/dashboard.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TOTAL_VESSELS } from "@/constants/totals.constants"
import {
getTopVesselsInActivity,
getTopVesselsInMpas,
getTopZonesVisited,
getVesselsAtSea,
getVesselsTrackedCount,
Expand All @@ -13,13 +13,13 @@ import { convertVesselDtoToItem, convertZoneDtoToItem } from "@/libs/mapper"
const TOP_ITEMS_SIZE = 5

type DashboardData = {
topVesselsInActivity: any[]
topVesselsInMpas: any[]
topAmpsVisited: any[]
totalVesselsInActivity: number
totalAmpsVisited: number
totalVesselsTracked: number
isLoading: {
topVesselsInActivity: boolean
topVesselsInMpas: boolean
topAmpsVisited: boolean
totalVesselsInActivity: boolean
totalAmpsVisited: boolean
Expand All @@ -32,21 +32,21 @@ export const useDashboardData = (
endAt: string
): DashboardData => {
const {
data: topVesselsInActivity = [],
isLoading: topVesselsInActivityLoading,
data: topVesselsInMpas = [],
isLoading: topVesselsInMpasLoading,
} = useSWR(
`topVesselsInActivity-${startAt}`,
`topVesselsInMpas-${startAt}`,
async () => {
try {
const response = await getTopVesselsInActivity(
const response = await getTopVesselsInMpas(
startAt,
endAt,
TOP_ITEMS_SIZE
)
return convertVesselDtoToItem(response?.data || [])
} catch (error) {
console.log(
"An error occurred while fetching top vessels in activity: " + error
"An error occurred while fetching top vessels in MPAs: " + error
)
return []
}
Expand Down Expand Up @@ -137,13 +137,13 @@ export const useDashboardData = (
)

return {
topVesselsInActivity,
topVesselsInMpas,
topAmpsVisited,
totalVesselsInActivity,
totalAmpsVisited,
totalVesselsTracked,
isLoading: {
topVesselsInActivity: topVesselsInActivityLoading,
topVesselsInMpas: topVesselsInMpasLoading,
topAmpsVisited: topAmpsVisitedLoading,
totalVesselsInActivity: totalVesselsInActivityLoading,
totalAmpsVisited: totalAmpsVisitedLoading,
Expand Down
2 changes: 1 addition & 1 deletion frontend/types/vessel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type Vessel = {

export type VesselMetrics = {
vessel: VesselDetails
total_time_at_sea: string
total_time_in_mpas: string
}

export type VesselDetails = {
Expand Down

0 comments on commit 2dcfa2a

Please sign in to comment.