Skip to content

Commit

Permalink
improving query to find best lifts and return the reps too..
Browse files Browse the repository at this point in the history
  • Loading branch information
bandinopla committed Jan 11, 2025
1 parent 1e5e87f commit 132eae3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
1 change: 1 addition & 0 deletions operations/journal.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ query GetUserInfo($userInfoUname: String!) {
}
best3 {
w
r
ymd
e {
id
Expand Down
71 changes: 30 additions & 41 deletions server/db/resolvers/journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,49 +95,37 @@ export const GetUserInfo = async ( requestorUID, requestedUserIdentifier, identi
const GetBestOfficialLiftsOf = async ( uid, onlyTheseTypes )=>{

let officialEnames = getAllOfficialEnames(onlyTheseTypes);
let officialETags = getAllOfficialETags(onlyTheseTypes);

// let result = await query(` SELECT
// A.wkg AS wkg,
// B.nombre AS ename,
// B.id AS eid,
// C.fecha_del_log AS ymd

// FROM erows AS A
// INNER JOIN exercises AS B ON B.id=A.eid
// INNER JOIN logs AS C ON A.logid=C.id
// WHERE
// A.uid=?
// AND A.reps>0 AND A.type=0
// AND (B.nombre IN (?) OR ${ officialETags.map( tag=>`B.nombre LIKE '%${tag}' OR B.nombre LIKE '%${tag} %'` ).join(" OR ") })

// GROUP BY B.id
// ORDER BY wkg DESC`, [ uid, officialEnames ]);
let result = await query(`SELECT
let officialETags = getAllOfficialETags(onlyTheseTypes);

let result = await query(` SELECT
e.id AS eid,
e.nombre AS ename,
er.wkg ,
er.wkg,
er.reps,
l.fecha_del_log AS ymd
FROM
erows er
JOIN
exercises e ON er.eid = e.id
JOIN
logs l ON er.logid = l.id
WHERE
er.uid=? AND er.reps>0 AND er.type=0
AND (e.nombre IN (?) OR ${ officialETags.map( tag=>`e.nombre LIKE '%${tag}' OR e.nombre LIKE '%${tag} %'` ).join(" OR ") })
AND
(er.eid, er.wkg) IN (
SELECT
eid, MAX(wkg)
FROM
erows
WHERE uid=? AND erows.reps>0
GROUP BY
eid
)
ORDER BY wkg DESC, ymd ASC`, [ uid, officialEnames, uid ]);
FROM erows er
JOIN exercises e ON er.eid = e.id
JOIN logs l ON er.logid = l.id
WHERE er.uid = ? AND er.reps>0
AND EXISTS (
SELECT 1
FROM erows er_sub
JOIN exercises e2 ON er_sub.eid = e2.id
WHERE er_sub.uid = er.uid
AND er_sub.reps > 0
AND er_sub.eid = er.eid
AND er_sub.wkg = (
SELECT MAX(erows_sub.wkg)
FROM erows erows_sub
WHERE erows_sub.uid = er_sub.uid
AND erows_sub.eid = er_sub.eid
AND erows_sub.reps > 0
)
AND er_sub.type = 0
AND (e2.nombre IN (?) OR ${ officialETags.map( tag=>`e2.nombre LIKE '%${tag}' OR e2.nombre LIKE '%${tag} %'` ).join(" OR ") })
)
ORDER BY er.wkg DESC, l.fecha_del_log ASC
`, [ uid, officialEnames ]);

return result.map( row => ({
...row,
Expand Down Expand Up @@ -299,7 +287,8 @@ export const JournalResolver = {
if( !ex ) return null;

return {
w: ex?.wkg || 0,
w: ex.wkg ?? 0,
r: ex.reps ?? 1,
ymd: ex.ymd,
e: {
id : ex.eid,
Expand Down
1 change: 1 addition & 0 deletions server/db/schema/JournalDay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const $types = gql`
type BestLift {
w:Float!
r:Int!
ymd:YMD!
e:Exercise!
}
Expand Down
3 changes: 2 additions & 1 deletion server/db/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type BestEStat = {
export type BestLift = {
__typename?: 'BestLift';
e: Exercise;
r: Scalars['Int'];
w: Scalars['Float'];
ymd: Scalars['YMD'];
};
Expand Down Expand Up @@ -1672,7 +1673,7 @@ export type GetUserInfoQueryVariables = Exact<{
}>;


export type GetUserInfoQuery = { __typename?: 'Query', userInfo: { __typename?: 'UserInfo', daysLogged: number, user: { __typename?: 'User', id: string, avatarhash: string, uname: string, cc?: string | null, slvl?: number | null, sok?: number | null, sleft?: number | null, age?: number | null, bw?: number | null, private?: number | null, isf?: number | null, joined?: string | null, usekg?: number | null, forumRole?: ForumRoleKey | null, custom1RM?: number | null, est1RMFactor?: number | null, jranges?: Array<number | null> | null, estimate1RMFormula?: string | null, socialLinks?: Array<string | null> | null }, forum?: { __typename?: 'ForumStatus', posts?: number | null, role?: { __typename?: 'ForumRole', title: string } | null } | null, best3?: Array<{ __typename?: 'BestLift', w: number, ymd: any, e: { __typename?: 'Exercise', id: string, name: string, type?: string | null } }> | null } };
export type GetUserInfoQuery = { __typename?: 'Query', userInfo: { __typename?: 'UserInfo', daysLogged: number, user: { __typename?: 'User', id: string, avatarhash: string, uname: string, cc?: string | null, slvl?: number | null, sok?: number | null, sleft?: number | null, age?: number | null, bw?: number | null, private?: number | null, isf?: number | null, joined?: string | null, usekg?: number | null, forumRole?: ForumRoleKey | null, custom1RM?: number | null, est1RMFactor?: number | null, jranges?: Array<number | null> | null, estimate1RMFormula?: string | null, socialLinks?: Array<string | null> | null }, forum?: { __typename?: 'ForumStatus', posts?: number | null, role?: { __typename?: 'ForumRole', title: string } | null } | null, best3?: Array<{ __typename?: 'BestLift', w: number, r: number, ymd: any, e: { __typename?: 'Exercise', id: string, name: string, type?: string | null } }> | null } };

export type GetUserBasicInfoQueryVariables = Exact<{
of?: InputMaybe<Scalars['ID']>;
Expand Down
2 changes: 1 addition & 1 deletion wxr-dev-db/sql/0_db-setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CREATE TABLE IF NOT EXISTS `erows` (
INDEX `idx_erows_reps` (`reps`),
INDEX `idx_erows_wkg_reps` (`wkg`, `reps`),
INDEX `idx_erows_just_logid` (`logid`),

INDEX `idx_erows_eid_wkg` (`eid`, `wkg`),

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Expand Down

0 comments on commit 132eae3

Please sign in to comment.