This commit is contained in:
Douglas Barone 2023-07-12 12:37:06 -04:00
parent e416bbf00b
commit 3d9f3f0135
3 changed files with 73 additions and 2 deletions

View File

@ -74,12 +74,17 @@ class PrinterController {
}
})
if (printer)
if (printer) {
const avgMonthPrint = await PrinterStatusService.avgMonthPrint(
printer.serialNumber
)
res.json({
...printer,
avgMonthPrint,
status: distributedCopy(printer.status, Number(take))
})
else res.status(400).json({ error: 'Printer not found' })
} else res.status(400).json({ error: 'Printer not found' })
}
static async edit(req: Request, res: Response) {

View File

@ -254,4 +254,52 @@ export class PrinterStatusService {
return printerInfo
}
static async avgMonthPrint(serialNumber: string) {
const gte = new Date(new Date().getTime() - 1000 * 3600 * 24 * 180)
const firstPrinterStatus = await prisma.printerStatus.findFirst({
where: {
printer: {
serialNumber
},
timestamp: {
gte
}
},
orderBy: { timestamp: 'asc' }
})
const lastPrinterStatus = await prisma.printerStatus.findFirst({
where: {
printer: {
serialNumber
},
timestamp: {
gte
}
},
orderBy: { timestamp: 'desc' }
})
if (!firstPrinterStatus || !lastPrinterStatus) return 0
const firstCounter = firstPrinterStatus.counter
const lastCounter = lastPrinterStatus.counter
const firstTimestamp = firstPrinterStatus.timestamp
const lastTimestamp = lastPrinterStatus.timestamp
const timeDiff = lastTimestamp.getTime() - firstTimestamp.getTime()
const counterDiff = lastCounter - firstCounter
const avgMonthPrint = Math.floor(
counterDiff / (timeDiff / 1000 / 3600 / 24 / 30)
)
console.log(counterDiff, timeDiff, avgMonthPrint)
return avgMonthPrint
}
}

View File

@ -13,6 +13,24 @@
<printer-counter-chart v-if="printer" :status="printer?.status" />
</v-col>
</v-row>
<v-row v-if="printer?.avgMonthPrint">
<v-col>
<v-card>
<v-card-title class="font-weight-regular">
Média mensal de impressões
</v-card-title>
<v-card-text>
<span class="text-h3">
{{
new Intl.NumberFormat('pt-BR').format(printer?.avgMonthPrint)
}}
</span>
<div>Baseado nos dados dos últimos 180 dias</div>
</v-card-text>
</v-card>
</v-col>
</v-row>
</v-container>
</template>
<script lang="ts" setup>