Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       //~ We have to round down the input to the nearest hour
  2.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 1. Rounding down the 'from' time to the nearest round period`);
  3.       from = moment(from).startOf('hour').format('YYYY-MM-DDTHH:mm:ss')+'Z'; //~ Round for hour
  4.  
  5.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] 2) Date check | From: ${from} | To: ${to}`);
  6.  
  7.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 2. Counting how many intervals exist in our to/from range`)
  8.       let numPeriods = moment(to).utc().diff(moment(from), 'hours');  //~ How many hours in the period
  9.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Time range is from ${from} to ${moment(to).format('YYYY-MM-DDTHH:mm:ss')+'Z'}`);
  10.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Number of 1 hour periods in range: ${numPeriods}`);
  11.  
  12.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 3. Generating an array with all of the potential time slots for bars that could exist in our period`);
  13.       let barsRange = [];
  14.       let fromMod = from;
  15.       for (let i=0; i<numPeriods; i++) {
  16.         let curVal = moment(fromMod).format('YYYY-MM-DDTHH:mm:ss')+'Z'
  17.         //console.log(`Adding to array: ${curVal}`);
  18.         barsRange[i] = curVal;
  19.         //console.log(`Set barsRange[${i}] to ${curVal}`);
  20.         fromMod = moment(fromMod).add(1, 'hour');
  21.       }
  22.  
  23.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 4. Checking to see if each bar exists in Redis (cache) before we go to the DB`);
  24.       var lastGoodBar;
  25.       var newResultBars = [];
  26.       for (const bar of barsRange) {
  27.         //~ console.log(`Checking for bar at hash: ${bar}`);
  28.         const rez = await client.hmget(bar, "open", "high", "low", "close");
  29.         //~ console.log(bar + ' ' + rez);
  30.         if (rez[0]) {
  31.           let newResultBar = {
  32.             timestamp: bar,
  33.             o: rez[0],
  34.             h: rez[1],
  35.             l: rez[2],
  36.             c: rez[3]
  37.           }
  38.           lastGoodBar = bar;
  39.           newResultBars.push(newResultBar);
  40.           console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Cache hit: adding new bar for [${bar}] o: ${rez[0]}, h: ${rez[1]}, l: ${rez[2]}, c: ${rez[3]}`)
  41.         }
  42.       }
  43.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Done getting elements from Redis`);
  44.       let emptySet = false;
  45.       if (lastGoodBar) {
  46.         console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Last cache hit: ${lastGoodBar}`);
  47.       } else {
  48.         console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] No results found in cache`);
  49.         lastGoodBar = from;
  50.         emptySet = true;
  51.       }
  52.  
  53.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 5. Calculating bars we need to get from the DB`);
  54.       if (!emptySet) {
  55.           lastGoodBar = moment(lastGoodBar).utc().add(1, 'hour');
  56.       }
  57.       numPeriods = moment.utc(to).diff(moment.utc(lastGoodBar), 'hours');  //~ How many hours in the period
  58.       //console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ********************************************************* ${to}`);
  59.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] New time range is from ${moment(lastGoodBar).utc().format('YYYY-MM-DDTHH:mm:ss')+'Z'} to ${moment(to).format('YYYY-MM-DDTHH:mm:ss')+'Z'}`);
  60.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Number of 1 hour periods in new range: ${numPeriods}`);
  61.  
  62.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Getting bars from DB`)
  63.       //~ Execute the SELECT statement using the new 'from' limit if applied above
  64.       console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] From: ${lastGoodBar} | To: ${to}`);
  65.       const dbResult = await dbRead.any(`
  66.         SELECT time_bucket($1, dt) AS timestamp,
  67.         (first(peos, dt)) o,
  68.         MAX(peos) h,
  69.         MIN(peos) l,
  70.         (last(peos, dt)) c
  71.         FROM eosram
  72.         WHERE dt BETWEEN $2 AND $3
  73.         GROUP BY timestamp
  74.         ORDER BY timestamp ASC;`, [req.params.interval + ' seconds', lastGoodBar, to]);
  75.         //console.log(dbResult);
  76.         //console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Results: ${dbResult}`);
  77.         console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Done getting bars from DB`)
  78.  
  79.  
  80.         console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Adding new bars to Redis & assembling final result`);
  81.         for (let i=0; i< dbResult.length; i++) {
  82.           newResultBars.push(dbResult[i]);
  83.           //~ console.log(`Added bar from DB to the final result: ${dbResult[i].timestamp}`);
  84.           const res = await client.hmset(dbResult[i].timestamp, "open", dbResult[i].o, "high", dbResult[i].h, "low", dbResult[i].l, "close", dbResult[i].c);
  85.         }
  86.         console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Done adding elements to Redis`);
  87.  
  88.         console.log('************************ Returning bars Result');
  89.         res.status(200).send(newResultBars);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement