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

View File

@ -131,6 +131,28 @@ export const getNames = async (): Promise<string[]> => {
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 ({
search,
limit,