2024-09-26 22:59:37 +05:30
|
|
|
import { readFile, readFiles } from "../workflow-management/storage";
|
2024-09-26 22:39:39 +05:30
|
|
|
import { Router, Request, Response } from 'express';
|
2024-09-26 22:41:46 +05:30
|
|
|
import { requireAPIKey } from "../middlewares/api";
|
2024-10-10 05:47:42 +05:30
|
|
|
import Robot from "../models/Robot";
|
2024-10-10 06:13:17 +05:30
|
|
|
import Run from "../models/Run";
|
2024-10-03 17:18:35 +05:30
|
|
|
const router = Router();
|
2024-09-26 22:39:39 +05:30
|
|
|
|
|
|
|
|
const formatRecording = (recordingData: any) => {
|
|
|
|
|
const recordingMeta = recordingData.recording_meta;
|
|
|
|
|
const workflow = recordingData.recording.workflow || [];
|
|
|
|
|
const firstWorkflowStep = workflow[0]?.where?.url || '';
|
2024-09-26 22:39:57 +05:30
|
|
|
|
2024-09-26 22:39:39 +05:30
|
|
|
const inputParameters = [
|
2024-09-26 22:39:57 +05:30
|
|
|
{
|
|
|
|
|
type: "string",
|
|
|
|
|
name: "originUrl",
|
|
|
|
|
label: "Origin URL",
|
|
|
|
|
required: true,
|
|
|
|
|
defaultValue: firstWorkflowStep,
|
|
|
|
|
},
|
2024-09-26 22:39:39 +05:30
|
|
|
];
|
2024-09-26 22:39:57 +05:30
|
|
|
|
2024-09-26 22:39:39 +05:30
|
|
|
return {
|
2024-09-26 22:39:57 +05:30
|
|
|
id: recordingMeta.id,
|
|
|
|
|
name: recordingMeta.name,
|
2024-10-08 20:50:17 +05:30
|
|
|
createdAt: new Date(recordingMeta.createdAt).getTime(),
|
2024-09-26 22:39:57 +05:30
|
|
|
inputParameters,
|
2024-09-26 22:39:39 +05:30
|
|
|
};
|
2024-09-26 22:39:57 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-10-03 17:18:35 +05:30
|
|
|
router.get("/robots", requireAPIKey, async (req: Request, res: Response) => {
|
2024-09-26 22:39:39 +05:30
|
|
|
try {
|
2024-10-10 06:03:56 +05:30
|
|
|
const robots = await Robot.findAll({ raw: true });
|
2024-10-10 05:57:49 +05:30
|
|
|
const formattedRecordings = robots.map(formatRecording);
|
2024-09-26 22:39:57 +05:30
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
robots: {
|
|
|
|
|
totalCount: formattedRecordings.length,
|
|
|
|
|
items: formattedRecordings,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
2024-09-26 22:39:39 +05:30
|
|
|
} catch (error) {
|
2024-09-26 22:39:57 +05:30
|
|
|
console.error("Error fetching recordings:", error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
messageCode: "error",
|
|
|
|
|
message: "Failed to retrieve recordings",
|
|
|
|
|
});
|
2024-09-26 22:39:39 +05:30
|
|
|
}
|
2024-09-26 22:39:57 +05:30
|
|
|
});
|
2024-09-26 22:56:44 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
const formatRecordingById = (recordingData: any) => {
|
|
|
|
|
const recordingMeta = recordingData.recording_meta;
|
|
|
|
|
const workflow = recordingData.recording.workflow || [];
|
|
|
|
|
const firstWorkflowStep = workflow[0]?.where?.url || '';
|
2024-09-26 22:57:07 +05:30
|
|
|
|
2024-09-26 22:56:44 +05:30
|
|
|
const inputParameters = [
|
2024-09-26 22:57:07 +05:30
|
|
|
{
|
|
|
|
|
type: "string",
|
|
|
|
|
name: "originUrl",
|
|
|
|
|
label: "Origin URL",
|
|
|
|
|
required: true,
|
|
|
|
|
defaultValue: firstWorkflowStep,
|
|
|
|
|
},
|
2024-09-26 22:56:44 +05:30
|
|
|
];
|
2024-09-26 22:57:07 +05:30
|
|
|
|
2024-09-26 22:56:44 +05:30
|
|
|
return {
|
2024-09-26 22:57:07 +05:30
|
|
|
id: recordingMeta.id,
|
|
|
|
|
name: recordingMeta.name,
|
2024-10-08 20:50:17 +05:30
|
|
|
createdAt: new Date(recordingMeta.createdAt).getTime(),
|
2024-09-26 22:57:07 +05:30
|
|
|
inputParameters,
|
2024-09-26 22:56:44 +05:30
|
|
|
};
|
2024-09-26 22:57:07 +05:30
|
|
|
};
|
2024-09-26 22:56:44 +05:30
|
|
|
|
2024-10-10 06:08:11 +05:30
|
|
|
router.get("/robots/:id", requireAPIKey, async (req: Request, res: Response) => {
|
2024-09-26 22:56:44 +05:30
|
|
|
try {
|
2024-10-10 06:08:11 +05:30
|
|
|
const robot = await Robot.findOne({
|
|
|
|
|
where: {
|
2024-10-10 06:08:26 +05:30
|
|
|
'recording_meta.id': req.params.id
|
2024-10-10 06:08:11 +05:30
|
|
|
},
|
|
|
|
|
raw: true
|
2024-10-10 06:08:26 +05:30
|
|
|
});
|
2024-09-26 22:57:07 +05:30
|
|
|
|
2024-10-10 06:08:11 +05:30
|
|
|
const formattedRecording = formatRecordingById(robot);
|
2024-09-26 22:57:07 +05:30
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
robot: formattedRecording,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
2024-09-26 22:56:44 +05:30
|
|
|
} catch (error) {
|
2024-09-26 22:57:07 +05:30
|
|
|
console.error("Error fetching recording:", error);
|
|
|
|
|
res.status(404).json({
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
messageCode: "not_found",
|
|
|
|
|
message: `Recording with name "${req.params.fileName}" not found.`,
|
|
|
|
|
});
|
2024-09-26 22:56:44 +05:30
|
|
|
}
|
2024-09-26 22:57:07 +05:30
|
|
|
});
|
2024-10-03 17:18:35 +05:30
|
|
|
|
2024-10-10 06:13:17 +05:30
|
|
|
router.get("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const runs = await Run.findAll({
|
|
|
|
|
where: {
|
|
|
|
|
robotId: req.params.id
|
|
|
|
|
},
|
|
|
|
|
raw: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
runs: {
|
|
|
|
|
totalCount: runs.length,
|
|
|
|
|
items: runs,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching runs:", error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
messageCode: "error",
|
|
|
|
|
message: "Failed to retrieve runs",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-10 06:15:07 +05:30
|
|
|
router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const run = await Run.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
runId: req.params.runId
|
|
|
|
|
},
|
|
|
|
|
raw: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
run: run,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching run:", error);
|
|
|
|
|
res.status(404).json({
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
messageCode: "not_found",
|
|
|
|
|
message: `Run with id "${req.params.runId}" not found.`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-03 17:18:35 +05:30
|
|
|
export default router;
|