Compare commits

..

No commits in common. "master" and "contrib" have entirely different histories.

73 changed files with 70536 additions and 11016 deletions

1
.gitignore vendored
View File

@ -31,7 +31,6 @@ yarn-error.log*
# yarn vs npm conflict # yarn vs npm conflict
package-lock.json ## use yarn.lock instead package-lock.json ## use yarn.lock instead
# yarn.lock
# Local Netlify folder # Local Netlify folder
.netlify .netlify

View File

@ -1,7 +0,0 @@
Copyright 2023 Quantified Uncertainty Research Institute.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -11,8 +11,8 @@ This repository includes the source code for both the website and the library th
### 1. Download this repository ### 1. Download this repository
``` ```
$ git clone https://github.com/quantified-uncertainty/metaforecast $ git clone https://github.com/QURIresearch/metaforecast
$ cd metaforecast $ cd metaforecasts
$ npm install $ npm install
``` ```
@ -26,28 +26,17 @@ See [./docs/configuration.md](./docs/configuration.md) for details.
### 3. Actually run ### 3. Actually run
After installing and building (`npm run build`) the application, `npm run cli` starts a local CLI which presents the user with choices. If you would like to skip that step, use the option name instead, e.g., `npm run cli wildeford`. `npm run cli` starts a local CLI which presents the user with choices. If you would like to skip that step, use the option name instead, e.g., `npm run cli wildeford`.
![](./public/screenshot-cli.png) ![](./public/screenshot-cli.png)
`npm run next-dev` starts a Next.js dev server with the website on `http://localhost:3000`. `npm run next-dev` starts a Next.js dev server with the website on `http://localhost:3000`.
So overall this would look like
```
$ git clone https://github.com/quantified-uncertainty/metaforecast
$ cd metaforecast
$ npm install
$ npm run build
$ npm run cli
$ npm run next-dev
```
### 4. Example: download the metaforecasts database ### 4. Example: download the metaforecasts database
``` ```
$ git clone https://github.com/quantified-uncertainty/metaforecast $ git clone https://github.com/QURIresearch/metaforecast
$ cd metaforecast $ cd metaforecasts
$ npm install $ npm install
$ node src/backend/manual/manualDownload.js $ node src/backend/manual/manualDownload.js
``` ```
@ -74,7 +63,7 @@ In general, if you want to integrate metaforecast into your service, we want to
## What are "stars" and how are they computed ## What are "stars" and how are they computed
Star ratings—e.g. ★★★☆☆—are an indicator of the quality of an aggregate forecast for a question. These ratings currently try to reflect my own best judgment and the best judgment of forecasting experts I've asked, based on our collective experience forecasting on these platforms. Thus, stars have a strong subjective component which could be formalized and refined in the future. You can see the code used to decide how many stars a forecast should get by looking at the function `calculateStars()` in the files for every platform [here](./src/backend/platforms). Star ratings—e.g. ★★★☆☆—are an indicator of the quality of an aggregate forecast for a question. These ratings currently try to reflect my own best judgment and the best judgment of forecasting experts I've asked, based on our collective experience forecasting on these platforms. Thus, stars have a strong subjective component which could be formalized and refined in the future. You can see the code used to decide how many stars to assign [here](./src/backend/utils/stars.js).
With regards the quality, I am most uncertain about Smarkets, Hypermind, Ladbrokes and WilliamHill, as I haven't used them as much. Also note that, whatever other redeeming features they might have, prediction markets rarely go above 95% or below 5%. With regards the quality, I am most uncertain about Smarkets, Hypermind, Ladbrokes and WilliamHill, as I haven't used them as much. Also note that, whatever other redeeming features they might have, prediction markets rarely go above 95% or below 5%.
@ -89,22 +78,5 @@ Overall, the services which we use are:
## Various notes ## Various notes
- This repository is released under the [MIT license](https://opensource.org/licenses/MIT). See `LICENSE.md`
- Commits follow [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) - Commits follow [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/#summary)
- For elicit and metaculus, this library currently filters out questions with <10 predictions. - For elicit and metaculus, this library currently filters out questions with <10 predictions.
- The database is updated once a day, at 3:00 AM UTC, with the command `ts-node -T src/backend/flow/doEverythingForScheduler.ts`. The frontpage is updated after that, at 6:00 AM UTC with the command `ts-node -T src/backend/index.ts frontpage`. It's possible that either of these two operations makes the webpage briefly go down.
## To do
- [x] Update Metaculus and Manifold Markets fetchers
- [x] Add markets from [Insight Prediction](https://insightprediction.com/).
- [ ] Use <https://news.manifold.markets/p/above-the-fold-midterms-special> to update stars calculation for Manifold.
- [ ] Add a few more snippets, with fetching individual questions, questions with histories, questions added within the last 24h to the /contrib folder (good first issue)
- [ ] Refactor code so that users can capture and push the question history chart to imgur (good first issue)
- [ ] Upgrade to [React 18](https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html). This will require dealing with the workaround we used for [this issue](https://github.com/vercel/next.js/issues/36019#issuecomment-1103266481)
- [ ] Add database of resolutions
- [ ] Allow users to embed predictions in the EA Forum/LessWrong (in progress)
- [ ] Find a long-term mantainer for this project
- [ ] Allow users to record their own predictions
- [ ] Release snapshots (I think @niplav is working on this)
- [ ] ...

View File

@ -22,8 +22,7 @@ let buildQuery = (endCursor) => `{
numForecasts numForecasts
stars stars
} }
firstSeenStr timestamp
fetchedStr
} }
} }
pageInfo { pageInfo {

69680
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -24,101 +24,93 @@
"build": "prisma generate && next build", "build": "prisma generate && next build",
"next-start": "next start", "next-start": "next start",
"next-export": "next export", "next-export": "next export",
"dbshell": ". .env && psql $DIGITALOCEAN_POSTGRES", "dbshell": ". .env && psql $DIGITALOCEAN_POSTGRES"
"upgrade-interactive": "yarn upgrade-interactive --latest"
}, },
"dependencies": { "dependencies": {
"@floating-ui/react-dom": "^0.7.2", "@floating-ui/react-dom": "^0.7.0",
"@graphql-yoga/plugin-response-cache": "^1.1.0", "@graphql-yoga/node": "^2.1.0",
"@pothos/core": "^3.22.8", "@pothos/core": "^3.5.1",
"@pothos/plugin-prisma": "^3.35.6", "@pothos/plugin-prisma": "^3.4.0",
"@pothos/plugin-relay": "^3.28.6", "@pothos/plugin-relay": "^3.10.0",
"@prisma/client": "^3.15.2", "@prisma/client": "^3.11.1",
"@quri/squiggle-lang": "^0.5.1", "@quri/squiggle-lang": "^0.2.8",
"@tailwindcss/forms": "^0.4.1", "@tailwindcss/forms": "^0.4.0",
"@tailwindcss/typography": "^0.5.7", "@tailwindcss/typography": "^0.5.1",
"@types/chroma-js": "^2.1.4", "@types/chroma-js": "^2.1.3",
"@types/dom-to-image": "^2.6.4", "@types/dom-to-image": "^2.6.4",
"@types/google-spreadsheet": "^3.3.0", "@types/google-spreadsheet": "^3.2.1",
"@types/jsdom": "^16.2.15", "@types/jsdom": "^16.2.14",
"@types/nprogress": "^0.2.0", "@types/nprogress": "^0.2.0",
"@types/react": "<18.0.0", "@types/react": "^17.0.39",
"@types/react-copy-to-clipboard": "^5.0.4", "@types/react-copy-to-clipboard": "^5.0.2",
"@types/textversionjs": "^1.1.1", "@types/textversionjs": "^1.1.1",
"@types/tunnel": "^0.0.3", "@types/tunnel": "^0.0.3",
"airtable": "^0.11.5", "airtable": "^0.11.1",
"ajv": "^8.11.0", "algoliasearch": "^4.10.3",
"algoliasearch": "^4.14.2", "autoprefixer": "^10.1.0",
"autoprefixer": "10.4.5", "axios": "^0.25.0",
"axios": "^1.2.0",
"chroma-js": "^2.4.2", "chroma-js": "^2.4.2",
"critters": "^0.0.16", "critters": "^0.0.16",
"date-fns": "^2.29.3", "date-fns": "^2.28.0",
"dom-to-image": "^2.6.0", "dom-to-image": "^2.6.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.0",
"fetch": "^1.1.0", "fetch": "^1.1.0",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",
"fuse.js": "^6.6.2", "fuse.js": "^6.4.6",
"google-spreadsheet": "^3.3.0", "google-spreadsheet": "^3.1.15",
"graphql": "^16.6.0", "graphql": "^16.3.0",
"graphql-request": "^5.0.0", "graphql-request": "^4.0.0",
"graphql-yoga": "^3.0.0-next.10", "html-to-image": "^1.7.0",
"html-to-image": "^1.10.8",
"https": "^1.0.0", "https": "^1.0.0",
"isomorphic-fetch": "^3.0.0", "isomorphic-fetch": "^3.0.0",
"jsdom": "^19.0.0", "jsdom": "^19.0.0",
"json2csv": "^5.0.7", "json2csv": "^5.0.5",
"multiselect-react-dropdown": "^2.0.25", "multiselect-react-dropdown": "^2.0.17",
"next": "^12.3.1", "next": "12",
"next-plausible": "^3.6.3", "next-plausible": "^3.1.6",
"next-urql": "^3.3.3", "next-urql": "^3.3.2",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"open": "^7.4.2", "open": "^7.3.1",
"papaparse": "^5.3.2", "papaparse": "^5.3.0",
"pg": "^8.8.0", "pg": "^8.7.3",
"postcss": "^8.4.18", "postcss": "^8.2.1",
"postcss-flexbugs-fixes": "^5.0.2", "postcss-flexbugs-fixes": "^5.0.2",
"postcss-preset-env": "^7.8.2", "postcss-preset-env": "^7.3.2",
"prisma": "^3.15.2", "prisma": "^3.11.1",
"query-string": "^7.1.1", "query-string": "^7.1.1",
"re-resizable": "^6.9.9",
"react": "^17.0.2", "react": "^17.0.2",
"react-component-export-image": "^1.0.6", "react-component-export-image": "^1.0.6",
"react-compound-slider": "^3.4.0", "react-compound-slider": "^3.3.1",
"react-copy-to-clipboard": "^5.1.0", "react-copy-to-clipboard": "^5.0.3",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-dropdown": "^1.11.0", "react-dropdown": "^1.9.2",
"react-hook-form": "^7.38.0", "react-hook-form": "^7.27.0",
"react-icons": "^4.6.0", "react-icons": "^4.2.0",
"react-is": "^18.2.0", "react-is": "^18.0.0",
"react-markdown": "^8.0.3", "react-markdown": "^8.0.0",
"react-safe": "^1.3.0", "react-safe": "^1.3.0",
"react-select": "^5.5.4", "react-select": "^5.2.2",
"remark-gfm": "^3.0.1", "remark-gfm": "^3.0.1",
"tabletojson": "^2.0.7", "tabletojson": "^2.0.4",
"tailwindcss": "^3.2.0", "tailwindcss": "^3.0.22",
"textversionjs": "^1.1.3", "textversionjs": "^1.1.3",
"ts-node": "^10.9.1", "ts-node": "^10.7.0",
"tunnel": "^0.0.6", "tunnel": "^0.0.6",
"urql": "^2.2.3", "urql": "^2.2.0",
"urql-custom-scalars-exchange": "^0.1.6", "urql-custom-scalars-exchange": "^0.1.5",
"victory": "^36.6.8" "victory": "^36.3.2"
},
"resolutions": {
"@types/react": "<18.0.0"
}, },
"devDependencies": { "devDependencies": {
"@graphql-codegen/cli": "^2.13.7", "@graphql-codegen/cli": "^2.6.2",
"@graphql-codegen/introspection": "^2.2.1", "@graphql-codegen/introspection": "^2.1.1",
"@graphql-codegen/near-operation-file-preset": "^2.4.3", "@graphql-codegen/near-operation-file-preset": "^2.2.9",
"@graphql-codegen/schema-ast": "^2.5.1", "@graphql-codegen/schema-ast": "^2.4.1",
"@graphql-codegen/typed-document-node": "^2.3.5", "@graphql-codegen/typed-document-node": "^2.2.8",
"@graphql-codegen/typescript": "^2.7.5", "@graphql-codegen/typescript": "^2.4.8",
"@graphql-codegen/typescript-operations": "^2.5.5", "@graphql-codegen/typescript-operations": "^2.3.5",
"@svgr/cli": "^6.5.0", "@netlify/plugin-nextjs": "^4.2.4",
"@svgr/cli": "^6.2.1",
"@types/pg": "^8.6.5", "@types/pg": "^8.6.5",
"eslint": "^8.25.0", "netlify-cli": "^9.13.6"
"eslint-config-next": "^12.3.1",
"typescript": "4.9.3"
} }
} }

View File

@ -1,28 +0,0 @@
-- questions
ALTER TABLE "questions"
ADD COLUMN "fetched" TIMESTAMP(6),
ADD COLUMN "first_seen" TIMESTAMP(6);
UPDATE "questions"
SET "fetched" = "timestamp", "first_seen" = "timestamp";
ALTER TABLE "questions"
ALTER COLUMN "fetched" SET NOT NULL,
ALTER COLUMN "first_seen" SET NOT NULL;
-- history
ALTER TABLE "history"
ADD COLUMN "fetched" TIMESTAMP(6);
UPDATE "history" SET "fetched" = "timestamp";
ALTER TABLE "history"
ALTER COLUMN "fetched" SET NOT NULL;
-- populate first_seen
UPDATE questions
SET "first_seen" = h.fs
FROM (
SELECT id, MIN(fetched) AS fs FROM history GROUP BY id
) as h
WHERE questions.id = h.id;

View File

@ -1,14 +0,0 @@
-- CreateIndex
CREATE INDEX "history_platform_idx" ON "history"("platform");
-- CreateIndex
CREATE INDEX "history_fetched_idx" ON "history"("fetched");
-- CreateIndex
CREATE INDEX "questions_platform_idx" ON "questions"("platform");
-- CreateIndex
CREATE INDEX "questions_fetched_idx" ON "questions"("fetched");
-- CreateIndex
CREATE INDEX "questions_first_seen_idx" ON "questions"("first_seen");

View File

@ -1,12 +0,0 @@
/*
Warnings:
- You are about to drop the column `timestamp` on the `history` table. All the data in the column will be lost.
- You are about to drop the column `timestamp` on the `questions` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "history" DROP COLUMN "timestamp";
-- AlterTable
ALTER TABLE "questions" DROP COLUMN "timestamp";

View File

@ -1,5 +0,0 @@
-- DropForeignKey
ALTER TABLE "FrontpageId" DROP CONSTRAINT "FrontpageId_id_fkey";
-- AddForeignKey
ALTER TABLE "FrontpageId" ADD CONSTRAINT "FrontpageId_id_fkey" FOREIGN KEY ("id") REFERENCES "questions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -24,6 +24,24 @@ model Dashboard {
@@map("dashboards") @@map("dashboards")
} }
model History {
id String
idref String?
question Question? @relation(fields: [idref], references: [id], onDelete: SetNull, onUpdate: Restrict)
title String
url String
platform String
description String
options Json
timestamp DateTime @db.Timestamp(6)
qualityindicators Json
extra Json
pk Int @id @default(autoincrement())
@@index([id])
@@map("history")
}
model Question { model Question {
/// E.g. "fantasyscotus-580" /// E.g. "fantasyscotus-580"
id String @id id String @id
@ -50,8 +68,7 @@ model Question {
// } // }
// ] // ]
options Json options Json
fetched DateTime @db.Timestamp(6) timestamp DateTime @db.Timestamp(6)
firstSeen DateTime @map("first_seen") @db.Timestamp(6)
// { // {
// "numforecasts": 120, // "numforecasts": 120,
@ -63,33 +80,10 @@ model Question {
onFrontpage FrontpageId? onFrontpage FrontpageId?
history History[] history History[]
@@index([platform])
@@index([fetched])
@@index([firstSeen])
@@map("questions") @@map("questions")
} }
model History {
id String
idref String?
question Question? @relation(fields: [idref], references: [id], onDelete: SetNull, onUpdate: Restrict)
title String
url String
platform String
description String
options Json
fetched DateTime @db.Timestamp(6)
qualityindicators Json
extra Json
pk Int @id @default(autoincrement())
@@index([id])
@@index([platform])
@@index([fetched])
@@map("history")
}
model FrontpageId { model FrontpageId {
question Question @relation(fields: [id], references: [id], onDelete: Cascade) question Question @relation(fields: [id], references: [id])
id String @unique id String @unique
} }

View File

@ -41,14 +41,6 @@ scalar Date
type History implements QuestionShape { type History implements QuestionShape {
description: String! description: String!
"""Last timestamp at which metaforecast fetched the question"""
fetched: Date!
"""
Last timestamp at which metaforecast fetched the question, in ISO 8601 format
"""
fetchedStr: String!
"""History items are identified by their integer ids""" """History items are identified by their integer ids"""
id: ID! id: ID!
options: [ProbabilityOption!]! options: [ProbabilityOption!]!
@ -58,8 +50,8 @@ type History implements QuestionShape {
"""Unique string which identifies the question""" """Unique string which identifies the question"""
questionId: ID! questionId: ID!
"""Last timestamp at which metaforecast fetched the question""" """Timestamp at which metaforecast fetched the question"""
timestamp: Date! @deprecated(reason: "Renamed to `fetched`") timestamp: Date!
title: String! title: String!
""" """
@ -120,15 +112,15 @@ type QualityIndicators {
type Query { type Query {
"""Look up a single dashboard by its id""" """Look up a single dashboard by its id"""
dashboard(id: ID!): Dashboard dashboard(id: ID!): Dashboard!
"""Get a list of questions that are currently on the frontpage""" """Get a list of questions that are currently on the frontpage"""
frontpage: [Question!]! frontpage: [Question!]!
platforms: [Platform!]! platforms: [Platform!]!
"""Look up a single question by its id""" """Look up a single question by its id"""
question(id: ID!): Question question(id: ID!): Question!
questions(after: String, before: String, first: Int, last: Int, orderBy: QuestionsOrderBy): QueryQuestionsConnection! questions(after: String, before: String, first: Int, last: Int): QueryQuestionsConnection!
""" """
Search for questions; uses Algolia instead of the primary metaforecast database Search for questions; uses Algolia instead of the primary metaforecast database
@ -148,22 +140,6 @@ type QueryQuestionsConnectionEdge {
type Question implements QuestionShape { type Question implements QuestionShape {
description: String! description: String!
"""Last timestamp at which metaforecast fetched the question"""
fetched: Date!
"""
Last timestamp at which metaforecast fetched the question, in ISO 8601 format
"""
fetchedStr: String!
"""First timestamp at which metaforecast fetched the question"""
firstSeen: Date!
"""
First timestamp at which metaforecast fetched the question, in ISO 8601 format
"""
firstSeenStr: String!
history: [History!]! history: [History!]!
"""Unique string which identifies the question""" """Unique string which identifies the question"""
@ -172,8 +148,8 @@ type Question implements QuestionShape {
platform: Platform! platform: Platform!
qualityIndicators: QualityIndicators! qualityIndicators: QualityIndicators!
"""Last timestamp at which metaforecast fetched the question""" """Timestamp at which metaforecast fetched the question"""
timestamp: Date! @deprecated(reason: "Renamed to `fetched`") timestamp: Date!
title: String! title: String!
""" """
@ -185,20 +161,12 @@ type Question implements QuestionShape {
interface QuestionShape { interface QuestionShape {
description: String! description: String!
"""Last timestamp at which metaforecast fetched the question"""
fetched: Date!
"""
Last timestamp at which metaforecast fetched the question, in ISO 8601 format
"""
fetchedStr: String!
options: [ProbabilityOption!]! options: [ProbabilityOption!]!
platform: Platform! platform: Platform!
qualityIndicators: QualityIndicators! qualityIndicators: QualityIndicators!
"""Last timestamp at which metaforecast fetched the question""" """Timestamp at which metaforecast fetched the question"""
timestamp: Date! @deprecated(reason: "Renamed to `fetched`") timestamp: Date!
title: String! title: String!
""" """
@ -207,10 +175,6 @@ interface QuestionShape {
url: String! url: String!
} }
enum QuestionsOrderBy {
FIRST_SEEN_DESC
}
input SearchInput { input SearchInput {
"""List of platform ids to filter by""" """List of platform ids to filter by"""
forecastingPlatforms: [String!] forecastingPlatforms: [String!]

View File

@ -1,91 +0,0 @@
/* Imports */
import fs from "fs";
import axios from "axios";
/* Definitions */
const VERBOSE = true;
let ISO_DATE_TODAY = new Date().toISOString().slice(0, 10);
let print = (message) => (VERBOSE ? console.log(message) : null);
let graphQLendpoint = "https://metaforecast.org/api/graphql";
let buildQuery = (endCursor) => `{
questions(first: 1000 ${
!!endCursor ? `after: "${endCursor}"` : ""
} orderBy: FIRST_SEEN_DESC) {
edges {
node {
id
title
url
description
options {
name
probability
}
qualityIndicators {
numForecasts
stars
}
firstSeenStr
fetchedStr
}
}
pageInfo {
endCursor
startCursor
}
}
}
`;
/* Support functions */
let getSomeMetaforecastPredictions = async (query) => {
let response = await axios({
url: graphQLendpoint,
method: "POST",
headers: { "Content-Type": "application/json" },
data: JSON.stringify({ query: query }),
})
.then((res) => res.data)
.then((res) => res.data); // not a typo
return response;
};
let save = (questions) => {
print("Saving Results");
fs.writeFileSync("forecasts-today.json", JSON.stringify(questions, null, 4));
};
let getNodes = (questions) => {
let edges = questions.edges;
let nodes = edges.map((edge) => edge.node);
return nodes;
};
// main
let getAllMetaforecastPredictions = async () => {
print("Fetching forecasts");
let results = [];
let firstQuery = await getSomeMetaforecastPredictions(buildQuery());
let nodes = getNodes(firstQuery.questions);
let nodesToday = nodes.filter(
(node) => node.firstSeenStr.slice(0, 10) == ISO_DATE_TODAY
);
results.push(...nodesToday);
let endCursor = firstQuery.questions.pageInfo.endCursor;
while (endCursor && nodesToday.length > 0) {
print("Cursor: " + endCursor);
let queryResults = await getSomeMetaforecastPredictions(
buildQuery(endCursor)
);
let nodes = getNodes(queryResults.questions);
nodesToday = nodes.filter(
(node) => node.firstSeenStr.slice(0, 10) == ISO_DATE_TODAY
);
results.push(...nodesToday);
endCursor = queryResults.questions.pageInfo.endCursor;
}
//results = results.map((result) => result.node);
save(results);
return results;
};
getAllMetaforecastPredictions();

8
src/Global.d.ts vendored
View File

@ -1,8 +0,0 @@
// Workaround related to: https://github.com/vercel/next.js/issues/29788
// https://github.com/vercel/next.js/issues/29788#issuecomment-1000595524
declare type StaticImageData = {
src: string;
height: number;
width: number;
placeholder?: string;
};

View File

@ -1,12 +1,9 @@
import { getPlatforms } from "../platforms/registry"; import { platforms } from "../platforms/registry";
import { executeJobByName } from "./jobs"; import { executeJobByName } from "./jobs";
/* Do everything */ /* Do everything */
export async function doEverything() { export async function doEverything() {
let jobNames = [ let jobNames = [...platforms.map((platform) => platform.name), "algolia"];
...getPlatforms().map((platform) => platform.name),
"algolia",
];
console.log(""); console.log("");
console.log(""); console.log("");

View File

@ -1,7 +1,7 @@
import { doEverything } from "../flow/doEverything"; import { doEverything } from "../flow/doEverything";
import { rebuildFrontpage } from "../frontpage"; import { rebuildFrontpage } from "../frontpage";
import { processPlatform } from "../platforms"; import { processPlatform } from "../platforms";
import { getPlatforms } from "../platforms/registry"; import { platforms } from "../platforms/registry";
import { rebuildAlgoliaDatabase } from "../utils/algolia"; import { rebuildAlgoliaDatabase } from "../utils/algolia";
import { sleep } from "../utils/sleep"; import { sleep } from "../utils/sleep";
@ -14,7 +14,7 @@ interface Job<ArgNames extends string = ""> {
} }
export const jobs: Job<string>[] = [ export const jobs: Job<string>[] = [
...getPlatforms().map((platform) => ({ ...platforms.map((platform) => ({
name: platform.name, name: platform.name,
message: `Download predictions from ${platform.name}`, message: `Download predictions from ${platform.name}`,
...(platform.version === "v2" ? { args: platform.fetcherArgs } : {}), ...(platform.version === "v2" ? { args: platform.fetcherArgs } : {}),

View File

@ -26,7 +26,7 @@ export async function rebuildFrontpage() {
AND questions.description != '' AND questions.description != ''
AND JSONB_ARRAY_LENGTH(questions.options) > 0 AND JSONB_ARRAY_LENGTH(questions.options) > 0
GROUP BY questions.id GROUP BY questions.id
HAVING COUNT(DISTINCT history.fetched) >= 7 HAVING COUNT(DISTINCT history.timestamp) >= 7
ORDER BY RANDOM() LIMIT 50 ORDER BY RANDOM() LIMIT 50
`; `;

View File

@ -1,5 +1,4 @@
#!/bin/bash #!/bin/bash
cd /home/loki/Documents/core/software/fresh/js/metaforecast/metaforecast-monorepo cd /home/loki/Documents/core/software/fresh/js/metaforecast/metaforecast-monorepo
/home/loki/.nvm/versions/node/v17.5.0/bin/npm run cli 3 > ../last-superforecast-fetch.txt
/home/loki/.nvm/versions/node/v16.15.0/lib/node_modules/npm/bin/npm-cli.js run cli goodjudgment > ../last-superforecast-fetch.txt

View File

@ -79,7 +79,7 @@ export const givewellopenphil: Platform = {
const dataWithDate = data.map((datum: any) => ({ const dataWithDate = data.map((datum: any) => ({
...datum, ...datum,
platform: platformName, platform: platformName,
// timestamp: new Date("2021-02-23"), timestamp: new Date("2021-02-23"),
})); }));
return dataWithDate; return dataWithDate;
}, },

View File

@ -5,7 +5,6 @@ import {Tabletojson} from "tabletojson";
import { average } from "../../utils"; import { average } from "../../utils";
import { hash } from "../utils/hash"; import { hash } from "../utils/hash";
import { FetchedQuestion, Platform } from "./"; import { FetchedQuestion, Platform } from "./";
import {FullQuestionOption} from "../../common/types";
/* Definitions */ /* Definitions */
const platformName = "goodjudgment"; const platformName = "goodjudgment";
@ -42,15 +41,17 @@ export const goodjudgment: Platform = {
// }, // },
// }); // });
const content = await axios.request({ const content = await axios
.request({
url: "https://goodjudgment.io/superforecasts/", url: "https://goodjudgment.io/superforecasts/",
method: "get", method: "get",
headers: { headers: {
"User-Agent": "Chrome" "User-Agent": "Chrome",
}, },
// agent, // agent,
// port: 80, // port: 80,
}).then((query) => query.data); })
.then((query) => query.data);
// Processing // Processing
let results: FetchedQuestion[] = []; let results: FetchedQuestion[] = [];
@ -62,21 +63,38 @@ export const goodjudgment: Platform = {
let title = table[0]["0"].split("\t\t\t").splice(3)[0]; let title = table[0]["0"].split("\t\t\t").splice(3)[0];
if (title != undefined) { if (title != undefined) {
title = title.replaceAll("</a>", ""); title = title.replaceAll("</a>", "");
const id = `${platformName}-${ const id = `${platformName}-${hash(title)}`;
hash(title) const description = table
}`; .filter((row: any) => row["0"].includes("BACKGROUND:"))
const description = table.filter((row : any) => row["0"].includes("BACKGROUND:")).map((row : any) => row["0"]).map((text : any) => text.split("BACKGROUND:")[1].split("Examples of Superforecaster")[0].split("AT A GLANCE")[0].replaceAll("\n\n", "\n").split("\n").slice(3).join(" ").replaceAll(" ", "").replaceAll("<br> ", ""))[0]; .map((row: any) => row["0"])
const options = table.filter((row : any) => "4" in row).map((row : any) => ({ .map((text: any) =>
name: row["2"].split('<span class="qTitle">')[1].replace("</span>", ""), text
.split("BACKGROUND:")[1]
.split("Examples of Superforecaster")[0]
.split("AT A GLANCE")[0]
.replaceAll("\n\n", "\n")
.split("\n")
.slice(3)
.join(" ")
.replaceAll(" ", "")
.replaceAll("<br> ", "")
)[0];
const options = table
.filter((row: any) => "4" in row)
.map((row: any) => ({
name: row["2"]
.split('<span class="qTitle">')[1]
.replace("</span>", ""),
probability: Number(row["3"].split("%")[0]) / 100, probability: Number(row["3"].split("%")[0]) / 100,
type: "PROBABILITY" type: "PROBABILITY",
})); }));
let analysis = table.filter((row : any) => row[0] ? row[0].toLowerCase().includes("commentary") : false); let analysis = table.filter((row: any) =>
row[0] ? row[0].toLowerCase().includes("commentary") : false
);
// "Examples of Superforecaster Commentary" / Analysis // "Examples of Superforecaster Commentary" / Analysis
// The following is necessary twice, because we want to check if there is an empty list, and then get the first element of the first element of the list. // The following is necessary twice, because we want to check if there is an empty list, and then get the first element of the first element of the list.
analysis = analysis ? analysis[0] : ""; analysis = analysis ? analysis[0] : "";
analysis = analysis ? analysis[0] : ""; analysis = analysis ? analysis[0] : ""; // not a duplicate
// not a duplicate
// console.log(analysis) // console.log(analysis)
let standardObj: FetchedQuestion = { let standardObj: FetchedQuestion = {
id, id,
@ -86,14 +104,16 @@ export const goodjudgment: Platform = {
options, options,
qualityindicators: {}, qualityindicators: {},
extra: { extra: {
superforecastercommentary: analysis || "" superforecastercommentary: analysis || "",
} },
}; };
results.push(standardObj); results.push(standardObj);
} }
} }
console.log("Failing is not unexpected; see utils/pullSuperforecastsManually.sh/js"); console.log(
"Failing is not unexpected; see utils/pullSuperforecastsManually.sh/js"
);
return results; return results;
}, },
@ -104,5 +124,5 @@ export const goodjudgment: Platform = {
let starsDecimal = average([nuno()]); //, eli(), misha()]) let starsDecimal = average([nuno()]); //, eli(), misha()])
let starsInteger = Math.round(starsDecimal); let starsInteger = Math.round(starsDecimal);
return starsInteger; return starsInteger;
} },
}; };

View File

@ -7,7 +7,6 @@ import {applyIfSecretExists} from "../utils/getSecrets";
import { sleep } from "../utils/sleep"; import { sleep } from "../utils/sleep";
import toMarkdown from "../utils/toMarkdown"; import toMarkdown from "../utils/toMarkdown";
import { FetchedQuestion, Platform } from "./"; import { FetchedQuestion, Platform } from "./";
import {FullQuestionOption} from "../../common/types";
/* Definitions */ /* Definitions */
const platformName = "goodjudgmentopen"; const platformName = "goodjudgmentopen";
@ -18,26 +17,19 @@ const annoyingPromptUrls = [
"https://www.gjopen.com/questions/1779-are-there-any-forecasting-tips-tricks-and-experiences-you-would-like-to-share-and-or-discuss-with-your-fellow-forecasters", "https://www.gjopen.com/questions/1779-are-there-any-forecasting-tips-tricks-and-experiences-you-would-like-to-share-and-or-discuss-with-your-fellow-forecasters",
"https://www.gjopen.com/questions/2246-are-there-any-forecasting-tips-tricks-and-experiences-you-would-like-to-share-and-or-discuss-with-your-fellow-forecasters-2022-thread", "https://www.gjopen.com/questions/2246-are-there-any-forecasting-tips-tricks-and-experiences-you-would-like-to-share-and-or-discuss-with-your-fellow-forecasters-2022-thread",
"https://www.gjopen.com/questions/2237-what-forecasting-questions-should-we-ask-what-questions-would-you-like-to-forecast-on-gjopen", "https://www.gjopen.com/questions/2237-what-forecasting-questions-should-we-ask-what-questions-would-you-like-to-forecast-on-gjopen",
"https://www.gjopen.com/questions/2437-what-forecasting-questions-should-we-ask-what-questions-would-you-like-to-forecast-on-gjopen"
]; ];
const DEBUG_MODE: "on" | "off" = "off"; // "on" const DEBUG_MODE: "on" | "off" = "off"; // "on"
const id = () => 0; const id = () => 0;
/* Support functions */ /* Support functions */
function cleanDescription(text : string) {
let md = toMarkdown(text);
let result = md.replaceAll("---", "-").replaceAll(" ", " ");
return result;
}
async function fetchPage(page: number, cookie: string) { async function fetchPage(page: number, cookie: string) {
const response: string = await axios({ const response: string = await axios({
url: htmlEndPoint + page, url: htmlEndPoint + page,
method: "GET", method: "GET",
headers: { headers: {
Cookie: cookie Cookie: cookie,
} },
}).then((res) => res.data); }).then((res) => res.data);
//console.log(response) //console.log(response)
return response; return response;
@ -48,68 +40,102 @@ async function fetchStats(questionUrl : string, cookie : string) {
url: questionUrl + "/stats", url: questionUrl + "/stats",
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "text/html",
Cookie: cookie, Cookie: cookie,
Referer: questionUrl Referer: questionUrl,
} },
}).then((res) => res.data); }).then((res) => res.data);
//console.log(response)
if (response.includes("Sign up or sign in to forecast")) { // Is binary?
throw Error("Not logged in"); let isbinary = response.includes("binary?&quot;:true");
}
// Init
let options: FullQuestionOption[] = [];
// Parse the embedded json let options: FetchedQuestion["options"] = [];
if (isbinary) {
// Crowd percentage
let htmlElements = response.split("\n"); let htmlElements = response.split("\n");
let jsonLines = htmlElements.filter((element) => element.includes("data-react-props")); let h3Element = htmlElements.filter((str) => str.includes("<h3>"))[0];
let embeddedJsons = jsonLines.map((jsonLine, i) => { // console.log(h3Element)
let innerJSONasHTML = jsonLine.split('data-react-props="')[1].split('"')[0]; let crowdpercentage = h3Element.split(">")[1].split("<")[0];
let json = JSON.parse(innerJSONasHTML.replaceAll("&quot;", '"')); let probability = Number(crowdpercentage.replace("%", "")) / 100;
return json; options.push(
}); {
let firstEmbeddedJson = embeddedJsons[0]; name: "Yes",
let title = firstEmbeddedJson.question.name; probability: probability,
let description = cleanDescription(firstEmbeddedJson.question.description); type: "PROBABILITY",
let comments_count = firstEmbeddedJson.question.comments_count; },
let numforecasters = firstEmbeddedJson.question.predictors_count; {
let numforecasts = firstEmbeddedJson.question.prediction_sets_count; name: "No",
let questionType = firstEmbeddedJson.question.type; probability: +(1 - probability).toFixed(2), // avoids floating point shenanigans
if (questionType.includes("Binary") || questionType.includes("NonExclusiveOpinionPoolQuestion") || questionType.includes("Forecast::Question") || ! questionType.includes("Forecast::MultiTimePeriodQuestion")) { type: "PROBABILITY",
options = firstEmbeddedJson.question.answers.map((answer : any) => ({name: answer.name, probability: answer.normalized_probability, type: "PROBABILITY"}));
if (options.length == 1 && options[0].name == "Yes") {
let probabilityNo = options[0].probability > 1 ? 1 - options[0].probability / 100 : 1 - options[0].probability;
options.push({name: "No", probability: probabilityNo, type: "PROBABILITY"});
} }
);
} else {
let optionsHtmlElement = "<table" + response.split("tbody")[1] + "table>";
let tablesAsJson = Tabletojson.convert(optionsHtmlElement);
let firstTable = tablesAsJson[0];
options = firstTable.map((element: any) => ({
name: element["0"],
probability: Number(element["1"].replace("%", "")) / 100,
type: "PROBABILITY",
}));
//console.log(optionsHtmlElement)
//console.log(options)
} }
// Description
let descriptionraw = response.split(
`<div id="question-background" class="collapse smb">`
)[1];
let descriptionprocessed1 = descriptionraw.split(`</div>`)[0];
let descriptionprocessed2 = toMarkdown(descriptionprocessed1);
let descriptionprocessed3 = descriptionprocessed2
.split("\n")
.filter((string) => !string.includes("Confused? Check our"))
.join("\n");
let description = descriptionprocessed3;
// Number of forecasts
let numforecasts = response
.split("prediction_sets_count&quot;:")[1]
.split(",")[0];
//console.log(numforecasts)
// Number of predictors
let numforecasters = response
.split("predictors_count&quot;:")[1]
.split(",")[0];
//console.log(numpredictors)
let result = { let result = {
description: description, description,
options: options, options,
qualityindicators: { qualityindicators: {
numforecasts: Number(numforecasts), numforecasts: Number(numforecasts),
numforecasters: Number(numforecasters), numforecasters: Number(numforecasters),
comments_count: Number(comments_count) },
} // this mismatches the code below, and needs to be fixed, but I'm doing typescript conversion and don't want to touch any logic for now
}; } as any;
// console.log(JSON.stringify(result, null, 4));
return result; return result;
} }
function isSignedIn(html: string) { function isSignedIn(html: string) {
let isSignedInBool = !(html.includes("You need to sign in or sign up before continuing") || html.includes("Sign up")); let isSignedInBool = !(
html.includes("You need to sign in or sign up before continuing") ||
html.includes("Sign up")
);
// console.log(html) // console.log(html)
if (!isSignedInBool) { if (!isSignedInBool) {
console.log("Error: Not signed in."); console.log("Error: Not signed in.");
} }
console.log(`is signed in? ${ console.log(`is signed in? ${isSignedInBool ? "yes" : "no"}`);
isSignedInBool ? "yes" : "no"
}`);
return isSignedInBool; return isSignedInBool;
} }
function reachedEnd(html: string) { function reachedEnd(html: string) {
let reachedEndBool = html.includes("No questions match your filter"); let reachedEndBool = html.includes("No questions match your filter");
if (reachedEndBool) { // console.log(html) if (reachedEndBool) {
//console.log(html)
} }
console.log(`Reached end? ${reachedEndBool}`); console.log(`Reached end? ${reachedEndBool}`);
return reachedEndBool; return reachedEndBool;
@ -124,7 +150,6 @@ async function goodjudgmentopen_inner(cookie : string) {
let results = []; let results = [];
let init = Date.now(); let init = Date.now();
// console.log("Downloading... This might take a couple of minutes. Results will be shown.") // console.log("Downloading... This might take a couple of minutes. Results will be shown.")
console.log("Page #1")
while (!reachedEnd(response) && isSignedIn(response)) { while (!reachedEnd(response) && isSignedIn(response)) {
let htmlLines = response.split("\n"); let htmlLines = response.split("\n");
DEBUG_MODE == "on" ? htmlLines.forEach((line) => console.log(line)) : id(); DEBUG_MODE == "on" ? htmlLines.forEach((line) => console.log(line)) : id();
@ -139,11 +164,12 @@ async function goodjudgmentopen_inner(cookie : string) {
await sleep(1000 + Math.random() * 1000); // don't be as noticeable await sleep(1000 + Math.random() * 1000); // don't be as noticeable
try { try {
let moreinfo = await fetchStats(url, cookie); let moreinfo = await fetchStats(url, cookie);
/*if (moreinfo.isbinary) { if (moreinfo.isbinary) {
if (! moreinfo.crowdpercentage) { // then request again. if (!moreinfo.crowdpercentage) {
// then request again.
moreinfo = await fetchStats(url, cookie); moreinfo = await fetchStats(url, cookie);
} }
}*/ }
let questionNumRegex = new RegExp("questions/([0-9]+)"); let questionNumRegex = new RegExp("questions/([0-9]+)");
const questionNumMatch = url.match(questionNumRegex); const questionNumMatch = url.match(questionNumRegex);
if (!questionNumMatch) { if (!questionNumMatch) {
@ -156,19 +182,19 @@ async function goodjudgmentopen_inner(cookie : string) {
title: title, title: title,
url: url, url: url,
platform: platformName, platform: platformName,
... moreinfo ...moreinfo,
}; };
if (j % 30 == 0 || DEBUG_MODE == "on") { if (j % 30 == 0 || DEBUG_MODE == "on") {
console.log(`Page #${i}`); console.log(`Page #${i}`);
console.log(question); console.log(question);
} else {
console.log(question.title)
} }
// console.log(question) // console.log(question)
results.push(question); results.push(question);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
console.log(`We encountered some error when fetching the URL: ${url}, so it won't appear on the final json`); console.log(
`We encountered some error when fetching the URL: ${url}, so it won't appear on the final json`
);
} }
} }
j = j + 1; j = j + 1;
@ -181,7 +207,9 @@ async function goodjudgmentopen_inner(cookie : string) {
response = await fetchPage(i, cookie); response = await fetchPage(i, cookie);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
console.log(`We encountered some error when fetching page #${i}, so it won't appear on the final json`); console.log(
`We encountered some error when fetching page #${i}, so it won't appear on the final json`
);
} }
} }
@ -192,11 +220,9 @@ async function goodjudgmentopen_inner(cookie : string) {
let end = Date.now(); let end = Date.now();
let difference = end - init; let difference = end - init;
console.log(`Took ${ console.log(
difference / 1000 `Took ${difference / 1000} seconds, or ${difference / (1000 * 60)} minutes.`
} seconds, or ${ );
difference / (1000 * 60)
} minutes.`);
return results; return results;
} }
@ -211,15 +237,20 @@ export const goodjudgmentopen: Platform = {
return (await applyIfSecretExists(cookie, goodjudgmentopen_inner)) || null; return (await applyIfSecretExists(cookie, goodjudgmentopen_inner)) || null;
}, },
calculateStars(data) { calculateStars(data) {
let minProbability = Math.min(...data.options.map((option) => option.probability || 0)); let minProbability = Math.min(
let maxProbability = Math.max(...data.options.map((option) => option.probability || 0)); ...data.options.map((option) => option.probability || 0)
);
let maxProbability = Math.max(
...data.options.map((option) => option.probability || 0)
);
let nuno = () => ((data.qualityindicators.numforecasts || 0) > 100 ? 3 : 2); let nuno = () => ((data.qualityindicators.numforecasts || 0) > 100 ? 3 : 2);
let eli = () => 3; let eli = () => 3;
let misha = () => minProbability > 0.1 || maxProbability < 0.9 ? 3.1 : 2.5; let misha = () =>
minProbability > 0.1 || maxProbability < 0.9 ? 3.1 : 2.5;
let starsDecimal = average([nuno(), eli(), misha()]); let starsDecimal = average([nuno(), eli(), misha()]);
let starsInteger = Math.round(starsDecimal); let starsInteger = Math.round(starsDecimal);
return starsInteger; return starsInteger;
} },
}; };

