Skip to content

Commit

Permalink
Release 1.0.4
Browse files Browse the repository at this point in the history
This release implements custom rage functionallity and fixes the
primary key builder.

- Refactor PrimaryKey Builder <https://github.com/victorhqc>.
- Implemented Custom Range
  • Loading branch information
Jonathan Casarrubias committed Apr 1, 2016
1 parent 610e813 commit 40924b8
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Loopback Stats Mixin - CHANGELOG

The **loopback-stats-mixin** module change .

- **Version 1.0.4**.-
- Refactor PrimaryKey Builder <https://github.com/victorhqc>.
- Implemented Custom Range
- **Version 1.0.3**.-
- Refactor builders into different files.
- Improved properties description for swagger by more specific details.
Expand Down
3 changes: 2 additions & 1 deletion builders/accept-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = class AcceptBuilder {
accepts.push({ arg: 'relation', type: 'string', required: true, description: 'Relationship name' });
if (this.ctx.type === "nested")
accepts.push({ arg: 'nested', type: 'string', required: true, description: 'Nested array property name' });
accepts.push({ arg: 'range', type: 'string', required: true, description: 'Scale range (daily, weekly, monthly, annual)' });
accepts.push({ arg: 'range', type: 'string', required: true, description: 'daily, weekly, monthly, annual, custom' });
accepts.push({ arg: 'custom', type: 'object', required: false, description: '{"start": date, "end": date }' });
accepts.push({ arg: 'where', type: 'object', description: 'Statement to filter ' + (this.ctx.relation || this.ctx.nested) });
return accepts;
}
Expand Down
28 changes: 28 additions & 0 deletions builders/now-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
/**
* Stats Builder Dependencies
*/
var moment = require('moment');
/**
* Builds Now Time Moment
*/
module.exports = class NowBuilder {

constructor(ctx) { this.ctx = ctx; }

build() {
let now;
switch (this.ctx.params.range) {
case 'daily':
case 'weekly':
case 'monthly':
case 'annual':
now = moment();
break;
case 'custom':
now = moment(this.ctx.params.custom.end);
break;
}
return now;
}
}
8 changes: 4 additions & 4 deletions builders/params-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module.exports = class ParamsBuilder {
*/
build() {
if (this.ctx.type === "model")
return { range: this.ctx.arguments[0], where: this.ctx.arguments[1], next: this.ctx.arguments[2] };
return { range: this.ctx.arguments[0], custom: this.ctx.arguments[1], where: this.ctx.arguments[2], next: this.ctx.arguments[3] };
if (this.ctx.type === "relation" && this.ctx.relation)
return { id: this.ctx.arguments[0], range: this.ctx.arguments[1], where: this.ctx.arguments[2], next: this.ctx.arguments[3] };
return { id: this.ctx.arguments[0], range: this.ctx.arguments[1], custom: this.ctx.arguments[2], where: this.ctx.arguments[3], next: this.ctx.arguments[4] };
if (this.ctx.type === "relation" && !this.ctx.relation)
return { id: this.ctx.arguments[0], relation: this.ctx.arguments[1], range: this.ctx.arguments[2], where: this.ctx.arguments[3], next: this.ctx.arguments[4] };
return { id: this.ctx.arguments[0], relation: this.ctx.arguments[1], range: this.ctx.arguments[2], custom: this.ctx.arguments[3], where: this.ctx.arguments[4], next: this.ctx.arguments[5] };
if (this.ctx.type === "nested")
return { id: this.ctx.arguments[0], nested: this.ctx.arguments[1], range: this.ctx.arguments[2], where: this.ctx.arguments[3], next: this.ctx.arguments[4] };
return { id: this.ctx.arguments[0], nested: this.ctx.arguments[1], range: this.ctx.arguments[2], custom: this.ctx.arguments[3], where: this.ctx.arguments[4], next: this.ctx.arguments[5] };
}
}
4 changes: 3 additions & 1 deletion builders/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Query Builder Dependencies
*/
var moment = require('moment');
var TypeBuilder = require('./type-builder');
/**
* Builds Loopback Query
*/
Expand Down Expand Up @@ -30,7 +31,8 @@ module.exports = class QueryBuilder {
// Set Range
if (this.ctx.params.range && this.ctx.count.on) {
query.where[this.ctx.count.on] = {};
switch (this.ctx.params.range) {
let range = new TypeBuilder(this.ctx).build();
switch (range) {
case 'weekly':
query.where[this.ctx.count.on].gt = moment(this.ctx.nowISOString).subtract(7, 'days').toDate();
break;
Expand Down
35 changes: 23 additions & 12 deletions builders/stats-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Stats Builder Dependencies
*/
var moment = require('moment');
var TypeBuilder = require('./type-builder');
/**
* Builds Statistic Array from List of Resuls
*/
Expand All @@ -14,12 +15,12 @@ module.exports = class StatsBuilder {
this.list = list;
let dataset = [];
let iterator = this.getIteratorCount();
for (let i = 1, dateIndex = iterator; i <= iterator; i++ , dateIndex--) {
for (let i = 0, dateIndex = iterator; i <= iterator; i++ , dateIndex--) {
let current = this.getCurrentMoment(dateIndex);
let count = this.getCurrentCount(current);
dataset.push({
date: current.toISOString(),
count: this.ctx.count.avg ? (count / list.length) : count
count: count === 0 ? 0 : this.ctx.count.avg ? (count / list.length) : count
});
}
return dataset;
Expand Down Expand Up @@ -76,26 +77,28 @@ module.exports = class StatsBuilder {
let current;
switch (this.ctx.params.range) {
case 'weekly':
current = moment(this.ctx.nowISOString).subtract(index - 1, 'days');
current = moment(this.ctx.nowISOString).subtract(index, 'days');
break;
case 'monthly':
current = moment(this.ctx.nowISOString).subtract(index - 1, 'days');
current = moment(this.ctx.nowISOString).subtract(index, 'days');
break;
case 'annual':
current = moment(this.ctx.nowISOString).subtract(index - 1, 'months');
current = moment(this.ctx.nowISOString).subtract(index, 'months');
break;
case 'daily':
default:
current = moment(this.ctx.nowISOString).subtract(index - 1, 'hours');
current = moment(this.ctx.nowISOString).subtract(index, 'hours');
break;
}
return current;
}

getIteratorCount() {
let iterator;

switch (this.ctx.params.range) {
case 'daily':
iterator = 24;
break;
case 'weekly':
iterator = 7;
break;
Expand All @@ -105,12 +108,20 @@ module.exports = class StatsBuilder {
case 'annual':
iterator = 12;
break;
case 'daily':
default:
iterator = 24;
break;
case 'custom':
let start = moment(this.ctx.params.custom.start);
let end = moment(this.ctx.params.custom.end);
iterator = 0;
['hour', 'day', 'week', 'month','year'].forEach(item => {
let plural = [item, 's'].join('');
let diff = end.diff(start, plural);
if (diff > 1 && diff < 25) {
iterator = item === 'week' ? end.diff(start, 'days') : diff;
this.ctx.params.range = new TypeBuilder(this.ctx).build();
}
});
break;
}

return iterator;
}
}
35 changes: 35 additions & 0 deletions builders/type-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
/**
* Stats Builder Dependencies
*/
var moment = require('moment');
/**
* Builds Now Time Moment
*/
module.exports = class NowBuilder {

constructor(ctx) { this.ctx = ctx; }

build() {
let type = this.ctx.params.range;
if (this.ctx.params.range === 'custom') {
let start = moment(this.ctx.params.custom.start);
let end = moment(this.ctx.params.custom.end);
['hour', 'day', 'week', 'month', 'year'].forEach(item => {
let plural = [item, 's'].join('');
let diff = end.diff(start, plural);
if (diff > 1 && diff < 25) {
type = item === 'week' ? end.diff(start, 'days') : diff;
switch (item) {
case 'hour': type = 'daily'; break;
case 'day': type = 'weekly'; break;
case 'week': type = 'monthly'; break;
case 'month': type = 'annual'; break;
}
}
});
}

return type;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-stats-mixin",
"version": "1.0.3",
"version": "1.0.4",
"author": "Jonathan Casarrubias <http://github.com/jonathan-casarrubias>",
"description": "A mixin to provide statistical functionallity for Loopback Models, Relations and Nested Datasets",
"main": "index.js",
Expand Down
9 changes: 6 additions & 3 deletions stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var ParamsBuilder = require('./builders/params-builder');
var QueryBuilder = require('./builders/query-builder');
var PrimaryKeyBuilder = require('./builders/pk-builder');
var StatsBuilder = require('./builders/stats-builder');
var NowBuilder = require('./builders/now-builder');
/**
* Stats Mixin
* @Author Jonathan Casarrubias
Expand All @@ -30,7 +31,7 @@ module.exports = function(Model, ctx) {
ctx.arguments = arguments;
ctx.params = new ParamsBuilder(ctx).build();
// Create instance of stats class
ctx.now = moment();
ctx.now = new NowBuilder(ctx).build();
ctx.nowISOString = ctx.now.toISOString();
ctx.stats = new StatsBuilder(ctx);
// Data Workflow
Expand All @@ -46,7 +47,8 @@ module.exports = function(Model, ctx) {
params: {
pk: new PrimaryKeyBuilder(Model).build(),
id: ctx.params.id,
relation: ctx.params.relation
relation: ctx.params.relation,
custom: ctx.params.custom
}
};
if (ctx.type === 'model') {
Expand All @@ -72,7 +74,8 @@ module.exports = function(Model, ctx) {
nowISOString: ctx.nowISOString,
params: {
range: ctx.params.range,
where: ctx.params.where
where: ctx.params.where,
custom: ctx.params.custom
}
});
builder.onComplete((err, query) => {
Expand Down

0 comments on commit 40924b8

Please sign in to comment.