Skip to content

Commit

Permalink
refactor: optimize the openrank sql (#1622)
Browse files Browse the repository at this point in the history
Signed-off-by: frank-zsy <syzhao1988@126.com>
  • Loading branch information
frank-zsy authored Sep 8, 2024
1 parent c1dac49 commit 8475d5b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/metrics/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const getRepoWhereClause = async (config: QueryConfig): Promise<string |
for (const p of config.idOrNames) {
if (p.repoNames && p.repoNames.length > 0) {
// convert repo name to id
const sql = `SELECT any(repo_id) AS id FROM events WHERE repo_name IN [${p.repoNames.map(n => `'${n}'`)}] AND platform='${p.platform}' GROUP BY repo_name`;
const sql = `SELECT any(repo_id) AS id FROM global_openrank WHERE repo_name IN [${p.repoNames.map(n => `'${n}'`)}] AND platform='${p.platform}' GROUP BY repo_name`;
const repoIds = (await query<any>(sql, { format: 'JSONEachRow' })).map(r => r.id);
repoWhereClauseArray.push(`(repo_id IN [${repoIds.join(',')}] AND platform='${p.platform}')`);
}
Expand All @@ -118,7 +118,7 @@ export const getRepoWhereClause = async (config: QueryConfig): Promise<string |

if (p.orgNames && p.orgNames.length > 0) {
// convert org name to id
const sql = `SELECT any(org_id) AS id FROM events WHERE org_login IN [${p.orgNames.map(n => `'${n}'`)}] AND platform='${p.platform}' GROUP BY org_login`;
const sql = `SELECT any(org_id) AS id FROM global_openrank WHERE org_login IN [${p.orgNames.map(n => `'${n}'`)}] AND platform='${p.platform}' GROUP BY org_login`;
const orgIds = (await query<any>(sql, { format: 'JSONEachRow' })).map(r => r.id);
repoWhereClauseArray.push(`(org_id IN [${orgIds.join(',')}] AND platform='${p.platform}')`);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ export const getUserWhereClause = async (config: QueryConfig, idCol: string = 'a
for (const p of config.idOrNames) {
if (p.userLogins && p.userLogins.length > 0) {
// convert user login to id
const sql = `SELECT any(actor_id) AS id FROM events WHERE actor_login IN [${p.userLogins.map(n => `'${n}'`)}] AND platform='${p.platform}' GROUP BY actor_login`;
const sql = `SELECT any(actor_id) AS id FROM global_openrank WHERE actor_login IN [${p.userLogins.map(n => `'${n}'`)}] AND platform='${p.platform}' GROUP BY actor_login`;
const userIds = (await query<any>(sql, { format: 'JSONEachRow' })).map(r => r.id);
userWhereClauseArray.push(`(${idCol} IN [${userIds.join(',')}] AND platform='${p.platform}')`);
}
Expand Down
4 changes: 4 additions & 0 deletions src/metrics/indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export const getUserCommunityOpenrank = async (config: QueryConfig<UserCommunity
const repoWhereClause = await getRepoWhereClause(config);
if (repoWhereClause) whereClause.push(repoWhereClause);
const userWhereClause = await getUserWhereClause(config, 'id');
const innerUserWhereClause = await getUserWhereClause(config);
if (innerUserWhereClause) {
whereClause.push(`repo_id IN (SELECT repo_id FROM community_openrank WHERE ${innerUserWhereClause})`);
}
const timeRangeClause = getTimeRangeWhereClause(config);
if (timeRangeClause) whereClause.push(timeRangeClause);
const limit = (config.options?.limit == undefined) ? 30 : config.options.limit;
Expand Down

0 comments on commit 8475d5b

Please sign in to comment.