View File

@ -2,8 +2,9 @@ import axios from "axios";
import { Question } from "@prisma/client"; import { Question } from "@prisma/client";
import { AlgoliaQuestion, questionToAlgoliaQuestion } from "../utils/algolia"; import { prisma } from "../database/prisma";
import { FetchedQuestion, Platform, prepareQuestion, upsertSingleQuestion } from "./"; import { AlgoliaQuestion } from "../utils/algolia";
import { FetchedQuestion, Platform, prepareQuestion } from "./";
/* Definitions */ /* Definitions */
const searchEndpoint = const searchEndpoint =
@ -22,7 +23,7 @@ const modelToQuestion = (model: any): ReturnType<typeof prepareQuestion> => {
title: model.name, title: model.name,
url: `https://www.getguesstimate.com/models/${model.id}`, url: `https://www.getguesstimate.com/models/${model.id}`,
// timestamp, // timestamp,
description: description || "", description,
options: [], options: [],
qualityindicators: { qualityindicators: {
numforecasts: 1, numforecasts: 1,
@ -54,11 +55,10 @@ async function search(query: string): Promise<AlgoliaQuestion[]> {
const models: any[] = response.data.hits; const models: any[] = response.data.hits;
const mappedModels: AlgoliaQuestion[] = models.map((model) => { const mappedModels: AlgoliaQuestion[] = models.map((model) => {
const q = modelToQuestion(model); const q = modelToQuestion(model);
return questionToAlgoliaQuestion({ return {
...q, ...q,
fetched: new Date(), timestamp: String(q.timestamp),
firstSeen: new Date(), };
});
}); });
// filter for duplicates. Surprisingly common. // filter for duplicates. Surprisingly common.
@ -76,8 +76,12 @@ async function search(query: string): Promise<AlgoliaQuestion[]> {
const fetchQuestion = async (id: number): Promise<Question> => { const fetchQuestion = async (id: number): Promise<Question> => {
const response = await axios({ url: `${apiEndpoint}/spaces/${id}` }); const response = await axios({ url: `${apiEndpoint}/spaces/${id}` });
const q = modelToQuestion(response.data); let q = modelToQuestion(response.data);
return await upsertSingleQuestion(q); return await prisma.question.upsert({
where: { id: q.id },
create: q,
update: q,
});
}; };
export const guesstimate: Platform & { export const guesstimate: Platform & {
@ -90,5 +94,5 @@ export const guesstimate: Platform & {
search, search,
version: "v1", version: "v1",
fetchQuestion, fetchQuestion,
calculateStars: (q) => (q.description?.length > 250 ? 2 : 1), calculateStars: (q) => (q.description.length > 250 ? 2 : 1),
}; };

View File

@ -28,13 +28,9 @@ export interface QualityIndicators {
export type FetchedQuestion = Omit< export type FetchedQuestion = Omit<
Question, Question,
| "extra" "extra" | "qualityindicators" | "timestamp" | "platform" | "options"
| "qualityindicators"
| "fetched"
| "firstSeen"
| "platform"
| "options"
> & { > & {
timestamp?: Date;
extra?: object; // required in DB but annoying to return empty; also this is slightly stricter than Prisma's JsonValue extra?: object; // required in DB but annoying to return empty; also this is slightly stricter than Prisma's JsonValue
options: QuestionOption[]; // stronger type than Prisma's JsonValue options: QuestionOption[]; // stronger type than Prisma's JsonValue
qualityindicators: Omit<QualityIndicators, "stars">; // slightly stronger type than Prisma's JsonValue qualityindicators: Omit<QualityIndicators, "stars">; // slightly stronger type than Prisma's JsonValue
@ -82,9 +78,8 @@ export type Platform<ArgNames extends string = ""> = {
// So here we build a new type which should be ok to use both in place of prisma's Question type and as an input to its update or create methods. // So here we build a new type which should be ok to use both in place of prisma's Question type and as an input to its update or create methods.
type PreparedQuestion = Omit< type PreparedQuestion = Omit<
Question, Question,
"extra" | "qualityindicators" | "options" | "fetched" | "firstSeen" "extra" | "qualityindicators" | "options"
> & { > & {
fetched: Date;
extra: NonNullable<Question["extra"]>; extra: NonNullable<Question["extra"]>;
qualityindicators: NonNullable<Question["qualityindicators"]>; qualityindicators: NonNullable<Question["qualityindicators"]>;
options: NonNullable<Question["options"]>; options: NonNullable<Question["options"]>;
@ -96,8 +91,8 @@ export const prepareQuestion = (
): PreparedQuestion => { ): PreparedQuestion => {
return { return {
extra: {}, extra: {},
timestamp: new Date(),
...q, ...q,
fetched: new Date(),
platform: platform.name, platform: platform.name,
qualityindicators: { qualityindicators: {
...q.qualityindicators, ...q.qualityindicators,
@ -106,20 +101,6 @@ export const prepareQuestion = (
}; };
}; };
export const upsertSingleQuestion = async (
q: PreparedQuestion
): Promise<Question> => {
return await prisma.question.upsert({
where: { id: q.id },
create: {
...q,
firstSeen: new Date(),
},
update: q,
});
// TODO - update history?
};
export const processPlatform = async <T extends string = "">( export const processPlatform = async <T extends string = "">(
platform: Platform<T>, platform: Platform<T>,
args?: { [k in T]: string } args?: { [k in T]: string }
@ -163,9 +144,9 @@ export const processPlatform = async <T extends string = "">(
for (const q of fetchedQuestions.map((q) => prepareQuestion(q, platform))) { for (const q of fetchedQuestions.map((q) => prepareQuestion(q, platform))) {
if (oldIdsSet.has(q.id)) { if (oldIdsSet.has(q.id)) {
// TODO - check if question has changed for better performance
updatedQuestions.push(q); updatedQuestions.push(q);
} else { } else {
// TODO - check if question has changed for better performance
createdQuestions.push(q); createdQuestions.push(q);
} }
} }
@ -173,10 +154,7 @@ export const processPlatform = async <T extends string = "">(
const stats: { created?: number; updated?: number; deleted?: number } = {}; const stats: { created?: number; updated?: number; deleted?: number } = {};
await prisma.question.createMany({ await prisma.question.createMany({
data: createdQuestions.map((q) => ({ data: createdQuestions,
...q,
firstSeen: new Date(),
})),
}); });
stats.created = createdQuestions.length; stats.created = createdQuestions.length;

View File

@ -1,339 +0,0 @@
/* Imports */
import {or} from "ajv/dist/compile/codegen";
import axios from "axios";
import {FetchedQuestion, Platform} from ".";
import {QuestionOption} from "../../common/types";
import toMarkdown from "../utils/toMarkdown";
import { average } from "../../utils";
/* Definitions */
const platformName = "insight";
const marketsEnpoint = "https://insightprediction.com/api/markets?orderBy=is_resolved&sortedBy=asc";
const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/markets/${id}`;
const SPORTS_CATEGORIES = [
'World Cup',
'MLB',
'Futures',
'Sports',
'EPL',
'Golf',
'NHL',
'College Football'
]
/* Support functions */
// Stubs
const excludeMarketFromTitle = (title : any) => {
if (!!title) {
return title.includes(" vs ") || title.includes(" Over: ") || title.includes("NFL") || title.includes("Will there be a first time winner") || title.includes("Premier League")
} else {
return true
}
}
const hasActiveYesNoOrderBook = (orderbook : any) => {
if (!!orderbook) {
let yes = !!orderbook.yes && !!orderbook.yes.buy && Array.isArray(orderbook.yes.buy) && orderbook.yes.buy.length != 0 && !!orderbook.yes.buy[0].price && !!orderbook.yes.sell && Array.isArray(orderbook.yes.sell) && orderbook.yes.sell.length != 0 && !!orderbook.yes.sell[0].price
let no = !!orderbook.no && !!orderbook.no.buy && Array.isArray(orderbook.no.buy) && orderbook.no.buy.length != 0 && !!orderbook.no.buy[0].price && !!orderbook.no.sell && Array.isArray(orderbook.no.sell) && orderbook.no.sell.length != 0 && !!orderbook.no.sell[0].price
return yes && no
} else {
return false
}
}
const isBinaryQuestion = (data : any) => Array.isArray(data) && data.length == 1
const geomMean = (a : number, b : number) => Math.sqrt(a * b)
const processRelativeUrls = (a : string) => a.replaceAll("] (/", "](http://insightprediction.com/").replaceAll("](/", "](http://insightprediction.com/")
const processDescriptionText = (text : any) => {
if (typeof text === 'string') {
return processRelativeUrls(toMarkdown(text))
} else {
return ""
}
}
const getOrderbookPrize = (orderbook : any) => {
let yes_min_cents = orderbook.yes.buy[0].price
let yes_max_cents = orderbook.yes.sell[0].price
let yes_min = Number(yes_min_cents.slice(0, -1))
let yes_max = Number(yes_max_cents.slice(0, -1))
let yes_price_orderbook = geomMean(yes_min, yes_max)
return yes_price_orderbook
}
const getAnswerProbability = (answer : any) => {
let orderbook = answer.orderbook
let latest_yes_price = answer.latest_yes_price
if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) {
let yes_price_orderbook = getOrderbookPrize(orderbook)
let yes_probability = (latest_yes_price ? geomMean(latest_yes_price, yes_price_orderbook) : yes_price_orderbook) / 100
return yes_probability
} else if (!! latest_yes_price) {
return latest_yes_price / 100
} else {
return -1
}
}
// Fetching
async function fetchPage(bearer: string, pageNum: number) {
let pageUrl = `${marketsEnpoint}&page=${pageNum}`
const response = await axios({
url: pageUrl, // &orderBy=is_resolved&sortedBy=desc`,
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${bearer}`
}
}).then((res) => res.data);
// console.log(response);
return response;
}
async function fetchMarket(bearer: string, marketId: number) {
const response = await axios({
url: getMarketEndpoint(marketId),
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${bearer}`
}
}).then((res) => res.data);
// console.log(response)
return response;
}
const processMarket = (market : any) => {
let options: FetchedQuestion["options"] = []
if (!!market && !!market.answer && !!market.answer.data) {
let data = market.answer.data
if (isBinaryQuestion(data)) { // Binary questions
let answer = data[0]
let probability = getAnswerProbability(answer)
if (probability != -1) {
options = [
{
name: "Yes",
probability: probability,
type: "PROBABILITY"
}, {
name: "No",
probability: 1 - probability,
type: "PROBABILITY"
},
];
}
} else { // non binary question
for (let answer of data) {
let probability = getAnswerProbability(answer)
if (probability != -1) {
let newOption: QuestionOption = ({
name: String(answer.title),
probability: probability,
type: "PROBABILITY"
});
options.push(newOption)
}
}
}
if (!! options && Array.isArray(options) && options.length > 0) {
const id = `${platformName}-${
market.id
}`
const result: FetchedQuestion = {
id: id,
title: market.title,
url: market.url,
description: processDescriptionText(market.rules),
options,
qualityindicators: market.coin_id == "USD" ? (
{volume: market.volume}
) : ({})
};
return result;
}
}
return null
}
async function fetchAllMarkets(bearer: string) {
let pageNum = 1
let markets = []
let categories = []
let isEnd = false
while (! isEnd) {
if(pageNum % 20 == 0){
console.log(`Fetching page #${pageNum}`) // : ${pageUrl}
}
let page = await fetchPage(bearer, pageNum)
// console.log(JSON.stringify(page, null, 2))
let data = page.data
if (!! data && Array.isArray(data) && data.length > 0) {
let lastMarket = data[data.length - 1]
let isLastMarketResolved = lastMarket.is_resolved
if (isLastMarketResolved == true) {
isEnd = true
}
let newMarkets = data.filter(market => !market.is_resolved && !market.is_expired && ! excludeMarketFromTitle(market.title))
for (let initMarketData of newMarkets) {
let fullMarketDataResponse = await fetchMarket(bearer, initMarketData.id)
let fullMarketData = fullMarketDataResponse.data
let processedMarketData = processMarket(fullMarketData)
if (processedMarketData != null && ! SPORTS_CATEGORIES.includes(fullMarketData.category)) {
console.log(`- Adding: ${
fullMarketData.title
}`)
console.group()
console.log(fullMarketData)
console.log(JSON.stringify(processedMarketData, null, 2))
console.groupEnd()
markets.push(processedMarketData)
}
let category = fullMarketData.category
categories.push(category)
}
} else {
isEnd = true
} pageNum = pageNum + 1
}
console.log(markets)
console.log(categories)
return markets
}
/*
async function fetchQuestionStats(bearer : string, marketId : number) {
const response = await axios({
url: getMarketEndpoint(marketId),
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${bearer}`
}
}).then((res) => res.data);
// console.log(response)
return response;
}
async function fetchData(bearer : string) {
let pageNum = 1;
let reachedEnd = false;
let results = [];
while (! reachedEnd) {
let newPage = await fetchPage(bearer, pageNum);
let newPageData = newPage.data;
let marketsFromPage = []
for (let market of newPageData) {
let response = await fetchQuestionStats(bearer, market.id);
let marketData = response.data
let marketAnswer = marketData.answer.data
delete marketData.answer
// These are the options and their prices.
let marketOptions = marketAnswer.map(answer => {
return({name: answer.title, probability: answer.latest_yes_price, type: "PROBABILITY"})
})
marketsFromPage.push({
... marketData,
options: marketOptions
});
}
let finalObject = marketsFromPage
console.log(`Page = #${pageNum}`);
// console.log(newPageData)
console.dir(finalObject, {depth: null});
results.push(... finalObject);
let newPagination = newPage.meta.pagination;
if (newPagination.total_pages == pageNum) {
reachedEnd = true;
} else {
pageNum = pageNum + 1;
}
}
return results
}
async function processPredictions(predictions : any[]) {
let results = await predictions.map((prediction) => {
const id = `${platformName}-${
prediction.id
}`;
const probability = prediction.probability;
const options: FetchedQuestion["options"] = [
{
name: "Yes",
probability: probability,
type: "PROBABILITY"
}, {
name: "No",
probability: 1 - probability,
type: "PROBABILITY"
},
];
const result: FetchedQuestion = {
id,
title: prediction.title,
url: "https://example.com",
description: prediction.description,
options,
qualityindicators: {
// other: prediction.otherx,
// indicators: prediction.indicatorx,
}
};
return result;
});
return results; // resultsProcessed
}
*/
/* Body */
export const insight: Platform = {
name: platformName,
label: "Insight Prediction",
color: "#ff0000",
version: "v1",
async fetcher() {
let bearer = process.env.INSIGHT_BEARER;
if (!! bearer) {
let data = await fetchAllMarkets(bearer);
return data
} else {
throw Error("No INSIGHT_BEARER available in environment")
}
// let results: FetchedQuestion[] = []; // await processPredictions(data); // somehow needed
// return results;
},
calculateStars(data) {
let nuno = () => {
if((data.qualityindicators.volume || 0) > 10000){
return 4
} else if((data.qualityindicators.volume || 0) > 1000){
return 3
} else{
return 2
}
}
let eli = () => null;
let misha = () => null;
let starsDecimal = average([nuno()]); //, eli(data), misha(data)])
let starsInteger = Math.round(starsDecimal);
return starsInteger;
}
};

View File

@ -6,12 +6,12 @@ import { FetchedQuestion, Platform } from "./";
/* Definitions */ /* Definitions */
const platformName = "manifold"; const platformName = "manifold";
const ENDPOINT = "https://manifold.markets/api/v0/markets"; const endpoint = "https://manifold.markets/api/v0/markets";
// See https://manifoldmarkets.notion.site/Manifold-Markets-API-5e7d0aef4dcf452bb04b319e178fabc5 // See https://manifoldmarkets.notion.site/Manifold-Markets-API-5e7d0aef4dcf452bb04b319e178fabc5
/* Support functions */ /* Support functions */
async function fetchPage(endpoint: string) { async function fetchData() {
let response = await axios({ let response = await axios({
url: endpoint, url: endpoint,
method: "GET", method: "GET",
@ -23,31 +23,6 @@ async function fetchPage(endpoint: string) {
return response; return response;
} }
async function fetchAllData(){
let endpoint = ENDPOINT
let end = false
let allData = []
let counter = 1
while(!end){
console.log(`Query #${counter}: ${endpoint}`)
let newData = await fetchPage(endpoint)
if(Array.isArray(newData)){
allData.push(...newData)
let hasReachedEnd = (newData.length == 0) || (newData[newData.length -1] == undefined) || (newData[newData.length -1].id == undefined)
if(!hasReachedEnd){
let lastId = newData[newData.length -1].id
endpoint = `${ENDPOINT}?before=${lastId}`
}else{
end = true
}
}else{
end = true
}
counter = counter +1
}
return allData
}
function showStatistics(results: FetchedQuestion[]) { function showStatistics(results: FetchedQuestion[]) {
console.log(`Num unresolved markets: ${results.length}`); console.log(`Num unresolved markets: ${results.length}`);
let sum = (arr: number[]) => arr.reduce((tally, a) => tally + a, 0); let sum = (arr: number[]) => arr.reduce((tally, a) => tally + a, 0);
@ -88,11 +63,11 @@ function processPredictions(predictions: any[]): FetchedQuestion[] {
id: id, id: id,
title: prediction.question, title: prediction.question,
url: prediction.url, url: prediction.url,
description: prediction.description || "", description: prediction.description,
options, options,
qualityindicators: { qualityindicators: {
createdTime: prediction.createdTime, createdTime: prediction.createdTime,
// volume7Days: prediction.volume7Days, // deprecated. volume7Days: prediction.volume7Days,
volume24Hours: prediction.volume24Hours, volume24Hours: prediction.volume24Hours,
pool: prediction.pool, // normally liquidity, but I don't actually want to show it. pool: prediction.pool, // normally liquidity, but I don't actually want to show it.
}, },
@ -115,16 +90,16 @@ export const manifold: Platform = {
color: "#793466", color: "#793466",
version: "v1", version: "v1",
async fetcher() { async fetcher() {
let data = await fetchAllData(); let data = await fetchData();
let results = processPredictions(data); // somehow needed let results = processPredictions(data); // somehow needed
showStatistics(results); showStatistics(results);
return results; return results;
}, },
calculateStars(data) { calculateStars(data) {
let nuno = () => let nuno = () =>
(data.qualityindicators.volume24Hours || 0) > 100 || (data.qualityindicators.volume7Days || 0) > 250 ||
((data.qualityindicators.pool || 0) > 500 && ((data.qualityindicators.pool || 0) > 500 &&
(data.qualityindicators.volume24Hours || 0) > 50) (data.qualityindicators.volume7Days || 0) > 100)
? 2 ? 2
: 1; : 1;
let eli = () => null; let eli = () => null;

View File

@ -0,0 +1,205 @@
/* Imports */
import axios from "axios";
import { average } from "../../utils";
import { sleep } from "../utils/sleep";
import toMarkdown from "../utils/toMarkdown";
import { FetchedQuestion, Platform } from "./";
/* Definitions */
const platformName = "metaculus";
let now = new Date().toISOString();
let DEBUG_MODE = "off";
let SLEEP_TIME = 5000;
/* Support functions */
async function fetchMetaculusQuestions(next: string) {
// Numbers about a given address: how many, how much, at what price, etc.
let response;
let data;
try {
response = await axios({
url: next,
method: "GET",
headers: { "Content-Type": "application/json" },
});
data = response.data;
} catch (error) {
console.log(`Error in async function fetchMetaculusQuestions(next)`);
console.log(error);
if (axios.isAxiosError(error)) {
if (error.response?.headers["retry-after"]) {
const timeout = error.response.headers["retry-after"];
console.log(`Timeout: ${timeout}`);
await sleep(Number(timeout) * 1000 + SLEEP_TIME);
} else {
await sleep(SLEEP_TIME);
}
}
} finally {
try {
response = await axios({
url: next,
method: "GET",
headers: { "Content-Type": "application/json" },
});
data = response.data;
} catch (error) {
console.log(error);
return { results: [] };
}
}
// console.log(response)
return data;
}
async function fetchMetaculusQuestionDescription(slug: string) {
try {
let response = await axios({
method: "get",
url: "https://www.metaculus.com" + slug,
}).then((response) => response.data);
return response;
} catch (error) {
console.log(`Error in: fetchMetaculusQuestionDescription`);
console.log(
`We encountered some error when attempting to fetch a metaculus page. Trying again`
);
if (
axios.isAxiosError(error) &&
typeof error.response != "undefined" &&
typeof error.response.headers != "undefined" &&
typeof error.response.headers["retry-after"] != "undefined"
) {
const timeout = error.response.headers["retry-after"];
console.log(`Timeout: ${timeout}`);
await sleep(Number(timeout) * 1000 + SLEEP_TIME);
} else {
await sleep(SLEEP_TIME);
}
try {
let response = await axios({
method: "get",
url: "https://www.metaculus.com" + slug,
}).then((response) => response.data);
// console.log(response)
return response;
} catch (error) {
console.log(
`We encountered some error when attempting to fetch a metaculus page.`
);
console.log("Error", error);
throw "Giving up";
}
}
}
export const metaculus: Platform = {
name: platformName,
label: "Metaculus",
color: "#006669",
version: "v1",
async fetcher() {
// let metaculusQuestionsInit = await fetchMetaculusQuestions(1)
// let numQueries = Math.round(Number(metaculusQuestionsInit.count) / 20)
// console.log(`Downloading... This might take a while. Total number of queries: ${numQueries}`)
// for (let i = 4; i <= numQueries; i++) { // change numQueries to 10 if one want to just test }
let all_questions = [];
let next = "https://www.metaculus.com/api2/questions/";
let i = 1;
while (next) {
if (i % 20 == 0) {
console.log("Sleeping for 500ms");
await sleep(SLEEP_TIME);
}
console.log(`\nQuery #${i}`);
let metaculusQuestions = await fetchMetaculusQuestions(next);
let results = metaculusQuestions.results;
let j = false;
for (let result of results) {
if (result.publish_time < now && now < result.resolve_time) {
await sleep(SLEEP_TIME / 2);
let questionPage = await fetchMetaculusQuestionDescription(
result.page_url
);
if (!questionPage.includes("A public prediction by")) {
// console.log(questionPage)
let descriptionraw = questionPage.split(
`<div class="content" ng-bind-html-compile="qctrl.question.description_html">`
)[1]; //.split(`<div class="question__content">`)[1]
let descriptionprocessed1 = descriptionraw.split("</div>")[0];
let descriptionprocessed2 = toMarkdown(descriptionprocessed1);
let description = descriptionprocessed2;
let isbinary = result.possibilities.type == "binary";
let options: FetchedQuestion["options"] = [];
if (isbinary) {
let probability = Number(result.community_prediction.full.q2);
options = [
{
name: "Yes",
probability: probability,
type: "PROBABILITY",
},
{
name: "No",
probability: 1 - probability,
type: "PROBABILITY",
},
];
}
let id = `${platformName}-${result.id}`;
let interestingInfo: FetchedQuestion = {
id,
title: result.title,
url: "https://www.metaculus.com" + result.page_url,
description,
options,
qualityindicators: {
numforecasts: Number(result.number_of_predictions),
},
extra: {
resolution_data: {
publish_time: result.publish_time,
resolution: result.resolution,
close_time: result.close_time,
resolve_time: result.resolve_time,
},
},
//"status": result.status,
//"publish_time": result.publish_time,
//"close_time": result.close_time,
//"type": result.possibilities.type, // We want binary ones here.
//"last_activity_time": result.last_activity_time,
};
if (Number(result.number_of_predictions) >= 10) {
console.log(`- ${interestingInfo.title}`);
all_questions.push(interestingInfo);
if ((!j && i % 20 == 0) || DEBUG_MODE == "on") {
console.log(interestingInfo);
j = true;
}
}
} else {
console.log("- [Skipping public prediction]");
}
}
}
next = metaculusQuestions.next;
i = i + 1;
}
return all_questions;
},
calculateStars(data) {
const { numforecasts } = data.qualityindicators;
let nuno = () =>
(numforecasts || 0) > 300 ? 4 : (numforecasts || 0) > 100 ? 3 : 2;
let eli = () => 3;
let misha = () => 3;
let starsDecimal = average([nuno(), eli(), misha()]);
let starsInteger = Math.round(starsDecimal);
return starsInteger;
},
};

View File

@ -1,262 +0,0 @@
import Ajv, { JTDDataType, ValidateFunction } from "ajv/dist/jtd";
import axios from "axios";
import { sleep } from "../../utils/sleep";
// Type examples:
// - group: https://www.metaculus.com/api2/questions/9866/
// - claim: https://www.metaculus.com/api2/questions/9668/
// - subquestion forecast: https://www.metaculus.com/api2/questions/10069/
// - basic forecast: https://www.metaculus.com/api2/questions/11005/
const RETRY_SLEEP_TIME = 5000;
const commonProps = {
id: {
type: "uint32",
},
title: {
type: "string",
},
} as const;
const predictableProps = {
publish_time: {
type: "string",
},
close_time: {
type: "string",
},
resolve_time: {
type: "string",
},
resolution: {
type: "float64",
nullable: true,
},
possibilities: {
properties: {
type: {
// Enum["binary", "continuous"], via https://github.com/quantified-uncertainty/metaforecast/pull/84#discussion_r878240875
// but metaculus might add new values in the future and we don't want the fetcher to break
type: "string",
},
},
additionalProperties: true,
},
number_of_predictions: {
type: "uint32",
},
community_prediction: {
properties: {
full: {
// q1/q2/q3 can be missing, e.g. https://www.metaculus.com/api2/questions/1633/
optionalProperties: {
q1: {
type: "float64",
},
q2: {
type: "float64",
},
q3: {
type: "float64",
},
},
additionalProperties: true,
},
},
nullable: true,
additionalProperties: true,
},
} as const;
const pageProps = {
page_url: {
type: "string",
},
group: {
type: "uint32",
nullable: true,
},
} as const;
// these are missing in /api2/questions/ requests, and building two schemas is too much pain
const optionalPageProps = {
description: {
type: "string",
},
description_html: {
type: "string",
},
} as const;
const questionSchema = {
discriminator: "type",
mapping: {
forecast: {
properties: {
...commonProps,
...pageProps,
...predictableProps,
},
optionalProperties: {
...optionalPageProps,
},
additionalProperties: true,
},
group: {
properties: {
...commonProps,
...pageProps,
},
optionalProperties: {
...optionalPageProps,
sub_questions: {
elements: {
properties: {
...commonProps,
...predictableProps,
},
additionalProperties: true,
},
},
},
additionalProperties: true,
},
// we're not interested in claims currently (but we should be?)
claim: {
properties: {
...commonProps,
...pageProps,
},
optionalProperties: {
...optionalPageProps,
},
additionalProperties: true,
},
discussion: {
optionalProperties: {
...optionalPageProps,
},
additionalProperties: true,
},
},
} as const;
const knownQuestionTypes = Object.keys(questionSchema.mapping);
const shallowMultipleQuestionsSchema = {
properties: {
results: {
elements: {
properties: {
type: {
type: "string",
},
},
additionalProperties: true,
},
},
next: {
type: "string",
nullable: true,
},
},
additionalProperties: true,
} as const;
export type ApiCommon = JTDDataType<{
properties: typeof commonProps;
}>;
export type ApiPredictable = JTDDataType<{
properties: typeof predictableProps;
}>;
export type ApiQuestion = JTDDataType<typeof questionSchema>;
type ApiShallowMultipleQuestions = JTDDataType<
typeof shallowMultipleQuestionsSchema
>;
export type ApiMultipleQuestions = {
results: ApiQuestion[];
next: ApiShallowMultipleQuestions["next"]; // Omit<ApiShallowMultipleQuestions, "results"> doesn't work correctly here
};
const validateQuestion = new Ajv().compile<ApiQuestion>(questionSchema);
const validateShallowMultipleQuestions =
new Ajv().compile<ApiShallowMultipleQuestions>(
shallowMultipleQuestionsSchema
);
async function fetchWithRetries<T = unknown>(url: string): Promise<T> {
try {
const response = await axios.get<T>(url);
return response.data;
} catch (error) {
console.log(`Error while fetching ${url}`);
console.log(error);
if (axios.isAxiosError(error)) {
if (error.response?.headers["retry-after"]) {
const timeout = error.response.headers["retry-after"];
console.log(`Timeout: ${timeout}`);
await sleep(Number(timeout) * 1000 + 1000);
} else {
await sleep(RETRY_SLEEP_TIME);
}
}
}
const response = await axios.get<T>(url);
return response.data;
}
const fetchAndValidate = async <T = unknown>(
url: string,
validator: ValidateFunction<T>
): Promise<T> => {
// console.log(url);
const data = await fetchWithRetries<object>(url);
if (validator(data)) {
return data;
}else{
console.log(data)
throw new Error(
`Response validation for url ${url} failed: ` +
JSON.stringify(validator.errors, null, 4)
);
}
};
export async function fetchApiQuestions(
next: string
): Promise<ApiMultipleQuestions> {
const data = await fetchAndValidate(next, validateShallowMultipleQuestions);
const isDefined = <T>(argument: T | undefined): argument is T => {
return argument !== undefined;
};
return {
...data,
results: data.results
.map((result) => {
if (!knownQuestionTypes.includes(result.type)) {
console.warn(`Unknown result type ${result.type}, skipping`);
return undefined;
}
if (!validateQuestion(result)) {
throw new Error(
`Response validation failed: ` +
JSON.stringify(validateQuestion.errors)
);
}
return result;
})
.filter(isDefined),
};
}
export async function fetchSingleApiQuestion(id: number): Promise<ApiQuestion> {
return await fetchAndValidate(
`https://www.metaculus.com/api2/questions/${id}/`,
validateQuestion
);
}

View File

@ -1,220 +0,0 @@
import Error from "next/error";
import {FetchedQuestion, Platform} from "..";
import {average} from "../../../utils";
import {sleep} from "../../utils/sleep";
import {
ApiCommon,
ApiMultipleQuestions,
ApiPredictable,
ApiQuestion,
fetchApiQuestions,
fetchSingleApiQuestion
} from "./api";
const platformName = "metaculus";
const now = new Date().toISOString();
const SLEEP_TIME = 1000;
async function apiQuestionToFetchedQuestions(apiQuestion: ApiQuestion): Promise<FetchedQuestion[]> {
// one item can expand:
// - to 0 questions if we don't want it;
// - to 1 question if it's a simple forecast
// - to multiple questions if it's a group (see https://github.com/quantified-uncertainty/metaforecast/pull/84 for details)
const skip = (q : ApiPredictable) : boolean => {
if (q.publish_time > now || now > q.resolve_time) {
return true;
}
if (q.number_of_predictions < 10) {
return true;
}
return false;
};
const buildFetchedQuestion = (q : ApiPredictable & ApiCommon) : Omit < FetchedQuestion,
"url" | "description" | "title" > => {
const isBinary = q.possibilities.type === "binary";
let options: FetchedQuestion["options"] = [];
if (isBinary) {
const probability = q.community_prediction?.full.q2;
if (probability !== undefined) {
options = [
{
name: "Yes",
probability: probability,
type: "PROBABILITY"
}, {
name: "No",
probability: 1 - probability,
type: "PROBABILITY"
},
];
}
}
return {
id: `${platformName}-${
q.id
}`,
options,
qualityindicators: {
numforecasts: q.number_of_predictions
},
extra: {
resolution_data: {
publish_time: apiQuestion.publish_time,
resolution: apiQuestion.resolution,
close_time: apiQuestion.close_time,
resolve_time: apiQuestion.resolve_time
}
}
};
};
if (apiQuestion.type === "group") {
await sleep(SLEEP_TIME);
let apiQuestionDetailsTemp
try{
apiQuestionDetailsTemp = await fetchSingleApiQuestion(apiQuestion.id);
}catch(error){
console.log(error)
return []
}
const apiQuestionDetails = apiQuestionDetailsTemp
if (apiQuestionDetails.type !== "group") {
console.log("Error: expected `group` type")
return [] //throw new Error("Expected `group` type"); // shouldn't happen, this is mostly for typescript
}else{
try{
let result = (apiQuestionDetails.sub_questions || []).filter((q) => ! skip(q)).map((sq) => {
const tmp = buildFetchedQuestion(sq);
return {
... tmp,
title: `${
apiQuestion.title
} (${
sq.title
})`,
description: apiQuestionDetails.description || "",
url: `https://www.metaculus.com${
apiQuestion.page_url
}?sub-question=${
sq.id
}`
};
});
return result
}catch(error){
console.log(error)
return []
}
}
} else if (apiQuestion.type === "forecast") {
if (apiQuestion.group) {
return []; // sub-question, should be handled on the group level
}
if (skip(apiQuestion)) {
console.log(`- [Skipping]: ${
apiQuestion.title
}`)
/*console.log(`Close time: ${
apiQuestion.close_time
}, resolve time: ${
apiQuestion.resolve_time
}`)*/
return [];
}
await sleep(SLEEP_TIME);
try{
const apiQuestionDetails = await fetchSingleApiQuestion(apiQuestion.id);
const tmp = buildFetchedQuestion(apiQuestion);
return [{
... tmp,
title: apiQuestion.title,
description: apiQuestionDetails.description || "",
url: "https://www.metaculus.com" + apiQuestion.page_url
},];
}catch(error){
console.log(error)
return []
}
} else {
if (apiQuestion.type !== "claim") { // should never happen, since `discriminator` in JTD schema causes a strict runtime check
console.log(`Unknown metaculus question type: ${
(apiQuestion as any).type
}, skipping`);
}
return [];
}
}
export const metaculus: Platform<"id" | "debug"> = {
name: platformName,
label: "Metaculus",
color: "#006669",
version: "v2",
fetcherArgs: [
"id", "debug"
],
async fetcher(opts) {
let allQuestions: FetchedQuestion[] = [];
if (opts.args ?. id) {
try{
console.log("Using optional id arg.")
const id = Number(opts.args.id);
const apiQuestion = await fetchSingleApiQuestion(id);
const questions = await apiQuestionToFetchedQuestions(apiQuestion);
console.log(questions);
return {questions, partial: true};
}catch(error){
console.log(error)
return {questions: [], partial: true};
}
}
let next: string | null = "https://www.metaculus.com/api2/questions/";
let i = 1;
while (next) {
console.log(`\nQuery #${i} - ${next}`);
await sleep(SLEEP_TIME);
const apiQuestions: ApiMultipleQuestions = await fetchApiQuestions(next);
const results = apiQuestions.results;
// console.log(results)
let j = false;
for (const result of results) {
const questions = await apiQuestionToFetchedQuestions(result);
// console.log(questions)
for (const question of questions) {
console.log(`- ${
question.title
}`);
if ((! j && i % 20 === 0) || opts.args ?. debug) {
console.log(question);
j = true;
}
allQuestions.push(question);
}
}
next = apiQuestions.next;
i += 1;
}
return {questions: allQuestions, partial: false};
},
calculateStars(data) {
const {numforecasts} = data.qualityindicators;
const nuno = () => (numforecasts || 0) > 300 ? 4 : (numforecasts || 0) > 100 ? 3 : 2;
const eli = () => 3;
const misha = () => 3;
const starsDecimal = average([nuno(), eli(), misha()]);
const starsInteger = Math.round(starsDecimal);
return starsInteger;
}
};

View File

@ -7,7 +7,6 @@ import { goodjudgmentopen } from "./goodjudgmentopen";
import { guesstimate } from "./guesstimate"; import { guesstimate } from "./guesstimate";
import { Platform, PlatformConfig } from "./index"; import { Platform, PlatformConfig } from "./index";
import { infer } from "./infer"; import { infer } from "./infer";
import { insight } from "./insight";
import { kalshi } from "./kalshi"; import { kalshi } from "./kalshi";
import { manifold } from "./manifold"; import { manifold } from "./manifold";
import { metaculus } from "./metaculus"; import { metaculus } from "./metaculus";
@ -18,9 +17,7 @@ import { smarkets } from "./smarkets";
import { wildeford } from "./wildeford"; import { wildeford } from "./wildeford";
import { xrisk } from "./xrisk"; import { xrisk } from "./xrisk";
// function instead of const array, this helps to fight circular dependencies export const platforms: Platform<string>[] = [
export const getPlatforms = (): Platform<string>[] => {
return [
betfair, betfair,
fantasyscotus, fantasyscotus,
foretold, foretold,
@ -29,7 +26,6 @@ export const getPlatforms = (): Platform<string>[] => {
goodjudgmentopen, goodjudgmentopen,
guesstimate, guesstimate,
infer, infer,
insight,
kalshi, kalshi,
manifold, manifold,
metaculus, metaculus,
@ -40,22 +36,11 @@ export const getPlatforms = (): Platform<string>[] => {
wildeford, wildeford,
xrisk, xrisk,
]; ];
};
let _nameToLabelCache: { [k: string]: string } | undefined;
export function platformNameToLabel(name: string): string {
if (!_nameToLabelCache) {
_nameToLabelCache = Object.fromEntries(
getPlatforms().map((platform) => [platform.name, platform.label])
);
}
return _nameToLabelCache[name] || name;
}
// get frontend-safe version of platforms data // get frontend-safe version of platforms data
export const getPlatformsConfig = (): PlatformConfig[] => { export const getPlatformsConfig = (): PlatformConfig[] => {
const platformsConfig = getPlatforms().map((platform) => ({ const platformsConfig = platforms.map((platform) => ({
name: platform.name, name: platform.name,
label: platform.label, label: platform.label,
color: platform.color, color: platform.color,

View File

@ -162,6 +162,7 @@ async function processEventMarkets(event: any, ctx: Context) {
url: "https://smarkets.com/event/" + market.event_id + market.slug, url: "https://smarkets.com/event/" + market.event_id + market.slug,
description: market.description, description: market.description,
options, options,
timestamp: new Date(),
qualityindicators: {}, qualityindicators: {},
extra: { extra: {
contracts, contracts,

View File

@ -96,8 +96,7 @@ async function processPredictions(
url: prediction["url"], url: prediction["url"],
description: prediction["Notes"] || "", description: prediction["Notes"] || "",
options, options,
//// TODO - use `created` field for this timestamp: new Date(Date.parse(prediction["Prediction Date"] + "Z")),
// timestamp: new Date(Date.parse(prediction["Prediction Date"] + "Z")),
qualityindicators: {}, qualityindicators: {},
}; };
return result; return result;

View File

@ -3,19 +3,16 @@ import algoliasearch from "algoliasearch";
import { Question } from "@prisma/client"; import { Question } from "@prisma/client";
import { prisma } from "../database/prisma"; import { prisma } from "../database/prisma";
import { platformNameToLabel } from "../platforms/registry"; import { platforms } from "../platforms/registry";
const cookie = process.env.ALGOLIA_MASTER_API_KEY || ""; let cookie = process.env.ALGOLIA_MASTER_API_KEY || "";
const algoliaAppId = process.env.NEXT_PUBLIC_ALGOLIA_APP_ID || ""; const algoliaAppId = process.env.NEXT_PUBLIC_ALGOLIA_APP_ID || "";
const client = algoliasearch(algoliaAppId, cookie); const client = algoliasearch(algoliaAppId, cookie);
const index = client.initIndex("metaforecast"); const index = client.initIndex("metaforecast");
export type AlgoliaQuestion = Omit<Question, "fetched" | "firstSeen"> & { export type AlgoliaQuestion = Omit<Question, "timestamp"> & {
fetched: string; timestamp: string;
firstSeen: string;
optionsstringforsearch?: string; optionsstringforsearch?: string;
platformLabel: string;
objectID: string;
}; };
const getoptionsstringforsearch = (record: Question): string => { const getoptionsstringforsearch = (record: Question): string => {
@ -29,23 +26,23 @@ const getoptionsstringforsearch = (record: Question): string => {
return result; return result;
}; };
export const questionToAlgoliaQuestion = (
question: Question
): AlgoliaQuestion => {
return {
...question,
fetched: question.fetched.toISOString(),
firstSeen: question.firstSeen.toISOString(),
platformLabel: platformNameToLabel(question.platform),
objectID: question.id,
optionsstringforsearch: getoptionsstringforsearch(question),
};
};
export async function rebuildAlgoliaDatabase() { export async function rebuildAlgoliaDatabase() {
const questions = await prisma.question.findMany(); const questions = await prisma.question.findMany();
const records: AlgoliaQuestion[] = questions.map(questionToAlgoliaQuestion); const platformNameToLabel = Object.fromEntries(
platforms.map((platform) => [platform.name, platform.label])
);
const records: AlgoliaQuestion[] = questions.map(
(question, index: number) => ({
...question,
timestamp: `${question.timestamp}`,
platformLabel:
platformNameToLabel[question.platform] || question.platform,
objectID: index,
optionsstringforsearch: getoptionsstringforsearch(question),
})
);
if (await index.exists()) { if (await index.exists()) {
console.log("Index exists"); console.log("Index exists");

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
import { prisma } from "../../backend/database/prisma"; import { prisma } from "../../backend/database/prisma";
import { getPlatforms } from "../../backend/platforms/registry"; import { platforms } from "../../backend/platforms/registry";
import { builder } from "../builder"; import { builder } from "../builder";
export const PlatformObj = builder.objectRef<string>("Platform").implement({ export const PlatformObj = builder.objectRef<string>("Platform").implement({
@ -20,7 +20,7 @@ export const PlatformObj = builder.objectRef<string>("Platform").implement({
return "Guesstimate"; return "Guesstimate";
} }
// kinda slow and repetitive, TODO - store a map {name => platform} somewhere and `getPlatform` util function? // kinda slow and repetitive, TODO - store a map {name => platform} somewhere and `getPlatform` util function?
const platform = getPlatforms().find((p) => p.name === platformName); const platform = platforms.find((p) => p.name === platformName);
if (!platform) { if (!platform) {
throw new Error(`Unknown platform ${platformName}`); throw new Error(`Unknown platform ${platformName}`);
} }
@ -36,10 +36,10 @@ export const PlatformObj = builder.objectRef<string>("Platform").implement({
platform: platformName, platform: platformName,
}, },
_max: { _max: {
fetched: true, timestamp: true,
}, },
}); });
return res._max.fetched; return res._max.timestamp;
}, },
}), }),
}), }),
@ -49,7 +49,7 @@ builder.queryField("platforms", (t) =>
t.field({ t.field({
type: [PlatformObj], type: [PlatformObj],
resolve: async (parent, args) => { resolve: async (parent, args) => {
return getPlatforms().map((platform) => platform.name); return platforms.map((platform) => platform.name);
}, },
}) })
); );

View File

@ -70,21 +70,8 @@ const QuestionShapeInterface = builder
}), }),
timestamp: t.field({ timestamp: t.field({
type: "Date", type: "Date",
description: description: "Timestamp at which metaforecast fetched the question",
"Last timestamp at which metaforecast fetched the question", resolve: (parent) => parent.timestamp,
deprecationReason: "Renamed to `fetched`",
resolve: (parent) => parent.fetched,
}),
fetched: t.field({
type: "Date",
description:
"Last timestamp at which metaforecast fetched the question",
resolve: (parent) => parent.fetched,
}),
fetchedStr: t.string({
description:
"Last timestamp at which metaforecast fetched the question, in ISO 8601 format",
resolve: (parent) => parent.fetched.toISOString(),
}), }),
qualityIndicators: t.field({ qualityIndicators: t.field({
type: QualityIndicatorsObj, type: QualityIndicatorsObj,
@ -127,20 +114,10 @@ export const QuestionObj = builder.prismaObject("Question", {
resolve: (parent) => (parent.extra as any)?.visualization, // used for guesstimate only, see searchGuesstimate.ts resolve: (parent) => (parent.extra as any)?.visualization, // used for guesstimate only, see searchGuesstimate.ts
nullable: true, nullable: true,
}), }),
firstSeen: t.field({
type: "Date",
description: "First timestamp at which metaforecast fetched the question",
resolve: (parent) => parent.firstSeen,
}),
firstSeenStr: t.string({
description:
"First timestamp at which metaforecast fetched the question, in ISO 8601 format",
resolve: (parent) => parent.firstSeen.toISOString(),
}),
history: t.relation("history", { history: t.relation("history", {
query: () => ({ query: () => ({
orderBy: { orderBy: {
fetched: "asc", timestamp: "asc",
}, },
}), }),
}), }),
@ -153,21 +130,7 @@ builder.queryField("questions", (t) =>
type: "Question", type: "Question",
cursor: "id", cursor: "id",
maxSize: 1000, maxSize: 1000,
args: { resolve: (query) => prisma.question.findMany({ ...query }),
orderBy: t.arg({
type: builder.enumType("QuestionsOrderBy", {
values: ["FIRST_SEEN_DESC"] as const,
}),
}),
},
resolve: (query, parent, args) => {
return prisma.question.findMany({
...query,
...(args.orderBy === "FIRST_SEEN_DESC"
? { orderBy: [{ firstSeen: "desc" }, { id: "asc" }] }
: {}), // TODO - explicit default order?
});
},
}, },
{}, {},
{} {}

View File

@ -63,8 +63,7 @@ builder.queryField("searchQuestions", (t) =>
return results.map((q) => ({ return results.map((q) => ({
...q, ...q,
fetched: new Date(q.fetched), timestamp: new Date(q.timestamp),
firstSeen: new Date(q.firstSeen),
})); }));
}, },
}) })

View File

@ -46,10 +46,6 @@ export type Dashboard = {
export type History = QuestionShape & { export type History = QuestionShape & {
__typename?: 'History'; __typename?: 'History';
description: Scalars['String']; description: Scalars['String'];
/** Last timestamp at which metaforecast fetched the question */
fetched: Scalars['Date'];
/** Last timestamp at which metaforecast fetched the question, in ISO 8601 format */
fetchedStr: Scalars['String'];
/** History items are identified by their integer ids */ /** History items are identified by their integer ids */
id: Scalars['ID']; id: Scalars['ID'];
options: Array<ProbabilityOption>; options: Array<ProbabilityOption>;
@ -57,10 +53,7 @@ export type History = QuestionShape & {
qualityIndicators: QualityIndicators; qualityIndicators: QualityIndicators;
/** Unique string which identifies the question */ /** Unique string which identifies the question */
questionId: Scalars['ID']; questionId: Scalars['ID'];
/** /** Timestamp at which metaforecast fetched the question */
* Last timestamp at which metaforecast fetched the question
* @deprecated Renamed to `fetched`
*/
timestamp: Scalars['Date']; timestamp: Scalars['Date'];
title: Scalars['String']; title: Scalars['String'];
/** Non-unique, a very small number of platforms have a page for more than one prediction */ /** Non-unique, a very small number of platforms have a page for more than one prediction */
@ -121,12 +114,12 @@ export type QualityIndicators = {
export type Query = { export type Query = {
__typename?: 'Query'; __typename?: 'Query';
/** Look up a single dashboard by its id */ /** Look up a single dashboard by its id */
dashboard?: Maybe<Dashboard>; dashboard: Dashboard;
/** Get a list of questions that are currently on the frontpage */ /** Get a list of questions that are currently on the frontpage */
frontpage: Array<Question>; frontpage: Array<Question>;
platforms: Array<Platform>; platforms: Array<Platform>;
/** Look up a single question by its id */ /** Look up a single question by its id */
question?: Maybe<Question>; question: Question;
questions: QueryQuestionsConnection; questions: QueryQuestionsConnection;
/** Search for questions; uses Algolia instead of the primary metaforecast database */ /** Search for questions; uses Algolia instead of the primary metaforecast database */
searchQuestions: Array<Question>; searchQuestions: Array<Question>;
@ -148,7 +141,6 @@ export type QueryQuestionsArgs = {
before?: InputMaybe<Scalars['String']>; before?: InputMaybe<Scalars['String']>;
first?: InputMaybe<Scalars['Int']>; first?: InputMaybe<Scalars['Int']>;
last?: InputMaybe<Scalars['Int']>; last?: InputMaybe<Scalars['Int']>;
orderBy?: InputMaybe<QuestionsOrderBy>;
}; };
@ -171,24 +163,13 @@ export type QueryQuestionsConnectionEdge = {
export type Question = QuestionShape & { export type Question = QuestionShape & {
__typename?: 'Question'; __typename?: 'Question';
description: Scalars['String']; description: Scalars['String'];
/** Last timestamp at which metaforecast fetched the question */
fetched: Scalars['Date'];
/** Last timestamp at which metaforecast fetched the question, in ISO 8601 format */
fetchedStr: Scalars['String'];
/** First timestamp at which metaforecast fetched the question */
firstSeen: Scalars['Date'];
/** First timestamp at which metaforecast fetched the question, in ISO 8601 format */
firstSeenStr: Scalars['String'];
history: Array<History>; history: Array<History>;
/** Unique string which identifies the question */ /** Unique string which identifies the question */
id: Scalars['ID']; id: Scalars['ID'];
options: Array<ProbabilityOption>; options: Array<ProbabilityOption>;
platform: Platform; platform: Platform;
qualityIndicators: QualityIndicators; qualityIndicators: QualityIndicators;
/** /** Timestamp at which metaforecast fetched the question */
* Last timestamp at which metaforecast fetched the question
* @deprecated Renamed to `fetched`
*/
timestamp: Scalars['Date']; timestamp: Scalars['Date'];
title: Scalars['String']; title: Scalars['String'];
/** Non-unique, a very small number of platforms have a page for more than one prediction */ /** Non-unique, a very small number of platforms have a page for more than one prediction */
@ -198,27 +179,16 @@ export type Question = QuestionShape & {
export type QuestionShape = { export type QuestionShape = {
description: Scalars['String']; description: Scalars['String'];
/** Last timestamp at which metaforecast fetched the question */
fetched: Scalars['Date'];
/** Last timestamp at which metaforecast fetched the question, in ISO 8601 format */
fetchedStr: Scalars['String'];
options: Array<ProbabilityOption>; options: Array<ProbabilityOption>;
platform: Platform; platform: Platform;
qualityIndicators: QualityIndicators; qualityIndicators: QualityIndicators;
/** /** Timestamp at which metaforecast fetched the question */
* Last timestamp at which metaforecast fetched the question
* @deprecated Renamed to `fetched`
*/
timestamp: Scalars['Date']; timestamp: Scalars['Date'];
title: Scalars['String']; title: Scalars['String'];
/** Non-unique, a very small number of platforms have a page for more than one prediction */ /** Non-unique, a very small number of platforms have a page for more than one prediction */
url: Scalars['String']; url: Scalars['String'];
}; };
export enum QuestionsOrderBy {
FirstSeenDesc = 'FIRST_SEEN_DESC'
}
export type SearchInput = { export type SearchInput = {
/** List of platform ids to filter by */ /** List of platform ids to filter by */
forecastingPlatforms?: InputMaybe<Array<Scalars['String']>>; forecastingPlatforms?: InputMaybe<Array<Scalars['String']>>;

View File

@ -17,14 +17,12 @@ Router.events.on("routeChangeStart", (as, { shallow }) => {
Router.events.on("routeChangeComplete", () => NProgress.done()); Router.events.on("routeChangeComplete", () => NProgress.done());
Router.events.on("routeChangeError", () => NProgress.done()); Router.events.on("routeChangeError", () => NProgress.done());
function MyApp({ Component, pageProps }: AppProps) { function MyApp({ Component, pageProps }: AppProps) {
return ( return (
<PlausibleProvider domain="metaforecast.org"> <PlausibleProvider domain="metaforecast.org">
<Component {...pageProps} /> <Component {...pageProps} />
</PlausibleProvider> </PlausibleProvider>
); );
// Workaround in package.json for: https://github.com/vercel/next.js/issues/36019#issuecomment-1103266481
} }
export default withUrqlClient((ssr) => getUrqlClientOptions(ssr), { export default withUrqlClient((ssr) => getUrqlClientOptions(ssr), {

View File

@ -1,24 +1,13 @@
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
// apollo-server-micro is problematic since v3, see https://github.com/apollographql/apollo-server/issues/5547, so we use graphql-yoga instead // apollo-server-micro is problematic since v3, see https://github.com/apollographql/apollo-server/issues/5547, so we use graphql-yoga instead
import {createYoga} from "graphql-yoga"; import { createServer } from "@graphql-yoga/node";
import {useResponseCache} from '@graphql-yoga/plugin-response-cache'
import { schema } from "../../graphql/schema"; import { schema } from "../../graphql/schema";
const server = createYoga < { const server = createServer<{
req: NextApiRequest; req: NextApiRequest;
res: NextApiResponse; res: NextApiResponse;
} > ({ }>({ schema });
schema,
graphqlEndpoint: '/api/graphql',
plugins: [useResponseCache(
{ // global cache
session: () => null,
ttl: 2 * 60 * 60 * 1000,
// ^ 2h * 60 mins per hour, 60 seconds per min 1000 miliseconds per second
}
)]
});
export default server; export default server;

View File

@ -35,7 +35,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
props: { props: {
// reduntant: page component doesn't do graphql requests, but it's still nice/more consistent to have data in cache // reduntant: page component doesn't do graphql requests, but it's still nice/more consistent to have data in cache
urqlState: ssrCache.extractData(), urqlState: ssrCache.extractData(),
dashboard: dashboard || undefined, dashboard,
numCols: !numCols ? undefined : numCols < 5 ? numCols : 4, numCols: !numCols ? undefined : numCols < 5 ? numCols : 4,
}, },
}; };

View File

@ -33,7 +33,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
props: { props: {
// reduntant: page component doesn't do graphql requests, but it's still nice/more consistent to have data in cache // reduntant: page component doesn't do graphql requests, but it's still nice/more consistent to have data in cache
urqlState: ssrCache.extractData(), urqlState: ssrCache.extractData(),
dashboard: dashboard || undefined, dashboard,
}, },
}; };
}; };

View File

@ -1,7 +1,7 @@
import { GetServerSideProps, NextPage } from "next"; import { GetServerSideProps, NextPage } from "next";
import React from "react"; import React from "react";
import { getPlatforms, getPlatformsConfig } from "../backend/platforms/registry"; import { getPlatformsConfig, platforms } from "../backend/platforms/registry";
import { Layout } from "../web/common/Layout"; import { Layout } from "../web/common/Layout";
import { Props, QueryParameters, SearchScreen } from "../web/search/components/SearchScreen"; import { Props, QueryParameters, SearchScreen } from "../web/search/components/SearchScreen";
import { FrontpageDocument, SearchDocument } from "../web/search/queries.generated"; import { FrontpageDocument, SearchDocument } from "../web/search/queries.generated";
@ -19,7 +19,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
query: "", query: "",
starsThreshold: 2, starsThreshold: 2,
forecastsThreshold: 0, forecastsThreshold: 0,
forecastingPlatforms: getPlatforms().map((platform) => platform.name), forecastingPlatforms: platforms.map((platform) => platform.name),
}; };
const initialQueryParameters: QueryParameters = { const initialQueryParameters: QueryParameters = {

View File

@ -1,4 +0,0 @@
export {
default,
getServerSideProps,
} from "../../../web/questions/pages/EmbedQuestionPage";

View File

@ -3,7 +3,7 @@
import { GetServerSideProps, NextPage } from "next"; import { GetServerSideProps, NextPage } from "next";
import React from "react"; import React from "react";
import { getPlatforms } from "../backend/platforms/registry"; import { platforms } from "../backend/platforms/registry";
import { QuestionFragment } from "../web/fragments.generated"; import { QuestionFragment } from "../web/fragments.generated";
import { QuestionCard } from "../web/questions/components/QuestionCard"; import { QuestionCard } from "../web/questions/components/QuestionCard";
import { SearchDocument } from "../web/search/queries.generated"; import { SearchDocument } from "../web/search/queries.generated";
@ -23,7 +23,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
query: "", query: "",
starsThreshold: 2, starsThreshold: 2,
forecastsThreshold: 0, forecastsThreshold: 0,
forecastingPlatforms: getPlatforms().map((platform) => platform.name), forecastingPlatforms: platforms.map((platform) => platform.name),
...urlQuery, ...urlQuery,
}; };
@ -65,7 +65,6 @@ const SecretEmbedPage: NextPage<Props> = ({ results }) => {
question={result} question={result}
showTimeStamp={true} showTimeStamp={true}
expandFooterToFullWidth={true} expandFooterToFullWidth={true}
showExpandButton={false}
/> />
) : null} ) : null}
</div> </div>

View File

@ -1,23 +0,0 @@
import { FaExternalLinkAlt } from "react-icons/fa";
type Props = {
url: string;
size?: "normal" | "small";
};
export const BoxedLink: React.FC<Props> = ({
url,
size = "normal",
children,
}) => (
<a
className={`px-2 py-1 border-2 border-gray-400 rounded-lg text-black no-underline hover:bg-gray-100 inline-flex flex-nowrap space-x-1 items-center text-xs md:text-lg ${
size === "small" ? "text-sm" : ""
}`}
href={url}
target="_blank"
>
<span>{children}</span>
<FaExternalLinkAlt className="text-gray-400 inline " />
</a>
);

View File

@ -1,44 +0,0 @@
import { useState } from "react";
import { FaCaretDown, FaCaretRight } from "react-icons/fa";
export const Collapsible: React.FC<{
title: string;
children: () => React.ReactElement | null;
}> = ({ title, children }) => {
const [open, setOpen] = useState(false);
const expand = (e: React.SyntheticEvent) => {
e.preventDefault();
setOpen(true);
};
const collapse = (e: React.SyntheticEvent) => {
e.preventDefault();
setOpen(false);
};
if (open) {
return (
<div>
<a
href="#"
className="decoration-dashed inline-flex items-center"
onClick={collapse}
>
{title} <FaCaretDown />
</a>
<div>{children()}</div>
</div>
);
} else {
return (
<a
href="#"
className="decoration-dashed inline-flex items-center"
onClick={expand}
>
{title} <FaCaretRight />
</a>
);
}
};

View File

@ -11,7 +11,7 @@ export const Spinner: React.FC = () => (
cy="12" cy="12"
r="10" r="10"
stroke="currentColor" stroke="currentColor"
strokeWidth="4" stroke-width="4"
></circle> ></circle>
<path <path
className="opacity-75" className="opacity-75"

View File

@ -2,21 +2,21 @@ import * as Types from '../../graphql/types.generated';
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'; import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
import { QuestionFragmentDoc } from '../fragments.generated'; import { QuestionFragmentDoc } from '../fragments.generated';
export type DashboardFragment = { __typename?: 'Dashboard', id: string, title: string, description: string, creator: string, questions: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> }; export type DashboardFragment = { __typename?: 'Dashboard', id: string, title: string, description: string, creator: string, questions: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> };
export type DashboardByIdQueryVariables = Types.Exact<{ export type DashboardByIdQueryVariables = Types.Exact<{
id: Types.Scalars['ID']; id: Types.Scalars['ID'];
}>; }>;
export type DashboardByIdQuery = { __typename?: 'Query', result?: { __typename?: 'Dashboard', id: string, title: string, description: string, creator: string, questions: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> } | null }; export type DashboardByIdQuery = { __typename?: 'Query', result: { __typename?: 'Dashboard', id: string, title: string, description: string, creator: string, questions: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> } };
export type CreateDashboardMutationVariables = Types.Exact<{ export type CreateDashboardMutationVariables = Types.Exact<{
input: Types.CreateDashboardInput; input: Types.CreateDashboardInput;
}>; }>;
export type CreateDashboardMutation = { __typename?: 'Mutation', result: { __typename?: 'CreateDashboardResult', dashboard: { __typename?: 'Dashboard', id: string, title: string, description: string, creator: string, questions: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> } } }; export type CreateDashboardMutation = { __typename?: 'Mutation', result: { __typename?: 'CreateDashboardResult', dashboard: { __typename?: 'Dashboard', id: string, title: string, description: string, creator: string, questions: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> } } };
export const DashboardFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Dashboard"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Dashboard"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"creator"}},{"kind":"Field","name":{"kind":"Name","value":"questions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Question"}}]}}]}},...QuestionFragmentDoc.definitions]} as unknown as DocumentNode<DashboardFragment, unknown>; export const DashboardFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Dashboard"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Dashboard"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"creator"}},{"kind":"Field","name":{"kind":"Name","value":"questions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Question"}}]}}]}},...QuestionFragmentDoc.definitions]} as unknown as DocumentNode<DashboardFragment, unknown>;
export const DashboardByIdDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"DashboardById"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"id"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"result"},"name":{"kind":"Name","value":"dashboard"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"id"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Dashboard"}}]}}]}},...DashboardFragmentDoc.definitions]} as unknown as DocumentNode<DashboardByIdQuery, DashboardByIdQueryVariables>; export const DashboardByIdDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"DashboardById"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"id"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"result"},"name":{"kind":"Name","value":"dashboard"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"id"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Dashboard"}}]}}]}},...DashboardFragmentDoc.definitions]} as unknown as DocumentNode<DashboardByIdQuery, DashboardByIdQueryVariables>;

