Skip to content

Commit

Permalink
added .indentifySources()
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed Apr 23, 2013
1 parent 4d260d4 commit a2c7b06
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,18 @@ Only aggregate values, source will not be aggregated and return values aggregati

Metrics.aggregate = function(name, resolution, valueAggregator, sourceAggregator, query, callback){}

##### identifySources

identify sources available for a given metric

@param {String} name - name of the metric
@param {Object} query - mongodb query for filtering out metrics
only supports date only
@param {Function} callback - callback function
callback(err, resultsArray)

Metrics.indentifySources = function (name, query, callback) {}

#### Example

~~~js
Expand Down
49 changes: 48 additions & 1 deletion lib/metrics.js
Expand Up @@ -117,7 +117,7 @@ function Metrics (mongoUrl, collection) {
possible values: "sum", "avg", "min", "max"
@param {Array} sourceFilter - list of sources need to be return in the result. if empty all the sources will be retured
@param {Object} query - mongodb query for filtering out metrics
only supports date and source only
only supports date only
@param {Function} callback - callback function
callback(err, results)
*/
Expand Down Expand Up @@ -164,6 +164,53 @@ function Metrics (mongoUrl, collection) {
});
};

/*
identify sources available for a given metric
@param {String} name - name of the metric
@param {Object} query - mongodb query for filtering out metrics
only supports date only
@param {Function} callback - callback function
callback(err, resultsArray)
*/
this.identifySources = function identifySources(name, query, callback) {

if(typeof(query) == 'function') {
callback = query;
query = {};
}

var filter = { name: name };
['date'].forEach(function(field) {

if(query[field]) {
filter[field] = query[field];
}
});
var aggreation = [
{$match: filter},
{$group: {_id: "$source", val: {$sum: 1}}}
];

mongoDatabase.ready(function() {

mongoDatabase.db.collection(collection).aggregate(aggreation, filterOnlySourceNames);
});

function filterOnlySourceNames(err, results) {

if(err) {
callback(err);
} else {
var sourceNames = [];
results.forEach(function(item) {
sourceNames.push(item._id);
});
callback(null, sourceNames);
}
}
};

this._generateValueGrouping = function _generateValueGrouping(resolution, aggregator) {

var _id = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "minum",
"version": "0.2.0",
"version": "0.2.1",
"description": "Core 'minum' module with backend tracking and aggregation",
"keywords": [
"metrics",
Expand Down
63 changes: 63 additions & 0 deletions test/metrics.js
Expand Up @@ -199,6 +199,69 @@ suite('Metrics', function() {
}
}));
});

suite('.identifySources()', function() {

test('without query', _clean(function(done) {

var metrics = [
{name: 'mp3', value: 10, source: 'b1', date: new Date('2012 01 01 10:45 GMT')},
{name: 'mp3', value: 8, source: 'b2', date: new Date('2012 01 01 10:45 GMT')},
{name: 'mp3', value: 8, source: 'b1', date: new Date('2012 01 01 10:45 GMT')},
{name: 'mp3', value: 6, source: 'b2', date: new Date('2012 01 01 10:45 GMT')},
{name: 'other', value: 6, source: 'b2', date: new Date('2012 01 01 10:46 GMT')}
];

var mm = metricsIo(MONGO_URL, COLLECTION);

addBulkMetrics(mm, metrics, function(err) {

assert.equal(err, undefined);
mm.identifySources('mp3', validateMetrics);
});

function validateMetrics(err, results) {

assert.equal(err, null);
results.sort(function(a, b) {
return a > b;
});
assert.deepEqual(results, ['b1', 'b2']);
done();
}

}));

test('with query', _clean(function(done) {

var metrics = [
{name: 'mp3', value: 10, source: 'b1', date: new Date('2012 01 01 10:45 GMT')},
{name: 'mp3', value: 8, source: 'b2', date: new Date('2013 01 01 10:45 GMT')},
{name: 'mp3', value: 8, source: 'b1', date: new Date('2012 01 01 10:45 GMT')},
{name: 'mp3', value: 6, source: 'b2', date: new Date('2013 01 01 10:45 GMT')},
{name: 'other', value: 6, source: 'b2', date: new Date('2012 01 01 10:46 GMT')}
];

var mm = metricsIo(MONGO_URL, COLLECTION);

addBulkMetrics(mm, metrics, function(err) {

assert.equal(err, undefined);
mm.identifySources('mp3', {date: {$lt: new Date('2012 12 31').getTime()}}, validateMetrics);
});

function validateMetrics(err, results) {

assert.equal(err, null);
results.sort(function(a, b) {
return a > b;
});
assert.deepEqual(results, ['b1']);
done();
}

}));
});
});

function addBulkMetrics (metricsTracker, metrics, callback) {
Expand Down

0 comments on commit a2c7b06

Please sign in to comment.