Skip to content

Commit

Permalink
enable event limiting / pagination via WCL's built-in event count mec…
Browse files Browse the repository at this point in the history
…hanism
  • Loading branch information
emallson committed Jul 24, 2024
1 parent 6aafa94 commit 7efe7e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/route/wcl/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as api from "../../wcl/api";
import { gql } from "graphql-request";
import { ReportParams, camelCase, compress, wrapEndpoint } from "./common";

const EVENT_LIMIT = 20000;

// TODO: migrate useAbilityIDs to true.
// requires frontend changes, but means we no longer need to compress the event response (probably)
// requires frontend changes, but is more efficient
const eventQuery = gql`
query getEvents(
$code: String!
Expand All @@ -12,6 +14,7 @@ const eventQuery = gql`
$endTime: Float!
$playerId: Int
$filter: String
$limit: Int!
) {
reportData {
report(code: $code) {
Expand All @@ -23,6 +26,7 @@ const eventQuery = gql`
filterExpression: $filter
includeResources: true
useAbilityIDs: false
limit: $limit
) {
data
nextPageTimestamp
Expand Down Expand Up @@ -67,6 +71,7 @@ const events = wrapEndpoint<EventsQuery>(
endTime: number;
playerId?: number;
filter?: string;
limit: number;
}
>(eventQuery, {
code: req.params.code,
Expand All @@ -75,6 +80,7 @@ const events = wrapEndpoint<EventsQuery>(
endTime: Number(req.query.end),
playerId: req.query.actorid ? Number(req.query.actorid) : undefined,
filter: req.query.filter,
limit: EVENT_LIMIT,
});
const { data: events, nextPageTimestamp } =
rawData.reportData.report.events;
Expand All @@ -90,7 +96,10 @@ const events = wrapEndpoint<EventsQuery>(
);
export default events;

export const eventsByType = wrapEndpoint<EventsQuery, ReportParams & { type: string }>(
export const eventsByType = wrapEndpoint<
EventsQuery,
ReportParams & { type: string }
>(
"/i/v1/report/events/:type/:code",
"wcl-events",
async (req) => {
Expand All @@ -104,6 +113,7 @@ export const eventsByType = wrapEndpoint<EventsQuery, ReportParams & { type: str
playerId?: number;
filter?: string;
type?: string;
limit: number;
}
>(eventQuery, {
type: req.params.type ? camelCase(req.params.type) : undefined,
Expand All @@ -113,6 +123,7 @@ export const eventsByType = wrapEndpoint<EventsQuery, ReportParams & { type: str
endTime: Number(req.query.end),
playerId: req.query.actorid ? Number(req.query.actorid) : undefined,
filter: req.query.filter,
limit: EVENT_LIMIT,
});
const { data: events, nextPageTimestamp } =
rawData.reportData.report.events;
Expand Down
6 changes: 4 additions & 2 deletions src/wcl/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { request, Variables } from "graphql-request";
import axios from "axios";

const HOST = "https://www.warcraftlogs.com";

async function fetchToken(): Promise<string | undefined> {
const basicAuth = Buffer.from(
`${process.env.WCL_CLIENT_ID}:${process.env.WCL_CLIENT_SECRET}`,
).toString("base64");
const response = await axios.postForm(
"https://www.warcraftlogs.com/oauth/token",
`${HOST}/oauth/token`,
{
grant_type: "client_credentials",
},
Expand Down Expand Up @@ -37,7 +39,7 @@ export async function query<T, V extends Variables>(
): Promise<T> {
let token = await getToken();
const run = () =>
request<T>("https://www.warcraftlogs.com/api/v2/client", gql, variables, {
request<T>(`${HOST}/api/v2/client`, gql, variables, {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
"Accept-Encoding": "deflate,gzip",
Expand Down

0 comments on commit 7efe7e0

Please sign in to comment.