Skip to content

Commit cfce8bc

Browse files
authored
feat: update api /alerts and /detections (#181) (#188)
1 parent fd88aaf commit cfce8bc

File tree

17 files changed

+117
-279
lines changed

17 files changed

+117
-279
lines changed

src/components/Alerts/AlertDetails/AlertImages/DetectionImageWithBoundingBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const parseDetectionBox = (
1818
return null;
1919
}
2020

21-
const match = /\(([^)]+)\)/.exec(detection.bboxes);
21+
const match = /\(([^)]+)\)/.exec(detection.bbox);
2222
if (!match) {
2323
return null;
2424
}

src/components/Alerts/AlertDetails/AlertInfos/AlertInfos.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export const AlertInfos = ({
7373
</AlertInfosSection>
7474
<AlertInfosSection
7575
title={t('subtitleAzimuth')}
76-
withTextToCopy={sequence.coneAzimuth.toString()}
76+
withTextToCopy={sequence.azimuth.toString()}
7777
>
78-
{formatAzimuth(sequence.coneAzimuth, 1)}
78+
{formatAzimuth(sequence.azimuth, 1)}
7979
</AlertInfosSection>
8080
<AlertInfosSection
8181
title={t('subtitleCameraLocalisation')}

src/components/Alerts/AlertDetails/AlertInfos/AlertMap.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const AlertMap = ({ alert, height = '100%' }: AlertMap) => {
4747
visionPolygonPoints: buildVisionPolygon(
4848
seq.camera.lat,
4949
seq.camera.lon,
50-
seq.coneAzimuth,
50+
seq.azimuth,
5151
seq.coneAngle,
5252
DEFAULT_CAM_RANGE_KM
5353
),

src/components/Alerts/AlertsContainer.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ export const AlertsContainer = ({
4444

4545
const selectedAlert = useMemo(
4646
() =>
47-
alertsList.find((alert) => alert.id === searchParams.get('alert')) ??
48-
null,
47+
alertsList.find(
48+
(alert) => alert.id.toString() === searchParams.get('alert')
49+
) ?? null,
4950
[alertsList, searchParams]
5051
);
5152

5253
const setSelectedAlert = useCallback(
5354
(alert: AlertType) => {
54-
setSearchParams({ alert: alert.id });
55+
setSearchParams({ alert: alert.id.toString() });
5556
},
5657
[setSearchParams]
5758
);

src/components/Alerts/AlertsList/AlertCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const AlertCard = ({
9494
flexGrow={1}
9595
>
9696
<Typography variant="caption" fontWeight={500}>
97-
{formatAzimuth(sequence.coneAzimuth)}
97+
{formatAzimuth(sequence.azimuth)}
9898
</Typography>
9999

100100
<Typography variant="caption">

src/components/Live/LiveContent/Control/SelectionCameraWithAlert.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const SelectionCameraWithAlert = ({
6565
{formatIsoToDateTime(currentSequence.startedAt)}
6666
</LiveAlertInfosSection>
6767
<LiveAlertInfosSection title={t('subtitleAzimuth')}>
68-
{formatAzimuth(currentSequence.coneAzimuth, 1)}
68+
{formatAzimuth(currentSequence.azimuth, 1)}
6969
</LiveAlertInfosSection>
7070
<LiveAlertInfosSection title={t('subtitleCameraLocalisation')}>
7171
{formatPosition(

src/pages/AlertsPage.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { useQuery, useQueryClient } from '@tanstack/react-query';
22
import { useCallback, useMemo } from 'react';
33

44
import { AlertsContainer } from '@/components/Alerts/AlertsContainer';
5-
import { getUnlabelledLatestSequences } from '@/services/alerts';
5+
import { getUnlabelledLatestAlerts } from '@/services/alerts';
66
import appConfig from '@/services/appConfig';
77
import { STATUS_ERROR, STATUS_LOADING, STATUS_SUCCESS } from '@/services/axios';
88
import { getCameraList } from '@/services/camera';
9-
import { type AlertType, convertSequencesToAlerts } from '@/utils/alerts';
9+
import { type AlertType, mapAlertTypeApiToAlertType } from '@/utils/alerts';
1010
import { isDateToday } from '@/utils/dates';
11-
import { useDetectNewSequences } from '@/utils/useDetectNewSequences';
11+
import { useDetectNewSequences as useDetectNewAlerts } from '@/utils/useDetectNewSequences';
1212

1313
const ALERTS_LIST_REFRESH_INTERVAL_SECONDS =
1414
appConfig.getConfig().ALERTS_LIST_REFRESH_INTERVAL_SECONDS;
@@ -19,10 +19,10 @@ export const AlertsPage = () => {
1919
isFetching,
2020
dataUpdatedAt,
2121
status: statusSequences,
22-
data: sequenceList,
22+
data: alertList,
2323
} = useQuery({
24-
queryKey: ['unlabelledSequences'],
25-
queryFn: getUnlabelledLatestSequences,
24+
queryKey: ['unlabelledAlerts'],
25+
queryFn: getUnlabelledLatestAlerts,
2626
refetchInterval: ALERTS_LIST_REFRESH_INTERVAL_SECONDS * 1000,
2727
});
2828

@@ -31,23 +31,23 @@ export const AlertsPage = () => {
3131
queryFn: getCameraList,
3232
});
3333

34-
const todaySequences = useMemo(
35-
() => (sequenceList ?? []).filter((seq) => isDateToday(seq.started_at)),
36-
[sequenceList]
34+
const todayAlerts = useMemo(
35+
() => (alertList ?? []).filter((alert) => isDateToday(alert.started_at)),
36+
[alertList]
3737
);
3838

3939
const alertsList: AlertType[] = useMemo(
40-
() => convertSequencesToAlerts(todaySequences, cameraList ?? []),
41-
[todaySequences, cameraList]
40+
() => mapAlertTypeApiToAlertType(todayAlerts, cameraList ?? []),
41+
[todayAlerts, cameraList]
4242
);
4343

44-
const { hasNewSequence } = useDetectNewSequences(
45-
todaySequences,
44+
const { hasNewSequence: hasNewAlert } = useDetectNewAlerts(
45+
todayAlerts,
4646
dataUpdatedAt
4747
);
4848

4949
const invalidateAndRefreshData = useCallback(() => {
50-
void queryClient.invalidateQueries({ queryKey: ['unlabelledSequences'] });
50+
void queryClient.invalidateQueries({ queryKey: ['unlabelledAlerts'] });
5151
}, [queryClient]);
5252

5353
const status = useMemo(() => {
@@ -67,7 +67,7 @@ export const AlertsPage = () => {
6767
lastUpdate={dataUpdatedAt}
6868
invalidateAndRefreshData={invalidateAndRefreshData}
6969
alertsList={alertsList}
70-
hasNewSequence={hasNewSequence}
70+
hasNewSequence={hasNewAlert}
7171
/>
7272
);
7373
};

src/pages/HistoryPage.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
} from '@tanstack/react-query';
66
import { useCallback, useMemo, useState } from 'react';
77

8-
import { getSequencesByFilters } from '@/services/alerts';
8+
import { getAlertsByFilters } from '@/services/alerts';
99
import appConfig from '@/services/appConfig';
1010
import { STATUS_ERROR, STATUS_LOADING, STATUS_SUCCESS } from '@/services/axios';
1111
import { getCameraList } from '@/services/camera';
12-
import { type AlertType, convertSequencesToAlerts } from '@/utils/alerts';
12+
import { type AlertType, mapAlertTypeApiToAlertType } from '@/utils/alerts';
1313
import { formatDateToApi } from '@/utils/dates';
1414
import {
1515
type FiltersType,
@@ -37,9 +37,9 @@ export const HistoryPage = () => {
3737
isFetchingNextPage,
3838
} = useInfiniteQuery({
3939
enabled: isQuerySequencesEnabled,
40-
queryKey: ['sequenceList', filters],
40+
queryKey: ['alertsList', filters],
4141
queryFn: async ({ pageParam }) => {
42-
return await getSequencesByFilters(
42+
return await getAlertsByFilters(
4343
filters.date ? formatDateToApi(filters.date) : '',
4444
HISTORY_NB_ALERTS_PER_PAGE,
4545
pageParam * HISTORY_NB_ALERTS_PER_PAGE
@@ -62,11 +62,11 @@ export const HistoryPage = () => {
6262
});
6363

6464
const invalidateAndRefreshData = useCallback(() => {
65-
void queryClient.invalidateQueries({ queryKey: ['sequenceList'] });
65+
void queryClient.invalidateQueries({ queryKey: ['alertsList'] });
6666
}, [queryClient]);
6767

6868
const alertsList: AlertType[] = useMemo(() => {
69-
return convertSequencesToAlerts(
69+
return mapAlertTypeApiToAlertType(
7070
sequenceList?.pages.flat() ?? [],
7171
cameraList ?? []
7272
);

src/services/alerts.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,49 @@ import { apiInstance } from './axios';
77
const apiSequenceResponseSchema = z.object({
88
id: z.number(),
99
camera_id: z.number(),
10-
azimuth: z.nullable(z.number()),
11-
cone_azimuth: z.number(),
10+
pose_id: z.number(),
11+
sequence_azimuth: z.number(),
1212
cone_angle: z.number(),
1313
is_wildfire: z.nullable(z.string()),
1414
started_at: z.nullable(z.iso.datetime({ local: true })),
1515
last_seen_at: z.nullable(z.string()),
16-
event_groups: z.union([z.undefined(), z.array(z.array(z.number()))]),
17-
event_smoke_locations: z.union([
18-
z.undefined(),
19-
z.array(z.tuple([z.number(), z.number()])),
20-
]),
16+
});
17+
18+
const apiAlertResponseSchema = z.object({
19+
id: z.number(),
20+
organization_id: z.number(),
21+
lat: z.nullable(z.number()),
22+
lon: z.nullable(z.number()),
23+
started_at: z.iso.datetime({ local: true }),
24+
last_seen_at: z.iso.datetime({ local: true }),
25+
sequences: z.array(apiSequenceResponseSchema),
2126
});
2227

2328
const apiDetectionResponseSchema = z.object({
2429
id: z.number(),
2530
camera_id: z.number(),
26-
azimuth: z.nullable(z.number()),
31+
pose_id: z.number(),
32+
sequence_id: z.number(),
2733
bucket_key: z.string(),
28-
bboxes: z.string(),
34+
bbox: z.string(),
35+
others_bboxes: z.nullable(z.string()),
2936
created_at: z.iso.datetime({ local: true }),
3037
url: z.string(),
3138
});
3239

33-
export type SequenceType = z.infer<typeof apiSequenceResponseSchema>;
34-
const apiSequenceListResponseSchema = z.array(apiSequenceResponseSchema);
40+
export type SequenceTypeApi = z.infer<typeof apiSequenceResponseSchema>;
41+
export type AlertTypeApi = z.infer<typeof apiAlertResponseSchema>;
42+
const apiAlertListResponseSchema = z.array(apiAlertResponseSchema);
3543

3644
export type DetectionType = z.infer<typeof apiDetectionResponseSchema>;
3745
const apiDetectionListResponseSchema = z.array(apiDetectionResponseSchema);
3846

39-
export const getUnlabelledLatestSequences = async (): Promise<
40-
SequenceType[]
41-
> => {
47+
export const getUnlabelledLatestAlerts = async (): Promise<AlertTypeApi[]> => {
4248
return apiInstance
43-
.get('/api/v1/sequences/unlabeled/latest')
49+
.get('/api/v1/alerts/unlabeled/latest')
4450
.then((response: AxiosResponse) => {
4551
try {
46-
const result = apiSequenceListResponseSchema.safeParse(response.data);
52+
const result = apiAlertListResponseSchema.safeParse(response.data);
4753
return result.data ?? [];
4854
} catch {
4955
throw new Error('INVALID_API_RESPONSE');
@@ -55,21 +61,21 @@ export const getUnlabelledLatestSequences = async (): Promise<
5561
});
5662
};
5763

58-
export const getSequencesByFilters = async (
64+
export const getAlertsByFilters = async (
5965
fromDate: string,
6066
limit: number,
6167
offset: number
62-
): Promise<SequenceType[]> => {
68+
): Promise<AlertTypeApi[]> => {
6369
const params = {
6470
from_date: fromDate,
6571
limit,
6672
offset,
6773
};
6874
return apiInstance
69-
.get('/api/v1/sequences/all/fromdate', { params })
75+
.get('/api/v1/alerts/all/fromdate', { params })
7076
.then((response: AxiosResponse) => {
7177
try {
72-
const result = apiSequenceListResponseSchema.safeParse(response.data);
78+
const result = apiAlertListResponseSchema.safeParse(response.data);
7379
return result.data ?? [];
7480
} catch {
7581
throw new Error('INVALID_API_RESPONSE');
@@ -89,6 +95,7 @@ export const getDetectionsBySequence = async (
8995
.then((response: AxiosResponse) => {
9096
try {
9197
const result = apiDetectionListResponseSchema.safeParse(response.data);
98+
console.log(result);
9299
if (result.data) {
93100
result.data.sort(
94101
(d1, d2) =>

0 commit comments

Comments
 (0)