Skip to content

Commit

Permalink
refactor: rename forecast -> question (see #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
berekuk committed Apr 12, 2022
1 parent 132deea commit 5b06f6f
Show file tree
Hide file tree
Showing 31 changed files with 190 additions and 196 deletions.
2 changes: 1 addition & 1 deletion docs/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# React

- create one file per one component (tiny helper components in the same file are fine)
- name file identically to the component it describes (e.g. `const DisplayForecasts: React.FC<Props> = ...` in `DisplayForecasts.ts`)
- name file identically to the component it describes (e.g. `const DisplayQuestions: React.FC<Props> = ...` in `DisplayQuestions.ts`)
- use named export instead of default export for all React components
- it's better for refactoring
- and it plays well with `React.FC` typing
Expand Down
14 changes: 7 additions & 7 deletions src/backend/database/pg-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Pool, PoolClient } from "pg";

import { Forecast } from "../platforms";
import { Question } from "../platforms";
import { hash } from "../utils/hash";
import { measureTime } from "../utils/measureTime";
import { roughSizeOfObject } from "../utils/roughSize";

const forecastTableNames = ["questions", "history"];
const questionTableNames = ["questions", "history"];

const allTableNames = [...forecastTableNames, "dashboards", "frontpage"];
const allTableNames = [...questionTableNames, "dashboards", "frontpage"];

