Skip to content

Commit

Permalink
fix tables
Browse files Browse the repository at this point in the history
  • Loading branch information
owens1127 committed Nov 6, 2024
1 parent 0e490bd commit da9830d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/data/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const getVersionId = async (
export const getActivityVersion = async (activityPath: string, versionPath: string) => {
return await postgres.queryRow<{ activityId: number; versionId: number }>(
`SELECT activity_id AS "activityId", version_id AS "versionId"
FROM activity_hash
JOIN activity_definition ON activity_hash.activity_id = activity_definition.id
JOIN version_definition ON activity_hash.version_id = version_definition.id
FROM activity_version
JOIN activity_definition ON activity_version.activity_id = activity_definition.id
JOIN version_definition ON activity_version.version_id = version_definition.id
WHERE activity_definition.path = $1 AND version_definition.path = $2
LIMIT 1`,
{
Expand Down Expand Up @@ -76,6 +76,6 @@ export const listHashes = async () => {
hash,
activity_id AS "activityId",
version_id AS "versionId"
FROM activity_hash`
FROM activity_version`
)
}
18 changes: 9 additions & 9 deletions src/data/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const getActivities = async (
hash AS "hash",
activity_id AS "activityId",
version_id AS "versionId",
activity.completed AS "completed",
instance.completed AS "completed",
player_count AS "playerCount",
score AS "score",
fresh AS "fresh",
Expand All @@ -47,15 +47,15 @@ export const getActivities = async (
date_completed < COALESCE(contest_end, TIMESTAMP 'epoch') AS "isContest",
date_completed < COALESCE(week_one_end, TIMESTAMP 'epoch') AS "isWeekOne",
JSONB_BUILD_OBJECT(
'completed', activity_player.completed,
'sherpas', activity_player.sherpas,
'isFirstClear', activity_player.is_first_clear,
'timePlayedSeconds', activity_player.time_played_seconds
'completed', instance_player.completed,
'sherpas', instance_player.sherpas,
'isFirstClear', instance_player.is_first_clear,
'timePlayedSeconds', instance_player.time_played_seconds
) as player
FROM activity_player
INNER JOIN activity USING (instance_id)
INNER JOIN activity_hash USING (hash)
INNER JOIN activity_definition ON activity_definition.id = activity_hash.activity_id
FROM instance_player
INNER JOIN instance USING (instance_id)
INNER JOIN activity_version USING (hash)
INNER JOIN activity_definition ON activity_definition.id = activity_version.activity_id
WHERE membership_id = $1::bigint
AND date_completed < ${cursor ? "$3" : "(SELECT last_seen + interval '1 second' FROM player WHERE membership_id = $1::bigint)"}
${cutoff ? `AND date_completed > ${cursor ? "$4" : "$3"}` : ""}
Expand Down
14 changes: 7 additions & 7 deletions src/data/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export async function getInstance(instanceId: bigint | string): Promise<Instance
date_completed < COALESCE(day_one_end, TIMESTAMP 'epoch') AS "isDayOne",
date_completed < COALESCE(contest_end, TIMESTAMP 'epoch') AS "isContest",
date_completed < COALESCE(week_one_end, TIMESTAMP 'epoch') AS "isWeekOne"
FROM activity
INNER JOIN activity_hash USING (hash)
INNER JOIN activity_definition ON activity_definition.id = activity_hash.activity_id
FROM instance
INNER JOIN activity_version USING (hash)
INNER JOIN activity_definition ON activity_definition.id = activity_version.activity_id
WHERE instance_id = $1::bigint
LIMIT 1;`,
{
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function getInstanceExtended(
'isPrivate', "is_private"
) AS "playerInfo",
"t1"."characters_json" AS "characters"
FROM "activity_player" "ap"
FROM "instance_player" "ap"
LEFT JOIN "player" USING (membership_id)
LEFT JOIN LATERAL (
SELECT JSONB_AGG(
Expand All @@ -79,7 +79,7 @@ export async function getInstanceExtended(
'weapons', "t2"."weapons_json"
)
) AS "characters_json"
FROM "activity_character" "ac"
FROM "instance_character" "ac"
LEFT JOIN LATERAL (
SELECT COALESCE(JSONB_AGG(
JSONB_BUILD_OBJECT(
Expand All @@ -88,7 +88,7 @@ export async function getInstanceExtended(
'precisionKills', "precision_kills"
)
), '[]'::jsonb) AS "weapons_json"
FROM "activity_character_weapon" AS "acw"
FROM "instance_character_weapon" AS "acw"
WHERE "acw"."character_id" = "ac"."character_id"
AND "acw"."membership_id" = "ac"."membership_id"
AND "acw"."instance_id" = "ac"."instance_id"
Expand Down Expand Up @@ -125,7 +125,7 @@ export async function getInstanceMetadataByHash(hash: number | string): Promise<
ad.name AS "activityName",
vd.name AS "versionName",
ad.is_raid AS "isRaid"
FROM activity_hash ah
FROM activity_version ah
INNER JOIN activity_definition ad ON ad.id = ah.activity_id
INNER JOIN version_definition vd ON vd.id = ah.version_id
WHERE hash = $1::bigint
Expand Down
6 changes: 3 additions & 3 deletions src/data/leaderboard/team/contest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export const getContestTeamLeaderboard = async ({
'isPrivate', is_private
)
) as "players"
FROM activity_player
FROM instance_player
INNER JOIN player USING (membership_id)
WHERE activity_player.instance_id = world_first_contest_leaderboard.instance_id
AND activity_player.completed
WHERE instance_player.instance_id = world_first_contest_leaderboard.instance_id
AND instance_player.completed
) as "lateral" ON true
WHERE position > $1 AND position <= ($1 + $2)
AND activity_id = $3
Expand Down
6 changes: 3 additions & 3 deletions src/data/leaderboard/team/first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const getFirstTeamActivityVersionLeaderboard = async ({
'isPrivate', is_private
)
) as "players"
FROM activity_player
FROM instance_player
INNER JOIN player USING (membership_id)
WHERE activity_player.instance_id = team_activity_version_leaderboard.instance_id
AND activity_player.completed
WHERE instance_player.instance_id = team_activity_version_leaderboard.instance_id
AND instance_player.completed
) as "lateral" ON true
WHERE position > $1 AND position <= ($1 + $2)
AND activity_id = $3 AND version_id = $4
Expand Down
4 changes: 2 additions & 2 deletions src/data/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export const getPlayerActivityStats = async (membershipId: bigint | string) => {
FROM activity_definition
LEFT JOIN player_stats ON activity_definition.id = player_stats.activity_id
AND player_stats.membership_id = $1::bigint
LEFT JOIN activity fastest ON player_stats.fastest_instance_id = fastest.instance_id
LEFT JOIN activity_hash fastest_ah ON fastest.hash = fastest_ah.hash
LEFT JOIN instance fastest ON player_stats.fastest_instance_id = fastest.instance_id
LEFT JOIN activity_version fastest_ah ON fastest.hash = fastest_ah.hash
ORDER BY activity_definition.id`,
{
params: [membershipId],
Expand Down
4 changes: 2 additions & 2 deletions src/data/teammates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getTeammates = async (membershipId: bigint | string, { count }: { c
`WITH self AS (
SELECT
instance_id, time_played_seconds, completed
FROM activity_player
FROM instance_player
WHERE membership_id = $1::bigint
), agg_data AS (
SELECT
Expand All @@ -15,7 +15,7 @@ export const getTeammates = async (membershipId: bigint | string, { count }: { c
SUM(CASE WHEN teammate.completed AND self.completed THEN 1 ELSE 0 END) AS clears,
COUNT(*) AS count
FROM self
JOIN activity_player AS teammate USING (instance_id)
JOIN instance_player AS teammate USING (instance_id)
WHERE membership_id <> $1::bigint
GROUP BY (membership_id)
ORDER BY clears DESC, time_played DESC
Expand Down
14 changes: 7 additions & 7 deletions src/routes/admin/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ describe("admin query 200", () => {

test("SELECT 1", () => t("SELECT 1", "SELECT"))

test("EXPLAIN", () => t("SELECT * FROM activity;", "EXPLAIN"))
test("EXPLAIN", () => t("SELECT * FROM instance;", "EXPLAIN"))

test("SELECT * ", () => t("SELECT * FROM activity_player LIMIT 10;", "SELECT"))
test("SELECT * ", () => t("SELECT * FROM instance_player LIMIT 10;", "SELECT"))

test("SELECT with ignore cost ", () =>
t("SELECT * FROM activity_player LIMIT 100000;", "SELECT", true))
t("SELECT * FROM instance_player LIMIT 100000;", "SELECT", true))

test(
"Complex",
Expand All @@ -34,11 +34,11 @@ describe("admin query 200", () => {
ARRAY_AGG(p.bungie_global_display_name ORDER BY p.bungie_global_display_name) AS players_concatenated,
a.date_completed
FROM
activity a
instance a
JOIN
activity_player pa ON a.instance_id = pa.instance_id
instance_player pa ON a.instance_id = pa.instance_id
JOIN
activity_hash ah ON ah.hash = a.hash
activity_version ah ON ah.hash = a.hash
JOIN
player p ON p.membership_id = pa.membership_id
WHERE
Expand All @@ -63,7 +63,7 @@ describe("admin query syntax error", () => {

test("Bad table", () => t("SELECT * from fasdhfahfuiasdf", "SELECT"))

test("Bad syntax", () => t("SELECT * FROM activity LIMIT 10 WHERE 1 = 1;", "EXPLAIN"))
test("Bad syntax", () => t("SELECT * FROM instance LIMIT 10 WHERE 1 = 1;", "EXPLAIN"))

test("Bad keywords", () => t("SELECTFRO FROM abc;", "SELECT"))

Expand Down
2 changes: 1 addition & 1 deletion src/routes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const getLatestActivityByDate = async () => {
SELECT
date_completed AT TIME ZONE 'UTC' AS "dateCompleted",
instance_id::text AS "instanceId"
FROM activity
FROM instance
ORDER BY instance_id DESC
LIMIT 50
) AS t1
Expand Down

0 comments on commit da9830d

Please sign in to comment.