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

🎨 Refactor: move route func's to separate handlers #2670

Draft
wants to merge 58 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
a2bac9d
:art: refactor: move matches route handler to separate module
ff137 Sep 15, 2023
3fb904f
:art: refactor: move playersByRank route handler to separate module
ff137 Sep 15, 2023
83c56b9
:art: refactor: move getPlayersByAccountId route handler to players m…
ff137 Sep 15, 2023
da1ede6
:art: refactor: move getPlayersByAccountIdWl route handler to players…
ff137 Sep 15, 2023
e5bda04
:art: refactor: move getPlayersByAccountIdRecentMatches route handler…
ff137 Sep 15, 2023
427f04e
:art: refactor: move getPlayersByAccountIdMatches route handler to pl…
ff137 Sep 15, 2023
a7064d0
:art: refactor: move getPlayersByAccountIdHeroes route handler to pla…
ff137 Sep 15, 2023
7334731
:art: refactor: move getPlayersByAccountIdPeers route handler to play…
ff137 Sep 15, 2023
2632f2d
:art: refactor: move getPlayersByAccountIdPros route handler to playe…
ff137 Sep 15, 2023
31bc13b
:art: refactor: move getPlayersByAccountIdTotals route handler to pla…
ff137 Sep 15, 2023
f1d7274
:art: refactor: move getPlayersByAccountIdCounts route handler to pla…
ff137 Sep 15, 2023
df7f01d
:art: refactor: move getPlayersByAccountIdHistogramsByField route han…
ff137 Sep 15, 2023
0ddf937
:art: refactor: move getPlayersByAccountIdWardMap route handler to pl…
ff137 Sep 15, 2023
bcb3cd0
:art: refactor: move getPlayersByAccountIdWordCloud route handler to …
ff137 Sep 15, 2023
32b9c89
:art: refactor: move getPlayersByAccountIdRatings route handler to pl…
ff137 Sep 15, 2023
4dfd502
:art: refactor: move getPlayersByAccountIdRankings route handler to p…
ff137 Sep 15, 2023
26774c7
:art: refactor: move getPlayersByAccountIdRefresh route handler to pl…
ff137 Sep 15, 2023
97fbef6
:art: refactor: move getProPlayers route handler to players module
ff137 Sep 15, 2023
39d8050
exports
ff137 Sep 15, 2023
cd75ab9
:art: imports
ff137 Sep 15, 2023
5813fc0
:art: drop `async` from method definition, as await is not used
ff137 Sep 15, 2023
41b130a
:art: refactor: move getProMatches route handler to matches module
ff137 Sep 15, 2023
f8b22e6
:art: refactor: move getPublicMatches route handler to matches module
ff137 Sep 15, 2023
0614143
:art: refactor: move getParsedMatches route handler to matches module
ff137 Sep 15, 2023
f549ee7
:art: refactor: move getHeroRankings route handler to heroes module
ff137 Sep 15, 2023
ba82edf
:art: refactor: move searchPlayers route handler to players module
ff137 Sep 15, 2023
a7a1523
:art: refactor: move findMatches route handler to matches module
ff137 Sep 15, 2023
b59f730
:art: refactor: move getHeroBenchmarks route handler to heroes module
ff137 Sep 15, 2023
5b03eb4
:art: refactor: move getHeroData route handler to heroes module
ff137 Sep 15, 2023
a2e3da3
:art: refactor: move getHeroStats route handler to heroes module
ff137 Sep 15, 2023
488348e
:art: refactor: move getRecentMatchesByHeroId route handler to heroes…
ff137 Sep 15, 2023
6284575
:art: refactor: move getMatchupsByHeroId route handler to heroes module
ff137 Sep 15, 2023
910c3d4
:art: refactor: move getMatchDurationsByHeroId route handler to heroe…
ff137 Sep 15, 2023
24ecf8b
:art: refactor: move getPlayersByHeroId route handler to heroes module
ff137 Sep 15, 2023
0154323
:art: refactor: move getItemPopularityByHeroId route handler to heroe…
ff137 Sep 15, 2023
0de8e7e
:art:
ff137 Sep 15, 2023
2b1ea95
:art: refactor: move getTeamsData route handler to teams module
ff137 Sep 15, 2023
49a971a
:art: refactor: move getTeamById route handler to teams module
ff137 Sep 15, 2023
746d9a9
:art: refactor: move getMatchesByTeamId route handler to teams module
ff137 Sep 15, 2023
2f2a621
:art: refactor: move getPlayersByTeamId route handler to teams module
ff137 Sep 15, 2023
e557db7
:art: refactor: move getHeroesByTeamId route handler to teams module
ff137 Sep 15, 2023
2aa319c
:art: refactor: move explorer route handler to database module
ff137 Sep 15, 2023
d93be57
:art: refactor: move getLeagues route handler to leagues module
ff137 Sep 15, 2023
75b3386
:art: refactor: move getLeaguesById route handler to leagues module
ff137 Sep 15, 2023
c592f82
:art: refactor: move getMatchesByLeagueId route handler to leagues mo…
ff137 Sep 15, 2023
b805713
:art: refactor: move getTeamsByLeagueId route handler to leagues module
ff137 Sep 15, 2023
dc34b17
:art: refactor: move getSchema route handler to database module
ff137 Sep 15, 2023
a9a6e6a
:art: refactor: move getMmrDistributions route handler to database mo…
ff137 Sep 15, 2023
2f0ea1a
:art: refactor: move getBuildStatus route handler to database module
ff137 Sep 15, 2023
9b4e382
:art: refactor: move getLiveMatches route handler to matches module
ff137 Sep 15, 2023
9e2f09e
:art: refactor: move getRecordsByField route handler to database module
ff137 Sep 15, 2023
3446c39
:art: refactor: move getReplayData route handler to database module
ff137 Sep 15, 2023
29ad7e0
:art: refactor: move requestParse and getRequestState route handler t…
ff137 Sep 15, 2023
ab2fee5
:art: refactor: move getMetadata route handler to database module
ff137 Sep 15, 2023
9db9772
:art: refactor: move getHealth route handler to database module
ff137 Sep 15, 2023
463a87a
:art: refactor: move getConstants and getConstantsByResource route ha…
ff137 Sep 15, 2023
6c709db
:art: refactor: move scenario route handlers to database module
ff137 Sep 15, 2023
8f16935
:art:
ff137 Sep 15, 2023
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
234 changes: 234 additions & 0 deletions routes/handlers/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
const constants = require("dotaconstants");
const { Client } = require("pg");
const buildStatus = require("../../store/buildStatus");
const config = require("../../config");
const db = require("../../store/db");
const queries = require("../../store/queries");
const queue = require("../../store/queue");
const redis = require("../../store/redis");
const utility = require("../../util/utility");

