Added history state

This commit is contained in:
NunoSempere 2021-04-10 12:49:05 +02:00
parent 0ae360a1d7
commit 3841dbfd1a
4 changed files with 101 additions and 4 deletions

View File

@ -16,20 +16,21 @@ import {predictit} from "./platforms/predictit-fetch.js"
import {omen} from "./platforms/omen-fetch.js" import {omen} from "./platforms/omen-fetch.js"
import {smarkets} from "./platforms/smarkets-fetch.js" import {smarkets} from "./platforms/smarkets-fetch.js"
import {williamhill} from "./platforms/williamhill-fetch.js" import {williamhill} from "./platforms/williamhill-fetch.js"
import {mergeEverything} from "./utils/mergeEverything.js" import {mergeEverything} from "./utils/mergeEverything.js"
import {addToHistory} from "./utils/addToHistory.js"
import {rebuildNetlifySiteWithNewData} from "./utils/rebuildNetliftySiteWithNewData.js" import {rebuildNetlifySiteWithNewData} from "./utils/rebuildNetliftySiteWithNewData.js"
import {doEverything, tryCatchTryAgain} from "./utils/doEverything.js" import {doEverything, tryCatchTryAgain} from "./utils/doEverything.js"
/* Support functions */ /* Support functions */
let functions = [csetforetell, elicit, /* estimize, */ fantasyscotus, foretold, goodjudgment, goodjudgmentopen, hypermind, ladbrokes, metaculus, polymarket, predictit, omen, smarkets, williamhill, mergeEverything, rebuildNetlifySiteWithNewData, doEverything] let functions = [csetforetell, elicit, /* estimize, */ fantasyscotus, foretold, goodjudgment, goodjudgmentopen, hypermind, ladbrokes, metaculus, polymarket, predictit, omen, smarkets, williamhill, mergeEverything, addToHistory, rebuildNetlifySiteWithNewData, doEverything]
let functionNames = functions.map(fun => fun.name) let functionNames = functions.map(fun => fun.name)
let whattodoMessage = functionNames let whattodoMessage = functionNames
.slice(0,functionNames.length-2) .slice(0,functionNames.length-2)
.map((functionName,i) => `[${i}]: Download predictions from ${functionName}`) .map((functionName,i) => `[${i}]: Download predictions from ${functionName}`)
.join('\n') + .join('\n') +
`\n[${functionNames.length-3}]: Merge jsons them into one big json (and push it to mongodb database)` + `\n[${functionNames.length-4}]: Merge jsons them into one big json (and push it to mongodb database)` +
`\n[${functionNames.length-3}]: Add to history file` +
`\n[${functionNames.length-2}]: Rebuild netlify site with new data` + `\n[${functionNames.length-2}]: Rebuild netlify site with new data` +
// `\n[${functionNames.length-1}]: Add to history` + // `\n[${functionNames.length-1}]: Add to history` +
`\n[${functionNames.length-1}]: All of the above` + `\n[${functionNames.length-1}]: All of the above` +

57
src/utils/addToHistory.js Normal file
View File

@ -0,0 +1,57 @@
import { mongoRead, upsert } from "./mongo-wrapper.js"
let isEmptyArray = arr => arr.length == 0
export async function addToHistory(){
let currentJSON = await mongoRead("metaforecasts")
// console.log(currentJSON)
let historyJSON = await mongoRead("metaforecast_history")
// console.log(historyJSON)
let currentForecastsWithAHistory = currentJSON.filter(element => !isEmptyArray(historyJSON.filter(historyElement => historyElement.title == element.title && historyElement.url == element.url )))
// console.log(currentForecastsWithAHistory)
let currentForecastsWithoutAHistory = currentJSON.filter(element => isEmptyArray(historyJSON.filter(historyElement => historyElement.title == element.title && historyElement.url == element.url )))
// console.log(currentForecastsWithoutAHistory)
// 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)
}else{
newHistoryJSON.push(historyElement)
}
}
for(let currentForecast of currentForecastsWithoutAHistory){
let newHistoryElement = ({...currentForecast, "history": [{
"timestamp": currentForecast.timestamp,
"options": currentForecast.options,
"qualityindicators": currentForecast.qualityindicators
}]})
delete newHistoryElement.timestamp
delete newHistoryElement.options
delete newHistoryElement.qualityindicators
newHistoryJSON.push(newHistoryElement)
}
upsert(newHistoryJSON, "metaforecast_history")
console.log(newHistoryJSON.slice(0,5))
// writefile(JSON.stringify(newHistoryJSON, null, 2), "metaforecasts_history", "", ".json")
/*
let forecastsAlreadyInHistory = currentJSON.filter(element => !isEmptyArray(historyJSON.filter(historyElement => historyElement.title == element.title && historyElement.url == element.url )))
*/
console.log(new Date().toISOString())
}
addToHistory()

View File

@ -14,6 +14,7 @@ import {omen} from "../platforms/omen-fetch.js"
import {smarkets} from "../platforms/smarkets-fetch.js" import {smarkets} from "../platforms/smarkets-fetch.js"
import {williamhill} from "../platforms/williamhill-fetch.js" import {williamhill} from "../platforms/williamhill-fetch.js"
import {mergeEverything} from "./mergeEverything.js" import {mergeEverything} from "./mergeEverything.js"
import {addToHistory} from "./addToHistory.js"
import {rebuildNetlifySiteWithNewData} from "./rebuildNetliftySiteWithNewData.js" import {rebuildNetlifySiteWithNewData} from "./rebuildNetliftySiteWithNewData.js"
/* Do everything */ /* Do everything */
@ -33,7 +34,7 @@ export async function tryCatchTryAgain (fun) {
} }
} }
export async function doEverything(){ export async function doEverything(){
let functions = [csetforetell, elicit, /* estimize, */ fantasyscotus, foretold, goodjudgment, goodjudgmentopen, hypermind, ladbrokes, metaculus, polymarket, predictit, omen, smarkets, williamhill, mergeEverything, rebuildNetlifySiteWithNewData] let functions = [csetforetell, elicit, /* estimize, */ fantasyscotus, foretold, goodjudgment, goodjudgmentopen, hypermind, ladbrokes, metaculus, polymarket, predictit, omen, smarkets, williamhill, mergeEverything, addToHistory, rebuildNetlifySiteWithNewData]
console.log("") console.log("")
console.log("") console.log("")

View File

@ -76,3 +76,41 @@ export async function mongoRead (documentName, collectionName="metaforecastColle
console.log(documentContents.slice(0,10)); console.log(documentContents.slice(0,10));
return documentContents return documentContents
} }
export async function mongoReadWithReadCredentials (documentName, collectionName="metaforecastCollection", databaseName="metaforecastDatabase"){
const url = "mongodb+srv://metaforecast-frontend:hJr5c9kDhbutBtF1@metaforecastdatabaseclu.wgk8a.mongodb.net/?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true"; // This user only has read permissions, so I'm not excessively worried, and would even be pleased, if someone read this and decided to do something cool with the database.
const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let documentContents
try {
await client.connect();
// console.log(`Connected correctly to server to read ${documentName}`);
const db = client.db(databaseName);
// Use the collection "data"
const collection = db.collection(collectionName);
// Search options
const query = { "name": documentName };
const options = {
// sort matched documents in descending order by rating
sort: { rating: -1 },
};
// Insert a single document, wait for promise so we can read it back
// const p = await collection.insertOne(metaforecastDocument);
const document = await collection.findOne(query, options);
documentContents = document.contentsArray
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
// console.log(documentContents.slice(0,10));
return documentContents
}