Skip to content

Slightly better typing and doc comment for query clickhouse #6607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions torchci/lib/clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function queryClickhouse(
params: Record<string, unknown>,
query_id?: string,
useQueryCache?: boolean
): Promise<any[]> {
): Promise<{ [key: string]: any }[]> {
if (query_id === undefined) {
query_id = "adhoc";
}
Expand Down Expand Up @@ -61,23 +61,26 @@ export async function queryClickhouse(
return (await res.json()) as any[];
}

/**
* Returns the result of a query from the clickhouse database.
*
* The format is an array of json dictionaries/objects ex [{name: "foo", age: 42}, {name: "bar", age: 43}]
*
* @param queryName: string, the name of the query, which is the name of the folder in clickhouse_queries
* @param inputParams: Record<string, unknown>, the parameters to the query, an object where keys are the parameter names
* @param useQueryCache: boolean, if true, cache the query result on Ch side (1 minute TTL)
*
* This function will filter the inputParams to only include the parameters
* that are in the query params json file.
*
* During local development, if this fails due to "cannot find module ...
* params.json", delete the .next folder and try again.
*/
export async function queryClickhouseSaved(
queryName: string,
inputParams: Record<string, unknown>,
useQueryCache?: boolean
) {
/**
* queryClickhouseSaved
* @param queryName: string, the name of the query, which is the name of the folder in clickhouse_queries
* @param inputParams: Record<string, unknown>, the parameters to the query, an object where keys are the parameter names
* @param useQueryCache: boolean, if true, cache the query result on Ch side (1 minute TTL)
*
* This function will filter the inputParams to only include the parameters
* that are in the query params json file.
*
* During local development, if this fails due to "cannot find module ...
* params.json", delete the .next folder and try again.
*/
const query = readFileSync(
// https://stackoverflow.com/questions/74924100/vercel-error-enoent-no-such-file-or-directory
`${process.cwd()}/clickhouse_queries/${queryName}/query.sql`,
Expand Down
9 changes: 4 additions & 5 deletions torchci/lib/drciUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,11 @@ export async function getPRMergeCommits(
});

// If the array is empty, the PR hasn't been merged yet
return results.reduce((acc: { [prNumber: number]: string[] }, row: any) => {
if (!acc[row.pr_num]) {
acc[row.pr_num] = [];
return results.reduce((acc: Map<number, string[]>, row: any) => {
if (!acc.has(row.pr_num)) {
acc.set(row.pr_num, []);
}

acc[row.pr_num].push(row.merge_commit_sha);
acc.get(row.pr_num)!.push(row.merge_commit_sha);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk what happened to the typing here, it starts out as a map, but then acc is {key: string} and the return value is also a map

so im just making it always a map

return acc;
}, new Map<number, string[]>());
}
Expand Down
4 changes: 2 additions & 2 deletions torchci/lib/fetchDisabledNonFlakyTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { DisabledNonFlakyTestData } from "./types";
export default async function fetchDisabledNonFlakyTests(): Promise<
DisabledNonFlakyTestData[]
> {
return await queryClickhouseSaved("flaky_tests/disabled_non_flaky_tests", {
return (await queryClickhouseSaved("flaky_tests/disabled_non_flaky_tests", {
max_num_red: 0,
min_num_green: 150,
});
})) as DisabledNonFlakyTestData[];
}
4 changes: 2 additions & 2 deletions torchci/lib/fetchFlakyTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function fetchFlakyTests(
name: testName,
suite: testSuite,
file: testFile,
});
}) as any;
}

export async function fetchFlakyTestsAcrossJobs(): Promise<FlakyTestData[]> {
Expand Down Expand Up @@ -77,7 +77,7 @@ export async function fetchFlakyTestsAcrossFileReruns(
if (!workflowJobMap.has(curr.job_id)) {
return accum;
}
const job_info = workflowJobMap.get(curr.job_id);
const job_info = workflowJobMap.get(curr.job_id)!;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know why suddenly need ! but typechecker complains about undefined later

if (val === undefined) {
accum.set(key, {
file: curr.file,
Expand Down
4 changes: 2 additions & 2 deletions torchci/lib/fetchIssuesByLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export default async function fetchIssuesByLabel(
label: string,
useChCache?: boolean
): Promise<IssueData[]> {
return await queryClickhouseSaved(
return (await queryClickhouseSaved(
"issue_query",
{
label,
},
useChCache
);
)) as IssueData[];
}
8 changes: 4 additions & 4 deletions torchci/lib/fetchRecentWorkflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export async function fetchRecentWorkflows(
prNumbers: Array<number> = [],
numMinutes: string = "30"
): Promise<RecentWorkflowsData[]> {
return await queryClickhouseSaved("recent_pr_workflows_query", {
return (await queryClickhouseSaved("recent_pr_workflows_query", {
numMinutes,
prNumbers,
repo,
});
})) as RecentWorkflowsData[];
}

export async function fetchFailedJobsFromCommits(
shas: string[]
): Promise<RecentWorkflowsData[]> {
return await queryClickhouseSaved("commit_failed_jobs", {
return (await queryClickhouseSaved("commit_failed_jobs", {
shas,
});
})) as RecentWorkflowsData[];
}
8 changes: 7 additions & 1 deletion torchci/lib/getAuthors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ where
shas: _.map(jobs, (job: RecentWorkflowsData) => job.head_sha),
});

return _.keyBy(results, (record) => record.sha);
return _.keyBy(results, (record) => record.sha) as {
[key: string]: {
email: string;
commit_username: string;
pr_username: string;
};
};
}
6 changes: 3 additions & 3 deletions torchci/lib/utilization/fetchListUtilizationMetadataInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ export default async function fetchListUtilizationMetadataInfo(
return {
workflow_id: params.workflow_id,
workflow_name: meta_resp[0].workflow_name,
metadata_list: meta_resp ? meta_resp : ([] as UtilizationMetadataInfo[]),
metadata_list: meta_resp ? meta_resp : [],
};
}

async function listUtilizationMetadataInfo(
workflow_id: string,
repo: string = UTILIZATION_DEFAULT_REPO
) {
): Promise<UtilizationMetadataInfo[]> {
const response = await queryClickhouseSaved(
LIST_UTIL_METADATA_INFO_QUERY_FOLDER_NAME,
{
workflowId: workflow_id,
repo: repo,
}
);
return response;
return response as UtilizationMetadataInfo[];
}

async function listUtilizationMetadataWithStats(
Expand Down
4 changes: 2 additions & 2 deletions torchci/lib/utilization/fetchUtilization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ async function getUtilizationMetadata(
job_id: string,
run_attempt: string,
repo: string = UTILIZATION_DEFAULT_REPO
) {
): Promise<UtilizationMetadata[]> {
const response = await queryClickhouseSaved(UTIL_METADATA_QUERY_FOLDER_NAME, {
workflowId: workflow_id,
jobId: job_id,
runAttempt: run_attempt,
type: UTILIZATION_TYPE,
repo: repo,
});
return response;
return response as UtilizationMetadata[];
}

function getDisplayName(name: string) {
Expand Down
2 changes: 1 addition & 1 deletion torchci/pages/api/flaky-tests/3dStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function getTest(
for (const row of res) {
row.conclusions = _.countBy(row.conclusions);
}
return res;
return res as TestInfoAPIResponse;
}

export type TestInfoAPIResponse = {
Expand Down
2 changes: 1 addition & 1 deletion torchci/pages/api/flaky-tests/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function listTests(

return {
count: (await count)[0].count,
tests: await result,
tests: (await result) as any,
};
}

Expand Down