Skip to content

Commit

Permalink
stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jeka-kiselyov committed Apr 21, 2016
1 parent c5af262 commit 806c880
Show file tree
Hide file tree
Showing 20 changed files with 1,031 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function(grunt) {
// Delay before server listens on port
setTimeout(function() {
require('fs').writeFileSync('.rebooted', 'rebooted');
}, 1000);
}, 3000);
});
}
}
Expand Down Expand Up @@ -94,6 +94,7 @@ module.exports = function(grunt) {
require: [],
reporter: 'spec',
bail: true,
sort: true,
env: {
NODE_ENV: 'test'
}
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ mkdir dimeshift
cd dimeshift
git clone https://github.com/jeka-kiselyov/dimeshift.git .
npm install
bower install
```
* [Install npm](https://docs.npmjs.com/getting-started/installing-node) if you don't have it.
* [Install bower](http://bower.io/#install-bower) if you don't have it.
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"bootstrap-tour": "~0.10.2",
"js-xlsx": "~0.8.0",
"moment": "~2.10.6",
"eonasdan-bootstrap-datetimepicker": "^4.17.37"
"eonasdan-bootstrap-datetimepicker": "^4.17.37",
"font-awesome": "fontawesome#^4.6.1"
}
}
1 change: 1 addition & 0 deletions config/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"./public/vendors/mprogress/build/css/mprogress.css",
"./public/vendors/magnific-popup/dist/magnific-popup.css",
"./public/vendors/bootstrap/dist/css/bootstrap.min.css",
"./public/vendors/font-awesome/css/font-awesome.css",
"./public/vendors/bootstrap-tour/build/css/bootstrap-tour.min.css",
"./public/vendors/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css",
"./public/css/main.css",
Expand Down
130 changes: 129 additions & 1 deletion includes/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var crypto = require('crypto');
var rfr = require('rfr');
var demo = rfr('includes/demo.js');
var mailer = rfr('includes/mailer.js');
var moment = require('moment');

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
Expand Down Expand Up @@ -307,7 +308,7 @@ module.exports = function(sequelize, DataTypes) {
this.login = login;
this.email = email;
this.password = password;
this.is_demo = false;
this.is_demo = 0;
this.registration_ip = ip;

sequelize.db.WalletAccess.checkAccessForNewUser(this);
Expand Down Expand Up @@ -422,6 +423,133 @@ module.exports = function(sequelize, DataTypes) {
},
getWalletIfHasAccess: function(wallet_id) {
return sequelize.db.WalletAccess.getWalletIfHasAccess(this, wallet_id);
},
getStats: function(params) {
var user = this;
var period = params.period || 'week';
if (period != 'week' && period != 'month' && period != 'year')
period = 'week';
var utcOffset = params.utcOffset || 0;
var wallet_id = params.wallet_id || null;

return new sequelize.Promise(function(resolve, reject) {
var now = moment();
now.utcOffset(utcOffset);

var retPeriods = [];
var walletsCurrencies = {};

var lowestTimestamp = now.unix();
var highestTimestamp = now.unix();

var count = 7;
if (period == 'month')
count = 31;
else if (period == 'year')
count = 12;

for (var i = 0; i < count; i++)
{
var newPeriod = {
month: now.month() + 1,
year: now.year(),
utcOffset: now.format('Z')
};

if (period == 'year')
now.startOf('month');
else {
newPeriod.day = now.date();
now.startOf('day');
}
newPeriod.fromTimestamp = now.unix(); // >=
if (newPeriod.fromTimestamp < lowestTimestamp)
lowestTimestamp = newPeriod.fromTimestamp;
if (period == 'year')
now.endOf('month');
else
now.endOf('day');
newPeriod.toTimestamp = now.unix(); // <=
if (newPeriod.toTimestamp > highestTimestamp)
highestTimestamp = newPeriod.toTimestamp;

if (period == 'year')
now.subtract(1, 'months');
else
now.subtract(1, 'days');

retPeriods.push(newPeriod);
}


user.getWallets().then(function(wallets){
var foundWallet = false;
if (wallets) {
for (var k in wallets) {
if ((wallet_id && wallets[k].id == wallet_id) || !wallet_id)
{
walletsCurrencies[wallets[k].id] = wallets[k].currency;
foundWallet = true;
}
}
}

if (!foundWallet)
return resolve(null);

var allCurrencies = [];
for (k in walletsCurrencies)
if (allCurrencies.indexOf(walletsCurrencies[k]) == -1)
allCurrencies.push(walletsCurrencies[k]);

for (k in retPeriods) {
retPeriods[k].stats = {};
for (var ak in allCurrencies)
retPeriods[k].stats[allCurrencies[ak]] = {
expense: 0,
profit: 0
}
};

var where = {
user_id: user.id,
datetime: {
$lte: highestTimestamp,
$gte: lowestTimestamp
}
};
if (wallet_id > 0)
where.wallet_id = wallet_id;

sequelize.db.Transaction.findAll({
where: where
}).then(function(transactions){
if (transactions)
for (var kt in transactions)
for (var kp in retPeriods)
{
if (transactions[kt].datetime >= retPeriods[kp].fromTimestamp && transactions[kt].datetime <= retPeriods[kp].toTimestamp)
{
var currency = walletsCurrencies[transactions[kt].wallet_id];
if (transactions[kt].amount < 0)
retPeriods[kp].stats[currency].expense += Math.abs(transactions[kt].amount);
else
retPeriods[kp].stats[currency].profit += Math.abs(transactions[kt].amount);
}
}

for (var k in retPeriods)
{
delete retPeriods[k].fromTimestamp;
delete retPeriods[k].toTimestamp;
}

resolve(retPeriods);
});
});


});
}
}
});
Expand Down
20 changes: 20 additions & 0 deletions includes/routes/stats/month.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var rfr = require('rfr');
var db = rfr('includes/models');
var errors = rfr('includes/errors.js');
var api = rfr('includes/api.js');

exports.route = '/api/stats/month';
exports.method = 'get';

exports.handler = function(req, res, next) {

var utcoffset = parseInt(req.params.utcoffset || 0, 10); // http://momentjs.com/docs/#/manipulating/utc-offset/

api.requireSignedIn(req, function(user) {
user.getStats({period: 'month', utcOffset: utcoffset}).then(function(stats){
res.send(stats);
next();
});
});

};
Loading

0 comments on commit 806c880

Please sign in to comment.