Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict typescript #79

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
759 changes: 160 additions & 599 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@
"@pothos/plugin-prisma": "^3.4.0",
"@pothos/plugin-relay": "^3.10.0",
"@prisma/client": "^3.11.1",
"@quri/squiggle-lang": "^0.2.8",
"@tailwindcss/forms": "^0.4.0",
"@tailwindcss/typography": "^0.5.1",
"@types/chroma-js": "^2.1.3",
"@types/dom-to-image": "^2.6.4",
"@types/google-spreadsheet": "^3.2.1",
"@types/jsdom": "^16.2.14",
"@types/nprogress": "^0.2.0",
"@types/react": "^17.0.39",
"@types/react-copy-to-clipboard": "^5.0.2",
"@types/textversionjs": "^1.1.1",
"@types/tunnel": "^0.0.3",
"airtable": "^0.11.1",
"algoliasearch": "^4.10.3",
"autoprefixer": "^10.1.0",
Expand Down Expand Up @@ -86,7 +91,6 @@
"react-safe": "^1.3.0",
"react-select": "^5.2.2",
"remark-gfm": "^3.0.1",
"squiggle-experimental": "^0.1.9",
"tabletojson": "^2.0.4",
"tailwindcss": "^3.0.22",
"textversionjs": "^1.1.3",
Expand Down
9 changes: 1 addition & 8 deletions src/backend/flow/doEverything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import { executeJobByName } from "./jobs";

