Merge pull request #81 from amhsirak/develop

feat: additional robot metadata
This commit is contained in:
Karishma Shukla
2024-10-21 23:15:57 +05:30
committed by GitHub
5 changed files with 41 additions and 11 deletions

View File

@@ -114,7 +114,6 @@ router.get("/robots/:id", requireAPIKey, async (req: Request, res: Response) =>
} }
}); });
// TODO: Format runs to send more data formatted
router.get("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => { router.get("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => {
try { try {
const runs = await Run.findAll({ const runs = await Run.findAll({

View File

@@ -1,6 +1,8 @@
import { Model, DataTypes, Optional } from 'sequelize'; import { Model, DataTypes, Optional } from 'sequelize';
import sequelize from '../storage/db'; import sequelize from '../storage/db';
import { WorkflowFile, Where, What, WhereWhatPair } from 'maxun-core'; import { WorkflowFile, Where, What, WhereWhatPair } from 'maxun-core';
import User from './User'; // Import User model
import Run from './Run';
interface RobotMeta { interface RobotMeta {
name: string; name: string;
@@ -17,6 +19,7 @@ interface RobotWorkflow {
interface RobotAttributes { interface RobotAttributes {
id: string; id: string;
userId?: number;
recording_meta: RobotMeta; recording_meta: RobotMeta;
recording: RobotWorkflow; recording: RobotWorkflow;
google_sheet_email?: string | null; google_sheet_email?: string | null;
@@ -30,6 +33,7 @@ interface RobotCreationAttributes extends Optional<RobotAttributes, 'id'> { }
class Robot extends Model<RobotAttributes, RobotCreationAttributes> implements RobotAttributes { class Robot extends Model<RobotAttributes, RobotCreationAttributes> implements RobotAttributes {
public id!: string; public id!: string;
public userId!: number;
public recording_meta!: RobotMeta; public recording_meta!: RobotMeta;
public recording!: RobotWorkflow; public recording!: RobotWorkflow;
public google_sheet_email!: string | null; public google_sheet_email!: string | null;
@@ -46,6 +50,10 @@ Robot.init(
defaultValue: DataTypes.UUIDV4, defaultValue: DataTypes.UUIDV4,
primaryKey: true, primaryKey: true,
}, },
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
recording_meta: { recording_meta: {
type: DataTypes.JSONB, type: DataTypes.JSONB,
allowNull: false, allowNull: false,
@@ -82,4 +90,9 @@ Robot.init(
} }
); );
// Robot.hasMany(Run, {
// foreignKey: 'robotId',
// as: 'runs', // Alias for the relation
// });
export default Robot; export default Robot;

View File

@@ -1,5 +1,6 @@
import { DataTypes, Model, Optional } from 'sequelize'; import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../storage/db'; import sequelize from '../storage/db';
import Robot from './Robot';
interface UserAttributes { interface UserAttributes {
id: number; id: number;
@@ -79,4 +80,9 @@ User.init(
} }
); );
// User.hasMany(Robot, {
// foreignKey: 'userId',
// as: 'robots', // Alias for the relation
// });
export default User; export default User;

View File

@@ -126,10 +126,12 @@ export class WorkflowGenerator {
* @private * @private
*/ */
private registerEventHandlers = (socket: Socket) => { private registerEventHandlers = (socket: Socket) => {
socket.on('save', async (fileName: string) => { socket.on('save', (data) => {
logger.log('debug', `Saving workflow ${fileName}`); console.log('Received data:', data);
await this.saveNewWorkflow(fileName) const { fileName, userId } = data;
}); logger.log('debug', `Saving workflow ${fileName} for user ID ${userId}`);
this.saveNewWorkflow(fileName, userId);
});
socket.on('new-recording', () => this.workflowRecord = { socket.on('new-recording', () => this.workflowRecord = {
workflow: [], workflow: [],
}); });
@@ -477,7 +479,7 @@ export class WorkflowGenerator {
* @param fileName The name of the file. * @param fileName The name of the file.
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
public saveNewWorkflow = async (fileName: string) => { public saveNewWorkflow = async (fileName: string, userId: number) => {
const recording = this.optimizeWorkflow(this.workflowRecord); const recording = this.optimizeWorkflow(this.workflowRecord);
try { try {
this.recordingMeta = { this.recordingMeta = {
@@ -489,6 +491,7 @@ export class WorkflowGenerator {
params: this.getParams() || [], params: this.getParams() || [],
} }
const robot = await Robot.create({ const robot = await Robot.create({
userId,
recording_meta: this.recordingMeta, recording_meta: this.recordingMeta,
recording: recording, recording: recording,
}); });
@@ -497,7 +500,7 @@ export class WorkflowGenerator {
} }
catch (e) { catch (e) {
const { message } = e as Error; const { message } = e as Error;
logger.log('warn', `Cannot save the file to the local file system`) logger.log('warn', `Cannot save the file to the local file system ${e}`)
} }
this.socket.emit('fileSaved'); this.socket.emit('fileSaved');
} }

View File

@@ -1,8 +1,9 @@
import React, { useCallback, useEffect, useState } from 'react'; import React, { useCallback, useEffect, useState, useContext } from 'react';
import { IconButton, Button, Box, LinearProgress, Tooltip } from "@mui/material"; import { IconButton, Button, Box, LinearProgress, Tooltip } from "@mui/material";
import { GenericModal } from "../atoms/GenericModal"; import { GenericModal } from "../atoms/GenericModal";
import { stopRecording } from "../../api/recording"; import { stopRecording } from "../../api/recording";
import { useGlobalInfoStore } from "../../context/globalInfo"; import { useGlobalInfoStore } from "../../context/globalInfo";
import { AuthContext } from '../../context/auth';
import { useSocketStore } from "../../context/socket"; import { useSocketStore } from "../../context/socket";
import { TextField, Typography } from "@mui/material"; import { TextField, Typography } from "@mui/material";
import { WarningText } from "../atoms/texts"; import { WarningText } from "../atoms/texts";
@@ -24,6 +25,8 @@ export const SaveRecording = ({ fileName }: SaveRecordingProps) => {
const { browserId, setBrowserId, notify, recordings } = useGlobalInfoStore(); const { browserId, setBrowserId, notify, recordings } = useGlobalInfoStore();
const { socket } = useSocketStore(); const { socket } = useSocketStore();
const { state, dispatch } = useContext(AuthContext);
const { user } = state;
const navigate = useNavigate(); const navigate = useNavigate();
const handleChangeOfTitle = (event: React.ChangeEvent<HTMLInputElement>) => { const handleChangeOfTitle = (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -56,9 +59,15 @@ export const SaveRecording = ({ fileName }: SaveRecordingProps) => {
// notifies backed to save the recording in progress, // notifies backed to save the recording in progress,
// releases resources and changes the view for main page by clearing the global browserId // releases resources and changes the view for main page by clearing the global browserId
const saveRecording = async () => { const saveRecording = async () => {
socket?.emit('save', recordingName) if (user) {
setWaitingForSave(true); const payload = { fileName: recordingName, userId: user.id };
} socket?.emit('save', payload);
setWaitingForSave(true);
console.log(`Saving the recording as ${recordingName} for userId ${user.id}`);
} else {
console.error('User not logged in. Cannot save recording.');
}
};
useEffect(() => { useEffect(() => {
socket?.on('fileSaved', exitRecording); socket?.on('fileSaved', exitRecording);