Skip to content

Commit

Permalink
Fixed scenario where records table could be dropped!
Browse files Browse the repository at this point in the history
Fixed update filestart for FAT32 filesystems
  • Loading branch information
Mattk70 committed Sep 28, 2024
1 parent 5551e2e commit 8780d9a
Show file tree
Hide file tree
Showing 4 changed files with 3,110 additions and 3,104 deletions.
1 change: 1 addition & 0 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4317,6 +4317,7 @@ DOM.gain.addEventListener('input', () => {
wavesurfer = undefined;
adjustSpecDims(true, fftSamples);
document.getElementById('frequency-range').classList.remove('text-warning');
updatePrefs('config.json', config);
break;
}
case 'speciesFilter': { speciesFilter(e); break}
Expand Down
74 changes: 42 additions & 32 deletions js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const wavefileReader = require('wavefile-reader');
const SunCalc = require('suncalc');
const ffmpeg = require('fluent-ffmpeg');
const png = require('fast-png');
const {utimesSync} = require('utimes');
const {writeToPath} = require('@fast-csv/format');
const merge = require('lodash.merge');
import { State } from './state.js';
Expand Down Expand Up @@ -3043,8 +3044,8 @@ const getValidSpecies = async (file) => {

const onUpdateFileStart = async (args) => {
let file = args.file;
const newfileMtime = Math.round(args.start + (metadata[file].duration * 1000));
fs.utimesSync(file, newfileMtime, newfileMtime);
const newfileMtime = new Date(Math.round(args.start + (metadata[file].duration * 1000)));
utimesSync(file, {atime: Date.now(), mtime: newfileMtime});
metadata[file].fileStart = args.start;
let db = STATE.db;
let row = await db.getAsync(`
Expand All @@ -3060,60 +3061,70 @@ const onUpdateFileStart = async (args) => {
const id = row.id;
const { changes } = await db.runAsync('UPDATE files SET filestart = ? where id = ?', args.start, id);
DEBUG && console.log(changes ? `Changed ${file}` : `No changes made`);
// Fill with new values

try {
// Begin transaction
await db.runAsync('BEGIN TRANSACTION');

// Create a temporary table with the same structure as the records table
await db.runAsync(`
CREATE TEMPORARY TABLE temp_records AS
CREATE TABLE temp_records AS
SELECT * FROM records;
`);

// Update the temp_records table with the new filestart values
await db.runAsync('UPDATE temp_records SET dateTime = (position * 1000) + ? WHERE fileID = ?', args.start, id);

// Drop the original records table
await db.runAsync('DROP TABLE records;');


// Check if temp_records exists and drop the original records table
const tempExists = await db.getAsync(`SELECT name FROM sqlite_master WHERE type='table' AND name='temp_records';`);

if (tempExists) {
// Drop the original records table
await db.runAsync('DROP TABLE records;');
}

// Rename the temp_records table to replace the original records table
await db.runAsync('ALTER TABLE temp_records RENAME TO records;');

// Recreate the UNIQUE constraint on the new records table
await db.runAsync(`
CREATE UNIQUE INDEX idx_unique_record ON records (dateTime, fileID, speciesID);
`);

// Update the daylight flag if necessary
let lat, lon;
if (row.locationID){
const location = await db.getAsync('SELECT lat, lon FROM locations WHERE locationID = ?', row.locationID)
if (row.locationID) {
const location = await db.getAsync('SELECT lat, lon FROM locations WHERE locationID = ?', row.locationID);
lat = location.lat;
lon = location.lon;
} else {
lat = STATE.lat;
lon = STATE.lon;
}

// Collect updates to be performed on each record
const updatePromises = [];

db.each(`
SELECT rowid, dateTime, fileID, speciesID, confidence, isDaylight from records WHERE fileID = ${id}
`, async (err, row) => {
if (err) {
throw err;
}
const isDaylight = isDuringDaylight(row.dateTime, lat, lon) ? 1 : 0;
// Update the isDaylight column for this record
await db.runAsync('UPDATE records SET isDaylight = ? WHERE isDaylight != ? AND rowid = ?', isDaylight, isDaylight, row.rowid);
}, async (err, count) => {
if (err) {
throw err;
}
// Commit transaction once all rows are processed
await db.run('COMMIT');
DEBUG && console.log(`File ${file} updated successfully.`);
await Promise.all([getResults(), getSummary()] );
});
SELECT rowid, dateTime, fileID, speciesID, confidence, isDaylight FROM records WHERE fileID = ${id}
`, async (err, row) => {
if (err) {
throw err; // This will trigger rollback
}
const isDaylight = isDuringDaylight(row.dateTime, lat, lon) ? 1 : 0;
// Update the isDaylight column for this record
updatePromises.push(db.runAsync('UPDATE records SET isDaylight = ? WHERE isDaylight != ? AND rowid = ?', isDaylight, isDaylight, row.rowid));
}, async (err, count) => {
if (err) {
throw err; // This will trigger rollback
}
// Wait for all updates to finish
await Promise.all(updatePromises);
// Commit transaction once all rows are processed
await db.runAsync('COMMIT');
DEBUG && console.log(`File ${file} updated successfully.`);
await Promise.all([getResults(), getSummary()]);
});
} catch (error) {
// Rollback in case of error
await db.runAsync('ROLLBACK');
Expand All @@ -3122,7 +3133,6 @@ const onUpdateFileStart = async (args) => {
}
};


async function onDelete({
file,
start,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
"sqlite3": "5.1.6",
"suncalc": "^1.9.0",
"uuid": "^8.3.2",
"utimes": "5.2.1",
"wavefile-reader": "^1.1.1",
"wavesurfer.js": "6.6.4"
}
Expand Down
Loading

0 comments on commit 8780d9a

Please sign in to comment.