Skip to content

Commit

Permalink
Fixed species/time sort
Browse files Browse the repository at this point in the history
Fixed sql bugs
  • Loading branch information
Mattk70 committed Dec 15, 2023
1 parent 62a2fa2 commit f326138
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
1 change: 1 addition & 0 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,7 @@ listToUse.addEventListener('change', function (e) {
config.list = e.target.value;
updateListIcon();
updatePrefs();
resetResults({clearSummary: true, clearPagination: true, clearResults: true});
worker.postMessage({ action: 'update-list', list: config.list })
})

Expand Down
49 changes: 23 additions & 26 deletions js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,42 +438,41 @@ const getSummaryParams = () => {
}

const prepSummaryStatement = () => {
const blocked = STATE.blocked.length && !STATE.selection ? STATE.blocked : [];
const range = STATE.mode === 'explore' ? STATE.explore.range : undefined;
const useRange = range?.start;
let summaryStatement = `
WITH ranked_records AS (
SELECT records.dateTime, records.confidence, files.name, cname, sname, COALESCE(callCount, 1) as callCount, speciesID, isDaylight as isDaylight,
SELECT records.dateTime, records.confidence, files.name, cname, sname, COALESCE(callCount, 1) as callCount, speciesID, isDaylight,
RANK() OVER (PARTITION BY records.dateTime ORDER BY records.confidence DESC) AS rank
FROM records
JOIN files ON files.id = records.fileID
JOIN species ON species.id = records.speciesID
WHERE confidence >= ? `;
let extraClause = '';
if (['analyse', 'archive'].includes(STATE.mode)) {
extraClause += ` AND name IN (${prepParams(STATE.filesToAnalyse)}) `;
summaryStatement += ` AND name IN (${prepParams(STATE.filesToAnalyse)}) `;
}
else if (useRange) {
extraClause += ' AND dateTime BETWEEN ? AND ? ';
summaryStatement += ' AND dateTime BETWEEN ? AND ? ';
}
if (STATE.detect.nocmig){
extraClause += ' AND COALESCE(isDaylight, 0) != 1 ';

if (STATE.blocked.length && !STATE.selection) {
const excluded = prepParams(STATE.blocked);
summaryStatement += ` AND speciesID NOT IN (${excluded}) `;
}
if (blocked.length) {
const excluded = prepParams(blocked);
extraClause += ` AND speciesID NOT IN (${excluded}) `;
if (STATE.detect.nocmig){
summaryStatement += ' AND COALESCE(isDaylight, 0) != 1 ';
}

if (STATE.locationID) {
extraClause += ' AND locationID = ? ';
summaryStatement += ' AND locationID = ? ';
}
summaryStatement += extraClause;
summaryStatement += `
)
SELECT cname, sname, COUNT(*) as count, SUM(callcount) as calls, ROUND(MAX(ranked_records.confidence) / 10.0, 0) as max
SELECT speciesID, cname, sname, COUNT(cname) as count, SUM(callcount) as calls, ROUND(MAX(ranked_records.confidence) / 10.0, 0) as max
FROM ranked_records
WHERE ranked_records.rank <= ${STATE.topRankin}
GROUP BY speciesID ORDER BY cname`;
WHERE ranked_records.rank <= ${STATE.topRankin}`;

summaryStatement += ` GROUP BY speciesID ORDER BY cname`;
STATE.GET_SUMMARY_SQL = STATE.db.prepare(summaryStatement);
//console.log('Summary SQL statement:\n' + summaryStatement)
}
Expand Down Expand Up @@ -512,13 +511,12 @@ const getTotal = async ({species = undefined, offset = 0}) => {
const getResultsParams = (species, confidence, offset, limit, topRankin) => {
const params = [];
params.push(confidence);
species && params.push(species);
const blocked = (! species && STATE.blocked.length && !STATE.selection) ?
STATE.blocked : [];
blocked.length && params.push(...blocked);
['analyse', 'archive'].includes(STATE.mode) && !STATE.selection && params.push(...STATE.filesToAnalyse);
const blocked = (STATE.blocked.length && !STATE.selection) ? STATE.blocked : [];
blocked.length && params.push(...blocked);
limit !== Infinity && params.push(limit, offset);
params.push(topRankin);
species && params.push(species);
return params
}

Expand Down Expand Up @@ -548,22 +546,21 @@ const prepResultsStatement = (species, noLimit) => {
WHERE confidence >= ?
`;

if (species) resultStatement+= ` AND speciesID = (SELECT id FROM species WHERE cname = ?) `;

const blocked = STATE.selection || species ? [] : STATE.blocked;
if (blocked.length) resultStatement += ` AND speciesID NOT IN (${prepParams(blocked)}) `;
//if (species) resultStatement+= ` AND speciesID = (SELECT id FROM species WHERE cname = ?) `;

// might have two locations with same dates - so need to add files
if (['analyse', 'archive'].includes(STATE.mode) && !STATE.selection) {
resultStatement += ` AND name IN (${prepParams(STATE.filesToAnalyse)}) `;
}
// Prioritise selection ranges
const range = STATE.selection?.start ? STATE.selection :
STATE.mode === 'explore' ? STATE.explore.range : false;

const useRange = range?.start;
if (useRange) {
resultStatement += ` AND dateTime BETWEEN ${range.start} AND ${range.end} `;
}
}
const blocked = STATE.selection ? [] : STATE.blocked;
if (blocked.length) resultStatement += ` AND speciesID NOT IN (${prepParams(blocked)}) `;
if (STATE.selection) resultStatement += ` AND name = '${FILE_QUEUE[0]}' `;
if (STATE.locationID) {
resultStatement += ` AND locationID = ${STATE.locationID} `;
Expand Down Expand Up @@ -593,7 +590,7 @@ const prepResultsStatement = (species, noLimit) => {
FROM
ranked_records
WHERE confidence_rank <= ? `;

if (species) resultStatement+= ` AND cname = ? `;
STATE.GET_RESULT_SQL = STATE.db.prepare(resultStatement);
}

Expand Down

0 comments on commit f326138

Please sign in to comment.