-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog.ts
More file actions
96 lines (82 loc) · 3.04 KB
/
catalog.ts
File metadata and controls
96 lines (82 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { ipcMain } from 'electron';
import {
getDetections,
searchSpecies,
getSpeciesSummary,
getSpeciesLocations,
getLocationSpecies,
getCatalogStats,
} from '../db/detections';
import { clearDatabase } from '../db/database';
import { getLocations, getLocationsWithCounts } from '../db/locations';
import { getRunsWithStats, deleteRun } from '../db/runs';
import { resolveAll, searchByCommonName } from '../labels/label-service';
import type {
Detection,
DetectionFilter,
SpeciesSummary,
EnrichedDetection,
EnrichedSpeciesSummary,
} from '$shared/types';
function enrichDetections(detections: Detection[]): EnrichedDetection[] {
const scientificNames = [...new Set(detections.map((d) => d.scientific_name))];
const nameMap = resolveAll(scientificNames);
return detections.map((d) => ({
...d,
common_name: nameMap.get(d.scientific_name) ?? d.scientific_name,
}));
}
function enrichSpeciesSummaries(summaries: SpeciesSummary[]): EnrichedSpeciesSummary[] {
const scientificNames = summaries.map((s) => s.scientific_name);
const nameMap = resolveAll(scientificNames);
return summaries.map((s) => ({
...s,
common_name: nameMap.get(s.scientific_name) ?? s.scientific_name,
}));
}
export function registerCatalogHandlers(): void {
ipcMain.handle('catalog:get-runs', () => {
return getRunsWithStats();
});
ipcMain.handle('catalog:delete-run', (_event, id: number) => {
deleteRun(id);
});
ipcMain.handle('catalog:get-detections', (_event, filter: DetectionFilter) => {
// If species filter is set, also resolve common name matches from label service
if (filter.species) {
const matchingScientific = searchByCommonName(filter.species);
if (matchingScientific.length > 0) {
filter = { ...filter, scientific_names: matchingScientific };
}
}
const result = getDetections(filter);
return { detections: enrichDetections(result.detections), total: result.total };
});
ipcMain.handle('catalog:search-species', (_event, query: string) => {
// Get scientific names matching the common name query from label service
const matchingScientific = searchByCommonName(query);
const dbResults = searchSpecies(query, matchingScientific.length > 0 ? matchingScientific : undefined);
return enrichSpeciesSummaries(dbResults);
});
ipcMain.handle('catalog:get-species-summary', () => {
return enrichSpeciesSummaries(getSpeciesSummary());
});
ipcMain.handle('catalog:species-locations', (_event, scientificName: string) => {
return getSpeciesLocations(scientificName);
});
ipcMain.handle('catalog:location-species', (_event, locationId: number) => {
return enrichSpeciesSummaries(getLocationSpecies(locationId));
});
ipcMain.handle('catalog:get-locations', () => {
return getLocations();
});
ipcMain.handle('catalog:get-locations-with-counts', () => {
return getLocationsWithCounts();
});
ipcMain.handle('catalog:stats', () => {
return getCatalogStats();
});
ipcMain.handle('catalog:clear-database', () => {
return clearDatabase();
});
}