mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-07 11:16:37 +00:00
♻️ cleanup code for searching & filtering
This commit is contained in:
@@ -148,43 +148,6 @@ const unhealthy_targets = sql<number>`SUM(
|
|||||||
END
|
END
|
||||||
) `;
|
) `;
|
||||||
|
|
||||||
function countResourcesBase() {
|
|
||||||
return db
|
|
||||||
.select({ count: count() })
|
|
||||||
.from(resources)
|
|
||||||
.leftJoin(
|
|
||||||
resourcePassword,
|
|
||||||
eq(resourcePassword.resourceId, resources.resourceId)
|
|
||||||
)
|
|
||||||
.leftJoin(
|
|
||||||
resourcePincode,
|
|
||||||
eq(resourcePincode.resourceId, resources.resourceId)
|
|
||||||
)
|
|
||||||
.leftJoin(
|
|
||||||
resourceHeaderAuth,
|
|
||||||
eq(resourceHeaderAuth.resourceId, resources.resourceId)
|
|
||||||
)
|
|
||||||
.leftJoin(
|
|
||||||
resourceHeaderAuthExtendedCompatibility,
|
|
||||||
eq(
|
|
||||||
resourceHeaderAuthExtendedCompatibility.resourceId,
|
|
||||||
resources.resourceId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.leftJoin(targets, eq(targets.resourceId, resources.resourceId))
|
|
||||||
.leftJoin(
|
|
||||||
targetHealthCheck,
|
|
||||||
eq(targetHealthCheck.targetId, targets.targetId)
|
|
||||||
)
|
|
||||||
.groupBy(
|
|
||||||
resources.resourceId,
|
|
||||||
resourcePassword.passwordId,
|
|
||||||
resourcePincode.pincodeId,
|
|
||||||
resourceHeaderAuth.headerAuthId,
|
|
||||||
resourceHeaderAuthExtendedCompatibility.headerAuthExtendedCompatibilityId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function queryResourcesBase() {
|
function queryResourcesBase() {
|
||||||
return db
|
return db
|
||||||
.select({
|
.select({
|
||||||
@@ -422,23 +385,20 @@ export async function listResources(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let baseQuery = queryResourcesBase();
|
const baseQuery = queryResourcesBase()
|
||||||
let countQuery = countResourcesBase().where(conditions);
|
|
||||||
|
|
||||||
if (aggregateFilters) {
|
|
||||||
// @ts-expect-error idk why this is causing a type error
|
|
||||||
baseQuery = baseQuery.having(aggregateFilters);
|
|
||||||
}
|
|
||||||
if (aggregateFilters) {
|
|
||||||
// @ts-expect-error idk why this is causing a type error
|
|
||||||
countQuery = countQuery.having(aggregateFilters);
|
|
||||||
}
|
|
||||||
|
|
||||||
const rows: JoinedRow[] = await baseQuery
|
|
||||||
.where(conditions)
|
.where(conditions)
|
||||||
.limit(pageSize)
|
.having(aggregateFilters ?? sql`1 = 1`);
|
||||||
.offset(pageSize * (page - 1))
|
|
||||||
.orderBy(asc(resources.resourceId));
|
// we need to add `as` so that drizzle filters the result as a subquery
|
||||||
|
const countQuery = db.$count(baseQuery.as("filtered_resources"));
|
||||||
|
|
||||||
|
const [rows, totalCount] = await Promise.all([
|
||||||
|
baseQuery
|
||||||
|
.limit(pageSize)
|
||||||
|
.offset(pageSize * (page - 1))
|
||||||
|
.orderBy(asc(resources.resourceId)),
|
||||||
|
countQuery
|
||||||
|
]);
|
||||||
|
|
||||||
const resourceIdList = rows.map((row) => row.resourceId);
|
const resourceIdList = rows.map((row) => row.resourceId);
|
||||||
const allResourceTargets =
|
const allResourceTargets =
|
||||||
@@ -495,9 +455,6 @@ export async function listResources(
|
|||||||
|
|
||||||
const resourcesList: ResourceWithTargets[] = Array.from(map.values());
|
const resourcesList: ResourceWithTargets[] = Array.from(map.values());
|
||||||
|
|
||||||
const totalCountResult = await countQuery;
|
|
||||||
const totalCount = totalCountResult[0]?.count ?? 0;
|
|
||||||
|
|
||||||
return response<ListResourcesResponse>(res, {
|
return response<ListResourcesResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
resources: resourcesList,
|
resources: resourcesList,
|
||||||
|
|||||||
@@ -226,7 +226,6 @@ export async function listSites(
|
|||||||
parsedQuery.data;
|
parsedQuery.data;
|
||||||
|
|
||||||
const accessibleSiteIds = accessibleSites.map((site) => site.siteId);
|
const accessibleSiteIds = accessibleSites.map((site) => site.siteId);
|
||||||
const baseQuery = querySitesBase();
|
|
||||||
|
|
||||||
let conditions = and(
|
let conditions = and(
|
||||||
inArray(sites.siteId, accessibleSiteIds),
|
inArray(sites.siteId, accessibleSiteIds),
|
||||||
@@ -245,27 +244,31 @@ export async function listSites(
|
|||||||
conditions = and(conditions, eq(sites.online, online));
|
conditions = and(conditions, eq(sites.online, online));
|
||||||
}
|
}
|
||||||
|
|
||||||
const countQuery = countSitesBase().where(conditions);
|
const baseQuery = querySitesBase().where(conditions);
|
||||||
|
|
||||||
|
// we need to add `as` so that drizzle filters the result as a subquery
|
||||||
|
const countQuery = db.$count(baseQuery.as("filtered_sites"));
|
||||||
|
|
||||||
const siteListQuery = baseQuery
|
const siteListQuery = baseQuery
|
||||||
.where(conditions)
|
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.offset(pageSize * (page - 1));
|
.offset(pageSize * (page - 1))
|
||||||
|
.orderBy(
|
||||||
if (sort_by) {
|
sort_by
|
||||||
siteListQuery.orderBy(
|
? order === "asc"
|
||||||
order === "asc" ? asc(sites[sort_by]) : desc(sites[sort_by])
|
? asc(sites[sort_by])
|
||||||
|
: desc(sites[sort_by])
|
||||||
|
: asc(sites.siteId)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
const totalCountResult = await countQuery;
|
const [totalCount, rows] = await Promise.all([
|
||||||
const totalCount = totalCountResult[0].count;
|
countQuery,
|
||||||
|
siteListQuery
|
||||||
|
]);
|
||||||
|
|
||||||
// Get latest version asynchronously without blocking the response
|
// Get latest version asynchronously without blocking the response
|
||||||
const latestNewtVersionPromise = getLatestNewtVersion();
|
const latestNewtVersionPromise = getLatestNewtVersion();
|
||||||
|
|
||||||
const sitesWithUpdates: SiteWithUpdateAvailable[] = (
|
const sitesWithUpdates: SiteWithUpdateAvailable[] = rows.map((site) => {
|
||||||
await siteListQuery
|
|
||||||
).map((site) => {
|
|
||||||
const siteWithUpdate: SiteWithUpdateAvailable = { ...site };
|
const siteWithUpdate: SiteWithUpdateAvailable = { ...site };
|
||||||
// Initially set to false, will be updated if version check succeeds
|
// Initially set to false, will be updated if version check succeeds
|
||||||
siteWithUpdate.newtUpdateAvailable = false;
|
siteWithUpdate.newtUpdateAvailable = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user