Skip to content

Commit

Permalink
Merge pull request #9 from jay-m-dev/code_exec_cleanup_fs
Browse files Browse the repository at this point in the history
removing temp folder and updated raml and api docs
  • Loading branch information
jay-m-dev authored Apr 12, 2023
2 parents 4ff3231 + dd74f28 commit d31850d
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docker-compose-doc-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
command: bash -c "npm i -g raml2html &&
raml2html /appsrc/lab/api.raml > /appsrc/target/ai_docs/html/lab_api_source.html &&
raml2html /appsrc/docs/APIs/openai.raml > /appsrc/target/ai_docs/html/openai_source.html &&
raml2html /appsrc/docs/APIs/chatapi.raml > /appsrc/target/ai_docs/html/chatapi_source.html"
raml2html /appsrc/docs/APIs/chatapi.raml > /appsrc/target/ai_docs/html/chatapi_source.html &&
raml2html /appsrc/docs/APIs/execapi.raml > /appsrc/target/ai_docs/html/execapi_source.html"
volumes:
- "./:/appsrc"
126 changes: 126 additions & 0 deletions docs/APIs/execapi.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#%RAML 1.0
title: Execution API
version: v1
baseUri: http://localhost:5080/execapi/{version}
mediaType: application/json

types:
file:
type: object
properties:
_id:
type: string
filename:
type: string
mimetype:
type: string
timestamp:
type: string
execution:
type: object
properties:
_id:
type: string
src_code:
type: string
status:
type: string
enum: [submitted, completed, error]
result:
type: string
files:
type: array
items: file
_dataset_file_id?:
type: string
_dataset_id?:
type: string
_experiment_id?:
type: string

/executions:
post:
description: Create a new execution
body:
application/json:
properties:
src_code:
type: string
dataset_file_id?:
type: string
dataset_id?:
type: string
experiment_id?:
type: string
example:
{
"src_code": "df_new = df.head(10)\ndf_new.to_csv('example.csv')",
"dataset_id": "444555666",
"experiment_id": "777888999"
}
responses:
200:
body:
application/json:
type: execution
example:
{
"_id": "987654321",
"src_code": "df_new = df.head(10)\ndf_new.to_csv('example.csv')",
"status": "completed",
"result": "Hello, world!",
"files": [
{
"_id": "123456789",
"filename": "example.csv",
"mimetype": "text/csv",
"timestamp": "2023-04-11T12:34:56Z"
}
],
"_dataset_file_id": "111222333",
"_dataset_id": "444555666",
"_experiment_id": "777888999"
}
400:
body:
application/json:
properties:
message:
type: string
example: { "message": 'No src_code provided' }
/{id}:
uriParameters:
id:
type: string
get:
description: Get execution by id
responses:
200:
body:
application/json:
type: execution
example:
{
"_id": "987654321",
"src_code": "df_new = df.head(10)\ndf_new.to_csv('example.csv')",
"status": "completed",
"result": "Hello, world!",
"files": [
{
"_id": "123456789",
"filename": "example.csv",
"mimetype": "text/csv",
"timestamp": "2023-04-11T12:34:56Z"
}
],
"_dataset_file_id": "111222333",
"_dataset_id": "444555666",
"_experiment_id": "777888999"
}
404:
body:
application/json:
properties:
message:
type: string
example: { message: 'Cannot find execution: 123456789' }
137 changes: 137 additions & 0 deletions docs/APIs/execapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
swagger: '2.0'
info:
title: Execution API
version: v1
basePath: /execapi/v1
consumes:
- application/json
produces:
- application/json

definitions:
file:
type: object
properties:
_id:
type: string
filename:
type: string
mimetype:
type: string
timestamp:
type: string
execution:
type: object
properties:
_id:
type: string
src_code:
type: string
status:
type: string
enum:
- submitted
- completed
- error
result:
type: string
files:
type: array
items:
$ref: '#/definitions/file'
_dataset_file_id:
type: string
_dataset_id:
type: string
_experiment_id:
type: string

