Skip to content

Commit

Permalink
Implemented priceData subdocument schema in Pair
Browse files Browse the repository at this point in the history
  • Loading branch information
EstDavid committed Feb 18, 2023
1 parent 81138cf commit ed25abf
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 36 deletions.
103 changes: 67 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,48 @@

require('dotenv').config()
const express = require("express");
const morgan = require('morgan')
var cors = require("cors");
var { format } = require("util");
var Multer = require( "multer");
var path = require('path');

const app = express();
app.use(express.static('build'))
app.use(express.json())
app.use(cors())
const port = process.env.PORT || 5000;

// Creating custom token 'data' which returns the request.data stringified object from
// the 'processData' middleware function
morgan.token('data', (request, response) => request.data)

const processData = (request, response, next) => {
request.data = JSON.stringify(request.body)
next()
}

// Using middleware function 'processData', which stringifies the request.body
app.use(processData)

// Using a custom format function which replicates the output of
// the 'tiny' format, and adds request data with HTTP POST resquests
app.use(morgan((tokens, request, response) => {
let customFormat = [
tokens.method(request, response),
tokens.url(request, response),
tokens.status(request, response),
tokens.res(request, response, 'content-length'), '-',
tokens['response-time'](request, response), 'ms'
]

// When the server receives a 'POST' request, the data of the request is added to the console log
if (request.method === 'POST') {
customFormat = customFormat.concat(tokens.data(request, response))
}
return customFormat.join(' ')
}))

const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
Expand All @@ -35,7 +69,6 @@ mongoose.connect(url)
})

const Pair = require('./models/pair')
const Observation = require('./models/observation')