async function explorer(req, res) {
// TODO handle NQL (@nicholashh query language)
const input = req.query.sql;
const client = new Client({
connectionString: config.READONLY_POSTGRES_URL,
statement_timeout: 10000,
});
client.connect();
let result = null;
let err = null;
try {
result = await client.query(input);
} catch (e) {
err = e;
}
client.end();
const final = { ...result, err: err && err.toString() };
return res.status(err ? 400 : 200).json(final);
}

function getBuildStatus(req, res, cb) {
buildStatus(db, redis, (err, status) => {
if (err) {
return cb(err);
}
return res.json(status);
});
}

function getConstants(req, res) {
return res.json(Object.keys(constants));
}

function getConstantsByResource(req, res, cb) {
const { resource } = req.params;
if (resource in constants) {
return res.json(constants[resource]);
}
return cb();
}

function getHealth(req, res, cb) {
redis.hgetall("health", (err, result) => {
if (err) {
return cb(err);
}
const response = result || {};
Object.keys(response).forEach((key) => {
response[key] = JSON.parse(response[key]);
});
if (!req.params.metric) {
return res.json(response);
}
const single = response[req.params.metric];
const healthy = single.metric < single.threshold;
return res.status(healthy ? 200 : 500).json(single);
});
}

function getItemTimings(req, res, cb) {
queries.getItemTimings(req, (err, result) => {
if (err) {
return cb(err);
}
return res.json(result.rows);
});
}