paths:
/executions:
post:
summary: Create a new execution
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: execution
schema:
type: object
properties:
src_code:
type: string
dataset_file_id:
type: string
dataset_id:
type: string
experiment_id:
type: string
example:
src_code: df_new = df.head(10)\ndf_new.to_csv('example.csv')
dataset_id: '444555666'
experiment_id: '777888999'
responses:
200:
description: Execution created successfully
schema:
$ref: '#/definitions/execution'
examples:
application/json:
_id: '987654321'
src_code: df_new = df.head(10)\ndf_new.to_csv('example.csv')
status: completed
result: Hello, world!
files:
- _id: '123456789'
filename: example.csv
mimetype: text/csv
timestamp: '2023-04-11T12:34:56Z'
_dataset_file_id: '111222333'
_dataset_id: '444555666'
_experiment_id: '777888999'
400:
description: Bad request
schema:
type: object
properties:
message:
type: string
example: { message: No src_code provided }
/{id}:
get:
summary: Get execution by id
parameters:
- in: path
name: id
description: ID of the execution
type: string
required: true
responses:
200:
description: Execution found
schema:
$ref: '#/definitions/execution'
examples:
application/json:
_id: '987654321'
src_code: df_new = df.head(10)\ndf_new.to_csv('example.csv')
status: completed
result: Hello, world!
files:
- _id: '123456789'
filename: example.csv
mimetype: text/csv
timestamp: '2023-04-11T12:34:56Z'
_dataset_file_id: '111222333'
_dataset_id: '444555666'
_experiment_id: '777888999'
404:
description: Execution not found
schema:
type: object
properties:
message:
type: string
example: { message: Cannot find execution 123456789 }
6 changes: 6 additions & 0 deletions docs/source/execapi.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Code Execution API
========

.. raw:: html

<embed src="execapi_source.html" width="100%" height="600px" style="border:none;" />
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ And, it has an *AI* assistant that can choose the analysis to run for you.
lab_api
openai_api
chatapi
execapi
ai
recommenders
api
Expand Down
26 changes: 10 additions & 16 deletions lab/routes/execapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ router.post('/executions', async (req, res, next) => {
// return res.status(400).json({ message: 'no chatlog_id provided' });
// }

// let request = {
// src_code: req.body.src_code,
// };
// create a new execution
let execution = new Execution({
src_code: req.body.src_code,
Expand All @@ -38,29 +35,21 @@ router.post('/executions', async (req, res, next) => {
});

if (req.body.dataset_file_id != null) {
// request.dataset_file_id = req.body.dataset_file_id;
execution._dataset_file_id = req.body.dataset_file_id;
} else if (req.body.dataset_id != null) {
execution._dataset_id = req.body.dataset_id;
let dataset = await getDatasetById(req.body.dataset_id);
if (dataset != null) {
// request.dataset_file_id = dataset.files[0]._id;
execution._dataset_file_id = dataset.files[0]._id;
}
}

// if (request.dataset_file_id != null) {
// execution._dataset_file_id = request.dataset_file_id;
// }

if (req.body.experiment_id != null) {
execution._experiment_id = req.body.experiment_id;
// request.experiment_id = req.body.experiment_id;
}

try {
const newExecution = await execution.save();
// request.execution_id = newExecution._id;
execution._id = newExecution._id;
} catch (err) {
return res.status(500).json({ message: err.message });
Expand All @@ -87,23 +76,29 @@ router.post('/executions', async (req, res, next) => {
headers: {
'Content-Type': 'application/json'
},
// body: JSON.stringify(request)
body: JSON.stringify(execution)
});
result = await result.json();

// update the execution status
execution.status = result.exec_results.status;
// execution._dataset_file_id = request.dataset_file_id;
execution.result = result.exec_results.result;

// add any generated files in tmppath to the execution.files array
// const files = await uploadExecFiles(request.execution_id, tmppath);
const files = await uploadExecFiles(execution._id, tmppath);
execution.files = files;

const updatedExecution = await execution.save();
// result.files = files;

// delete the tmp folder
fs.rm(tmppath, { recursive: true }, (err) => {
if (err) {
console.error(err);
} else {
console.log(tmppath + ' folder deleted');
}
});

res.send(execution);
}
catch (err) {
Expand Down Expand Up @@ -134,5 +129,4 @@ router.patch('/executions/:id', getExecutionById, async (req, res, next) => {
}
});


module.exports = router;

0 comments on commit d31850d

Please sign in to comment.