Fixed bug in addToHistory function

This commit is contained in:
NunoSempere 2021-04-10 15:47:32 +02:00
parent 3841dbfd1a
commit abc4f61432
2 changed files with 27 additions and 19 deletions

View File

@ -16,7 +16,7 @@ async function fetchAllContractInfo(){ // for info which the polymarket graphql
} }
*/ */
async function fetchAllContractInfo() { // for info which the polymarket graphql API async function fetchAllContractInfo() { // for info which the polymarket graphql API
let response = await axios.get('https://strapi-matic.poly.market/markets?active=true&_sort=volume:desc') let response = await axios.get('https://strapi-matic.poly.market/markets?active=true&_sort=volume:desc&_limit=-1')
.then(query => query.data); .then(query => query.data);
response = response.filter(res => res.closed != true) response = response.filter(res => res.closed != true)
return response return response

View File

@ -1,5 +1,6 @@
import { mongoRead, upsert } from "./mongo-wrapper.js" import { writeFileSync } from "fs"
import { mongoReadWithReadCredentials, upsert } from "./mongo-wrapper.js"
let mongoRead = mongoReadWithReadCredentials
let isEmptyArray = arr => arr.length == 0 let isEmptyArray = arr => arr.length == 0
export async function addToHistory(){ export async function addToHistory(){
let currentJSON = await mongoRead("metaforecasts") let currentJSON = await mongoRead("metaforecasts")
@ -16,21 +17,27 @@ export async function addToHistory(){
// Add both types of forecast // Add both types of forecast
let newHistoryJSON = [] let newHistoryJSON = []
for(let historyElement of historyJSON){ for(let historyElement of historyJSON){
let correspondingNewElement = currentForecastsWithAHistory.filter(element => historyElement.title == element.title && historyElement.url == element.url )[0] let correspondingNewElementArray = currentForecastsWithAHistory.filter(element => historyElement.title == element.title && historyElement.url == element.url )
let timeStampOfNewElement = correspondingNewElement.timestamp // console.log(correspondingNewElement)
let doesHistoryAlreadyContainElement = historyElement.history.map(element => element.timestamp).includes(timeStampOfNewElement) if(!isEmptyArray(correspondingNewElementArray)){
if(!doesHistoryAlreadyContainElement){ let correspondingNewElement = correspondingNewElementArray[0]
let historyWithNewElement = historyElement["history"].concat({ let timeStampOfNewElement = correspondingNewElement.timestamp
"timestamp": correspondingNewElement.timestamp, let doesHistoryAlreadyContainElement = historyElement.history.map(element => element.timestamp).includes(timeStampOfNewElement)
"options": correspondingNewElement.options, if(!doesHistoryAlreadyContainElement){
"qualityindicators": correspondingNewElement.qualityindicators let historyWithNewElement = historyElement["history"].concat({
}) "timestamp": correspondingNewElement.timestamp,
let newHistoryElement = {...historyElement, "history": historyWithNewElement} "options": correspondingNewElement.options,
newHistoryJSON.push(newHistoryElement) "qualityindicators": correspondingNewElement.qualityindicators
})
let newHistoryElement = {...historyElement, "history": historyWithNewElement}
newHistoryJSON.push(newHistoryElement)
}else{
newHistoryJSON.push(historyElement)
}
}else{ }else{
newHistoryJSON.push(historyElement) // console.log(historyElement)
newHistoryJSON.push(historyElement)
} }
} }
for(let currentForecast of currentForecastsWithoutAHistory){ for(let currentForecast of currentForecastsWithoutAHistory){
@ -46,12 +53,13 @@ export async function addToHistory(){
} }
upsert(newHistoryJSON, "metaforecast_history") upsert(newHistoryJSON, "metaforecast_history")
console.log(newHistoryJSON.slice(0,5)) // console.log(newHistoryJSON.slice(0,5))
// writeFileSync("metaforecast_history.json", JSON.stringify(newHistoryJSON, null, 2))
// writefile(JSON.stringify(newHistoryJSON, null, 2), "metaforecasts_history", "", ".json") // writefile(JSON.stringify(newHistoryJSON, null, 2), "metaforecasts_history", "", ".json")
//console.log(newHistoryJSON)
/* /*
let forecastsAlreadyInHistory = currentJSON.filter(element => !isEmptyArray(historyJSON.filter(historyElement => historyElement.title == element.title && historyElement.url == element.url ))) let forecastsAlreadyInHistory = currentJSON.filter(element => !isEmptyArray(historyJSON.filter(historyElement => historyElement.title == element.title && historyElement.url == element.url )))
*/ */
console.log(new Date().toISOString()) console.log(new Date().toISOString())
} }
addToHistory() // addToHistory()