Skip to content

Commit

Permalink
store whether it's daylight in db records table
Browse files Browse the repository at this point in the history
filter summary by nocmig mode
  • Loading branch information
Mattk70 committed Nov 5, 2023
1 parent 33353a5 commit a2087c1
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const createDB = async (file) => {
await db.runAsync(`CREATE TABLE records(
dateTime INTEGER, position INTEGER, fileID INTEGER,
speciesID INTEGER, confidence INTEGER, label TEXT,
comment TEXT, end INTEGER, callCount INTEGER,
comment TEXT, end INTEGER, callCount INTEGER, isDaylight INTEGER,
UNIQUE (dateTime, fileID, speciesID),
CONSTRAINT fk_files
FOREIGN KEY (fileID) REFERENCES files(id) ON DELETE CASCADE,
Expand Down Expand Up @@ -121,6 +121,14 @@ async function loadDB(path) {
if (count) {
UI.postMessage({ event: 'diskDB-has-records' })
}
const sql = 'PRAGMA table_info(records)';
const result = await diskDB.allAsync(sql);
// Update legacy tables
const columnExists = result.some((column) => column.name === 'isDaylight');
if (!columnExists) {
await diskDB.runAsync('ALTER TABLE records ADD COLUMN isDaylight INTEGER')
console.log('Added isDaylight column to records table')
}
console.log("Opened and cleaned disk db " + file)
}
return true
Expand Down Expand Up @@ -448,7 +456,7 @@ const prepSummaryStatement = () => {
const useRange = range?.start;
let summaryStatement = `
WITH ranked_records AS (
SELECT records.dateTime, records.speciesID, records.confidence, records.fileID, cname, sname, callCount,
SELECT records.dateTime, records.speciesID, records.confidence, records.fileID, cname, sname, callCount, isDaylight
RANK() OVER (PARTITION BY records.dateTime ORDER BY records.confidence DESC) AS rank
FROM records
JOIN files ON files.id = records.fileID
Expand All @@ -461,6 +469,9 @@ const prepSummaryStatement = () => {
else if (useRange) {
extraClause += ' AND dateTime BETWEEN ? AND ? ';
}
if (STATE.detect.nocmig){
extraClause += ' AND isDaylight IS NOT TRUE ';
}
if (STATE.blocked.length) {
const excluded = prepParams(STATE.blocked);
extraClause += ` AND speciesID NOT IN (${excluded}) `;
Expand Down Expand Up @@ -1584,9 +1595,8 @@ const terminateWorkers = () => {
predictWorkers = [];
}

const insertRecord = async (key, speciesID, confidence, file) => {

const offset = key * 1000;
const insertRecord = async (timestamp, key, speciesID, confidence, file) => {
const isDaylight = isDuringDaylight(timestamp, metadata[file].latitude, metadata[file].longitude)
let changes, fileID;
confidence = Math.round(confidence);
const db = memoryDB; //STATE.db;
Expand All @@ -1605,9 +1615,9 @@ const insertRecord = async (key, speciesID, confidence, file) => {
// No "OR IGNORE" in this statement because it should only run when the file is new
await db.runAsync(`INSERT OR IGNORE INTO duration VALUES ${durationSQL}`);
}
await db.runAsync('INSERT OR REPLACE INTO records VALUES (?,?,?,?,?,?,?,?,?)',
metadata[file].fileStart + offset, key, fileID, speciesID, confidence,
null, null, key + 3, null);
await db.runAsync('INSERT OR REPLACE INTO records VALUES (?,?,?,?,?,?,?,?,?,?)',
timestamp, key, fileID, speciesID, confidence,
null, null, key + 3, null, isDaylight);
}

async function batchInsertRecords(cname, label, toDisk, files, originalCname) {
Expand Down Expand Up @@ -1694,8 +1704,9 @@ const onInsertManualRecord = async ({ cname, start, end, comment, count, file, l

let response;
const dateTime = fileStart + startMilliseconds;
response = await db.runAsync('INSERT OR REPLACE INTO records VALUES ( ?,?,?,?,?,?,?,?,? )',
dateTime, start, fileID, speciesID, 2000, label, comment, end, parseInt(count));
const isDaylight = isDuringDaylight(dateTime, metadata[file].latitude, metadata[file].longitude)
response = await db.runAsync('INSERT OR REPLACE INTO records VALUES ( ?,?,?,?,?,?,?,?,?,?)',
dateTime, start, fileID, speciesID, 2000, label, comment, end, parseInt(count), isDaylight);

if (response.changes && toDisk) {
UI.postMessage({ event: 'diskDB-has-records' });
Expand Down Expand Up @@ -1726,7 +1737,7 @@ const parsePredictions = async (response) => {
updateUI = (confidence > STATE.detect.confidence &&
STATE.blocked.indexOf(speciesID) === -1);
//save all results to db, regardless of confidence
if (!STATE.selection) await insertRecord(key, speciesID, confidence, file);
if (!STATE.selection) await insertRecord(timestamp, key, speciesID, confidence, file);
if (STATE.selection || updateUI) {
let end, confidenceRequired;
if (STATE.selection) {
Expand Down

0 comments on commit a2087c1

Please sign in to comment.