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

enable event limiting / pagination via WCL's built-in event count mechanism #62

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps a cheeky environment variable?


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
Loading