mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-30 04:32:53 +00:00
Seed satatus data for resources, sites, and hc
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { db } from "@server/db/pg/driver";
|
import { db } from "@server/db/pg/driver";
|
||||||
import { sql } from "drizzle-orm";
|
import { sql } from "drizzle-orm";
|
||||||
|
import { SiJfrog } from "react-icons/si";
|
||||||
|
|
||||||
const version = "1.18.0";
|
const version = "1.18.0";
|
||||||
|
|
||||||
@@ -67,7 +68,8 @@ export default async function migration() {
|
|||||||
FROM "siteResources" sr
|
FROM "siteResources" sr
|
||||||
WHERE sr."siteId" IS NOT NULL`
|
WHERE sr."siteId" IS NOT NULL`
|
||||||
);
|
);
|
||||||
const existingSiteResourcesForNetwork = siteResourcesForNetworkQuery.rows as {
|
const existingSiteResourcesForNetwork =
|
||||||
|
siteResourcesForNetworkQuery.rows as {
|
||||||
siteResourceId: number;
|
siteResourceId: number;
|
||||||
orgId: string;
|
orgId: string;
|
||||||
siteId: number;
|
siteId: number;
|
||||||
@@ -446,10 +448,7 @@ export default async function migration() {
|
|||||||
`Migrated ${existingHealthChecks.length} targetHealthCheck row(s) with corrected IDs`
|
`Migrated ${existingHealthChecks.length} targetHealthCheck row(s) with corrected IDs`
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(
|
console.error("Error while migrating targetHealthCheck rows:", e);
|
||||||
"Error while migrating targetHealthCheck rows:",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -493,5 +492,90 @@ export default async function migration() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Seed statusHistory for all existing sites
|
||||||
|
try {
|
||||||
|
const sitesQuery = await db.execute(
|
||||||
|
sql`SELECT "siteId", "orgId", "online" FROM "sites"`
|
||||||
|
);
|
||||||
|
const allSites = sitesQuery.rows as {
|
||||||
|
siteId: number;
|
||||||
|
orgId: string;
|
||||||
|
online: boolean;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
|
for (const site of allSites) {
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO "statusHistory" ("entityType", "entityId", "orgId", "status", "timestamp")
|
||||||
|
VALUES ('site', ${site.siteId}, ${site.orgId}, ${site.online ? "online" : "offline"}, ${now})
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Seeded statusHistory for ${allSites.length} site(s)`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error while seeding statusHistory for sites:", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed statusHistory for all existing resources
|
||||||
|
try {
|
||||||
|
const resourcesQuery = await db.execute(
|
||||||
|
sql`SELECT "resourceId", "orgId", "health" FROM "resources"`
|
||||||
|
);
|
||||||
|
const allResources = resourcesQuery.rows as {
|
||||||
|
resourceId: number;
|
||||||
|
orgId: string;
|
||||||
|
health: string | null;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
|
for (const resource of allResources) {
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO "statusHistory" ("entityType", "entityId", "orgId", "status", "timestamp")
|
||||||
|
VALUES ('resource', ${resource.resourceId}, ${resource.orgId}, ${resource.health ?? "unknown"}, ${now})
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`Seeded statusHistory for ${allResources.length} resource(s)`
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error while seeding statusHistory for resources:", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed statusHistory for all existing health checks
|
||||||
|
try {
|
||||||
|
const healthChecksQuery = await db.execute(
|
||||||
|
sql`SELECT "targetHealthCheckId", "orgId", "hcHealth" FROM "targetHealthCheck"`
|
||||||
|
);
|
||||||
|
const allHealthChecks = healthChecksQuery.rows as {
|
||||||
|
targetHealthCheckId: number;
|
||||||
|
orgId: string;
|
||||||
|
hcHealth: string | null;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
|
for (const hc of allHealthChecks) {
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO "statusHistory" ("entityType", "entityId", "orgId", "status", "timestamp")
|
||||||
|
VALUES ('health_check', ${hc.targetHealthCheckId}, ${hc.orgId}, ${hc.hcHealth ?? "unknown"}, ${now})
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`Seeded statusHistory for ${allHealthChecks.length} health check(s)`
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(
|
||||||
|
"Error while seeding statusHistory for health checks:",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`${version} migration complete`);
|
console.log(`${version} migration complete`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -340,7 +340,6 @@ export default async function migration() {
|
|||||||
ALTER TABLE 'resources' ADD 'wildcard' integer DEFAULT false NOT NULL;
|
ALTER TABLE 'resources' ADD 'wildcard' integer DEFAULT false NOT NULL;
|
||||||
`
|
`
|
||||||
).run();
|
).run();
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
db.pragma("foreign_keys = ON");
|
db.pragma("foreign_keys = ON");
|
||||||
@@ -364,7 +363,11 @@ export default async function migration() {
|
|||||||
const result = insertNetwork.run("resource", sr.orgId);
|
const result = insertNetwork.run("resource", sr.orgId);
|
||||||
const networkId = result.lastInsertRowid as number;
|
const networkId = result.lastInsertRowid as number;
|
||||||
insertSiteNetwork.run(sr.siteId, networkId);
|
insertSiteNetwork.run(sr.siteId, networkId);
|
||||||
updateSiteResource.run(networkId, networkId, sr.siteResourceId);
|
updateSiteResource.run(
|
||||||
|
networkId,
|
||||||
|
networkId,
|
||||||
|
sr.siteResourceId
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -454,6 +457,87 @@ export default async function migration() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Migrated database`);
|
console.log(`Migrated database`);
|
||||||
|
|
||||||
|
// Seed statusHistory for all existing sites
|
||||||
|
const allSites = db
|
||||||
|
.prepare(`SELECT "siteId", "orgId", "online" FROM 'sites'`)
|
||||||
|
.all() as { siteId: number; orgId: string; online: number }[];
|
||||||
|
|
||||||
|
const insertSiteHistory = db.prepare(
|
||||||
|
`INSERT INTO 'statusHistory' ("entityType", "entityId", "orgId", "status", "timestamp") VALUES (?, ?, ?, ?, ?)`
|
||||||
|
);
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
const seedSites = db.transaction(() => {
|
||||||
|
for (const site of allSites) {
|
||||||
|
insertSiteHistory.run(
|
||||||
|
"site",
|
||||||
|
site.siteId,
|
||||||
|
site.orgId,
|
||||||
|
site.online ? "online" : "offline",
|
||||||
|
now
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
seedSites();
|
||||||
|
console.log(`Seeded statusHistory for ${allSites.length} site(s)`);
|
||||||
|
|
||||||
|
// Seed statusHistory for all existing resources
|
||||||
|
const allResources = db
|
||||||
|
.prepare(`SELECT "resourceId", "orgId", "health" FROM 'resources'`)
|
||||||
|
.all() as {
|
||||||
|
resourceId: number;
|
||||||
|
orgId: string;
|
||||||
|
health: string | null;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const insertResourceHistory = db.prepare(
|
||||||
|
`INSERT INTO 'statusHistory' ("entityType", "entityId", "orgId", "status", "timestamp") VALUES (?, ?, ?, ?, ?)`
|
||||||
|
);
|
||||||
|
const seedResources = db.transaction(() => {
|
||||||
|
for (const resource of allResources) {
|
||||||
|
insertResourceHistory.run(
|
||||||
|
"resource",
|
||||||
|
resource.resourceId,
|
||||||
|
resource.orgId,
|
||||||
|
resource.health ?? "unknown",
|
||||||
|
now
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
seedResources();
|
||||||
|
console.log(
|
||||||
|
`Seeded statusHistory for ${allResources.length} resource(s)`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Seed statusHistory for all existing health checks
|
||||||
|
const allHealthChecks = db
|
||||||
|
.prepare(
|
||||||
|
`SELECT "targetHealthCheckId", "orgId", "hcHealth" FROM 'targetHealthCheck'`
|
||||||
|
)
|
||||||
|
.all() as {
|
||||||
|
targetHealthCheckId: number;
|
||||||
|
orgId: string;
|
||||||
|
hcHealth: string | null;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const insertHealthCheckHistory = db.prepare(
|
||||||
|
`INSERT INTO 'statusHistory' ("entityType", "entityId", "orgId", "status", "timestamp") VALUES (?, ?, ?, ?, ?)`
|
||||||
|
);
|
||||||
|
const seedHealthChecks = db.transaction(() => {
|
||||||
|
for (const hc of allHealthChecks) {
|
||||||
|
insertHealthCheckHistory.run(
|
||||||
|
"health_check",
|
||||||
|
hc.targetHealthCheckId,
|
||||||
|
hc.orgId,
|
||||||
|
hc.hcHealth ?? "unknown",
|
||||||
|
now
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
seedHealthChecks();
|
||||||
|
console.log(
|
||||||
|
`Seeded statusHistory for ${allHealthChecks.length} health check(s)`
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Failed to migrate db:", e);
|
console.log("Failed to migrate db:", e);
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
Reference in New Issue
Block a user