From abc4f6143272577f9b6e3c1461b181dcbff92da0 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 10 Apr 2021 15:47:32 +0200 Subject: [PATCH] Fixed bug in addToHistory function --- src/platforms/polymarket-fetch.js | 2 +- src/utils/addToHistory.js | 44 ++++++++++++++++++------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/platforms/polymarket-fetch.js b/src/platforms/polymarket-fetch.js index c2a58f4..9c5b21b 100644 --- a/src/platforms/polymarket-fetch.js +++ b/src/platforms/polymarket-fetch.js @@ -16,7 +16,7 @@ async function fetchAllContractInfo(){ // for info which the polymarket graphql } */ 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); response = response.filter(res => res.closed != true) return response diff --git a/src/utils/addToHistory.js b/src/utils/addToHistory.js index d30d856..5dbd282 100644 --- a/src/utils/addToHistory.js +++ b/src/utils/addToHistory.js @@ -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 export async function addToHistory(){ let currentJSON = await mongoRead("metaforecasts") @@ -16,21 +17,27 @@ export async function addToHistory(){ // Add both types of forecast let newHistoryJSON = [] for(let historyElement of historyJSON){ - let correspondingNewElement = currentForecastsWithAHistory.filter(element => historyElement.title == element.title && historyElement.url == element.url )[0] - let timeStampOfNewElement = correspondingNewElement.timestamp - let doesHistoryAlreadyContainElement = historyElement.history.map(element => element.timestamp).includes(timeStampOfNewElement) - if(!doesHistoryAlreadyContainElement){ - let historyWithNewElement = historyElement["history"].concat({ - "timestamp": correspondingNewElement.timestamp, - "options": correspondingNewElement.options, - "qualityindicators": correspondingNewElement.qualityindicators - }) - let newHistoryElement = {...historyElement, "history": historyWithNewElement} - newHistoryJSON.push(newHistoryElement) + let correspondingNewElementArray = currentForecastsWithAHistory.filter(element => historyElement.title == element.title && historyElement.url == element.url ) + // console.log(correspondingNewElement) + if(!isEmptyArray(correspondingNewElementArray)){ + let correspondingNewElement = correspondingNewElementArray[0] + let timeStampOfNewElement = correspondingNewElement.timestamp + let doesHistoryAlreadyContainElement = historyElement.history.map(element => element.timestamp).includes(timeStampOfNewElement) + if(!doesHistoryAlreadyContainElement){ + let historyWithNewElement = historyElement["history"].concat({ + "timestamp": correspondingNewElement.timestamp, + "options": correspondingNewElement.options, + "qualityindicators": correspondingNewElement.qualityindicators + }) + let newHistoryElement = {...historyElement, "history": historyWithNewElement} + newHistoryJSON.push(newHistoryElement) + }else{ + newHistoryJSON.push(historyElement) + } }else{ - newHistoryJSON.push(historyElement) + // console.log(historyElement) + newHistoryJSON.push(historyElement) } - } for(let currentForecast of currentForecastsWithoutAHistory){ @@ -46,12 +53,13 @@ export async function addToHistory(){ } 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") + //console.log(newHistoryJSON) /* - let forecastsAlreadyInHistory = currentJSON.filter(element => !isEmptyArray(historyJSON.filter(historyElement => historyElement.title == element.title && historyElement.url == element.url ))) */ console.log(new Date().toISOString()) } -addToHistory() \ No newline at end of file +// addToHistory()