Use sql for counting todays sets in predictions

This commit is contained in:
Brandon Presley 2022-09-25 18:05:15 +13:00
parent acfd0e698d
commit 063425cd82
2 changed files with 32 additions and 18 deletions

View File

@ -12,7 +12,7 @@ import {HomePageParams} from './home-page-params';
import Page from './Page'; import Page from './Page';
import {getTodaysPlan} from './plan.service'; import {getTodaysPlan} from './plan.service';
import Set from './set'; import Set from './set';
import {defaultSet, getSets, getTodaysSets} from './set.service'; import {countToday, defaultSet, getSets, getToday} from './set.service';
import SetItem from './SetItem'; import SetItem from './SetItem';
import {settings} from './settings.service'; import {settings} from './settings.service';
@ -32,32 +32,24 @@ export default function SetList() {
const predict = useCallback(async () => { const predict = useCallback(async () => {
setCount(0); setCount(0);
console.log(`${SetList.name}.predict:`, {settings}); setSet({...defaultSet});
if (!settings.predict) return setSet({...defaultSet}); if (!settings.predict) return;
const todaysPlan = await getTodaysPlan(); const todaysPlan = await getTodaysPlan();
console.log(`${SetList.name}.predict:`, {todaysPlan}); if (todaysPlan.length === 0) return;
if (todaysPlan.length === 0) return setSet({...defaultSet}); const todaysSet = await getToday();
const todaysSets = await getTodaysSets();
const todaysWorkouts = todaysPlan[0].workouts.split(','); const todaysWorkouts = todaysPlan[0].workouts.split(',');
let workout = todaysWorkouts[0]; let workout = todaysWorkouts[0];
console.log(`${SetList.name}.predict:`, {todaysSet: todaysSets[0]});
console.log(`${SetList.name}.predict:`, {todaysWorkouts});
let best = await getBestSet(workout); let best = await getBestSet(workout);
if (todaysWorkouts.includes(todaysSets[0]?.name) && todaysSets.length > 0) { if (todaysSet && todaysWorkouts.includes(todaysSet.name)) {
const _count = todaysSets.filter( const _count = await countToday(todaysSet.name);
s => s.name === todaysSets[0].name, workout = todaysSet.name;
).length;
workout = todaysSets[0].name;
best = await getBestSet(workout); best = await getBestSet(workout);
const index = todaysWorkouts.indexOf(todaysSet.name) + 1;
if (_count >= Number(best.sets)) if (_count >= Number(best.sets))
best = await getBestSet( best = await getBestSet(todaysWorkouts[index]);
todaysWorkouts[todaysWorkouts.indexOf(todaysSets[0].name!) + 1],
);
if (best.name === '') setCount(0); if (best.name === '') setCount(0);
else setCount(_count); else setCount(_count);
} }
console.log(`${SetList.name}.predict:`, {workout});
console.log(`${SetList.name}.predict:`, {best});
setSet({...best}); setSet({...best});
setWorkouts(todaysWorkouts); setWorkouts(todaysWorkouts);
}, []); }, []);

View File

@ -131,6 +131,28 @@ export const getNames = async (): Promise<string[]> => {
return values.map(value => value.name); return values.map(value => value.name);
}; };
export const getToday = async (): Promise<Set | undefined> => {
const select = `
SELECT * FROM sets
WHERE NOT hidden
AND created LIKE strftime('%Y-%m-%d%%', 'now', 'localtime')
ORDER BY created DESC
LIMIT 1
`;
const [result] = await db.executeSql(select);
return result.rows.item(0);
};
export const countToday = async (name: string): Promise<number> => {
const select = `
SELECT COUNT(*) as total FROM sets
WHERE created LIKE strftime('%Y-%m-%d%%', 'now', 'localtime')
AND name = ?
`;
const [result] = await db.executeSql(select, [name]);
return Number(result.rows.item(0)?.total);
};
export const getDistinctSets = async ({ export const getDistinctSets = async ({
search, search,
limit, limit,