Skip to content
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

Create job #87

Closed
wants to merge 5 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ModelsModule from './models/modelsModule';
import DatabasesModule from './databases/databasesModule';
import ProjectsModule from './projects/projectsModule';
import JobsModule from './jobs/jobsModule';
import SQLModule from './sql/sqlModule';
import ViewsModule from './views/viewsModule';
import Constants from './constants';
Expand All @@ -26,6 +27,7 @@ import { MindsDbError } from './errors';
import { BatchQueryOptions, QueryOptions } from './models/queryOptions';
import { FinetuneOptions, TrainingOptions } from './models/trainingOptions';
import Project from './projects/project';
import Job from './jobs/job';
import SqlQueryResult from './sql/sqlQueryResult';
import Table from './tables/table';
import { JsonPrimitive, JsonValue } from './util/json';
Expand All @@ -49,6 +51,11 @@ const Projects = new ProjectsModule.ProjectsRestApiClient(
defaultAxiosInstance,
httpAuthenticator
);
const Jobs = new JobsModule.JobsRestApiClient(
SQL,
defaultAxiosInstance,
httpAuthenticator
);
const Tables = new TablesModule.TablesRestApiClient(SQL);
const Views = new ViewsModule.ViewsRestApiClient(SQL);
const MLEngines = new MLEnginesModule.MLEnginesRestApiClient(
Expand Down Expand Up @@ -83,6 +90,7 @@ const connect = async function (options: ConnectionOptions): Promise<void> {
const httpClient = getAxiosInstance(options);
SQL.client = httpClient;
Projects.client = httpClient;
Jobs.client = httpClient;
MLEngines.client = httpClient;
Callbacks.client = httpClient;

Expand Down Expand Up @@ -114,6 +122,7 @@ export default {
Databases,
Models,
Projects,
Jobs,
Tables,
Views,
MLEngines,
Expand All @@ -131,6 +140,7 @@ export {
FinetuneOptions,
TrainingOptions,
Project,
Job,
SqlQueryResult,
Table,
JsonPrimitive,
Expand Down
25 changes: 25 additions & 0 deletions src/jobs/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default class Job {
/** Name of the Job. */
name: string;
query: string;
if_query: string;
start_at: string;
end_at: string;
schedule_str: string;

constructor(
name: string,
query: string,
if_query: string,
start_at: string,
end_at: string,
schedule_str: string
) {
this.name = name;
this.query = query;
this.if_query = if_query;
this.start_at = start_at;
this.end_at = end_at;
this.schedule_str = schedule_str;
}
}
40 changes: 40 additions & 0 deletions src/jobs/jobsApiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Job from './job';

/** Abstract class outlining Jobs operations supported by the SDK. */
export default abstract class JobsApiClient {
/**
* Abstract method for removing a new job in a specified project.
* @returns {Promise<void>}
*/
abstract deleteJob(name: string, project: string): Promise<void>;

/**
* Abstract method for creating a new job in a specified project.
*
* This method should be implemented by subclasses to construct and
* execute a SQL query for creating a job with the given parameters.
* The job will be defined to run according to the specified schedule
* and conditions. Implementations should ensure that the job is
* created only if it does not already exist.
*
* @param project - The name of the project where the job will be created.
* @param name - The name of the job to be created.
* @param query - The SQL query that the job will execute.
* @param if_query - A condition that determines whether the job should run.
* @param start_at - The timestamp at which the job should start.
* @param end_at - The timestamp at which the job should end.
* @param schedule_str - A string representing the schedule for job execution.
*
* @returns A promise that resolves to a new Job instance upon successful creation.
* @throws MindsDbError if there is an error during the creation process.
*/
abstract createJob(
project: string,
name: string,
query: string,
if_query: string,
start_at: string,
end_at: string,
schedule_str: string
): Promise<Job>;
}
3 changes: 3 additions & 0 deletions src/jobs/jobsModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import JobsRestApiClient from './jobsRestApiClient';

export default { JobsRestApiClient };
92 changes: 92 additions & 0 deletions src/jobs/jobsRestApiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Axios } from 'axios';
import JobsApiClient from './jobsApiClient';
import mysql from 'mysql';
import SqlApiClient from '../sql/sqlApiClient';
import HttpAuthenticator from '../httpAuthenticator';
import { MindsDbError } from '../errors';
import Job from './job';

/** Implementation of JobsApiClient that goes through the REST API. */
export default class JobsRestApiClient extends JobsApiClient {
/** Axios client to send all HTTP requests. */
client: Axios;

/** Authenticator to use for reauthenticating if needed. */
authenticator: HttpAuthenticator;

/** SQL API client to send all SQL query requests. */
sqlClient: SqlApiClient;

/**
* Constructor for Jobs API client.
* @param {Axios} client - Axios instance to send all HTTP requests.
*/
constructor(client: Axios, authenticator: HttpAuthenticator, sqlClient: SqlApiClient) {
super();
this.client = client;
this.authenticator = authenticator;
this.sqlClient = sqlClient;
}

/**
* Delete a job
* @returns {Promise<Array<Project>>} - Drop a job
* @throws {MindsDbError} - Something went wrong fetching projects.
*/
override async deleteJob(name: string, project: string): Promise<void> {
const deleteQuery = `DROP JOB ${mysql.escapeId(project)}.${mysql.escapeId(
name
)}`;
const sqlQueryResult = await this.sqlClient.runQuery(deleteQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

/**
* Creates a new job in the specified project.
*
* This method constructs and executes a SQL query to create a job
* with the provided parameters. If the job already exists, it will
* not be created again. The job will be scheduled to run based on
* the specified timing and conditions.
*
* @param project - The name of the project where the job will be created.
* @param name - The name of the job to be created.
* @param query - The SQL query to be executed by the job.
* @param if_query - A condition that determines whether the job should run.
* @param start_at - The time at which the job should start.
* @param end_at - The time at which the job should end.
* @param schedule_str - The schedule for how often the job should run.
*
* @returns A promise that resolves to a new Job instance if successful.
* @throws MindsDbError if there is an error executing the SQL query.
*/
override async createJob(
project: string,
name: string,
query: string,
if_query: string,
start_at: string,
end_at: string,
schedule_str: string
): Promise<Job> {
const createClause = `CREATE JOB IF NOT EXISTS ${mysql.escapeId(
project
)}.${mysql.escapeId(name)} AS `;

const queryClause = `(${query})`;
const startClause = `START ${start_at}`;
const endClause = `END ${end_at}`;
const everyClause = `EVERY ${schedule_str}`;
const ifQueryClause = `IF (${if_query});`

const sqlQuery = [createClause, queryClause, startClause, endClause, everyClause, ifQueryClause].join('\n');

const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
return new Job(name, query, if_query, start_at, end_at, schedule_str);
}
}