From dff6905050d47d1c4f49046278b6615dca4dc8ec Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Tue, 20 Jun 2023 15:45:35 -0400 Subject: [PATCH] Get status --- src/controllers/PrinterController.ts | 16 ++++++++++++--- src/controllers/PrinterStatusController.ts | 3 ++- src/utils/distributedCopy.ts | 23 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/utils/distributedCopy.ts diff --git a/src/controllers/PrinterController.ts b/src/controllers/PrinterController.ts index c6c5af2..87dafff 100644 --- a/src/controllers/PrinterController.ts +++ b/src/controllers/PrinterController.ts @@ -3,6 +3,7 @@ import { Request, Response, Router } from 'express' import { hasRolesMiddleware } from '../middlewares/hasRolesMiddleware.js' import { prisma } from '../prisma.js' import { PrinterStatusService } from '../services/PrinterStatusService.js' +import { distributedCopy } from '../utils/distributedCopy.js' const router = Router() @@ -15,9 +16,9 @@ class PrinterController { static async show(req: Request, res: Response) { const { id } = req.params + const { take, minutes = 43200 } = req.query - // 30 days - const gte = new Date(Date.now() - 1000 * 60 * 60 * 24 * 30) + const gte = new Date(Date.now() - 1000 * 60 * Number(minutes)) const printer = await prisma.printer.findUnique({ where: { id: Number(id) }, @@ -27,12 +28,21 @@ class PrinterController { createdAt: { gte } + }, + + orderBy: { + createdAt: 'desc' } } } }) - res.json(printer) + if (printer) + res.json({ + ...printer, + status: distributedCopy(printer.status, Number(take)) + }) + else res.status(400).json({ error: 'Printer not found' }) } static async create(req: Request, res: Response) { diff --git a/src/controllers/PrinterStatusController.ts b/src/controllers/PrinterStatusController.ts index 12b0242..2058851 100644 --- a/src/controllers/PrinterStatusController.ts +++ b/src/controllers/PrinterStatusController.ts @@ -16,5 +16,6 @@ class PrinterStatusController { } } -router.get('/update', PrinterStatusController.update) +router.post('/update', PrinterStatusController.update) + export default router diff --git a/src/utils/distributedCopy.ts b/src/utils/distributedCopy.ts new file mode 100644 index 0000000..e2b1397 --- /dev/null +++ b/src/utils/distributedCopy.ts @@ -0,0 +1,23 @@ +/** + * Retrieve a fixed number of elements from an array, evenly distributed but + * always including the first and last elements. + * + * @param {Array} originalArray - The array to operate on. + * @param {number} take - The number of elements to extract. + * @returns {Array} + */ + +export function distributedCopy(originalArray: Array, take: number = 10) { + if (originalArray.length <= take) return [...originalArray] + + const newArray = [originalArray[0]] + + const interval = (originalArray.length - 2) / (take - 2) + + for (let i = 1; i < take - 1; i++) + newArray.push(originalArray[Math.floor(interval * i)]) + + newArray.push(originalArray[originalArray.length - 1]) + + return newArray +}