Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //~ We have to round down the input to the nearest hour
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 1. Rounding down the 'from' time to the nearest round period`);
- from = moment(from).startOf('hour').format('YYYY-MM-DDTHH:mm:ss')+'Z'; //~ Round for hour
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] 2) Date check | From: ${from} | To: ${to}`);
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 2. Counting how many intervals exist in our to/from range`)
- let numPeriods = moment(to).utc().diff(moment(from), 'hours'); //~ How many hours in the period
- 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'}`);
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Number of 1 hour periods in range: ${numPeriods}`);
- 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`);
- let barsRange = [];
- let fromMod = from;
- for (let i=0; i<numPeriods; i++) {
- let curVal = moment(fromMod).format('YYYY-MM-DDTHH:mm:ss')+'Z'
- //console.log(`Adding to array: ${curVal}`);
- barsRange[i] = curVal;
- //console.log(`Set barsRange[${i}] to ${curVal}`);
- fromMod = moment(fromMod).add(1, 'hour');
- }
- 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`);
- var lastGoodBar;
- var newResultBars = [];
- for (const bar of barsRange) {
- //~ console.log(`Checking for bar at hash: ${bar}`);
- const rez = await client.hmget(bar, "open", "high", "low", "close");
- //~ console.log(bar + ' ' + rez);
- if (rez[0]) {
- let newResultBar = {
- timestamp: bar,
- o: rez[0],
- h: rez[1],
- l: rez[2],
- c: rez[3]
- }
- lastGoodBar = bar;
- newResultBars.push(newResultBar);
- 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]}`)
- }
- }
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Done getting elements from Redis`);
- let emptySet = false;
- if (lastGoodBar) {
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Last cache hit: ${lastGoodBar}`);
- } else {
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] No results found in cache`);
- lastGoodBar = from;
- emptySet = true;
- }
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ 5. Calculating bars we need to get from the DB`);
- if (!emptySet) {
- lastGoodBar = moment(lastGoodBar).utc().add(1, 'hour');
- }
- numPeriods = moment.utc(to).diff(moment.utc(lastGoodBar), 'hours'); //~ How many hours in the period
- //console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ********************************************************* ${to}`);
- 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'}`);
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Number of 1 hour periods in new range: ${numPeriods}`);
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Getting bars from DB`)
- //~ Execute the SELECT statement using the new 'from' limit if applied above
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] From: ${lastGoodBar} | To: ${to}`);
- const dbResult = await dbRead.any(`
- SELECT time_bucket($1, dt) AS timestamp,
- (first(peos, dt)) o,
- MAX(peos) h,
- MIN(peos) l,
- (last(peos, dt)) c
- FROM eosram
- WHERE dt BETWEEN $2 AND $3
- GROUP BY timestamp
- ORDER BY timestamp ASC;`, [req.params.interval + ' seconds', lastGoodBar, to]);
- //console.log(dbResult);
- //console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] Results: ${dbResult}`);
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Done getting bars from DB`)
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Adding new bars to Redis & assembling final result`);
- for (let i=0; i< dbResult.length; i++) {
- newResultBars.push(dbResult[i]);
- //~ console.log(`Added bar from DB to the final result: ${dbResult[i].timestamp}`);
- const res = await client.hmset(dbResult[i].timestamp, "open", dbResult[i].o, "high", dbResult[i].h, "low", dbResult[i].l, "close", dbResult[i].c);
- }
- console.log(`[${moment.utc().format("YYYY-MM-DD HH:mm:ss.SSS")}] ************************ Done adding elements to Redis`);
- console.log('************************ Returning bars Result');
- res.status(200).send(newResultBars);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement