Skip to content

Commit

Permalink
api remaining tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHeitmann committed Jan 6, 2024
1 parent bc44476 commit f938f8b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/serverScripts/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ async function trackGenericProperty(key: string, value: string, data2: string):
}
}

type RedditApiUsageRecord = { clientId: string, timeMillisUtc: number, endpoint: string, api?: string };
type RedditApiUsageRecord = { clientId: string, timeMillisUtc: number, endpoint: string, api?: string, used?: number, remaining?: number };
async function trackRedditApiUsage(records: RedditApiUsageRecord[]): Promise<void> {
const connection = await getConnection();
if (connection === null)
Expand All @@ -258,9 +258,9 @@ async function trackRedditApiUsage(records: RedditApiUsageRecord[]): Promise<voi
try {
await connection.query(`
INSERT INTO redditApiUsage
(clientId, timeMillisUtc, endpoint, api)
(clientId, timeMillisUtc, endpoint, api, used, remaining)
VALUES ${records
.map(record => `(${connection.escape(record.clientId)}, ${connection.escape(record.timeMillisUtc)}, ${connection.escape(record.endpoint)}, ${connection.escape(record.api)})`)
.map(record => `(${connection.escape(record.clientId)}, ${connection.escape(record.timeMillisUtc)}, ${connection.escape(record.endpoint)}, ${connection.escape(record.api)}, ${connection.escape(record.used)}, ${connection.escape(record.remaining)})`)
.join(",")
}
`);
Expand Down Expand Up @@ -431,7 +431,9 @@ analyticsRouter.post("/redditApiUsage", safeExcAsync(async (req, res) => {
typeof record.clientId === "string" &&
typeof record.timeMillisUtc === "number" &&
record.timeMillisUtc >= 0 &&
typeof record.endpoint === "string"
typeof record.endpoint === "string" &&
(!("used" in record) || typeof record.used === "number") &&
(!("remaining" in record) || typeof record.remaining === "number")
)
}
if (!(records instanceof Array) || !records.every(isRecordValid)) {
Expand All @@ -441,6 +443,8 @@ analyticsRouter.post("/redditApiUsage", safeExcAsync(async (req, res) => {
for (const record of records) {
record.endpoint = record.endpoint.slice(0, 64);
record.api ??= null;
record.used ??= null;
record.remaining ??= null;
}
try {
await trackRedditApiUsage(records);
Expand Down
4 changes: 2 additions & 2 deletions src/static/scripts/api/redditApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
RedditSubredditObj,
RedditUserObj
} from "../types/redditTypes";
import {isObjectEmpty, randomString, splitPathQuery, throttle} from "../utils/utils";
import {isObjectEmpty, randomString, splitPathQuery, strToNumNonNan, throttle} from "../utils/utils";
import {SortPostsOrder, UserSection} from "../types/misc";
import {onApiUsage} from "./redditApiUsageTracking";

Expand All @@ -42,7 +42,6 @@ export async function redditApiRequest(pathAndQuery, params: string[][] | any, r
async function oauth2Request(pathAndQuery, params: string[][] | any, options: RequestInit, attempt = 0) {
pathAndQuery = fixUrl(pathAndQuery);
const [path, query] = splitPathQuery(pathAndQuery);
onApiUsage(path, "reddit");

let fetchOptions: RequestInit = {
...options,
Expand Down Expand Up @@ -70,6 +69,7 @@ async function oauth2Request(pathAndQuery, params: string[][] | any, options: Re
try {
const response = await fetch(`https://oauth.reddit.com${ path }?${ useUrlParams ? parameters.toString() : "" }`, fetchOptions);
const responseText = await response.text();
onApiUsage(path, "reddit", true, strToNumNonNan(response.headers.get("x-ratelimit-used")), strToNumNonNan(response.headers.get("x-ratelimit-remaining")));
rateLimitCheck(response.headers);
return responseText ? JSON.parse(responseText) : {};
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion src/static/scripts/api/redditApiUsageTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import Users from "../multiUser/userManagement";
import {RedditApiUsageRecord} from "../types/misc";
import {trackApiUsage} from "./photonApi";

export async function onApiUsage(endpoint: string, api: string, generalize = true) {
export async function onApiUsage(endpoint: string, api: string, generalize = true, used: number|undefined = undefined, remaining: number|undefined = undefined) {
if (generalize)
endpoint = generalizeEndpoint(endpoint);
const newRecord: RedditApiUsageRecord = {
clientId: Users.global.d.analytics.clientId,
timeMillisUtc: Date.now(),
endpoint,
api,
used,
remaining
};
let pendingUsages = Users.global.d.pendingRedditApiUsages;
pendingUsages = [...pendingUsages, newRecord];
Expand Down
2 changes: 2 additions & 0 deletions src/static/scripts/types/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,6 @@ export interface RedditApiUsageRecord {
timeMillisUtc: number;
endpoint: string;
api: string;
used?: number;
remaining?: number;
}
5 changes: 5 additions & 0 deletions src/static/scripts/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,8 @@ export function fromBase36(numB36: string): number {
}
return num;
}

export function strToNumNonNan<T>(numStr: string, fallback: T = undefined): number|T {
const num = Number(numStr);
return isNaN(num) ? fallback : num;
}

0 comments on commit f938f8b

Please sign in to comment.