/* Do everything */
export async function doEverything() {
let jobNames = [
...platforms.map((platform) => platform.name),
"merge",
"algolia",
"history",
"netlify",
];
// Removed Good Judgment from the fetcher, doing it using cron instead because cloudflare blocks the utility on heroku.
let jobNames = [...platforms.map((platform) => platform.name), "algolia"];

console.log("");
console.log("");
Expand Down
11 changes: 0 additions & 11 deletions src/backend/flow/history/updateHistory.ts

This file was deleted.

55 changes: 27 additions & 28 deletions src/backend/flow/jobs.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
import { doEverything } from "../flow/doEverything";
import { updateHistory } from "../flow/history/updateHistory";
import { rebuildNetlifySiteWithNewData } from "../flow/rebuildNetliftySiteWithNewData";
import { rebuildFrontpage } from "../frontpage";
import { platforms, processPlatform } from "../platforms";
import { rebuildAlgoliaDatabase } from "../utils/algolia";
import { sleep } from "../utils/sleep";

interface Job {
interface Job<ArgNames extends string = ""> {
name: string;
message: string;
run: () => Promise<void>;
args?: ArgNames[];
run: (args?: { [k in ArgNames]: string }) => Promise<void>;
separate?: boolean;
}

export const jobs: Job[] = [
export const jobs: Job<string>[] = [
...platforms.map((platform) => ({
name: platform.name,
message: `Download predictions from ${platform.name}`,
run: () => processPlatform(platform),
...(platform.version === "v2" ? { args: platform.fetcherArgs } : {}),
run: (args: any) => processPlatform(platform, args),
})),
{
name: "algolia",
message: 'Rebuild algolia database ("index")',
run: rebuildAlgoliaDatabase,
},
{
name: "history",
message: "Update history",
run: updateHistory,
},
{
name: "netlify",
message: `Rebuild netlify site with new data`,
run: rebuildNetlifySiteWithNewData,
},
{
name: "frontpage",
message: "Rebuild frontpage",
Expand All @@ -46,31 +37,39 @@ export const jobs: Job[] = [
},
];

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function tryCatchTryAgain(fun: () => Promise<void>) {
async function tryCatchTryAgain<T extends object = never>(
fun: (args: T) => Promise<void>,
args: T
) {
try {
console.log("Initial try");
await fun();
await fun(args);
} catch (error) {
sleep(10000);
console.log("Second try");
console.log(error);
try {
await fun();
await fun(args);
} catch (error) {
console.log(error);
}
}
}

export const executeJobByName = async (option: string) => {
const job = jobs.find((job) => job.name === option);
export const executeJobByName = async (
jobName: string,
jobArgs: { [k: string]: string } = {}
) => {
const job = jobs.find((job) => job.name === jobName);
if (!job) {
console.log(`Error, job ${option} not found`);
} else {
await tryCatchTryAgain(job.run);
console.log(`Error, job ${jobName} not found`);
return;
}
for (const key of Object.keys(jobArgs)) {
if (!job.args || job.args.indexOf(key) < 0) {
throw new Error(`Job ${jobName} doesn't accept ${key} argument`);
}
}

await tryCatchTryAgain(job.run, jobArgs);
};
15 changes: 0 additions & 15 deletions src/backend/flow/rebuildNetliftySiteWithNewData.ts

This file was deleted.

64 changes: 46 additions & 18 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import "dotenv/config";

import readline from "readline";
import util from "util";

import { executeJobByName, jobs } from "./flow/jobs";

let generateWhatToDoMessage = () => {
const generateWhatToDoMessage = () => {
const color = "\x1b[36m";
const resetColor = "\x1b[0m";
let completeMessages = [
Expand All @@ -23,27 +22,56 @@ let generateWhatToDoMessage = () => {
return completeMessages;
};

let whattodoMessage = generateWhatToDoMessage();
const whattodoMessage = generateWhatToDoMessage();

/* BODY */
let commandLineUtility = async () => {
const pickOption = async () => {
if (process.argv.length === 3) {
return process.argv[2]; // e.g., npm run cli polymarket
}
const askForJobName = async () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
const question = (query: string) => {
return new Promise((resolve: (s: string) => void) => {
rl.question(query, resolve);
});

const question = util.promisify(rl.question).bind(rl);
const answer = await question(whattodoMessage);
rl.close();
return answer;
};

await executeJobByName(await pickOption());
const answer = await question(whattodoMessage);
rl.close();

return answer;
};

const pickJob = async (): Promise<[string, { [k: string]: string }]> => {
if (process.argv.length < 3) {
const jobName = await askForJobName();
return [jobName, {}]; // e.g., npm run cli polymarket
}

const jobName = process.argv[2];
if ((process.argv.length - 3) % 2) {
throw new Error("Number of extra arguments must be even");
}

const args: { [k: string]: string } = {};
for (let i = 3; i < process.argv.length; i += 2) {
let argName = process.argv[i];
const argValue = process.argv[i + 1];
if (argName.slice(0, 2) !== "--") {
throw new Error(`${argName} should start with --`);
}
argName = argName.slice(2);
args[argName] = argValue;
}

return [jobName, args];
};

/* BODY */
const commandLineUtility = async () => {
const [jobName, jobArgs] = await pickJob();

await executeJobByName(jobName, jobArgs);
process.exit();
};

Expand Down
15 changes: 7 additions & 8 deletions src/backend/platforms/_example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Imports */
import axios from "axios";

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

/* Definitions */
Expand All @@ -22,11 +21,11 @@ async function fetchData() {
return response;
}

async function processPredictions(predictions) {
async function processPredictions(predictions: any[]) {
let results = await predictions.map((prediction) => {
const id = `${platformName}-${prediction.id}`;
const probability = prediction.probability;
const options = [
const options: FetchedQuestion["options"] = [
{
name: "Yes",
probability: probability,
Expand All @@ -41,14 +40,10 @@ async function processPredictions(predictions) {
const result: FetchedQuestion = {
id,
title: prediction.title,
url: `https://example.com`,
platform: platformName,
url: "https://example.com",
description: prediction.description,
options,
qualityindicators: {
stars: calculateStars(platformName, {
/* some: somex, factors: factors */
}),
// other: prediction.otherx,
// indicators: prediction.indicatorx,
},
Expand All @@ -64,9 +59,13 @@ export const example: Platform = {
name: platformName,
label: "Example platform",
color: "#ff0000",
version: "v1",
async fetcher() {
let data = await fetchData();
let results = await processPredictions(data); // somehow needed
return results;
},
calculateStars(data) {
return 2;
},
};
Loading