From 3669eafe0bc3b2854454a77ebc1a493d87f788cb Mon Sep 17 00:00:00 2001 From: Abhilash Date: Mon, 21 Oct 2024 16:45:28 +0530 Subject: [PATCH 1/3] initial commit --- src/jobs/job.ts | 10 +++++++ src/jobs/jobsApiClient.ts | 10 +++++++ src/jobs/jobsModule.ts | 3 ++ src/jobs/jobsRestApiClient.ts | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/jobs/job.ts create mode 100644 src/jobs/jobsApiClient.ts create mode 100644 src/jobs/jobsModule.ts create mode 100644 src/jobs/jobsRestApiClient.ts diff --git a/src/jobs/job.ts b/src/jobs/job.ts new file mode 100644 index 0000000..99025f9 --- /dev/null +++ b/src/jobs/job.ts @@ -0,0 +1,10 @@ +/** Structure of a MindsDB Project. */ +export default interface Job { + /** Name of the Job. */ + name: string; + query: string; + if_query: string; + start_at: string; + end_at: string; + schedule_str: string; +} diff --git a/src/jobs/jobsApiClient.ts b/src/jobs/jobsApiClient.ts new file mode 100644 index 0000000..b3ea193 --- /dev/null +++ b/src/jobs/jobsApiClient.ts @@ -0,0 +1,10 @@ +import Job from './job'; + +/** Abstract class outlining Project operations supported by the SDK. */ +export default abstract class JobsApiClient { + /** + * Removes a job for the current project + * @returns {Promise} + */ + abstract deleteJob(name: string, project: string): Promise; +} diff --git a/src/jobs/jobsModule.ts b/src/jobs/jobsModule.ts new file mode 100644 index 0000000..0ea5be8 --- /dev/null +++ b/src/jobs/jobsModule.ts @@ -0,0 +1,3 @@ +import JobsRestApiClient from './jobsRestApiClient'; + +export default { JobsRestApiClient }; diff --git a/src/jobs/jobsRestApiClient.ts b/src/jobs/jobsRestApiClient.ts new file mode 100644 index 0000000..7e42579 --- /dev/null +++ b/src/jobs/jobsRestApiClient.ts @@ -0,0 +1,55 @@ +import { Axios } from 'axios'; +import JobsApiClient from './jobsApiClient'; +import { getBaseRequestConfig } from '../util/http'; +import Constants from '../constants'; +import Job from './job'; +import HttpAuthenticator from '../httpAuthenticator'; +import { MindsDbError } from '../errors'; + +/** Implementation of ProjectsApiClient 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; + + /** + * Constructor for Projects API client. + * @param {Axios} client - Axios instance to send all HTTP requests. + */ + constructor(client: Axios, authenticator: HttpAuthenticator) { + super(); + this.client = client; + this.authenticator = authenticator; + } + + private getJobsUrl(): string { + const baseUrl = + this.client.defaults.baseURL || Constants.BASE_CLOUD_API_ENDPOINT; + const projectsUrl = new URL(Constants.BASE_PROJECTS_URI, baseUrl); + return projectsUrl.toString(); + } + + /** + * Gets all MindsDB projects for the current user. + * @returns {Promise>} - All projects. + * @throws {MindsDbError} - Something went wrong fetching projects. + */ + override async deleteJob(): Promise { + const jobsDeleteUrl = this.getJobsUrl(); + const { authenticator, client } = this; + try { + const projectsResponse = await client.delete( + projectsUrl, + getBaseRequestConfig(authenticator) + ); + if (!projectsResponse.data) { + return []; + } + return projectsResponse.data; + } catch (error) { + throw MindsDbError.fromHttpError(error, projectsUrl); + } + } +} From b63d3bd35531c02533a0c6aad8778dba39703015 Mon Sep 17 00:00:00 2001 From: Abhilash Date: Mon, 21 Oct 2024 17:01:00 +0530 Subject: [PATCH 2/3] updated code --- src/index.ts | 10 ++++++++ src/jobs/jobsApiClient.ts | 2 +- src/jobs/jobsRestApiClient.ts | 47 ++++++++++++++--------------------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index e42ce48..19ca649 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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'; @@ -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( @@ -83,6 +90,7 @@ const connect = async function (options: ConnectionOptions): Promise { const httpClient = getAxiosInstance(options); SQL.client = httpClient; Projects.client = httpClient; + Jobs.client = httpClient; MLEngines.client = httpClient; Callbacks.client = httpClient; @@ -114,6 +122,7 @@ export default { Databases, Models, Projects, + Jobs, Tables, Views, MLEngines, @@ -131,6 +140,7 @@ export { FinetuneOptions, TrainingOptions, Project, + Job, SqlQueryResult, Table, JsonPrimitive, diff --git a/src/jobs/jobsApiClient.ts b/src/jobs/jobsApiClient.ts index b3ea193..5f85ee2 100644 --- a/src/jobs/jobsApiClient.ts +++ b/src/jobs/jobsApiClient.ts @@ -1,6 +1,6 @@ import Job from './job'; -/** Abstract class outlining Project operations supported by the SDK. */ +/** Abstract class outlining Jobs operations supported by the SDK. */ export default abstract class JobsApiClient { /** * Removes a job for the current project diff --git a/src/jobs/jobsRestApiClient.ts b/src/jobs/jobsRestApiClient.ts index 7e42579..48afd3b 100644 --- a/src/jobs/jobsRestApiClient.ts +++ b/src/jobs/jobsRestApiClient.ts @@ -1,12 +1,11 @@ import { Axios } from 'axios'; import JobsApiClient from './jobsApiClient'; -import { getBaseRequestConfig } from '../util/http'; -import Constants from '../constants'; -import Job from './job'; +import mysql from 'mysql'; +import SqlApiClient from '../sql/sqlApiClient'; import HttpAuthenticator from '../httpAuthenticator'; import { MindsDbError } from '../errors'; -/** Implementation of ProjectsApiClient that goes through the REST API. */ +/** Implementation of JobsApiClient that goes through the REST API. */ export default class JobsRestApiClient extends JobsApiClient { /** Axios client to send all HTTP requests. */ client: Axios; @@ -14,42 +13,32 @@ export default class JobsRestApiClient extends JobsApiClient { /** Authenticator to use for reauthenticating if needed. */ authenticator: HttpAuthenticator; + /** SQL API client to send all SQL query requests. */ + sqlClient: SqlApiClient; + /** - * Constructor for Projects API client. + * Constructor for Jobs API client. * @param {Axios} client - Axios instance to send all HTTP requests. */ - constructor(client: Axios, authenticator: HttpAuthenticator) { + constructor(client: Axios, authenticator: HttpAuthenticator, sqlClient: SqlApiClient) { super(); this.client = client; this.authenticator = authenticator; - } - - private getJobsUrl(): string { - const baseUrl = - this.client.defaults.baseURL || Constants.BASE_CLOUD_API_ENDPOINT; - const projectsUrl = new URL(Constants.BASE_PROJECTS_URI, baseUrl); - return projectsUrl.toString(); + this.sqlClient = sqlClient; } /** - * Gets all MindsDB projects for the current user. - * @returns {Promise>} - All projects. + * Delete a job + * @returns {Promise>} - Drop a job * @throws {MindsDbError} - Something went wrong fetching projects. */ - override async deleteJob(): Promise { - const jobsDeleteUrl = this.getJobsUrl(); - const { authenticator, client } = this; - try { - const projectsResponse = await client.delete( - projectsUrl, - getBaseRequestConfig(authenticator) - ); - if (!projectsResponse.data) { - return []; - } - return projectsResponse.data; - } catch (error) { - throw MindsDbError.fromHttpError(error, projectsUrl); + override async deleteJob(name: string, project: string): Promise { + 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); } } } From 801f6bee325ba37000c2a35d7768bceb53f9cb25 Mon Sep 17 00:00:00 2001 From: Abhilash Date: Mon, 21 Oct 2024 17:33:26 +0530 Subject: [PATCH 3/3] changed Job from interface to class --- src/jobs/job.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/jobs/job.ts b/src/jobs/job.ts index 99025f9..0da44a1 100644 --- a/src/jobs/job.ts +++ b/src/jobs/job.ts @@ -1,5 +1,4 @@ -/** Structure of a MindsDB Project. */ -export default interface Job { +export default class Job { /** Name of the Job. */ name: string; query: string; @@ -7,4 +6,20 @@ export default interface Job { 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; + } }