View File

@ -1,9 +1,9 @@
import * as Types from '../graphql/types.generated'; import * as Types from '../graphql/types.generated';
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'; import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
export type QuestionFragment = { __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }; export type QuestionFragment = { __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } };
export type QuestionWithHistoryFragment = { __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, history: Array<{ __typename?: 'History', fetched: number, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }> }>, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }; export type QuestionWithHistoryFragment = { __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, history: Array<{ __typename?: 'History', timestamp: number, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }> }>, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } };
export const QuestionFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Question"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Question"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"url"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"fetched"}},{"kind":"Field","name":{"kind":"Name","value":"options"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"probability"}}]}},{"kind":"Field","name":{"kind":"Name","value":"platform"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"label"}}]}},{"kind":"Field","name":{"kind":"Name","value":"qualityIndicators"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"stars"}},{"kind":"Field","name":{"kind":"Name","value":"numForecasts"}},{"kind":"Field","name":{"kind":"Name","value":"numForecasters"}},{"kind":"Field","name":{"kind":"Name","value":"volume"}},{"kind":"Field","name":{"kind":"Name","value":"spread"}},{"kind":"Field","name":{"kind":"Name","value":"sharesVolume"}},{"kind":"Field","name":{"kind":"Name","value":"openInterest"}},{"kind":"Field","name":{"kind":"Name","value":"liquidity"}},{"kind":"Field","name":{"kind":"Name","value":"tradeVolume"}}]}},{"kind":"Field","name":{"kind":"Name","value":"visualization"}}]}}]} as unknown as DocumentNode<QuestionFragment, unknown>; export const QuestionFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Question"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Question"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"url"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"timestamp"}},{"kind":"Field","name":{"kind":"Name","value":"options"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"probability"}}]}},{"kind":"Field","name":{"kind":"Name","value":"platform"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"label"}}]}},{"kind":"Field","name":{"kind":"Name","value":"qualityIndicators"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"stars"}},{"kind":"Field","name":{"kind":"Name","value":"numForecasts"}},{"kind":"Field","name":{"kind":"Name","value":"numForecasters"}},{"kind":"Field","name":{"kind":"Name","value":"volume"}},{"kind":"Field","name":{"kind":"Name","value":"spread"}},{"kind":"Field","name":{"kind":"Name","value":"sharesVolume"}},{"kind":"Field","name":{"kind":"Name","value":"openInterest"}},{"kind":"Field","name":{"kind":"Name","value":"liquidity"}},{"kind":"Field","name":{"kind":"Name","value":"tradeVolume"}}]}},{"kind":"Field","name":{"kind":"Name","value":"visualization"}}]}}]} as unknown as DocumentNode<QuestionFragment, unknown>;
export const QuestionWithHistoryFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"QuestionWithHistory"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Question"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Question"}},{"kind":"Field","name":{"kind":"Name","value":"history"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"fetched"}},{"kind":"Field","name":{"kind":"Name","value":"options"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"probability"}}]}}]}}]}},...QuestionFragmentDoc.definitions]} as unknown as DocumentNode<QuestionWithHistoryFragment, unknown>; export const QuestionWithHistoryFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"QuestionWithHistory"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Question"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Question"}},{"kind":"Field","name":{"kind":"Name","value":"history"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"timestamp"}},{"kind":"Field","name":{"kind":"Name","value":"options"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"probability"}}]}}]}}]}},...QuestionFragmentDoc.definitions]} as unknown as DocumentNode<QuestionWithHistoryFragment, unknown>;

