diff --git a/src/database/schemas/production/printer.schema.js b/src/database/schemas/production/printer.schema.js index cf035b4..53c5447 100644 --- a/src/database/schemas/production/printer.schema.js +++ b/src/database/schemas/production/printer.schema.js @@ -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;