Skip to content

Commit

Permalink
final solution ultimate aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
faceyspacey committed Jun 29, 2015
1 parent 110e352 commit 02add59
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/ultimate_aggregate/helper_groupby.js
Expand Up @@ -30,7 +30,7 @@ Ultimate('CreateAggregateGroupByMethodsHelper').extends(CreateAggregateMethodsHe


execAggregateSync: function() {
var aggRows = UltimateAggregateRelationsObserver.prototype.exec(this.aggregate, this.modelClass, this.getGroupForeignKey(), null, this.getGroupByIds());
var aggRows = UltimateAggregateRelationObserver.prototype.exec(this.aggregate, this.modelClass, this.getGroupForeignKey(), null, this.getGroupByIds());

if(!this.getGroup()) return aggRows; //no related model found; return simple group by field array instead
else return this._combineModelsAndAggregates(aggRows);
Expand Down
2 changes: 1 addition & 1 deletion lib/ultimate_aggregate/helper_instance.js
Expand Up @@ -13,7 +13,7 @@ Ultimate('CreateAggregateInstanceMethodsHelper').extends(CreateAggregateMethodsH
this.fkId = modelInstance._id;
},
execAggregateSync: function() {
return UltimateAggregateRelationsObserver.prototype.exec(this._getExecSelector(), this.model, this.fk, true);
return UltimateAggregateRelationObserver.prototype.exec(this._getExecSelector(), this.model, this.fk, true);
},
execAggregateAsync: function(callback) {
this.callParent('execAggregateAsync', this.modelInstance.className, this.fkId, callback);
Expand Down
3 changes: 2 additions & 1 deletion lib/ultimate_aggregate/observer_collection.js
Expand Up @@ -5,8 +5,9 @@ Ultimate('UltimateAggregateCollectionObserver').extends(UltimateAggregateObserve
this.collection = this.modelClass.collection;
},
extractConfig: function(agg) {
agg.collection = this.collection._name;
agg.type = 'collection';
agg.collection = this.collection._name;
agg.selector = this._prepareSelector(agg.selector);
return agg;
},
exec: function(agg, collection) {
Expand Down
13 changes: 2 additions & 11 deletions lib/ultimate_aggregate/observer_relation.js
@@ -1,4 +1,4 @@
Ultimate('UltimateAggregateRelationsObserver').extends(UltimateAggregateObserver, {
Ultimate('UltimateAggregateRelationObserver').extends(UltimateAggregateObserver, {
construct: function(rel, agg, groupModel) {
this.fk = rel.foreign_key;
this.groupModel = groupModel;
Expand All @@ -9,19 +9,10 @@ Ultimate('UltimateAggregateRelationsObserver').extends(UltimateAggregateObserver
},

extractConfig: function(agg) {
if(_.isString(agg)) {
var name = agg;
agg = this.modelClass.prototype.aggregates[name];
agg = UltimateUtilities.extractConfig(agg, this.modelClass.prototype, this.publisher.userId);
agg.aggregate_name = name; //aggregate_name not already on agg as in relation aggregates
}

//if(!UltimateUtilities.isAllowed(agg, this.modelClass.prototype, this.publisher.userId)) return false;

agg.type = 'groupby';
agg.collection = this.collection._name;
agg.selector = this._prepareSelector(agg.selector);
agg.model = this.groupModel.className;
agg.type = 'groupby';

if(agg.formatter) agg.formatter = agg.formatter.bind(this.modelClass.prototype);
return agg;
Expand Down
14 changes: 1 addition & 13 deletions lib/ultimate_aggregate/publisher_collection.js
Expand Up @@ -29,12 +29,8 @@ Ultimate('UltimateAggregateCollectionPublisher').extends(UltimateAggregateObserv
return agg;
},

_prepareSelector: function(agg) {
return _.pick(agg, 'type', 'collection', 'operator', 'field', 'aggregate_name');
},

observeUltimateAggregates: function(agg) {
var selector = this._prepareSelector(agg);
var selector = UltimateAggregateCollectionObserver.prototype._upsertSelector(agg);

if(this.aggregateObserver) this.aggregateObserver.stop();

Expand All @@ -53,14 +49,6 @@ Ultimate('UltimateAggregateCollectionPublisher').extends(UltimateAggregateObserv
},


exec: function(agg, collection) {
return this.callParent('exec', agg, collection, null, true);
},
store: function(result, agg) {
var selector = this._prepareSelector(this._agg);
UltimateAggregates.upsert(selector, {$set: {result: result}});
},

handleCachedClientIds: function(doc) {
//For cached Ids, send 'changed' message also since 'added' message will non-fatally fail client side.
//The client will already have the id in the collection, and won't want to add it again.
Expand Down
40 changes: 33 additions & 7 deletions lib/ultimate_aggregate/ultimate_aggregate_behavior.js
Expand Up @@ -118,24 +118,37 @@ Ultimate('UltimateAggregateBehavior').extends(UltimateBehavior, {}, {
return helper.exec(callback);
};

//this.createAggregateObserver(name, rel);
this.createAggregateObserver(name, rel, true);
},

createAggregateObserver: function(name, rel) {


/** CREATE OBSERVERS FROM SAME METHOD AND AGGREGATE CONFIG INFO AS METHODS **/

createAggregateObserver: function(name, rel, relationOnly) {
if(Meteor.isClient || this.owner().isAbstract()) return;

Meteor.startup(function() {
var agg = this._getAggregateConfig(name, rel);
if(relationOnly) {
var interval = UltimateUtilities.extractConfig(rel, this.ownerProtoype()).interval;

if(!interval) this._createAggregateObserverRelationOnly(name, rel);
else this.setInterval(this._createAggregateObserverRelationOnly.bind(this, name, rel), interval, true);
}
else {
var interval = this._getAggregateConfig(name, rel).interval;

if(!agg.interval) this._createAggregateObserver(name, rel);
else this.setInterval(this._createAggregateObserver.bind(this, name, rel), agg.interval, true);
if(!interval) this._createAggregateObserver(name, rel);
else this.setInterval(this._createAggregateObserver.bind(this, name, rel), interval, true);
}
}.bind(this));
},
_createAggregateObserver: function(name, rel) {
var GroupModel = GroupModel = this.owner(),
var GroupModel = this.owner(),
rel = UltimateUtilities.extractConfig(rel, GroupModel.prototype),
agg = this._getAggregateConfig(name, rel), //call again, i.e in an interval, to get dynamic config returned from functions
aggPub = new UltimateAggregateRelationsObserver(rel, agg, this.owner());
ObserverClass = rel.foreign_key ? UltimateAggregateRelationObserver : UltimateAggregateCollectionObserver;
aggPub = new ObserverClass(rel, agg, this.owner());

aggPub.start();
},
Expand All @@ -150,6 +163,19 @@ Ultimate('UltimateAggregateBehavior').extends(UltimateBehavior, {}, {

return UltimateUtilities.extractConfig(agg, CollectionModel);
},
_createAggregateObserverRelationOnly: function(name, rel) {
var GroupModel = this.owner(),
rel = UltimateUtilities.extractConfig(rel, GroupModel.prototype),
agg = rel, //agg and rel share the same config map in this case,
CollectionModel = UltimateUtilities.classFrom(rel.model);

agg.model = CollectionModel;
agg.aggregate_name = name;

aggPub = new UltimateAggregateRelationObserver(rel, agg, this.owner());
aggPub.start();
},




Expand Down

0 comments on commit 02add59

Please sign in to comment.