View File

@ -3,7 +3,7 @@ fragment Question on Question {
url url
title title
description description
fetched timestamp
options { options {
name name
probability probability
@ -29,7 +29,7 @@ fragment Question on Question {
fragment QuestionWithHistory on Question { fragment QuestionWithHistory on Question {
...Question ...Question
history { history {
fetched timestamp
options { options {
name name
probability probability

View File

@ -1,6 +1,6 @@
import domtoimage from "dom-to-image"; // https://github.com/tsayen/dom-to-image import domtoimage from "dom-to-image"; // https://github.com/tsayen/dom-to-image
import { Resizable } from "re-resizable";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { Button } from "../../common/Button"; import { Button } from "../../common/Button";
import { CopyParagraph } from "../../common/CopyParagraph"; import { CopyParagraph } from "../../common/CopyParagraph";
import { QuestionFragment } from "../../fragments.generated"; import { QuestionFragment } from "../../fragments.generated";
@ -79,15 +79,6 @@ const MetaculusSource: React.FC<{
); );
}; };
const GrayContainer: React.FC<{ title: string }> = ({ title, children }) => (
<div className="bg-gray-100 p-2 space-y-1">
<div className="uppercase text-xs font-bold tracking-wide text-gray-600">
{title}:
</div>
<div>{children}</div>
</div>
);
interface Props { interface Props {
question: QuestionFragment; question: QuestionFragment;
} }
@ -127,19 +118,27 @@ export const CaptureQuestion: React.FC<Props> = ({ question }) => {
await exportAsPictureAndCode(); await exportAsPictureAndCode();
}; };
if (imgSrc) {
return ( return (
<div className="space-y-4"> <div className="grid grid-cols-1 md:grid-cols-2 gap-4 place-items-center">
<GrayContainer title="Generated image"> <div ref={containerRef}>
<a href={imgSrc} target="_blank"> <QuestionCard
question={question}
showTimeStamp={true}
showExpandButton={false}
expandFooterToFullWidth={true}
/>
</div>
<div>
<Button onClick={onCaptureButtonClick}>{mainButtonText}</Button>
</div>
{imgSrc ? (
<>
<div>
<img src={imgSrc} /> <img src={imgSrc} />
</a> </div>
</GrayContainer>
<div> <div>
<ImageSource question={question} imgSrc={imgSrc} /> <ImageSource question={question} imgSrc={imgSrc} />
</div> </div>
{question.platform.id === "metaculus" ? (
<>
<div className="justify-self-stretch"> <div className="justify-self-stretch">
<MetaculusEmbed question={question} /> <MetaculusEmbed question={question} />
</div> </div>
@ -150,34 +149,6 @@ export const CaptureQuestion: React.FC<Props> = ({ question }) => {
) : null} ) : null}
</div> </div>
); );
}
return (
<div className="space-y-2 flex flex-col">
<GrayContainer title="Resizable preview">
<Resizable
minWidth={320}
bounds="window"
enable={{ right: true, left: true }}
>
<div ref={containerRef}>
<QuestionCard
container={(children) => (
<div className="px-4 py-3 bg-white">{children}</div>
)}
question={question}
showTimeStamp={true}
showExpandButton={false}
expandFooterToFullWidth={true}
/>
</div>
</Resizable>
</GrayContainer>
<Button onClick={onCaptureButtonClick} size="small">
{mainButtonText}
</Button>
</div>
);
}; };
// Note: https://stackoverflow.com/questions/66016033/can-no-longer-upload-images-to-imgur-from-localhost // Note: https://stackoverflow.com/questions/66016033/can-no-longer-upload-images-to-imgur-from-localhost

View File

@ -3,33 +3,27 @@ import {
VictoryAxis, VictoryAxis,
VictoryChart, VictoryChart,
VictoryGroup, VictoryGroup,
VictoryStack,
VictoryLabel, VictoryLabel,
VictoryLine, VictoryLine,
VictoryScatter, VictoryScatter,
VictoryArea,
VictoryTheme, VictoryTheme,
VictoryTooltip, VictoryTooltip,
VictoryVoronoiContainer, VictoryVoronoiContainer,
} from "victory"; } from "victory";
import { chartColors, ChartData, ChartSeries, goldenRatio } from "./utils"; import { chartColors, ChartData, ChartSeries, height, width } from "./utils";
const height = 200 let dateFormat = "MMM do y"; // "yyyy-MM-dd"
const width = 200 * goldenRatio
let dateFormat = "dd/MM/yy"; // "yyyy-MM-dd" // "MMM do yy"
// can't be replaced with React component, VictoryChart requires VictoryGroup elements to be immediate children // can't be replaced with React component, VictoryChart requires VictoryGroup elements to be immediate children
const getVictoryGroup = ({ const getVictoryGroup = ({
data, data,
i, i,
highlight, highlight,
isBinary,
}: { }: {
data: ChartSeries; data: ChartSeries;
i: number; i: number;
highlight?: boolean; highlight?: boolean;
isBinary?: boolean;
}) => { }) => {
return ( return (
<VictoryGroup color={chartColors[i] || "darkgray"} data={data} key={i}> <VictoryGroup color={chartColors[i] || "darkgray"} data={data} key={i}>
@ -37,25 +31,13 @@ const getVictoryGroup = ({
name={`line-${i}`} name={`line-${i}`}
style={{ style={{
data: { data: {
// strokeOpacity: highlight ? 1 : 0.5, strokeOpacity: highlight ? 1 : 0.5,
strokeOpacity: highlight && !isBinary ? 0.8 : 0.6,
strokeWidth: highlight && !isBinary ? 2.5 : 1.5,
}, },
}} }}
/> />
{isBinary ? (
<VictoryArea
standalone={false}
style={{
data: { fill: chartColors[i], fillOpacity: 0.1, strokeOpacity: 0 },
}}
data={data}
/>
) : null}
<VictoryScatter <VictoryScatter
name={`scatter-${i}`} name={`scatter-${i}`}
size={({ active }) => (active || highlight ? 0 : 0)} //(active || highlight ? 3.75 : 3)} size={({ active }) => (active || highlight ? 3.75 : 3)}
/> />
</VictoryGroup> </VictoryGroup>
); );
@ -73,18 +55,15 @@ export const InnerChart: React.FC<Props> = ({
const domainMax = const domainMax =
maxProbability < 0.5 ? Math.round(10 * (maxProbability + 0.05)) / 10 : 1; maxProbability < 0.5 ? Math.round(10 * (maxProbability + 0.05)) / 10 : 1;
const padding = { const padding = {
top: 12, top: 20,
bottom: 33, bottom: 65,
left: 30, left: 60,
right: 17, right: 5,
}; };
const isBinary = seriesList.length == 1;
console.log(isBinary);
return ( return (
<VictoryChart <VictoryChart
domainPadding={{ x: 0 }} domainPadding={20}
padding={padding} padding={padding}
theme={VictoryTheme.material} theme={VictoryTheme.material}
height={height} height={height}
@ -101,12 +80,12 @@ export const InnerChart: React.FC<Props> = ({
<VictoryLabel <VictoryLabel
style={[ style={[
{ {
fontSize: 10, fontSize: 16,
fill: "black", fill: "black",
strokeWidth: 0.05, strokeWidth: 0.05,
}, },
{ {
fontSize: 10, fontSize: 16,
fill: "#777", fill: "#777",
strokeWidth: 0.05, strokeWidth: 0.05,
}, },
@ -120,7 +99,7 @@ export const InnerChart: React.FC<Props> = ({
)}` )}`
} }
style={{ style={{
fontSize: 10, // needs to be set here and not just in labelComponent for text size calculations fontSize: 17, // needs to be set here and not just in labelComponent for text size calculations
fontFamily: fontFamily:
'"Gill Sans", "Gill Sans MT", "Ser­avek", "Trebuchet MS", sans-serif', '"Gill Sans", "Gill Sans MT", "Ser­avek", "Trebuchet MS", sans-serif',
// default font family from Victory, need to be specified explicitly for some reason, otherwise text size gets miscalculated // default font family from Victory, need to be specified explicitly for some reason, otherwise text size gets miscalculated
@ -130,10 +109,10 @@ export const InnerChart: React.FC<Props> = ({
fill: "white", fill: "white",
}} }}
cornerRadius={4} cornerRadius={4}
flyoutPadding={{ top: 4, bottom: 4, left: 10, right: 10 }} flyoutPadding={{ top: 4, bottom: 4, left: 16, right: 16 }}
/> />
} }
radius={20} radius={50}
voronoiBlacklist={ voronoiBlacklist={
[...Array(seriesList.length).keys()].map((i) => `line-${i}`) [...Array(seriesList.length).keys()].map((i) => `line-${i}`)
// see: https://github.com/FormidableLabs/victory/issues/545 // see: https://github.com/FormidableLabs/victory/issues/545
@ -149,11 +128,6 @@ export const InnerChart: React.FC<Props> = ({
y: [0, domainMax], y: [0, domainMax],
}} }}
> >
{
// Note: axis is not in fact unaligned. Fetchers are at ~12:00
// whereas the date is at the beginning of the day
// however, it still doesn't look very pretty.
}
<VictoryAxis <VictoryAxis
tickCount={Math.min(7, differenceInDays(maxDate, minDate) + 1)} tickCount={Math.min(7, differenceInDays(maxDate, minDate) + 1)}
style={{ style={{
@ -161,10 +135,10 @@ export const InnerChart: React.FC<Props> = ({
}} }}
tickLabelComponent={ tickLabelComponent={
<VictoryLabel <VictoryLabel
dx={-10} dx={-38}
dy={0} dy={-5}
angle={-30} angle={-30}
style={{ fontSize: 9, fill: "#777" }} style={{ fontSize: 15, fill: "#777" }}
/> />
} }
scale={{ x: "time" }} scale={{ x: "time" }}
@ -176,38 +150,29 @@ export const InnerChart: React.FC<Props> = ({
grid: { stroke: "#D3D3D3", strokeWidth: 0.5 }, grid: { stroke: "#D3D3D3", strokeWidth: 0.5 },
}} }}
tickLabelComponent={ tickLabelComponent={
<VictoryLabel dy={0} dx={5} style={{ fontSize: 9, fill: "#777" }} /> <VictoryLabel dy={0} style={{ fontSize: 18, fill: "#777" }} />
} }
// tickFormat specifies how ticks should be displayed // tickFormat specifies how ticks should be displayed
tickFormat={(x) => `${x * 100}%`} tickFormat={(x) => `${x * 100}%`}
/> />
{[...Array(seriesList.length).keys()] {[...Array(seriesList.length).keys()]
.reverse() // affects svg render order, we want to render largest datasets on top of others .reverse() // affects svg render order, we want to render largest datasets on top of others
//.filter((i) => i !== highlight) .filter((i) => i !== highlight)
.map((i) => .map((i) =>
getVictoryGroup({ getVictoryGroup({
data: seriesList[i], data: seriesList[i],
i, i,
highlight: i == highlight, // false highlight: false,
isBinary: isBinary,
}) })
)} )}
{highlight === undefined
{
// note: this produces an annoying change of color effect
/*
highlight === undefined
? null ? null
: // render highlighted series on top of everything else : // render highlighted series on top of everything else
getVictoryGroup({ getVictoryGroup({
data: seriesList[highlight], data: seriesList[highlight],
i: highlight, i: highlight,
highlight: true, highlight: true,
}) })}
*/
}
</VictoryChart> </VictoryChart>
); );
}; };

View File

@ -23,7 +23,7 @@ export const HistoryChart: React.FC<Props> = ({ question }) => {
const data = useMemo(() => buildChartData(question), [question]); const data = useMemo(() => buildChartData(question), [question]);
return ( return (
<div className="flex items-center space-y-4 sm:flex-row sm:space-y-0 "> <div className="flex items-center flex-col space-y-4 sm:flex-row sm:space-y-0">
<InnerChart data={data} highlight={highlight} /> <InnerChart data={data} highlight={highlight} />
<Legend <Legend
items={data.seriesNames.map((name, i) => ({ items={data.seriesNames.map((name, i) => ({

View File

@ -18,7 +18,7 @@ export const chartColors = [
"#F59E0B", // amber-500 "#F59E0B", // amber-500
]; ];
export const goldenRatio = (1 + Math.sqrt(5)) / 2; const goldenRatio = (1 + Math.sqrt(5)) / 2;
// used both for chart and for ssr placeholder // used both for chart and for ssr placeholder
export const width = 750; export const width = 750;
export const height = width / goldenRatio; export const height = width / goldenRatio;
@ -58,16 +58,16 @@ export const buildChartData = (
let seriesList: ChartSeries[] = [...Array(seriesNames.length)].map((x) => []); let seriesList: ChartSeries[] = [...Array(seriesNames.length)].map((x) => []);
const sortedHistory = question.history.sort((a, b) => const sortedHistory = question.history.sort((a, b) =>
a.fetched < b.fetched ? -1 : 1 a.timestamp < b.timestamp ? -1 : 1
); );
{ {
let previousDate = -Infinity; let previousDate = -Infinity;
for (const item of sortedHistory) { for (const item of sortedHistory) {
if (item.fetched - previousDate < 12 * 60 * 60) { if (item.timestamp - previousDate < 12 * 60 * 60) {
continue; continue;
} }
const date = new Date(item.fetched * 1000); const date = new Date(item.timestamp * 1000);
for (const option of item.options) { for (const option of item.options) {
if (option.name == null || option.probability == null) { if (option.name == null || option.probability == null) {
@ -84,7 +84,7 @@ export const buildChartData = (
}; };
seriesList[idx].push(result); seriesList[idx].push(result);
} }
previousDate = item.fetched; previousDate = item.timestamp;
} }
} }
@ -96,12 +96,12 @@ export const buildChartData = (
} }
const minDate = sortedHistory.length const minDate = sortedHistory.length
? startOfDay(new Date(sortedHistory[0].fetched * 1000)) ? startOfDay(new Date(sortedHistory[0].timestamp * 1000))
: startOfToday(); : startOfToday();
const maxDate = sortedHistory.length const maxDate = sortedHistory.length
? addDays( ? addDays(
startOfDay( startOfDay(
new Date(sortedHistory[sortedHistory.length - 1].fetched * 1000) new Date(sortedHistory[sortedHistory.length - 1].timestamp * 1000)
), ),
1 1
) )

View File

@ -1,6 +0,0 @@
import { BoxedLink } from "../../common/BoxedLink";
import { QuestionFragment } from "../../fragments.generated";
export const PlatformLink: React.FC<{ question: QuestionFragment }> = ({
question,
}) => <BoxedLink url={question.url}>{question.platform.label}</BoxedLink>;

View File

@ -74,7 +74,7 @@ const getCurrencySymbolIfNeeded = ({
"openInterest", "openInterest",
"liquidity", "liquidity",
]; ];
let dollarPlatforms = ["predictit", "kalshi", "polymarket", "insight"]; let dollarPlatforms = ["predictit", "kalshi", "polymarket"];
if (indicatorsWhichNeedCurrencySymbol.includes(indicator)) { if (indicatorsWhichNeedCurrencySymbol.includes(indicator)) {
if (dollarPlatforms.includes(platform)) { if (dollarPlatforms.includes(platform)) {
return "$"; return "$";
@ -160,11 +160,9 @@ export const QuestionFooter: React.FC<Props> = ({
<div <div
className={`grid grid-cols-3 ${ className={`grid grid-cols-3 ${
expandFooterToFullWidth ? "justify-between" : "" expandFooterToFullWidth ? "justify-between" : ""
} text-gray-500`} } text-gray-500 mb-2 mt-1`}
> >
<div className="self-center">
<Stars num={question.qualityIndicators.stars} /> <Stars num={question.qualityIndicators.stars} />
</div>
<div <div
className={`${ className={`${
expandFooterToFullWidth ? "place-self-center" : "self-center" expandFooterToFullWidth ? "place-self-center" : "self-center"
@ -172,7 +170,6 @@ export const QuestionFooter: React.FC<Props> = ({
> >
{question.platform.label {question.platform.label
.replace("Good Judgment Open", "GJOpen") .replace("Good Judgment Open", "GJOpen")
.replace("Insight Prediction", "Insight")
.replace(/ /g, "\u00a0")} .replace(/ /g, "\u00a0")}
</div> </div>
<div <div

View File

@ -1,11 +1,11 @@
import Link from "next/link"; import Link from "next/link";
import { ReactElement, ReactNode } from "react";
import { FaExpand } from "react-icons/fa"; import { FaExpand } from "react-icons/fa";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
import { Card } from "../../../common/Card"; import { Card } from "../../../common/Card";
import { CopyText } from "../../../common/CopyText"; import { CopyText } from "../../../common/CopyText";
import { QuestionFragment } from "../../../fragments.generated"; import { QuestionFragment } from "../../../fragments.generated";
import { cleanText, isQuestionBinary } from "../../../utils"; import { cleanText } from "../../../utils";
import { QuestionOptions } from "../QuestionOptions"; import { QuestionOptions } from "../QuestionOptions";
import { QuestionFooter } from "./QuestionFooter"; import { QuestionFooter } from "./QuestionFooter";
@ -17,8 +17,8 @@ const truncateText = (length: number, text: string): string => {
return text; return text;
} }
const breakpoints = " .!?"; const breakpoints = " .!?";
let lastLetter let lastLetter: string | undefined = undefined;
let lastIndex let lastIndex: number | undefined = undefined;
for (let index = length; index > 0; index--) { for (let index = length; index > 0; index--) {
const letter = text[index]; const letter = text[index];
if (breakpoints.includes(letter)) { if (breakpoints.includes(letter)) {
@ -63,7 +63,6 @@ const LastUpdated: React.FC<{ timestamp: Date }> = ({ timestamp }) => (
// Main component // Main component
interface Props { interface Props {
container?: (children: ReactNode) => ReactElement;
question: QuestionFragment; question: QuestionFragment;
showTimeStamp: boolean; showTimeStamp: boolean;
expandFooterToFullWidth: boolean; expandFooterToFullWidth: boolean;
@ -72,7 +71,6 @@ interface Props {
} }
export const QuestionCard: React.FC<Props> = ({ export const QuestionCard: React.FC<Props> = ({
container = (children) => <Card>{children}</Card>,
question, question,
showTimeStamp, showTimeStamp,
expandFooterToFullWidth, expandFooterToFullWidth,
@ -80,11 +78,14 @@ export const QuestionCard: React.FC<Props> = ({
showExpandButton = true, showExpandButton = true,
}) => { }) => {
const { options } = question; const { options } = question;
const lastUpdated = new Date(question.fetched * 1000); const lastUpdated = new Date(question.timestamp * 1000);
const isBinary = isQuestionBinary(question); const isBinary =
options.length === 2 &&
(options[0].name === "Yes" || options[0].name === "No");
return container( return (
<Card>
<div className="h-full flex flex-col space-y-4"> <div className="h-full flex flex-col space-y-4">
<div className="flex-grow space-y-4"> <div className="flex-grow space-y-4">
{showIdToggle ? ( {showIdToggle ? (
@ -113,12 +114,21 @@ export const QuestionCard: React.FC<Props> = ({
</a> </a>
</Card.Title> </Card.Title>
</div> </div>
<div className={isBinary ? "flex justify-between" : "space-y-4"}> {isBinary ? (
<QuestionOptions question={question} maxNumOptions={5} /> <div className="flex justify-between">
<QuestionOptions question={question} />
<div className={`hidden ${showTimeStamp ? "sm:block" : ""}`}> <div className={`hidden ${showTimeStamp ? "sm:block" : ""}`}>
<LastUpdated timestamp={lastUpdated} /> <LastUpdated timestamp={lastUpdated} />
</div> </div>
</div> </div>
) : (
<div className="space-y-2">
<QuestionOptions question={question} />
<div className={`hidden ${showTimeStamp ? "sm:block" : ""} ml-6`}>
<LastUpdated timestamp={lastUpdated} />
</div>
</div>
)}
{question.platform.id !== "guesstimate" && options.length < 3 && ( {question.platform.id !== "guesstimate" && options.length < 3 && (
<div className="text-gray-500"> <div className="text-gray-500">
@ -141,13 +151,12 @@ export const QuestionCard: React.FC<Props> = ({
<LastUpdated timestamp={lastUpdated} /> <LastUpdated timestamp={lastUpdated} />
</div> </div>
<div className="w-full"> <div className="w-full">
<div className="mb-2 mt-1">
<QuestionFooter <QuestionFooter
question={question} question={question}
expandFooterToFullWidth={expandFooterToFullWidth} expandFooterToFullWidth={expandFooterToFullWidth}
/> />
</div> </div>
</div> </div>
</div> </Card>
); );
}; };

View File

@ -1,19 +0,0 @@
import { QuestionWithHistoryFragment } from "../../fragments.generated";
import { HistoryChart } from "./HistoryChart";
type Props = {
question: QuestionWithHistoryFragment;
};
export const QuestionChartOrVisualization: React.FC<Props> = ({ question }) =>
question.platform.id === "guesstimate" && question.visualization ? (
<a className="no-underline" href={question.url} target="_blank">
<img
className="rounded-sm"
src={question.visualization}
alt="Guesstimate Screenshot"
/>
</a>
) : question.options.length > 0 ? (
<HistoryChart question={question} />
) : null; /* Don't display chart if there are no options, for now. */

View File

@ -1,20 +0,0 @@
import { QuestionFragment } from "../../fragments.generated";
import { PlatformLink } from "./PlatformLink";
import { QuestionOptions } from "./QuestionOptions";
import { Stars } from "./Stars";
type Props = {
question: QuestionFragment;
};
export const QuestionInfoRow: React.FC<Props> = ({ question }) => (
<div className="flex items-center gap-2">
<PlatformLink question={question} />
<Stars num={question.qualityIndicators.stars} />
<QuestionOptions
question={{ ...question }}
maxNumOptions={1}
forcePrimaryMode={true}
/>
</div>
);

View File

@ -59,18 +59,6 @@ const primaryForecastColor = (probability: number) => {
} }
}; };
const chooseColor = (probability: number) => {
if (probability < 0.1) {
return "bg-blue-50 text-blue-500";
} else if (probability < 0.3) {
return "bg-blue-100 text-blue-600";
} else if (probability < 0.7) {
return "bg-blue-200 text-blue-700";
} else {
return "bg-blue-300 text-blue-800";
}
};
const primaryEstimateAsText = (probability: number) => { const primaryEstimateAsText = (probability: number) => {
if (probability < 0.03) { if (probability < 0.03) {
return "Exceptionally unlikely"; return "Exceptionally unlikely";
@ -89,48 +77,38 @@ const primaryEstimateAsText = (probability: number) => {
} }
}; };
type OptionProps = { const chooseColor = (probability: number) => {
option: FullQuestionOption; if (probability < 0.1) {
mode: "primary" | "normal"; // affects font size and colors return "bg-blue-50 text-blue-500";
textMode: "name" | "probability"; // whether to output option name or probability estimate as text } else if (probability < 0.3) {
return "bg-blue-100 text-blue-600";
} else if (probability < 0.7) {
return "bg-blue-200 text-blue-700";
} else {
return "bg-blue-300 text-blue-800";
}
}; };
const OptionRow: React.FC<OptionProps> = ({ option, mode, textMode }) => { const OptionRow: React.FC<{ option: FullQuestionOption }> = ({ option }) => {
return ( return (
<div className="flex items-center space-x-2"> <div className="flex items-center">
<div <div
className={`flex-none rounded-md text-center ${ className={`${chooseColor(
mode === "primary" option.probability
? "text-sm md:text-lg text-normal text-white px-2 py-0.5 font-bold" )} w-14 flex-none rounded-md py-0.5 text-sm text-center`}
: "text-sm w-14 py-0.5"
} ${
mode === "primary"
? primaryForecastColor(option.probability)
: chooseColor(option.probability)
}`}
> >
{formatProbability(option.probability)} {formatProbability(option.probability)}
</div> </div>
<div <div className="text-gray-700 pl-3 leading-snug text-sm">
className={`leading-snug ${ {option.name}
mode === "primary" ? "text-sm md:text-lg text-normal" : "text-sm"
} ${
mode === "primary" ? textColor(option.probability) : "text-gray-700"
}`}
>
{textMode === "name"
? option.name
: primaryEstimateAsText(option.probability)}
</div> </div>
</div> </div>
); );
}; };
export const QuestionOptions: React.FC<{ export const QuestionOptions: React.FC<{ question: QuestionFragment }> = ({
question: QuestionFragment; question,
maxNumOptions: number; }) => {
forcePrimaryMode?: boolean;
}> = ({ question, maxNumOptions, forcePrimaryMode = false }) => {
const isBinary = isQuestionBinary(question); const isBinary = isQuestionBinary(question);
if (isBinary) { if (isBinary) {
@ -141,26 +119,35 @@ export const QuestionOptions: React.FC<{
if (!isFullQuestionOption(yesOption)) { if (!isFullQuestionOption(yesOption)) {
return null; // missing data return null; // missing data
} }
return ( return (
<OptionRow option={yesOption} mode="primary" textMode="probability" /> <div className="space-x-2">
<span
className={`${primaryForecastColor(
yesOption.probability
)} text-white w-16 rounded-md px-1.5 py-0.5 font-bold`}
>
{formatProbability(yesOption.probability)}
</span>
<span
className={`${textColor(
yesOption.probability
)} text-gray-500 inline-block`}
>
{primaryEstimateAsText(yesOption.probability)}
</span>
</div>
); );
} else { } else {
const optionsSorted = question.options const optionsSorted = question.options
.filter(isFullQuestionOption) .filter(isFullQuestionOption)
.sort((a, b) => b.probability - a.probability); .sort((a, b) => b.probability - a.probability);
const optionsMaxN = optionsSorted.slice(0, maxNumOptions); // display max 5 options. const optionsMax5 = optionsSorted.slice(0, 5); // display max 5 options.
return ( return (
<div className="space-y-2"> <div className="space-y-2">
{optionsMaxN.map((option, i) => ( {optionsMax5.map((option, i) => (
<OptionRow <OptionRow option={option} key={i} />
key={i}
option={option}
mode={forcePrimaryMode ? "primary" : "normal"}
textMode="name"
/>
))} ))}
</div> </div>
); );

View File

@ -1,26 +0,0 @@
import { QuestionFragment } from "../../fragments.generated";
import { getBasePath } from "../../utils";
type Props = {
question: QuestionFragment;
linkToMetaforecast?: boolean;
};
export const QuestionTitle: React.FC<Props> = ({
question,
linkToMetaforecast,
}) => (
<h1 className="sm:text-3xl text-lg">
<a
className="text-black no-underline hover:text-gray-700"
href={
linkToMetaforecast
? getBasePath() + `/questions/${question.id}`
: question.url
}
target="_blank"
>
{question.title}
</a>
</h1>
);

View File

@ -54,5 +54,9 @@ function getStarsColor(numstars: number) {
} }
export const Stars: React.FC<{ num: number }> = ({ num }) => { export const Stars: React.FC<{ num: number }> = ({ num }) => {
return <div className={getStarsColor(num) + " text-xs md:text-lg"}>{getstars(num)}</div>; return (
<div className={`self-center col-span-1 ${getStarsColor(num)}`}>
{getstars(num)}
</div>
);
}; };

View File

@ -1,64 +0,0 @@
import { GetServerSideProps, NextPage } from "next";
import NextError from "next/error";
import { Query } from "../../common/Query";
import { ssrUrql } from "../../urql";
import { QuestionChartOrVisualization } from "../components/QuestionChartOrVisualization";
import { QuestionInfoRow } from "../components/QuestionInfoRow";
import { QuestionTitle } from "../components/QuestionTitle";
import { QuestionPageDocument } from "../queries.generated";
interface Props {
id: string;
}
export const getServerSideProps: GetServerSideProps<Props> = async (
context
) => {
const [ssrCache, client] = ssrUrql();
const id = context.query.id as string;
const question =
(await client.query(QuestionPageDocument, { id }).toPromise()).data
?.result || null;
if (!question) {
context.res.statusCode = 404;
}
return {
props: {
urqlState: ssrCache.extractData(),
id,
question
},
};
};
const EmbedQuestionPage: NextPage<Props> = ({ id }) => {
return (
<div className="block bg-white min-h-screen">
<Query document={QuestionPageDocument} variables={{ id }}>
{({ data: { result: question } }) =>
question ? (
<div className="flex flex-col p-2 w-full h-12/12">
{/*<QuestionTitle question={question} linkToMetaforecast={true} /> */}
<div className="mb-1 mt-1">
<QuestionInfoRow question={question} />
</div>
<div className="mb-0">
<QuestionChartOrVisualization question={question} />
</div>
</div>
) : (
<NextError statusCode={404} />
)
}
</Query>
</div>
);
};
export default EmbedQuestionPage;

View File

@ -1,20 +1,17 @@
import { GetServerSideProps, NextPage } from "next"; import { GetServerSideProps, NextPage } from "next";
import NextError from "next/error"; import { FaExternalLinkAlt } from "react-icons/fa";
import React from "react";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
import { Card } from "../../common/Card"; import { Card } from "../../common/Card";
import { Collapsible } from "../../common/Collapsible";
import { CopyParagraph } from "../../common/CopyParagraph";
import { Layout } from "../../common/Layout"; import { Layout } from "../../common/Layout";
import { LineHeader } from "../../common/LineHeader";
import { Query } from "../../common/Query"; import { Query } from "../../common/Query";
import { QuestionWithHistoryFragment } from "../../fragments.generated"; import { QuestionWithHistoryFragment } from "../../fragments.generated";
import { ssrUrql } from "../../urql"; import { ssrUrql } from "../../urql";
import { getBasePath } from "../../utils";
import { CaptureQuestion } from "../components/CaptureQuestion"; import { CaptureQuestion } from "../components/CaptureQuestion";
import { HistoryChart } from "../components/HistoryChart";
import { IndicatorsTable } from "../components/IndicatorsTable"; import { IndicatorsTable } from "../components/IndicatorsTable";
import { QuestionChartOrVisualization } from "../components/QuestionChartOrVisualization"; import { Stars } from "../components/Stars";
import { QuestionInfoRow } from "../components/QuestionInfoRow";
import { QuestionTitle } from "../components/QuestionTitle";
import { QuestionPageDocument } from "../queries.generated"; import { QuestionPageDocument } from "../queries.generated";
interface Props { interface Props {
@ -43,68 +40,55 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
}; };
}; };
const Section: React.FC<{ title: string; id?: string }> = ({ const Section: React.FC<{ title: string }> = ({ title, children }) => (
title, <div className="space-y-2 flex flex-col items-start">
children, <h2 className="text-xl text-gray-900">{title}</h2>
id,
}) => (
<div className="space-y-4 flex flex-col items-start" id={id}>
<div className="border-b-2 border-gray-200 w-full group">
<h2 className="text-xl leading-3 text-gray-900">
<span>{title}</span>
{id ? (
<>
{" "}
<a
className="text-gray-300 no-underline hidden group-hover:inline"
href={`#${id}`}
>
#
</a>
</>
) : null}
</h2>
</div>
<div>{children}</div> <div>{children}</div>
</div> </div>
); );
const EmbedSection: React.FC<{ question: QuestionWithHistoryFragment }> = ({
question,
}) => {
const url = `https://${getBasePath()}/questions/embed/${question.id}`;
return (
<Section title="Embed" id="embed">
<CopyParagraph
text={`<iframe src="${url}" height="600" width="600" frameborder="0" />`}
buttonText="Copy HTML"
/>
<div className="mt-2">
<Collapsible title="Preview">
{() => <iframe src={url} height="600" width="600" frameBorder="0" />}
</Collapsible>
</div>
</Section>
);
};
const LargeQuestionCard: React.FC<{ const LargeQuestionCard: React.FC<{
question: QuestionWithHistoryFragment; question: QuestionWithHistoryFragment;
}> = ({ question }) => { }> = ({ question }) => (
return (
<Card highlightOnHover={false} large={true}> <Card highlightOnHover={false} large={true}>
<QuestionTitle question={question} /> <h1 className="sm:text-3xl text-xl">
<a
className="text-black no-underline hover:text-gray-700"
href={question.url}
target="_blank"
>
{question.title}{" "}
<FaExternalLinkAlt className="text-gray-400 inline sm:text-3xl text-xl mb-1" />
</a>
</h1>
<div className="mb-5 mt-5"> <div className="flex gap-2 mb-10">
<QuestionInfoRow question={question} /> <a
className="text-black no-underline bg-red-300 rounded p-1 px-2 text-xs hover:text-gray-600"
href={question.url}
target="_blank"
>
{question.platform.label}
</a>
<Stars num={question.qualityIndicators.stars} />
</div> </div>
<div className="mb-10"> <div className="mb-10">
<QuestionChartOrVisualization question={question} /> {question.platform.id === "guesstimate" && question.visualization ? (
<a className="no-underline" href={question.url} target="_blank">
<img
className="rounded-sm"
src={question.visualization}
alt="Guesstimate Screenshot"
/>
</a>
) : (
<HistoryChart question={question} />
)}
</div> </div>
<div className="mx-auto max-w-prose space-y-8"> <div className="mx-auto max-w-prose">
<Section title="Question description" id="description"> <Section title="Question description">
<ReactMarkdown <ReactMarkdown
linkTarget="_blank" linkTarget="_blank"
className="font-normal text-gray-900" className="font-normal text-gray-900"
@ -112,30 +96,35 @@ const LargeQuestionCard: React.FC<{
{question.description.replaceAll("---", "")} {question.description.replaceAll("---", "")}
</ReactMarkdown> </ReactMarkdown>
</Section> </Section>
<Section title="Indicators" id="indicators"> <div className="mt-5">
<Section title="Indicators">
<IndicatorsTable question={question} /> <IndicatorsTable question={question} />
</Section> </Section>
<Section title="Capture" id="capture"> </div>
<CaptureQuestion question={question} />
</Section>
<EmbedSection question={question} />
</div> </div>
</Card> </Card>
); );
};
const QuestionScreen: React.FC<{ question: QuestionWithHistoryFragment }> = ({
question,
}) => (
<div className="space-y-8">
<LargeQuestionCard question={question} />
<div className="space-y-4">
<LineHeader>
<h1>Capture</h1>
</LineHeader>
<CaptureQuestion question={question} />
</div>
</div>
);
const QuestionPage: NextPage<Props> = ({ id }) => { const QuestionPage: NextPage<Props> = ({ id }) => {
return ( return (
<Layout page="question"> <Layout page="question">
<div className="max-w-4xl mx-auto"> <div className="max-w-4xl mx-auto">
<Query document={QuestionPageDocument} variables={{ id }}> <Query document={QuestionPageDocument} variables={{ id }}>
{({ data }) => {({ data }) => <QuestionScreen question={data.result} />}
data.result ? (
<LargeQuestionCard question={data.result} />
) : (
<NextError statusCode={404} />
)
}
</Query> </Query>
</div> </div>
</Layout> </Layout>

View File

@ -7,7 +7,7 @@ export type QuestionPageQueryVariables = Types.Exact<{
}>; }>;
export type QuestionPageQuery = { __typename?: 'Query', result?: { __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, history: Array<{ __typename?: 'History', fetched: number, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }> }>, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } } | null }; export type QuestionPageQuery = { __typename?: 'Query', result: { __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, history: Array<{ __typename?: 'History', timestamp: number, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }> }>, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } } };
export const QuestionPageDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"QuestionPage"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"id"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"result"},"name":{"kind":"Name","value":"question"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"id"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"QuestionWithHistory"}}]}}]}},...QuestionWithHistoryFragmentDoc.definitions]} as unknown as DocumentNode<QuestionPageQuery, QuestionPageQueryVariables>; export const QuestionPageDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"QuestionPage"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"id"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"result"},"name":{"kind":"Name","value":"question"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"id"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"QuestionWithHistory"}}]}}]}},...QuestionWithHistoryFragmentDoc.definitions]} as unknown as DocumentNode<QuestionPageQuery, QuestionPageQueryVariables>;

View File

@ -5,14 +5,14 @@ import { QuestionFragmentDoc } from '../fragments.generated';
export type FrontpageQueryVariables = Types.Exact<{ [key: string]: never; }>; export type FrontpageQueryVariables = Types.Exact<{ [key: string]: never; }>;
export type FrontpageQuery = { __typename?: 'Query', result: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> }; export type FrontpageQuery = { __typename?: 'Query', result: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> };
export type SearchQueryVariables = Types.Exact<{ export type SearchQueryVariables = Types.Exact<{
input: Types.SearchInput; input: Types.SearchInput;
}>; }>;
export type SearchQuery = { __typename?: 'Query', result: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, fetched: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> }; export type SearchQuery = { __typename?: 'Query', result: Array<{ __typename?: 'Question', id: string, url: string, title: string, description: string, timestamp: number, visualization?: string | null, options: Array<{ __typename?: 'ProbabilityOption', name?: string | null, probability?: number | null }>, platform: { __typename?: 'Platform', id: string, label: string }, qualityIndicators: { __typename?: 'QualityIndicators', stars: number, numForecasts?: number | null, numForecasters?: number | null, volume?: number | null, spread?: number | null, sharesVolume?: number | null, openInterest?: number | null, liquidity?: number | null, tradeVolume?: number | null } }> };
export const FrontpageDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Frontpage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"result"},"name":{"kind":"Name","value":"frontpage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Question"}}]}}]}},...QuestionFragmentDoc.definitions]} as unknown as DocumentNode<FrontpageQuery, FrontpageQueryVariables>; export const FrontpageDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Frontpage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"result"},"name":{"kind":"Name","value":"frontpage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Question"}}]}}]}},...QuestionFragmentDoc.definitions]} as unknown as DocumentNode<FrontpageQuery, FrontpageQueryVariables>;

View File

@ -2,7 +2,7 @@ import { QuestionFragment } from "./fragments.generated";
export const getBasePath = () => { export const getBasePath = () => {
if (process.env.NEXT_PUBLIC_VERCEL_URL) { if (process.env.NEXT_PUBLIC_VERCEL_URL) {
return `https://metaforecast.org`;//`https://${process.env.NEXT_PUBLIC_VERCEL_URL}`; return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`;
} }
// can be used for local development if you prefer non-default port // can be used for local development if you prefer non-default port
@ -14,8 +14,8 @@ export const getBasePath = () => {
}; };
export const cleanText = (text: string): string => { export const cleanText = (text: string): string => {
// TODO - move to GraphQL: // Note: should no longer be necessary?
// { description(clean: true, truncate: 250) } // Still needed for e.g. /questions/rootclaim-what-caused-the-disappearance-of-malaysia-airlines-flight-370
let textString = !!text ? text : ""; let textString = !!text ? text : "";
textString = textString textString = textString
.replaceAll("] (", "](") .replaceAll("] (", "](")
@ -23,13 +23,12 @@ export const cleanText = (text: string): string => {
.replaceAll("( [", "([") .replaceAll("( [", "([")
.replaceAll(") ,", "),") .replaceAll(") ,", "),")
.replaceAll("==", "") // Denotes a title in markdown .replaceAll("==", "") // Denotes a title in markdown
.replaceAll(/^#+\s+/gm, "") .replaceAll("Background\n", "")
.replaceAll(/^Background\n/gm, "") .replaceAll("Context\n", "")
.replaceAll(/^Context\n/gm, "")
.replaceAll("--- \n", "- ") .replaceAll("--- \n", "- ")
.replaceAll(/\[(.*?)\]\(.*?\)/g, "$1"); .replaceAll(/\[(.*?)\]\(.*?\)/g, "$1");
textString = textString.slice(0, 1) == "=" ? textString.slice(1) : textString; textString = textString.slice(0, 1) == "=" ? textString.slice(1) : textString;
//console.log(textString)
return textString; return textString;
}; };

View File

@ -96,8 +96,7 @@ export default async function searchWithAlgolia({
title: "No search results match your query", title: "No search results match your query",
url: "https://metaforecast.org", url: "https://metaforecast.org",
platform: "metaforecast", platform: "metaforecast",
platformLabel: "metaforecast", description: "Maybe try a broader query?",
description: "Maybe try a broader query, e.g., reduce the number of 'stars' by clicking in 'Advanced options'?",
options: [ options: [
{ {
name: "Yes", name: "Yes",
@ -110,8 +109,7 @@ export default async function searchWithAlgolia({
type: "PROBABILITY", type: "PROBABILITY",
}, },
], ],
fetched: new Date().toISOString(), timestamp: `${new Date().toISOString().slice(0, 10)}`,
firstSeen: new Date().toISOString(),
qualityindicators: { qualityindicators: {
numforecasts: 1, numforecasts: 1,
numforecasters: 1, numforecasters: 1,
@ -128,11 +126,8 @@ export default async function searchWithAlgolia({
title: `Did you mean: ${queryString}?`, title: `Did you mean: ${queryString}?`,
url: "https://metaforecast.org/recursion?bypassEasterEgg=true", url: "https://metaforecast.org/recursion?bypassEasterEgg=true",
platform: "metaforecast", platform: "metaforecast",
platformLabel: "metaforecast",
description: description:
"Fatal error: Too much recursion. Click to proceed anyways", "Fatal error: Too much recursion. Click to proceed anyways",
fetched: new Date().toISOString(),
firstSeen: new Date().toISOString(),
options: [ options: [
{ {
name: "Yes", name: "Yes",
@ -145,6 +140,7 @@ export default async function searchWithAlgolia({
type: "PROBABILITY", type: "PROBABILITY",
}, },
], ],
timestamp: `${new Date().toISOString().slice(0, 10)}`,
qualityindicators: { qualityindicators: {
numforecasts: 1, numforecasts: 1,
numforecasters: 1, numforecasters: 1,
@ -165,8 +161,7 @@ export default async function searchWithAlgolia({
title: "No search results appear to match your query", title: "No search results appear to match your query",
url: "https://metaforecast.org", url: "https://metaforecast.org",
platform: "metaforecast", platform: "metaforecast",
platformLabel: "metaforecast", description: "Maybe try a broader query? That said, we could be wrong.",
description: "Maybe try a broader query? Maybe try a broader query, e.g., reduce the number of 'stars' by clicking in 'Advanced options'? That said, we could be wrong.",
options: [ options: [
{ {
name: "Yes", name: "Yes",
@ -179,8 +174,7 @@ export default async function searchWithAlgolia({
type: "PROBABILITY", type: "PROBABILITY",
}, },
], ],
fetched: new Date().toISOString(), timestamp: `${new Date().toISOString().slice(0, 10)}`,
firstSeen: new Date().toISOString(),
qualityindicators: { qualityindicators: {
numforecasts: 1, numforecasts: 1,
numforecasters: 1, numforecasters: 1,

View File

@ -5,7 +5,7 @@ export async function uploadToImgur(dataURL: string): Promise<string> {
method: "post", method: "post",
url: "https://api.imgur.com/3/image", url: "https://api.imgur.com/3/image",
headers: { headers: {
Authorization: `Bearer ${process.env.IMGUR_BEARER}`, Authorization: "Bearer 8e9666fb889318515a62208560d4e8393dac26d8",
}, },
data: { data: {
type: "base64", type: "base64",

8912
yarn.lock

File diff suppressed because it is too large Load Diff