-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add species lists with fetch, custom lists, and detection filtering #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c3a0fb8
feat: add species lists with fetch, custom lists, and detection filte…
tphakala 220294d
fix: address code review feedback for species lists feature
tphakala 06fb045
fix: add intermediate unknown cast for payload type assertion
tphakala c2439dc
fix: address second round of review feedback
tphakala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { execFile } from 'child_process'; | ||
| import { findBirda } from './runner'; | ||
| import type { BirdaSpeciesResponse } from '$shared/types'; | ||
|
|
||
| interface BirdaJsonEnvelope { | ||
| spec_version: string; | ||
| timestamp: string; | ||
| event: string; | ||
| payload?: Record<string, unknown>; | ||
| } | ||
|
|
||
| export async function fetchSpecies( | ||
| latitude: number, | ||
| longitude: number, | ||
| week: number, | ||
| threshold?: number, | ||
| ): Promise<BirdaSpeciesResponse> { | ||
| const birdaPath = await findBirda(); | ||
| const args = [ | ||
| '--output-mode', | ||
| 'json', | ||
| 'species', | ||
| '--lat', | ||
| String(latitude), | ||
| '--lon', | ||
| String(longitude), | ||
| '--week', | ||
| String(week), | ||
| ]; | ||
| if (threshold !== undefined) { | ||
| args.push('--threshold', String(threshold)); | ||
| } | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| execFile(birdaPath, args, { maxBuffer: 10 * 1024 * 1024, timeout: 30000 }, (err, stdout, stderr) => { | ||
| if (err) { | ||
| reject(new Error(`birda species command failed: ${stderr || err.message}`)); | ||
| return; | ||
| } | ||
| try { | ||
| const envelope = JSON.parse(stdout) as BirdaJsonEnvelope; | ||
| const payload = envelope.payload; | ||
| if (!payload || typeof payload !== 'object' || !('species' in payload)) { | ||
| reject(new Error('Unexpected payload format from birda species command')); | ||
| return; | ||
| } | ||
| resolve(payload as unknown as BirdaSpeciesResponse); | ||
| } catch (e) { | ||
| const detail = e instanceof Error ? e.message : String(e); | ||
| reject(new Error(`Failed to parse birda species output: ${detail}. Output: ${stdout.slice(0, 200)}`)); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { getDb } from './database'; | ||
| import type { SpeciesList, SpeciesListEntry, BirdaSpeciesResult } from '$shared/types'; | ||
|
|
||
| export function createSpeciesList( | ||
| name: string, | ||
| source: 'fetched' | 'custom', | ||
| species: BirdaSpeciesResult[], | ||
| opts?: { | ||
| description?: string; | ||
| latitude?: number; | ||
| longitude?: number; | ||
| week?: number; | ||
| threshold?: number; | ||
| }, | ||
| ): SpeciesList { | ||
| const db = getDb(); | ||
| return db.transaction(() => { | ||
| const result = db | ||
| .prepare( | ||
| `INSERT INTO species_lists (name, description, source, latitude, longitude, week, threshold, species_count) | ||
| VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, | ||
| ) | ||
| .run( | ||
| name, | ||
| opts?.description ?? null, | ||
| source, | ||
| opts?.latitude ?? null, | ||
| opts?.longitude ?? null, | ||
| opts?.week ?? null, | ||
| opts?.threshold ?? null, | ||
| species.length, | ||
| ); | ||
|
|
||
| const listId = result.lastInsertRowid as number; | ||
| const stmt = db.prepare( | ||
| `INSERT INTO species_list_entries (list_id, scientific_name, common_name, frequency) | ||
| VALUES (?, ?, ?, ?)`, | ||
| ); | ||
| for (const s of species) { | ||
| stmt.run(listId, s.scientific_name, s.common_name, s.frequency); | ||
| } | ||
|
|
||
| const created = getSpeciesListById(listId); | ||
| if (!created) throw new Error(`Failed to retrieve species list ${listId} after creation`); | ||
| return created; | ||
| })(); | ||
| } | ||
|
|
||
| export function getSpeciesLists(): SpeciesList[] { | ||
| const db = getDb(); | ||
| return db.prepare('SELECT * FROM species_lists ORDER BY created_at DESC').all() as SpeciesList[]; | ||
| } | ||
|
|
||
| function getSpeciesListById(id: number): SpeciesList | undefined { | ||
| const db = getDb(); | ||
| return db.prepare('SELECT * FROM species_lists WHERE id = ?').get(id) as SpeciesList | undefined; | ||
| } | ||
|
|
||
| export function getSpeciesListEntries(listId: number): SpeciesListEntry[] { | ||
| const db = getDb(); | ||
| return db | ||
| .prepare( | ||
| `SELECT * FROM species_list_entries | ||
| WHERE list_id = ? | ||
| ORDER BY frequency DESC, scientific_name ASC`, | ||
| ) | ||
| .all(listId) as SpeciesListEntry[]; | ||
| } | ||
|
|
||
| export function deleteSpeciesList(id: number): void { | ||
| const db = getDb(); | ||
| db.prepare('DELETE FROM species_lists WHERE id = ?').run(id); | ||
| } | ||
|
|
||
| export function createCustomSpeciesList(name: string, scientificNames: string[], description?: string): SpeciesList { | ||
| const db = getDb(); | ||
| return db.transaction(() => { | ||
| const result = db | ||
| .prepare( | ||
| `INSERT INTO species_lists (name, description, source, species_count) | ||
| VALUES (?, ?, 'custom', ?)`, | ||
| ) | ||
| .run(name, description ?? null, scientificNames.length); | ||
|
|
||
| const listId = result.lastInsertRowid as number; | ||
| const stmt = db.prepare( | ||
| `INSERT INTO species_list_entries (list_id, scientific_name) | ||
| VALUES (?, ?)`, | ||
| ); | ||
| for (const sn of scientificNames) { | ||
| stmt.run(listId, sn); | ||
| } | ||
|
|
||
| const created = getSpeciesListById(listId); | ||
| if (!created) throw new Error(`Failed to retrieve custom species list ${listId} after creation`); | ||
| return created; | ||
| })(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type assertion
as unknown as BirdaSpeciesResponseis unsafe and can lead to runtime errors if thebirdaCLI payload format changes. A simple property check would make this more robust. Additionally, thecatchblock for JSON parsing should capture the error object for better diagnostics.