-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
222 lines (194 loc) · 6.31 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { Question } from "@prisma/client";
import { QuestionOption } from "../../common/types";
import { prisma } from "../database/prisma";
// This file includes comon types and functions for working with platforms.
// The registry of all platforms is in a separate file, ./registry.ts, to avoid circular dependencies.
export interface QualityIndicators {
stars: number;
numforecasts?: number | string;
numforecasters?: number;
liquidity?: number | string;
volume?: number;
volume7Days?: number;
volume24Hours?: number;
address?: number;
tradevolume?: string;
pool?: any;
createdTime?: any;
shares_volume?: any;
yes_bid?: any;
yes_ask?: any;
spread?: any;
open_interest?: any;
trade_volume?: any;
}
export type FetchedQuestion = Omit<
Question,
| "extra"
| "qualityindicators"
| "fetched"
| "firstSeen"
| "platform"
| "options"
> & {
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
qualityindicators: Omit<QualityIndicators, "stars">; // slightly stronger type than Prisma's JsonValue
};
// fetcher should return null if platform failed to fetch questions for some reason
type PlatformFetcherV1 = () => Promise<FetchedQuestion[] | null>;
type PlatformFetcherV2Result = {
questions: FetchedQuestion[];
// if partial is true then we won't cleanup old questions from the database; this is useful when manually invoking a fetcher with arguments for updating a single question
partial: boolean;
} | null;
type PlatformFetcherV2<ArgNames extends string> = (opts: {
args?: { [k in ArgNames]: string };
}) => Promise<PlatformFetcherV2Result>;
export type PlatformFetcher<ArgNames extends string> =
| PlatformFetcherV1
| PlatformFetcherV2<ArgNames>;
// using "" as ArgNames default is technically incorrect, but shouldn't cause any real issues
// (I couldn't find a better solution for signifying an empty value, though there probably is one)
export type Platform<ArgNames extends string = ""> = {
name: string; // short name for ids and `platform` db column, e.g. "xrisk"
label: string; // longer name for displaying on frontend etc., e.g. "X-risk estimates"
color: string; // used on frontend
calculateStars: (question: FetchedQuestion) => number;
} & (
| {
version: "v1";
fetcher?: PlatformFetcherV1;
}
| {
version: "v2";
fetcherArgs?: ArgNames[];
fetcher?: PlatformFetcherV2<ArgNames>;
}
);
// Typing notes:
// There's a difference between prisma's Question type (type returned from `find` and `findMany`) and its input types due to JsonValue vs InputJsonValue mismatch.
// On the other hand, we can't use Prisma.QuestionUpdateInput or Prisma.QuestionCreateManyInput either, because we use this question in guesstimate's code for preparing questions from guesstimate models...
// 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<
Question,
"extra" | "qualityindicators" | "options" | "fetched" | "firstSeen"
> & {
fetched: Date;
extra: NonNullable<Question["extra"]>;
qualityindicators: NonNullable<Question["qualityindicators"]>;
options: NonNullable<Question["options"]>;
};
export const prepareQuestion = (
q: FetchedQuestion,
platform: Platform<any>
): PreparedQuestion => {
return {
extra: {},
...q,
fetched: new Date(),
platform: platform.name,
qualityindicators: {
...q.qualityindicators,
stars: platform.calculateStars(q),
},
};
};
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 = "">(
platform: Platform<T>,
args?: { [k in T]: string }
) => {
if (!platform.fetcher) {
console.log(`Platform ${platform.name} doesn't have a fetcher, skipping`);
return;
}
const result =
platform.version === "v1"
? { questions: await platform.fetcher(), partial: false } // this is not exactly PlatformFetcherV2Result, since `questions` can be null
: await platform.fetcher({ args });
if (!result) {
console.log(`Platform ${platform.name} didn't return any results`);
return;
}
const { questions: fetchedQuestions, partial } = result;
if (!fetchedQuestions || !fetchedQuestions.length) {
console.log(`Platform ${platform.name} didn't return any results`);
return;
}
const oldQuestions = await prisma.question.findMany({
where: {
platform: platform.name,
},
});
const fetchedIds = fetchedQuestions.map((q) => q.id);
const oldIds = oldQuestions.map((q) => q.id);
const fetchedIdsSet = new Set(fetchedIds);
const oldIdsSet = new Set(oldIds);
const createdQuestions: PreparedQuestion[] = [];
const updatedQuestions: PreparedQuestion[] = [];
const deletedIds = oldIds.filter((id) => !fetchedIdsSet.has(id));
for (const q of fetchedQuestions.map((q) => prepareQuestion(q, platform))) {
if (oldIdsSet.has(q.id)) {
// TODO - check if question has changed for better performance
updatedQuestions.push(q);
} else {
createdQuestions.push(q);
}
}
const stats: { created?: number; updated?: number; deleted?: number } = {};
await prisma.question.createMany({
data: createdQuestions.map((q) => ({
...q,
firstSeen: new Date(),
})),
});
stats.created = createdQuestions.length;
for (const q of updatedQuestions) {
await prisma.question.update({
where: { id: q.id },
data: q,
});
stats.updated ??= 0;
stats.updated++;
}
if (!partial) {
await prisma.question.deleteMany({
where: {
id: {
in: deletedIds,
},
},
});
stats.deleted = deletedIds.length;
}
await prisma.history.createMany({
data: [...createdQuestions, ...updatedQuestions].map((q) => ({
...q,
idref: q.id,
})),
});
console.log(
"Done, " +
Object.entries(stats)
.map(([k, v]) => `${v} ${k}`)
.join(", ")
);
};
export interface PlatformConfig {
name: string;
label: string;
color: string;
}