function getLaneRoles(req, res, cb) {
queries.getLaneRoles(req, (err, result) => {
if (err) {
return cb(err);
}
return res.json(result.rows);
});
}

function getMetadata(req, res, cb) {
queries.getMetadata(req, (err, result) => {
if (err) {
return cb(err);
}
return res.json(result);
});
}

function getMmrDistributions(req, res, cb) {
queries.getDistributions(redis, (err, result) => {
if (err) {
return cb(err);
}
return res.json(result);
});
}

function getRecordsByField(req, res, cb) {
redis.zrevrange(`records:${req.params.field}`, 0, 99, "WITHSCORES", (err, rows) => {
if (err) {
return cb(err);
}
const entries = rows
.map((r, i) => {
const matchId = parseInt(r.split(":")[0], 10);
const startTime = parseInt(r.split(":")[1], 10);
const heroId = parseInt(r.split(":")[2], 10);
const score = parseInt(rows[i + 1], 10);

return {
match_id: Number.isNaN(matchId) ? null : matchId,
start_time: Number.isNaN(startTime) ? null : startTime,
hero_id: Number.isNaN(heroId) ? null : heroId,
score: Number.isNaN(score) ? null : score,
};
})
.filter((r, i) => i % 2 === 0);
return res.json(entries);
});
}

function getReplayData(req, res, cb) {
db.select(["match_id", "cluster", "replay_salt"])
.from("match_gcdata")
.whereIn("match_id", [].concat(req.query.match_id || []).slice(0, 5))
.asCallback((err, result) => {
if (err) {
return cb(err);
}
return res.json(result);
});
}

function getRequestState(req, res, cb) {
queue.getJob(req.params.jobId, (err, job) => {
if (err) {
return cb(err);
}
if (job) {
return res.json({ ...job, jobId: job.id });
}
return res.json(null);
});
}

function getSchema(req, res, cb) {
db.select(["table_name", "column_name", "data_type"])
.from("information_schema.columns")
.where({
table_schema: "public",
})
.asCallback((err, result) => {
if (err) {
return cb(err);
}
return res.json(result);
});
}

function getTeamScenarios(req, res, cb) {
queries.getTeamScenarios(req, (err, result) => {
if (err) {
return cb(err);
}
return res.json(result.rows);
});
}

function requestParse(req, res) {
const matchId = req.params.match_id;
const match = {
match_id: Number(matchId),
};
function exitWithJob(err, parseJob) {
if (err) {
console.error(err);
}
res.status(err ? 400 : 200).json({
job: {
jobId: parseJob && parseJob.id,
},
});
}
if (match && match.match_id) {
// match id request, get data from API
return utility.getData(utility.generateJob("api_details", match).url, (err, body) => {
if (err) {
// couldn't get data from api, non-retryable
return exitWithJob(JSON.stringify(err));
}
// Count this request
utility.redisCount(redis, "request");
// match details response
const match = body.result;
return queries.insertMatch(
match,
{
type: "api",
attempts: 1,
priority: 1,
forceParse: true,
},
exitWithJob
);
});
}
return exitWithJob("invalid input");
}

module.exports = {
explorer,
getBuildStatus,
getConstants,
getConstantsByResource,
getHealth,
getItemTimings,
getLaneRoles,
getMetadata,
getMmrDistributions,
getRecordsByField,
getReplayData,
getRequestState,
getSchema,
getTeamScenarios,
requestParse,
};
Loading
Loading