Skip to content

Commit

Permalink
Merge pull request #100 from Better-Boy/list-job
Browse files Browse the repository at this point in the history
list/query jobs
  • Loading branch information
ZoranPandovski authored Nov 13, 2024
2 parents 1dfdcec + f49c28c commit d7e7dbe
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/jobs/jobsApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ export default abstract class JobsApiClient {
* @throws {MindsDbError} - Something went wrong while dropping the job.
*/
abstract dropJob(name: string, project: string): Promise<void>;

/**
* Lists all jobs from MindsDB.
* @param {string} name - Name of the job.
* @param {string} project - Project the job belongs to.
* @returns {Promise<void>} - Resolves after all the jobs are listed.
* @throws {MindsDbError} - Something went wrong while listing the job.
*/
abstract list(name?: string, project?: string): Promise<Array<Job>>;
}
56 changes: 56 additions & 0 deletions src/jobs/jobsRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,60 @@ export default class JobsRestApiClient extends JobsApiClient {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

/**
* Retrieves a list of jobs from the information schema.
*
* This asynchronous method queries the database for jobs, optionally filtering
* by project name and/or job name. If both parameters are provided, the method
* combines the filters to return a more specific list of jobs.
*
* @param {string} [name] - The optional name of the job to filter the results.
* If provided, only jobs matching this name will be included.
* @param {string} [project] - The optional project name to filter the results.
* If provided, only jobs associated with this project will be included.
*
* @returns {Promise<Array<Job>>} - A promise that resolves to an array of Job objects
* representing the jobs retrieved from the database.
*
* @throws {MindsDbError} - Throws an error if the SQL query execution fails,
* containing the error message returned from the database.
*
* @example
* const jobs = await list('myJob', 'myProject');
* console.log(jobs);
*/
override async list(name?: string, project?: string): Promise<Array<Job>> {
const selectClause = `SELECT * FROM information_schema.jobs`;
let projectClause = '';
let nameClause = '';
if(project){
projectClause = `WHERE project = ${mysql.escape(project)}`;
}

if(name){
nameClause = `name = ${mysql.escape(name)}`;
}

const listJobsQuery = [selectClause, projectClause, nameClause].join(
'\n'
);

const sqlQueryResult = await this.sqlClient.runQuery(listJobsQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
return sqlQueryResult.rows.map(
(row) => {
const job = new Job(this, row['NAME'], row['PROJECT']);
job.setEnd(row['END_AT']);
job.setEvery(row['SCHEDULE_STR']);
job.setStart(row['START_AT']);
job.setIfCondition(row['IF_QUERY']);
job.addQuery(row['QUERY']);
return job;
}
);
}

}

0 comments on commit d7e7dbe

Please sign in to comment.