data processing

This commit is contained in:
Zynh0722 2024-04-04 01:13:55 -07:00
parent e2bfbf77a5
commit 640a6cdcae

View file

@ -62,11 +62,11 @@ function fetchCache(endpoint: string) {
async function getTBAEndpoint(endpoint: string): Promise<TBAResponse> { async function getTBAEndpoint(endpoint: string): Promise<TBAResponse> {
const time = Date.now(); const time = Date.now();
if (tbaCache[endpoint] && tbaCache[endpoint].staleTime > time) { if (tbaCache[endpoint] && tbaCache[endpoint].staleTime > time) {
console.log("Cache hit... " + endpoint); console.error("Cache hit... " + endpoint);
return { data: fetchCache(endpoint).data, status: 200 }; return { data: fetchCache(endpoint).data, status: 200 };
} }
console.log("Fetching... " + endpoint); console.error("Fetching... " + endpoint);
const headers = new Headers(authHeaders); const headers = new Headers(authHeaders);
if (tbaCache[endpoint]) { if (tbaCache[endpoint]) {
headers.append("If-None-Match", tbaCache[endpoint].etag); headers.append("If-None-Match", tbaCache[endpoint].etag);
@ -89,7 +89,7 @@ async function getTBAEndpoint(endpoint: string): Promise<TBAResponse> {
// Safety: a 304 response indicates out cache is valid based on ETag // Safety: a 304 response indicates out cache is valid based on ETag
data = fetchCache(endpoint).data as string; data = fetchCache(endpoint).data as string;
console.log("Unchanged... " + endpoint); console.error("Unchanged... " + endpoint);
} else { } else {
data = await response.text(); data = await response.text();
} }
@ -104,7 +104,66 @@ const response = await getTBAEndpoint(
"/team/frc4043/event/2024pncmp/matches/simple", "/team/frc4043/event/2024pncmp/matches/simple",
); );
console.log(response.data); const data: any = JSON.parse(response.data);
const matchAlliances = data.map((match: any) => match.alliances);
function filterNerdHerd(teamKeys: string[]) {
return teamKeys.filter((team) => !team.includes("4043"));
}
const matchups = matchAlliances.map((match: any) => {
const blueAlliance = filterNerdHerd(match.blue["team_keys"]);
const redAlliance = filterNerdHerd(match.red["team_keys"]);
if (blueAlliance.length > redAlliance.length) {
return {
with: redAlliance,
against: blueAlliance,
};
} else {
return {
with: blueAlliance,
against: redAlliance,
};
}
}) as { with: string[]; against: string[] }[];
const withOccurences = matchups.reduce(
(acc, matchup) => {
for (const team of matchup.with) {
acc[team] = acc[team] + 1 || 1;
}
return acc;
},
{} as { [team: string]: number },
);
const againstOccurences = matchups.reduce(
(acc, matchup) => {
for (const team of matchup.against) {
acc[team] = acc[team] + 1 || 1;
}
return acc;
},
{} as { [team: string]: number },
);
console.log("With:");
for (const team of Object.entries(withOccurences).toSorted(
(a, b) => b[1] - a[1],
)) {
console.log(team);
}
console.log("\nAgainst:");
for (const team of Object.entries(againstOccurences).toSorted(
(a, b) => b[1] - a[1],
)) {
console.log(team);
}
writeCache(); writeCache();