const getObservationsObject = (observationsArray) => {
const observationsObject = {}
Expand Down Expand Up @@ -72,41 +105,39 @@ app.get("/get-url/:timeframe/:timeframeto/:symbol", (req, res) => {
poolFee,
arrayTypes,
extraMinutesData,
priceData
} = pair

Observation.findById(`${req.params.symbol}-${req.params.timeframe}`)
.then(observation => {
if (observation !== null && observation !== undefined) {
observation.set('to', parseInt(req.params.timeframeto))
const arrayOHLC = observation.get('arrayOHLC')

const earliestTimestamp = observation.earliest
const latestTimestamp = observation.latest

const symbolObject = {
symbol,
baseToken,
quoteToken,
poolAddress,
poolFee,
arrayTypes,
extraMinutesData,
observationTimeframe: {
name: observation.timeframe.name,
seconds: observation.timeframe.seconds
},
observations: {},
arrayOHLC,
startTimestamp: (earliestTimestamp / 1000).toString(),
endTimestamp: (latestTimestamp / 1000).toString(),
maxObservations: observation.data.length,
}

res.status(200).send(symbolObject)
} else {
res.status(404).end()
}
})
if (priceData.observations.length > 0) {
priceData.set('to', parseInt(req.params.timeframeto))
const arrayOHLC = pair.priceData.get('arrayOHLC')

const earliestTimestamp = priceData.earliest
const latestTimestamp = priceData.latest

const symbolObject = {
symbol,
baseToken,
quoteToken,
poolAddress,
poolFee,
arrayTypes,
extraMinutesData,
observationTimeframe: {
name: priceData.timeframe.name,
seconds: priceData.timeframe.seconds
},
observations: {},
arrayOHLC,
startTimestamp: (earliestTimestamp / 1000).toString(),
endTimestamp: (latestTimestamp / 1000).toString(),
maxObservations: priceData.observations.length,
}

res.status(200).send(symbolObject)
} else {
res.status(404).end()
}
} else {
res.status(404).end()
}
Expand All @@ -116,8 +147,8 @@ app.get("/get-url/:timeframe/:timeframeto/:symbol", (req, res) => {
// DOWNLOAD ALL FILENAMES AND CLASSIFY PAIRS ACCORDING TO QUOTE TOKEN
app.get("/get-symbols/", (req, res) => {
const symbols = []
Pair.find()

Pair.find({}, {symbol: 1, _id: 0})
.then(result => {
result.forEach(pair => {
symbols.push(pair.symbol)
Expand Down
2 changes: 2 additions & 0 deletions models/pair.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require('mongoose')
const priceDataSchema = require('./priceData')

const tokenSchema = new mongoose.Schema({
chanId: Number,
Expand All @@ -20,6 +21,7 @@ const pairSchema = new mongoose.Schema({
poolFee: String,
arrayTypes: [String],
extraMinutesData: Number,
priceData: priceDataSchema,
})

pairSchema.set('toJSON', {
Expand Down
135 changes: 135 additions & 0 deletions models/priceData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const mongoose = require('mongoose')

const dataPointSchema = new mongoose.Schema({
_id: String,
price: Number,
}, {
virtuals: {
timestamp: {
get() {
return new Date(parseInt(this._id) * 1000)
}
},
}
})

const priceDataSchema = new mongoose.Schema({
_id: String,
symbol: String,
timeframe: {
name: String,
seconds: Number,
},
timeframeTo: {
seconds: Number,
},
observations: [dataPointSchema]
}, {
virtuals: {
earliest: {
get() {
const getEarliest = (earliest, observation) => {
const current = observation.timestamp
return current <= earliest ? current : earliest
}

return this.observations.reduce(getEarliest, this.observations[0].timestamp)
}
},
latest: {
get() {
const getLatest = (latest, observation) => {
const current = observation.timestamp
return current >= latest ? current : latest
}

return this.observations.reduce(getLatest, this.observations[0].timestamp)
}
},
arrayOHLC: {
get() {
let timeframeFrom = this.timeframe.seconds;
let timeframeTo = this.timeframeTo.seconds;
let dataArray = [...this.observations]
let open = 0;
let high = 0;
let low = 0;
let close = 0;
let startTimeframe = 0;
let newCandleTimestamp = 0;
let priceArray = {};

if(timeframeTo % timeframeFrom !== 0) {
throw(`Timeframe to ${timeframeTo} is not a multiple of timeframe from ${timeframeFrom}`);
}

dataArray.sort((a, b) => {
return parseInt(a._id) - parseInt(b._id)
})

for(let i = 0; i < dataArray.length; i +=1) {
let timestamp = parseInt(dataArray[i]._id);
let priceObservation = dataArray[i].price;
close = priceObservation;
if(i === 0) { // Opening a new cande at the beginning of the series
startTimeframe = timestamp - (timestamp % timeframeTo);
newCandleTimestamp = startTimeframe + timeframeTo;
open = priceObservation;
high = priceObservation;
low = priceObservation;
priceArray[startTimeframe] = {
timestamp: startTimeframe,
open,
high,
low,
close
}
}
else if(timestamp < newCandleTimestamp) {
if(priceObservation > high) {
high = priceObservation;
priceArray[startTimeframe].high = high;
}
if(priceObservation < low) {
low = priceObservation;
priceArray[startTimeframe].low = low;
}
priceArray[startTimeframe].close = close;
}
else { // Opening a new candle
startTimeframe = timestamp - (timestamp % timeframeTo);
newCandleTimestamp = startTimeframe + timeframeTo;
open = priceObservation;
high = priceObservation;
low = priceObservation;
close = priceObservation;
priceArray[startTimeframe] = {
timestamp: startTimeframe,
open,
high,
low,
close
}
}
}
return priceArray;
}
},
to: {
set(v) {
this.timeframeTo.seconds = v
}
}
}

})

priceDataSchema.set('toJSON', {
transform: (document, returnedObject) => {
returnedObject.id = returnedObject._id.toString()
// delete returnedObject._id
delete returnedObject.__v
}
})

module.exports = priceDataSchema

0 comments on commit ed25abf

Please sign in to comment.