Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import mysql from 'mysql2';
  2.  
  3. const db = mysql
  4.   .createPool({
  5.     host: 'database-host',
  6.     port: process.env.DB_PORT || 3306,
  7.     user: 'root',
  8.     password: '#_1Hq$^DxV}x',
  9.     database: '1win'
  10.   })
  11.   .promise();
  12.  
  13. const getPreviousDate = (pastDays = 0) => {
  14.   const dt = new Date();
  15.   dt.setUTCHours(0, 0, 0, 0);
  16.   dt.setDate(dt.getDate() - pastDays);
  17.   retur
  18. };
  19.  
  20. const intervals = [
  21.   //getPreviousDate(1),
  22.   //getPreviousDate(7),
  23.   //getPreviousDate(30),
  24. //  1, 7, 30,
  25.  
  26. 90
  27. ];
  28.  
  29. intervals.forEach(async (date) => {
  30.   console.log('Started working with %s', date);
  31.  
  32.  
  33.   const [result] = await db.query(`
  34. SELECT id_user FROM ma_deposits
  35. WHERE time >= DATE_SUB(CURRENT_DATE(), INTERVAL ${5} DAY) and time <= DATE_SUB(CURRENT_DATE(), INTERVAL ${4} DAY)
  36. AND id_user NOT IN (
  37.   SELECT id_user FROM ma_deposits
  38.   WHERE time < DATE_SUB(CURRENT_DATE(), INTERVAL ${5} DAY)
  39. );  `);
  40.   console.log('Got result for %s: %i', date, result.length);
  41.  
  42.   const users = new Set();
  43.  
  44.   for (let i = 0; i < result.length; i++) {
  45.      const user = await db.query('select * from ma_users_meta where ? and ? and ?', [{id_user: result[i].id_user}, {key: 'withdrawal_block'}, {value: 'true'}]).then(r => r[0]);
  46.      if (user.length === 0) {
  47.        const {amount} = await db.query('select sum(amount) as amount from ma_bets where ?', {id_user: result[i].id_user}).then(r => r[0][0]);
  48. //       console.log(date, result[i].id_user, +amount);
  49.        if (+amount > 6600) {
  50. //         console.log(result[i].id_user);
  51.          users.add(result[i].id_user);
  52.        }
  53.      }
  54.   }
  55.  
  56.   console.log('For %s finnally %i users', date, users.size);
  57.   let deposits = 0;
  58.   let withdrawals = 0;
  59.   let balance = 0;
  60.  
  61.  
  62.   for (const user of users) {
  63.     const [deps, withd, bal] = await Promise.all([
  64.     db.query(`select * from ma_deposits where id_user = ? and status = 1 and payment_system not in ('casino', 'money-case')`, [user]).then(r => r[0]),
  65.     db.query(`select * from ma_withdrawal where id_user = ? and status = 1 and payment_system not in ('casino', 'money-case')`, [user]).then(r => r[0]),
  66.     db.query(`select * from ma_balance where id_user = ?`, [user]).then(r => parseFloat(r[0][0].amount)),
  67.     ]);
  68.     //console.log(`select * from ma_deposits where id_user = ${user} and time > ${date.getTime()} and payment_system not in ('casino', 'money-case')`);
  69.     deposits += deps.reduce((accum, dep) => accum += parseFloat(dep.amount), 0);
  70.     withdrawals += withd.reduce((accum, withd) => accum += parseFloat(withd.amount), 0);
  71.     //console.log(bal);
  72.     balance += bal;
  73.   }
  74.  
  75.   console.log(deposits, withdrawals, users.size, balance);
  76.  
  77.   const value1 = deposits - withdrawals;
  78.   const value2 = deposits - withdrawals - balance;
  79.  
  80.   const avg1 = value1 / (users.size || 1);
  81.   const avg2 = value2 / (users.size || 1);
  82.  
  83.   console.log(`Result for %s is ${avg1}, ${avg2}`, date);
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement