Added printer stats.

This commit is contained in:
Tom Butcher 2025-12-13 21:06:39 +00:00
parent 9aac0f8316
commit 15c2f2982e

View File

@ -1,6 +1,7 @@
import mongoose from 'mongoose';
import { generateId } from '../../utils.js';
const { Schema } = mongoose;
import { aggregateRollups, aggregateRollupsHistory } from '../../database.js';
// Define the moonraker connection schema
const moonrakerSchema = new Schema(
@ -56,6 +57,59 @@ const printerSchema = new Schema(
{ timestamps: true }
);
const rollupConfigs = [
{
name: 'standby',
filter: { 'state.type': 'standby' },
rollups: [{ name: 'standby', property: 'state.type', operation: 'count' }],
},
{
name: 'complete',
filter: { 'state.type': 'complete' },
rollups: [{ name: 'complete', property: 'state.type', operation: 'count' }],
},
{
name: 'printing',
filter: { 'state.type': 'printing' },
rollups: [{ name: 'printing', property: 'state.type', operation: 'count' }],
},
{
name: 'error',
filter: { 'state.type': 'error' },
rollups: [{ name: 'error', property: 'state.type', operation: 'count' }],
},
{
name: 'offline',
filter: { 'state.type': 'offline' },
rollups: [{ name: 'offline', property: 'state.type', operation: 'count' }],
},
];
printerSchema.statics.stats = async function () {
const results = await aggregateRollups({
model: this,
baseFilter: { active: true },
rollupConfigs: rollupConfigs,
});
console.log(results);
// Transform the results to match the expected format
return results;
};
printerSchema.statics.history = async function (from, to) {
const results = await aggregateRollupsHistory({
model: this,
startDate: from,
endDate: to,
rollupConfigs: rollupConfigs,
});
// Return time-series data array
return results;
};
// Add virtual id getter
printerSchema.virtual('id').get(function () {
return this._id;