feat: robot run analytics
This commit is contained in:
@@ -15,6 +15,7 @@ import { BinaryOutputService } from '../storage/mino';
|
|||||||
import { workflowQueue } from '../worker';
|
import { workflowQueue } from '../worker';
|
||||||
import { AuthenticatedRequest } from './record';
|
import { AuthenticatedRequest } from './record';
|
||||||
import { computeNextRun } from '../utils/schedule';
|
import { computeNextRun } from '../utils/schedule';
|
||||||
|
import captureServerAnalytics from "../utils/analytics";
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
@@ -59,11 +60,23 @@ router.get('/recordings/:id', requireSignIn, async (req, res) => {
|
|||||||
/**
|
/**
|
||||||
* DELETE endpoint for deleting a recording from the storage.
|
* DELETE endpoint for deleting a recording from the storage.
|
||||||
*/
|
*/
|
||||||
router.delete('/recordings/:id', requireSignIn, async (req, res) => {
|
router.delete('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
||||||
|
if (!req.user) {
|
||||||
|
return res.status(401).send({ error: 'Unauthorized' });
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await Robot.destroy({
|
await Robot.destroy({
|
||||||
where: { 'recording_meta.id': req.params.id }
|
where: { 'recording_meta.id': req.params.id }
|
||||||
});
|
});
|
||||||
|
captureServerAnalytics.capture({
|
||||||
|
distinctId: req.user?.id,
|
||||||
|
event: 'maxun-oss-robot-deleted',
|
||||||
|
properties: {
|
||||||
|
robotId: req.params.id,
|
||||||
|
user_id: req.user?.id,
|
||||||
|
deleted_at: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
})
|
||||||
return res.send(true);
|
return res.send(true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const { message } = e as Error;
|
const { message } = e as Error;
|
||||||
@@ -88,9 +101,21 @@ router.get('/runs', requireSignIn, async (req, res) => {
|
|||||||
/**
|
/**
|
||||||
* DELETE endpoint for deleting a run from the storage.
|
* DELETE endpoint for deleting a run from the storage.
|
||||||
*/
|
*/
|
||||||
router.delete('/runs/:id', requireSignIn, async (req, res) => {
|
router.delete('/runs/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
||||||
|
if (!req.user) {
|
||||||
|
return res.status(401).send({ error: 'Unauthorized' });
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await Run.destroy({ where: { runId: req.params.id } });
|
await Run.destroy({ where: { runId: req.params.id } });
|
||||||
|
captureServerAnalytics.capture({
|
||||||
|
distinctId: req.user?.id,
|
||||||
|
event: 'maxun-oss-run-deleted',
|
||||||
|
properties: {
|
||||||
|
runId: req.params.id,
|
||||||
|
user_id: req.user?.id,
|
||||||
|
deleted_at: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
})
|
||||||
return res.send(true);
|
return res.send(true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const { message } = e as Error;
|
const { message } = e as Error;
|
||||||
@@ -194,9 +219,9 @@ router.get('/runs/run/:id', requireSignIn, async (req, res) => {
|
|||||||
/**
|
/**
|
||||||
* PUT endpoint for finishing a run and saving it to the storage.
|
* PUT endpoint for finishing a run and saving it to the storage.
|
||||||
*/
|
*/
|
||||||
router.post('/runs/run/:id', requireSignIn, async (req, res) => {
|
router.post('/runs/run/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
||||||
try {
|
try {
|
||||||
console.log(`Params for POST /runs/run/:id`, req.params.id)
|
if (!req.user) { return res.status(401).send({ error: 'Unauthorized' }); }
|
||||||
|
|
||||||
const run = await Run.findOne({ where: { runId: req.params.id } });
|
const run = await Run.findOne({ where: { runId: req.params.id } });
|
||||||
if (!run) {
|
if (!run) {
|
||||||
@@ -228,6 +253,15 @@ router.post('/runs/run/:id', requireSignIn, async (req, res) => {
|
|||||||
serializableOutput: interpretationInfo.serializableOutput,
|
serializableOutput: interpretationInfo.serializableOutput,
|
||||||
binaryOutput: uploadedBinaryOutput,
|
binaryOutput: uploadedBinaryOutput,
|
||||||
});
|
});
|
||||||
|
captureServerAnalytics.capture({
|
||||||
|
distinctId: req.user?.id,
|
||||||
|
event: 'maxun-oss-run-created',
|
||||||
|
properties: {
|
||||||
|
runId: req.params.id,
|
||||||
|
user_id: req.user?.id,
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
})
|
||||||
try {
|
try {
|
||||||
googleSheetUpdateTasks[plainRun.runId] = {
|
googleSheetUpdateTasks[plainRun.runId] = {
|
||||||
robotId: plainRun.robotMetaId,
|
robotId: plainRun.robotMetaId,
|
||||||
@@ -349,6 +383,16 @@ router.put('/schedule/:id/', requireSignIn, async (req: AuthenticatedRequest, re
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
captureServerAnalytics.capture({
|
||||||
|
distinctId: req.user.id,
|
||||||
|
event: 'maxun-oss-robot-scheduled',
|
||||||
|
properties: {
|
||||||
|
robotId: id,
|
||||||
|
user_id: req.user.id,
|
||||||
|
scheduled_at: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Fetch updated schedule details after setting it
|
// Fetch updated schedule details after setting it
|
||||||
const updatedRobot = await Robot.findOne({ where: { 'recording_meta.id': id } });
|
const updatedRobot = await Robot.findOne({ where: { 'recording_meta.id': id } });
|
||||||
|
|
||||||
@@ -383,10 +427,14 @@ router.get('/schedule/:id', requireSignIn, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Endpoint to delete schedule
|
// Endpoint to delete schedule
|
||||||
router.delete('/schedule/:id', requireSignIn, async (req, res) => {
|
router.delete('/schedule/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
|
if (!req.user) {
|
||||||
|
return res.status(401).json({ error: 'Unauthorized' });
|
||||||
|
}
|
||||||
|
|
||||||
const robot = await Robot.findOne({ where: { 'recording_meta.id': id } });
|
const robot = await Robot.findOne({ where: { 'recording_meta.id': id } });
|
||||||
if (!robot) {
|
if (!robot) {
|
||||||
return res.status(404).json({ error: 'Robot not found' });
|
return res.status(404).json({ error: 'Robot not found' });
|
||||||
@@ -405,6 +453,16 @@ router.delete('/schedule/:id', requireSignIn, async (req, res) => {
|
|||||||
schedule: null
|
schedule: null
|
||||||
});
|
});
|
||||||
|
|
||||||
|
captureServerAnalytics.capture({
|
||||||
|
distinctId: req.user?.id,
|
||||||
|
event: 'maxun-oss-robot-schedule-deleted',
|
||||||
|
properties: {
|
||||||
|
robotId: id,
|
||||||
|
user_id: req.user?.id,
|
||||||
|
unscheduled_at: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
res.status(200).json({ message: 'Schedule deleted successfully' });
|
res.status(200).json({ message: 'Schedule deleted successfully' });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user