init
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "CostItem" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"name" TEXT NOT NULL,
|
||||
"region" TEXT NOT NULL,
|
||||
"year" INTEGER NOT NULL,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "SubCostItem" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"name" TEXT NOT NULL,
|
||||
"subCost" REAL NOT NULL,
|
||||
"costItemId" INTEGER NOT NULL,
|
||||
CONSTRAINT "SubCostItem_costItemId_fkey" FOREIGN KEY ("costItemId") REFERENCES "CostItem" ("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "TrendAiPercentage" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"region" TEXT NOT NULL,
|
||||
"year" INTEGER NOT NULL,
|
||||
"percentage" REAL NOT NULL
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "CostItem_region_year_idx" ON "CostItem"("region", "year");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "SubCostItem_costItemId_idx" ON "SubCostItem"("costItemId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "TrendAiPercentage_region_year_key" ON "TrendAiPercentage"("region", "year");
|
||||
@@ -0,0 +1,4 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Year" (
|
||||
"year" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "sqlite"
|
||||
@@ -0,0 +1,44 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `CostItem` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`region` VARCHAR(191) NOT NULL,
|
||||
`year` INTEGER NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `CostItem_region_year_idx`(`region`, `year`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `SubCostItem` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`subCost` DOUBLE NOT NULL,
|
||||
`costItemId` INTEGER NOT NULL,
|
||||
|
||||
INDEX `SubCostItem_costItemId_idx`(`costItemId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `TrendAiPercentage` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`region` VARCHAR(191) NOT NULL,
|
||||
`year` INTEGER NOT NULL,
|
||||
`percentage` DOUBLE NOT NULL,
|
||||
|
||||
UNIQUE INDEX `TrendAiPercentage_region_year_key`(`region`, `year`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Year` (
|
||||
`year` INTEGER NOT NULL,
|
||||
|
||||
PRIMARY KEY (`year`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `SubCostItem` ADD CONSTRAINT `SubCostItem_costItemId_fkey` FOREIGN KEY (`costItemId`) REFERENCES `CostItem`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "mysql"
|
||||
@@ -0,0 +1,61 @@
|
||||
// Production Prisma schema for the IT Cost Dashboard (MySQL / AWS RDS).
|
||||
//
|
||||
// This mirrors prisma/schema.prisma exactly except the datasource provider is
|
||||
// "mysql". Prisma does NOT support env() for the `provider` field, so dev
|
||||
// (SQLite) and prod (MySQL) each need their own schema file + migrations.
|
||||
//
|
||||
// Usage on the production EC2 box (see DEPLOY.md):
|
||||
// npm run prod:generate # prisma generate --schema=prisma/prod/schema.prisma
|
||||
// npm run prod:migrate # prisma migrate deploy --schema=prisma/prod/schema.prisma
|
||||
// npm run build:prod
|
||||
datasource db {
|
||||
provider = "mysql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
// One row per (name, region, year). e.g. "Cloud Hosting" in AMEA IT 2026 is a
|
||||
// distinct row from "Cloud Hosting" in EMEA IT 2026. Cost is never stored — it is
|
||||
// always derived as SUM(subItems.subCost).
|
||||
model CostItem {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
region String
|
||||
year Int
|
||||
subItems SubCostItem[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([region, year])
|
||||
}
|
||||
|
||||
model SubCostItem {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
subCost Float
|
||||
costItem CostItem @relation(fields: [costItemId], references: [id], onDelete: Cascade)
|
||||
costItemId Int
|
||||
|
||||
@@index([costItemId])
|
||||
}
|
||||
|
||||
// The TrendAI percentage that splits each item's Cost into TrendAI vs TrendLife
|
||||
// for a given region+year. Applies uniformly to all items in that region+year.
|
||||
model TrendAiPercentage {
|
||||
id Int @id @default(autoincrement())
|
||||
region String
|
||||
year Int
|
||||
percentage Float
|
||||
|
||||
@@unique([region, year])
|
||||
}
|
||||
|
||||
// The set of years available for selection across the app. Managed on the Year
|
||||
// Creation page. Cost items reference years by value (not FK) so a year can exist
|
||||
// with no cost data yet.
|
||||
model Year {
|
||||
year Int @id
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Prisma schema for the IT Cost Dashboard.
|
||||
//
|
||||
// Datasource provider is hardcoded to "sqlite" for local dev because Prisma does
|
||||
// NOT support env() for the `provider` field (only for `url`). Production on RDS
|
||||
// uses a separate MySQL schema at prisma/prod/schema.prisma with its own
|
||||
// migration history in prisma/prod/migrations (see DEPLOY.md). Keep the two
|
||||
// schemas' models in sync.
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
// One row per (name, region, year). e.g. "Cloud Hosting" in AMEA IT 2026 is a
|
||||
// distinct row from "Cloud Hosting" in EMEA IT 2026. Cost is never stored — it is
|
||||
// always derived as SUM(subItems.subCost).
|
||||
model CostItem {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
region String
|
||||
year Int
|
||||
subItems SubCostItem[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([region, year])
|
||||
}
|
||||
|
||||
model SubCostItem {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
subCost Float
|
||||
costItem CostItem @relation(fields: [costItemId], references: [id], onDelete: Cascade)
|
||||
costItemId Int
|
||||
|
||||
@@index([costItemId])
|
||||
}
|
||||
|
||||
// The TrendAI percentage that splits each item's Cost into TrendAI vs TrendLife
|
||||
// for a given region+year. Applies uniformly to all items in that region+year.
|
||||
model TrendAiPercentage {
|
||||
id Int @id @default(autoincrement())
|
||||
region String
|
||||
year Int
|
||||
percentage Float
|
||||
|
||||
@@unique([region, year])
|
||||
}
|
||||
|
||||
// The set of years available for selection across the app. Managed on the Year
|
||||
// Creation page. Cost items reference years by value (not FK) so a year can exist
|
||||
// with no cost data yet.
|
||||
model Year {
|
||||
year Int @id
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const REGIONS = ["AMEA IT", "EMEA IT", "NABU IT", "PH IT", "JP IT"] as const;
|
||||
const YEARS = [2022, 2023, 2024, 2025, 2026];
|
||||
|
||||
// Cost item catalog. Each item defines its sub-cost line names and a base cost
|
||||
// per sub-item. Actual seeded sub-costs scale by region + year (below).
|
||||
const ITEMS: { name: string; subs: { name: string; base: number }[] }[] = [
|
||||
{
|
||||
name: "Cloud Hosting",
|
||||
subs: [
|
||||
{ name: "Compute (VMs)", base: 40000 },
|
||||
{ name: "Storage", base: 18000 },
|
||||
{ name: "Networking / Egress", base: 9000 },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Software Licenses",
|
||||
subs: [
|
||||
{ name: "Productivity Suite", base: 22000 },
|
||||
{ name: "Design Tools", base: 8000 },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Security Tools",
|
||||
subs: [
|
||||
{ name: "Endpoint Protection", base: 15000 },
|
||||
{ name: "SIEM", base: 20000 },
|
||||
{ name: "Vulnerability Scanning", base: 6000 },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Support & Maintenance",
|
||||
subs: [{ name: "Vendor Support Contract", base: 30000 }],
|
||||
},
|
||||
{
|
||||
name: "Network Infra",
|
||||
subs: [
|
||||
{ name: "Circuits / Bandwidth", base: 25000 },
|
||||
{ name: "Hardware Refresh", base: 12000 },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
// Region weighting so regions differ in magnitude (NABU largest, PH smallest).
|
||||
const REGION_WEIGHT: Record<string, number> = {
|
||||
"NABU IT": 1.4,
|
||||
"EMEA IT": 1.2,
|
||||
"AMEA IT": 1.0,
|
||||
"JP IT": 0.9,
|
||||
"PH IT": 0.7,
|
||||
};
|
||||
|
||||
// TrendAI percentage per region grows over the years (AI adoption ramp).
|
||||
// Value at year Y for a region = base + (Y-2022)*step, clamped.
|
||||
const REGION_AI: Record<string, { base: number; step: number }> = {
|
||||
"NABU IT": { base: 12, step: 6 },
|
||||
"EMEA IT": { base: 10, step: 5 },
|
||||
"AMEA IT": { base: 8, step: 5 },
|
||||
"JP IT": { base: 15, step: 4 },
|
||||
"PH IT": { base: 6, step: 4 },
|
||||
};
|
||||
|
||||
function yearFactor(year: number): number {
|
||||
// Costs grow ~8%/yr off a 2022 base.
|
||||
return Math.pow(1.08, year - 2022);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log("Clearing existing data...");
|
||||
await prisma.subCostItem.deleteMany();
|
||||
await prisma.costItem.deleteMany();
|
||||
await prisma.trendAiPercentage.deleteMany();
|
||||
await prisma.year.deleteMany();
|
||||
|
||||
console.log("Seeding years...");
|
||||
for (const year of YEARS) {
|
||||
await prisma.year.create({ data: { year } });
|
||||
}
|
||||
|
||||
console.log("Seeding cost items + sub-costs...");
|
||||
for (const region of REGIONS) {
|
||||
const weight = REGION_WEIGHT[region];
|
||||
for (const year of YEARS) {
|
||||
const yf = yearFactor(year);
|
||||
for (const item of ITEMS) {
|
||||
await prisma.costItem.create({
|
||||
data: {
|
||||
name: item.name,
|
||||
region,
|
||||
year,
|
||||
subItems: {
|
||||
create: item.subs.map((s) => ({
|
||||
name: s.name,
|
||||
subCost: Math.round((s.base * weight * yf) / 100) * 100,
|
||||
})),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Seeding TrendAI percentages...");
|
||||
for (const region of REGIONS) {
|
||||
const ai = REGION_AI[region];
|
||||
for (const year of YEARS) {
|
||||
const pct = Math.min(60, ai.base + (year - 2022) * ai.step);
|
||||
await prisma.trendAiPercentage.create({
|
||||
data: { region, year, percentage: pct },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const items = await prisma.costItem.count();
|
||||
const subs = await prisma.subCostItem.count();
|
||||
const pcts = await prisma.trendAiPercentage.count();
|
||||
console.log(`Done. CostItems=${items}, SubCostItems=${subs}, TrendAiPercentage=${pcts}`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user