Skip to content

Commit

Permalink
api to add a medium background into the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalvagno committed Dec 12, 2024
1 parent 0a45928 commit 8c797e6
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 22 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ enum ScanType {
MASK
RAW_BACKGROUND
VIS
MEDIUM_BACKGROUND
OUT
}

Expand Down
47 changes: 40 additions & 7 deletions routes/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ module.exports = {

addurl: async (req,res) => {
console.log("------------------------------------------");
console.log("addurl",req);
console.log("route/background/addurl");
// console.log("route/background/addurl",req);
// console.log("create files",req.files);
console.log("addurl Req", req.query);
console.log("addurl Req.query", req.query);
console.log("------------------------------------------");

if ( !isRoleAllowed(req)){
Expand All @@ -283,15 +284,46 @@ module.exports = {
}
console.log("req.query.projectId: ", req.query.projectId);

try {
return await background.addurl({
let params = {
userId:id,
//image:req.body,
url: req.body.url,
instrumentId:req.params.instrumentId,
projectId: req.query.projectId
/*, type:BackgroundType.BACKGROUND*/
})
}


if ( req.body.taskId ){
params = {
...params,
taskId: req.body.taskId
}
} else {
params = {
...params,
taskId: "RAW_BACKGROUND"
}
}

if ( req.body.type && req.body.type == "MEDIUM_BACKGROUND+"){
params = {
...params,
type: req.body.type
}
}


try {
// return await background.addurl({
// userId:id,
// //image:req.body,
// url: req.body.url,
// instrumentId:req.params.instrumentId,
// projectId: req.query.projectId
// /*, type:BackgroundType.BACKGROUND*/
// })
return await background.addurl(params)
.then(result => {
// console.log("OK", res)
return res.status(200).json(result)
Expand Down Expand Up @@ -321,6 +353,7 @@ module.exports = {

addurl2: async (req,res) => {
console.log("------------------------------------------");
console.log("route/background/addurl2");
// console.log("create",req);
// console.log("create files",req.files);
console.log("------------------------------------------");
Expand All @@ -329,7 +362,7 @@ module.exports = {
return res.status(401).send("You are not authorized to access this resource")
}

console.log("req.jwt: ", req.jwt)
// console.log("req.jwt: ", req.jwt)
const userID = req.jwt.id

// plus besoin géré dans openapi
Expand All @@ -340,7 +373,7 @@ module.exports = {
// if ( req.body.subSampleId == undefined) {
// return res.status(400).json({error:"subsampleId is required"})
// }
console.log("req.body: ", req.body);
// console.log("req.body: ", req.body);
console.log("req.body.url: ", req.body.url);
console.log("req.params: ", req.params);

Expand Down
4 changes: 2 additions & 2 deletions routes/scanne.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ addurl: async (req,res) => {
return res.status(401).send("You are not authorized to access this resource")
}

console.log("req.jwt: ", req.jwt);
// console.log("req.jwt: ", req.jwt);
const userID = req.jwt.id

if ( req.body.url == undefined){
return res.status(400).json({error:"URL is required"})
}
console.log("req.body: ", req.body);
// console.log("req.body: ", req.body);
console.log("req.body.url: ", req.body.url);

return scan.addurl({
Expand Down
30 changes: 21 additions & 9 deletions routes/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const handlers = {


status: async (req,res) => {
console.debug("status() - Headers:", req.headers);
console.debug("route Task status() - Headers:", req.headers);

// ... existing role check ...

Expand Down Expand Up @@ -204,7 +204,7 @@ const handlers = {


run: async (req, res) => {
console.log("run", req.body);
console.log("route Task.run()", req.body);

if (!isRoleAllowed(req)) {
return res.status(401).json({
Expand Down Expand Up @@ -240,14 +240,26 @@ const handlers = {
// }, { status: 500 });
// }

try {
const result = await tasks.run(req.body, authHeader);
return res.status(200).json(result);
} catch (error) {
console.error("Task::run() Error:", error);
return res.status(500).json({error: error.message || error});
}
// try {
// const result = await tasks.run(req.body, authHeader);
// return res.status(200).json(result);
// } catch (error) {
// console.error("Task::run() Error:", error);
// return res.status(500).json({error: error.message || error});
// }

tasks.run({taskId: req.params.taskId}, req.headers.authorization)
.then(result => {

console.log("route Task run() OK", result)

return res.status(200).json(result);
})
.catch(error => {
console.error('route Task run error:', error);
return res.status(500).json({error: error.toString()});
});

},

delete: async (req,res) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/validate_tags.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

module.exports = {
isRoleAllowed : (req) => {
isRoleAllowed : (req, forcedRole) => {
const allowedRoles = req.openapi.schema.tags;
if ( req.jwt && req.jwt.role){
const role = req.jwt.role
Expand Down
95 changes: 95 additions & 0 deletions services/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const { SubSamples } = require("./prisma/subSample");

module.exports.Background = class {

zooProcessApiUrl = "http://zooprocess.imev-mer.fr:8081/v1/"
happyPipelineUrl = 'http://zooprocess.imev-mer.fr:8000/'

constructor() {
// const background = true

Expand Down Expand Up @@ -282,6 +285,7 @@ module.exports.Background = class {

}

// used by POST /background/{instrumentId}/url?projectId={projectId}
async addurl({ instrumentId , url , userId , projectId /*, type*/}) {

console.log("background::addurl")
Expand Down Expand Up @@ -606,5 +610,96 @@ async addurl3({ url , userId , subsampleId, type, move}) {
return this.scans.add(data)
}


async medium(data, bearer, taskInstance){
console.log("tasks::medium()")
console.debug("tasks::process data:", data)
console.debug("tasks::process bearer:", bearer)

const taskId = data.id
console.debug("process - taskId:",taskId)
taskInstance.setTaskStatus(taskId, {status:"ANALYSING",log:"analysing"})

taskInstance.setTaskStatus(taskId, {status:"RUNNING",log:"running"})

const body = {

taskId: data.id,
instrumentId: data.params.instrumentId,
// src: "srcpath",
// dst: "dstpath",
// back1: data.params.background[0],
// back2: data.params.background[1],
projectId: data.params.project,
background:data.params.background,

// scanId: data.params.scanId,
// taskId: taskId,
bearer: bearer,
db: "http://zooprocess.imev-mer.fr:8081/v1/",
}


console.debug("body:", body)


const processUrl = `${this.happyPipelineUrl}background/`
console.debug("processUrl:",processUrl)
// fetch(processUrl, {
// method: 'POST',
// body: JSON.stringify(body),
// headers: {
// 'Content-type': 'application/json; charset=UTF-8',
// "Accept": "application/json",
// "User-Agent": "Zooprocess v10",
// "Authorization": bearer

// },
// })
// .then(async (response) => {
// console.debug("---------> response", response)
// if (! response.ok) {
// console.debug("Happy reponse:",response.status)
// // const text = await response.text();
// try {
// const text = response.text();
// console.error("Error details:", text);
// return Promise.reject(`Cannot launch the task ${taskId} - Error: ${response.status}`);
// // throw Error(`Cannot launch the task ${taskId} - Error: ${response.status}`);
// }
// catch (error) {
// // console.error("Error details:", error);
// // return Promise.reject(`Cannot launch the task ${taskId} - Error: ${error}`);
// return Promise.reject(error);

// }
// }
// return response.json()
// })
try {
const response = await fetch(processUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-type': 'application/json; charset=UTF-8',
"Accept": "application/json",
"User-Agent": "Zooprocess v10",
"Authorization": bearer
},
});

if (!response.ok) {
const text = await response.text();
console.error("Error details:", text);
throw new Error(`Cannot launch the task ${taskId} - Error: ${response.status}`);
}

return await response.json();
} catch (error) {
throw error;
}

}

}

16 changes: 14 additions & 2 deletions services/tasks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// const background = require('../routes/background');
// const { update } = require('../routes/task');
const background = require('../routes/background');
const { Background } = require('./background');
const { Prisma } = require('./client');
const { Process } = require('./process');
Expand Down Expand Up @@ -189,9 +190,11 @@ class Tasks {

// taskId = data.taskId

console.log("Task::run(taskId=", taskId)
console.log("Service Task::run(taskId=", taskId)
console.log("Task::run(authHeader=", authHeader)
console.trace()

try {
const bearer = authHeader.split(" ")[1]

const task = await this.prisma.task.findFirst({
Expand Down Expand Up @@ -225,18 +228,27 @@ class Tasks {

const process = new Process()
// return await this.process(task, bearer)
return await process.process(task, bearer,this)
return await process.process(task, bearer, this)

//TODO change the promise with code to do the processing job
// return Promise(function(resolve, reject) {
// return resolve({taskId})
// })

case TaskType.background.toUpperCase():

const background = new Background()
return await background.medium(task, bearer, this)


default:

throw new Error("TaskType not valid")

}
} catch (error) {
throw error; // This will be caught by the route handler's try/catch
}

}

Expand Down
6 changes: 5 additions & 1 deletion zooprocess.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,10 @@ paths:
security:
- BearerAuth: [ ]

# put:
# summary: add a background (medium)
# description: Add a background to use with share folder,
# operationId: Background.addurl

# /scan/{instrumentId}/url:
/scan/{subSampleId}/url:
Expand Down Expand Up @@ -1291,7 +1295,7 @@ paths:
- BearerAuth: [ ]

post:
description: Change task informtion
description: Change task information
operationId: Task.update
responses:
'200':
Expand Down

0 comments on commit 8c797e6

Please sign in to comment.