-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
414fc31
commit 749a704
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
VFiniteNumber, | ||
VHumanString, | ||
VInteger, | ||
VStruct, | ||
Valid, | ||
} from "@wzlin/valid"; | ||
import assertInstanceOf from "@xtjs/lib/assertInstanceOf"; | ||
import { QueryGroupByOutput, makeQuery } from "../query"; | ||
|
||
const input = new VStruct({ | ||
query: new VHumanString(1, 512), | ||
simMinHundredths: new VInteger(80, 100), | ||
}); | ||
|
||
export const endpointAnalyzePopularity = { | ||
input, | ||
handler: async ({ query, simMinHundredths }: Valid<typeof input>) => { | ||
const simThreshold = simMinHundredths / 100; | ||
const res = await makeQuery({ | ||
dataset: "post", | ||
queries: [query], | ||
scales: { | ||
sim: { min: simThreshold, max: 1.0 }, | ||
}, | ||
post_filter_clip: { | ||
sim: { min: simThreshold, max: 1.0 }, | ||
// Some posts have UNIX timestamp 0. | ||
ts_day: { min: 1, max: Number.MAX_SAFE_INTEGER }, | ||
}, | ||
weights: { | ||
votes: "sim", | ||
}, | ||
outputs: [ | ||
{ | ||
group_by: { | ||
by: "ts_day", | ||
bucket: 7, | ||
cols: [["final_score", "sum"]], | ||
}, | ||
}, | ||
], | ||
}); | ||
const data = assertInstanceOf(res[0], QueryGroupByOutput); | ||
return { | ||
timestamps: [...data.groups(new VInteger())].map( | ||
(d) => new Date(d * 7 * 24 * 60 * 60 * 1000), | ||
), | ||
scores: [...data.column("final_score", new VFiniteNumber())], | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
VFiniteNumber, | ||
VHumanString, | ||
VInteger, | ||
VStruct, | ||
Valid, | ||
} from "@wzlin/valid"; | ||
import assertInstanceOf from "@xtjs/lib/assertInstanceOf"; | ||
import { QueryItemsOutput, makeQuery } from "../query"; | ||
|
||
const input = new VStruct({ | ||
query: new VHumanString(1, 512), | ||
limit: new VInteger(1, 20), | ||
simMinHundredths: new VInteger(80, 100), | ||
}); | ||
|
||
export const endpointTopPosts = { | ||
input, | ||
handler: async ({ query, limit, simMinHundredths }: Valid<typeof input>) => { | ||
const simThreshold = simMinHundredths / 100; | ||
const res = await makeQuery({ | ||
dataset: "post", | ||
queries: [query], | ||
post_filter_clip: { | ||
sim: { min: simThreshold, max: 1 }, | ||
}, | ||
outputs: [ | ||
{ | ||
items: { | ||
cols: ["id", "sim"], | ||
limit, | ||
order_asc: false, | ||
order_by: "votes", | ||
}, | ||
}, | ||
], | ||
}); | ||
const data = assertInstanceOf(res[0], QueryItemsOutput); | ||
return [ | ||
...data.items({ | ||
id: new VInteger(1), | ||
sim: new VFiniteNumber(), | ||
}), | ||
]; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
VFiniteNumber, | ||
VHumanString, | ||
VInteger, | ||
VString, | ||
VStruct, | ||
Valid, | ||
} from "@wzlin/valid"; | ||
import assertInstanceOf from "@xtjs/lib/assertInstanceOf"; | ||
import { QueryGroupByOutput, makeQuery } from "../query"; | ||
|
||
const input = new VStruct({ | ||
query: new VHumanString(1, 512), | ||
limit: new VInteger(1, 20), | ||
simMinHundredths: new VInteger(80, 100), | ||
}); | ||
|
||
export const endpointTopUsers = { | ||
input, | ||
handler: async ({ query, limit, simMinHundredths }: Valid<typeof input>) => { | ||
const simThreshold = simMinHundredths / 100; | ||
const res = await makeQuery({ | ||
dataset: "comment", | ||
queries: [query], | ||
scales: { | ||
sim: { | ||
min: simThreshold, | ||
max: 1, | ||
}, | ||
}, | ||
weights: { | ||
// We can't multiply by votes, because the HN API does not expose votes for anything except posts. | ||
sim_scaled: 1, | ||
}, | ||
outputs: [ | ||
{ | ||
group_by: { | ||
by: "user", | ||
cols: [["final_score", "sum"]], | ||
order_by: "final_score", | ||
order_asc: false, | ||
limit, | ||
}, | ||
}, | ||
], | ||
}); | ||
const data = assertInstanceOf(res[0], QueryGroupByOutput); | ||
return [ | ||
...data.entries(new VString(), { | ||
final_score: new VFiniteNumber(), | ||
}), | ||
].map((e) => ({ | ||
user: e[0], | ||
score: e[1].final_score, | ||
})); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters