| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- "use strict";
- /**
- * A class for constructing accumulators to calculate avg.
- * @class AvgAccumulator
- * @namespace mungedb-aggregate.pipeline.accumulators
- * @module mungedb-aggregate
- * @constructor
- **/
- var AvgAccumulator = module.exports = function AvgAccumulator(){
- this.reset();
- base.call(this);
- }, klass = AvgAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
- var Value = require("../Value");
- var SUB_TOTAL_NAME = "subTotal";
- var COUNT_NAME = "count";
- proto.processInternal = function processInternal(input, merging) {
- if (!merging) {
- // non numeric types have no impact on average
- if (typeof input != "number") return;
- this._total += input;
- this._count += 1;
- } else {
- // We expect an object that contains both a subtotal and a count.
- // This is what getValue(true) produced below.
- if (!(input instanceof Object)) throw new Error("Assertion error");
- this._total += input[SUB_TOTAL_NAME];
- this._count += input[COUNT_NAME];
- }
- };
- klass.create = function create() {
- return new AvgAccumulator();
- };
- proto.getValue = function getValue(toBeMerged) {
- if (!toBeMerged) {
- if (this._count === 0)
- return 0.0;
- return this._total / this._count;
- } else {
- var doc = {};
- doc[SUB_TOTAL_NAME] = this._total;
- doc[COUNT_NAME] = this._count;
- return doc;
- }
- };
- proto.reset = function reset() {
- this._total = 0;
- this._count = 0;
- };
- proto.getOpName = function getOpName() {
- return "$avg";
- };
|