Key Waka data by ID, not name
This commit is contained in:
parent
742a5019d8
commit
cefba8c30c
1 changed files with 33 additions and 6 deletions
|
@ -9,10 +9,14 @@ const beeline = require("honeycomb-beeline")({
|
||||||
|
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
|
import connectToDb from "../src/server/db";
|
||||||
|
|
||||||
async function handle(req, res) {
|
async function handle(req, res) {
|
||||||
let itemValues;
|
const allNcItemNamesAndIdsPromise = loadAllNcItemNamesAndIds();
|
||||||
|
|
||||||
|
let itemValuesByName;
|
||||||
try {
|
try {
|
||||||
itemValues = await loadWakaValues();
|
itemValuesByName = await loadWakaValuesByName();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
res.setHeader("Content-Type", "text/plain");
|
res.setHeader("Content-Type", "text/plain");
|
||||||
|
@ -20,6 +24,15 @@ async function handle(req, res) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restructure the value data to use IDs as keys, instead of names.
|
||||||
|
const allNcItemNamesAndIds = await allNcItemNamesAndIdsPromise;
|
||||||
|
const itemValues = {};
|
||||||
|
for (const { name, id } of allNcItemNamesAndIds) {
|
||||||
|
if (name in itemValuesByName) {
|
||||||
|
itemValues[id] = itemValuesByName[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cache for 1 minute, and immediately serve stale data for a day after.
|
// Cache for 1 minute, and immediately serve stale data for a day after.
|
||||||
// This should keep it fast and responsive, and stay well within our API key
|
// This should keep it fast and responsive, and stay well within our API key
|
||||||
// limits. (This will cause the client to send more requests than necessary,
|
// limits. (This will cause the client to send more requests than necessary,
|
||||||
|
@ -32,7 +45,21 @@ async function handle(req, res) {
|
||||||
return res.send(itemValues);
|
return res.send(itemValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadWakaValues() {
|
async function loadAllNcItemNamesAndIds() {
|
||||||
|
const db = await connectToDb();
|
||||||
|
|
||||||
|
const [rows] = await db.query(`
|
||||||
|
SELECT items.id, item_translations.name FROM items
|
||||||
|
INNER JOIN item_translations ON item_translations.item_id = items.id
|
||||||
|
WHERE
|
||||||
|
items.rarity_index IN (0, 500) OR is_manually_nc = 1
|
||||||
|
AND item_translations.locale = "en"
|
||||||
|
`);
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadWakaValuesByName() {
|
||||||
if (!process.env["GOOGLE_API_KEY"]) {
|
if (!process.env["GOOGLE_API_KEY"]) {
|
||||||
throw new Error(`GOOGLE_API_KEY environment variable must be provided`);
|
throw new Error(`GOOGLE_API_KEY environment variable must be provided`);
|
||||||
}
|
}
|
||||||
|
@ -63,12 +90,12 @@ async function loadWakaValues() {
|
||||||
// Reformat the rows as a map from item name to value. We offer the item data
|
// Reformat the rows as a map from item name to value. We offer the item data
|
||||||
// as an object with a single field `value` for extensibility, but we omit
|
// as an object with a single field `value` for extensibility, but we omit
|
||||||
// the spreadsheet columns that we don't use on DTI, like Notes.
|
// the spreadsheet columns that we don't use on DTI, like Notes.
|
||||||
const itemValues = {};
|
const itemValuesByName = {};
|
||||||
for (const [itemName, value] of rows) {
|
for (const [itemName, value] of rows) {
|
||||||
itemValues[itemName] = { value };
|
itemValuesByName[itemName] = { value };
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemValues;
|
return itemValuesByName;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
|
|
Loading…
Reference in a new issue