/* Postgres database connection code */
const databaseURL = process.env.DIGITALOCEAN_POSTGRES;
Expand Down Expand Up @@ -51,11 +51,11 @@ export async function pgBulkInsert({
tableName,
client,
}: {
data: Forecast[];
data: Question[];
tableName: string;
client: PoolClient;
}) {
if (!forecastTableNames.includes(tableName)) {
if (!questionTableNames.includes(tableName)) {
throw Error(
`Table ${tableName} not in whitelist; stopping to avoid tricky sql injections`
);
Expand Down Expand Up @@ -171,11 +171,11 @@ export async function pgUpsert({
tableName,
replacePlatform,
}: {
contents: Forecast[];
contents: Question[];
tableName: string;
replacePlatform?: string;
}) {
if (!forecastTableNames.includes(tableName)) {
if (!questionTableNames.includes(tableName)) {
throw Error(
`Table ${tableName} not in whitelist; stopping to avoid tricky sql injections`
);
Expand Down
6 changes: 3 additions & 3 deletions src/backend/frontpage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { pgRead, pool } from "./database/pg-wrapper";
import { Forecast } from "./platforms";
import { Question } from "./platforms";

export async function getFrontpage(): Promise<Forecast[]> {
export async function getFrontpage(): Promise<Question[]> {
const res = await pool.query(
"SELECT frontpage_sliced FROM frontpage ORDER BY id DESC LIMIT 1"
);
if (!res.rows.length) return [];
return res.rows[0].frontpage_sliced;
}

export async function getFrontpageFull(): Promise<Forecast[]> {
export async function getFrontpageFull(): Promise<Question[]> {
const res = await pool.query(
"SELECT frontpage_full FROM frontpage ORDER BY id DESC LIMIT 1"
);
Expand Down
4 changes: 2 additions & 2 deletions src/backend/platforms/betfair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axios from "axios";
import https from "https";

import { calculateStars } from "../utils/stars";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

const platformName = "betfair";

Expand Down Expand Up @@ -80,7 +80,7 @@ async function whipIntoShape(data) {
async function processPredictions(data) {
let predictions = await whipIntoShape(data);
// console.log(JSON.stringify(predictions, null, 4))
let results: Forecast[] = predictions.map((prediction) => {
let results: Question[] = predictions.map((prediction) => {
/* if(Math.floor(Math.random() * 10) % 20 ==0){
console.log(JSON.stringify(prediction, null, 4))
} */
Expand Down
6 changes: 3 additions & 3 deletions src/backend/platforms/fantasyscotus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import axios from "axios";

import { calculateStars } from "../utils/stars";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

const platformName = "fantasyscotus";

Expand Down Expand Up @@ -67,15 +67,15 @@ async function processData(data) {
let historicalPercentageCorrect = data.stats.pcnt_correct;
let historicalProbabilityCorrect =
Number(historicalPercentageCorrect.replace("%", "")) / 100;
let results: Forecast[] = [];
let results: Question[] = [];
for (let event of events) {
if (event.accuracy == "") {
let id = `${platformName}-${event.id}`;
// if the thing hasn't already resolved
let predictionData = await getPredictionsData(event.docket_url);
let pAffirm = predictionData.proportionAffirm;
//let trackRecord = event.prediction.includes("Affirm") ? historicalProbabilityCorrect : 1-historicalProbabilityCorrect
let eventObject: Forecast = {
let eventObject: Question = {
id: id,
title: `In ${event.short_name}, the SCOTUS will affirm the lower court's decision`,
url: `https://fantasyscotus.net/user-predictions${event.docket_url}`,
Expand Down
8 changes: 4 additions & 4 deletions src/backend/platforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { smarkets } from "./smarkets";
import { wildeford } from "./wildeford";
import { xrisk } from "./xrisk";

export interface Forecast {
export interface Question {
id: string;
// "fantasyscotus-580"

Expand Down Expand Up @@ -63,8 +63,8 @@ export interface Forecast {
extra?: any;
}

// fetcher should return null if platform failed to fetch forecasts for some reason
export type PlatformFetcher = () => Promise<Forecast[] | null>;
// fetcher should return null if platform failed to fetch questions for some reason
export type PlatformFetcher = () => Promise<Question[] | null>;

export interface Platform {
name: string; // short name for ids and `platform` db column, e.g. "xrisk"
Expand All @@ -76,7 +76,7 @@ export interface Platform {
// draft for the future callback-based streaming/chunking API:
// interface FetchOptions {
// since?: string; // some kind of cursor, Date object or opaque string?
// save: (forecasts: Forecast[]) => Promise<void>;
// save: (questions: Question[]) => Promise<void>;
// }

// export type PlatformFetcher = (options: FetchOptions) => Promise<void>;
Expand Down
17 changes: 8 additions & 9 deletions src/backend/platforms/infer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* Imports */
import axios from "axios";
import { Tabletojson } from "tabletojson";

import { applyIfSecretExists } from "../utils/getSecrets";
import { measureTime } from "../utils/measureTime";
import { calculateStars } from "../utils/stars";
import toMarkdown from "../utils/toMarkdown";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

/* Definitions */
const platformName = "infer";
Expand Down Expand Up @@ -78,12 +77,12 @@ async function fetchStats(questionUrl, cookie) {
let comments_count = firstEmbeddedJson.question.comments_count;
let numforecasters = firstEmbeddedJson.question.predictors_count;
let numforecasts = firstEmbeddedJson.question.prediction_sets_count;
let forecastType = firstEmbeddedJson.question.type;
let questionType = firstEmbeddedJson.question.type;
if (
forecastType.includes("Binary") ||
forecastType.includes("NonExclusiveOpinionPoolQuestion") ||
forecastType.includes("Forecast::Question") ||
!forecastType.includes("Forecast::MultiTimePeriodQuestion")
questionType.includes("Binary") ||
questionType.includes("NonExclusiveOpinionPoolQuestion") ||
questionType.includes("Forecast::Question") ||
!questionType.includes("Forecast::MultiTimePeriodQuestion")
) {
options = firstEmbeddedJson.question.answers.map((answer) => ({
name: answer.name,
Expand Down Expand Up @@ -148,7 +147,7 @@ function sleep(ms) {
async function infer_inner(cookie: string) {
let i = 1;
let response = await fetchPage(i, cookie);
let results: Forecast[] = [];
let results: Question[] = [];

await measureTime(async () => {
// console.log("Downloading... This might take a couple of minutes. Results will be shown.")
Expand Down Expand Up @@ -179,7 +178,7 @@ async function infer_inner(cookie: string) {
let questionNumRegex = new RegExp("questions/([0-9]+)");
let questionNum = url.match(questionNumRegex)[1]; //.split("questions/")[1].split("-")[0];
let id = `${platformName}-${questionNum}`;
let question: Forecast = {
let question: Question = {
id: id,
title: title,
description: moreinfo.description,
Expand Down
8 changes: 4 additions & 4 deletions src/backend/platforms/manifold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import axios from "axios";

import { calculateStars } from "../utils/stars";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

/* Definitions */
const platformName = "manifold";
Expand All @@ -23,7 +23,7 @@ async function fetchData() {
return response;
}

function showStatistics(results: Forecast[]) {
function showStatistics(results: Question[]) {
console.log(`Num unresolved markets: ${results.length}`);
let sum = (arr) => arr.reduce((tally, a) => tally + a, 0);
let num2StarsOrMore = results.filter(
Expand All @@ -44,7 +44,7 @@ function showStatistics(results: Forecast[]) {
}

async function processPredictions(predictions) {
let results: Forecast[] = await predictions.map((prediction) => {
let results: Question[] = await predictions.map((prediction) => {
let id = `${platformName}-${prediction.id}`; // oops, doesn't match platform name
let probability = prediction.probability;
let options = [
Expand All @@ -59,7 +59,7 @@ async function processPredictions(predictions) {
type: "PROBABILITY",
},
];
const result: Forecast = {
const result: Question = {
id: id,
title: prediction.question,
url: prediction.url,
Expand Down
6 changes: 3 additions & 3 deletions src/backend/platforms/polymarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import axios from "axios";

import { calculateStars } from "../utils/stars";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

/* Definitions */
const platformName = "polymarket";
Expand Down Expand Up @@ -68,7 +68,7 @@ export const polymarket: Platform = {
label: "PolyMarket",
color: "#00314e",
async fetcher() {
let results: Forecast[] = [];
let results: Question[] = [];
let webpageEndpointData = await fetchAllContractInfo();
for (let marketInfo of webpageEndpointData) {
let address = marketInfo.marketMakerAddress;
Expand Down Expand Up @@ -102,7 +102,7 @@ export const polymarket: Platform = {
});
}

let result: Forecast = {
let result: Question = {
id: id,
title: marketInfo.question,
url: "https://polymarket.com/market/" + marketInfo.slug,
Expand Down
6 changes: 3 additions & 3 deletions src/backend/platforms/rootclaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSDOM } from "jsdom";

import { calculateStars } from "../utils/stars";
import toMarkdown from "../utils/toMarkdown";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

const platformName = "rootclaim";
const jsonEndpoint =
Expand Down Expand Up @@ -50,7 +50,7 @@ export const rootclaim: Platform = {
color: "#0d1624",
async fetcher() {
const claims = await fetchAllRootclaims();
const results: Forecast[] = [];
const results: Question[] = [];

for (const claim of claims) {
const id = `${platformName}-${claim.slug.toLowerCase()}`;
Expand All @@ -71,7 +71,7 @@ export const rootclaim: Platform = {

const description = await fetchDescription(url, claim.isclaim);

let obj: Forecast = {
let obj: Question = {
id,
title: toMarkdown(claim.question).replace("\n", ""),
url,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/platforms/smarkets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";

import { calculateStars } from "../utils/stars";
import { Forecast, Platform } from "./";
import { Platform, Question } from "./";

/* Definitions */
const platformName = "smarkets";
Expand Down Expand Up @@ -159,7 +159,7 @@ export const smarkets: Platform = {
name = name+ (contractName=="Yes"?'':` (${contracts["contracts"][0].name})`)
}
*/
let result: Forecast = {
let result: Question = {
id: id,
title: name,
url: "https://smarkets.com/event/" + market.event_id + market.slug,
Expand Down
24 changes: 12 additions & 12 deletions src/backend/utils/evaluations/pullForecastsToCSVForRating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { pgRead } from "../../database/pg-wrapper";
/* Utilities */

/* Support functions */
let getQualityIndicators = (forecast) =>
Object.entries(forecast.qualityindicators)
const getQualityIndicators = (question) =>
Object.entries(question.qualityindicators)
.map((entry) => `${entry[0]}: ${entry[1]}`)
.join("; ");

/* Body */

let main = async () => {
const main = async () => {
let highQualityPlatforms = [
"CSET-foretell",
"Foretold",
Expand All @@ -24,21 +24,21 @@ let main = async () => {
"PredictIt",
"Rootclaim",
];
let json = await pgRead({ tableName: "questions" });
const json = await pgRead({ tableName: "questions" });
console.log(json.length);
//let uniquePlatforms = [...new Set(json.map(forecast => forecast.platform))]
//console.log(uniquePlatforms)

let forecastsFromGoodPlatforms = json.filter((forecast) =>
highQualityPlatforms.includes(forecast.platform)
const questionsFromGoodPlatforms = json.filter((question) =>
highQualityPlatforms.includes(question.platform)
);
let tsv =
const tsv =
"index\ttitle\turl\tqualityindicators\n" +
forecastsFromGoodPlatforms
.map((forecast, index) => {
let row = `${index}\t${forecast.title}\t${
forecast.url
}\t${getQualityIndicators(forecast)}`;
questionsFromGoodPlatforms
.map((question, index) => {
let row = `${index}\t${question.title}\t${
question.url
}\t${getQualityIndicators(question)}`;
console.log(row);
return row;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { pgRead } from "../../database/pg-wrapper";
/* Utilities */

/* Support functions */
let getQualityIndicators = (forecast) =>
Object.entries(forecast.qualityindicators)
let getQualityIndicators = (question) =>
Object.entries(question.qualityindicators)
.map((entry) => `${entry[0]}: ${entry[1]}`)
.join("; ");

Expand All @@ -28,22 +28,22 @@ let main = async () => {
let highQualityPlatforms = ["Metaculus"]; // ['CSET-foretell', 'Foretold', 'Good Judgment Open', 'Metaculus', 'PredictIt', 'Rootclaim']
let json = await pgRead({ tableName: "questions" });
console.log(json.length);
//let uniquePlatforms = [...new Set(json.map(forecast => forecast.platform))]
//let uniquePlatforms = [...new Set(json.map(question => question.platform))]
//console.log(uniquePlatforms)

let forecastsFromGoodPlatforms = json.filter((forecast) =>
highQualityPlatforms.includes(forecast.platform)
let questionsFromGoodPlatforms = json.filter((question) =>
highQualityPlatforms.includes(question.platform)
);
let forecastsFromGoodPlatformsShuffled = shuffleArray(
forecastsFromGoodPlatforms
let questionsFromGoodPlatformsShuffled = shuffleArray(
questionsFromGoodPlatforms
);
let tsv =
"index\ttitle\turl\tqualityindicators\n" +
forecastsFromGoodPlatforms
.map((forecast, index) => {
let row = `${index}\t${forecast.title}\t${
forecast.url
}\t${getQualityIndicators(forecast)}`;
questionsFromGoodPlatforms
.map((question, index) => {
let row = `${index}\t${question.title}\t${
question.url
}\t${getQualityIndicators(question)}`;
console.log(row);
return row;
})
Expand Down
Loading

0 comments on commit 5b06f6f

Please sign in to comment.