mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-20 11:12:31 +02:00
@@ -16,6 +16,7 @@ import {
|
|||||||
aiModels,
|
aiModels,
|
||||||
aiUsageRecords,
|
aiUsageRecords,
|
||||||
db,
|
db,
|
||||||
|
logsDb,
|
||||||
userOrgRoles
|
userOrgRoles
|
||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import { modelKeyMatches } from "@server/lib/aiModelKeyMatch";
|
import { modelKeyMatches } from "@server/lib/aiModelKeyMatch";
|
||||||
@@ -168,7 +169,7 @@ async function sumUsageAmount(
|
|||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const column =
|
const column =
|
||||||
unit === "usd" ? aiUsageRecords.costUsd : aiUsageRecords.totalTokens;
|
unit === "usd" ? aiUsageRecords.costUsd : aiUsageRecords.totalTokens;
|
||||||
const [row] = await db
|
const [row] = await logsDb
|
||||||
.select({ total: sql<number>`coalesce(sum(${column}), 0)` })
|
.select({ total: sql<number>`coalesce(sum(${column}), 0)` })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(where);
|
.where(where);
|
||||||
@@ -200,7 +201,7 @@ export async function sumUsageForBudget(
|
|||||||
if (!model) {
|
if (!model) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
const rows = await db
|
const rows = await logsDb
|
||||||
.select({
|
.select({
|
||||||
requestedModel: aiUsageRecords.requestedModel,
|
requestedModel: aiUsageRecords.requestedModel,
|
||||||
costUsd: aiUsageRecords.costUsd,
|
costUsd: aiUsageRecords.costUsd,
|
||||||
@@ -496,7 +497,7 @@ async function flushUsageRecords() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Use a transaction to ensure all inserts succeed or fail together
|
// Use a transaction to ensure all inserts succeed or fail together
|
||||||
await db.transaction(async (tx) => {
|
await logsDb.transaction(async (tx) => {
|
||||||
// Batch insert in groups to avoid overwhelming the database
|
// Batch insert in groups to avoid overwhelming the database
|
||||||
const DB_BATCH_SIZE = 25;
|
const DB_BATCH_SIZE = 25;
|
||||||
for (let i = 0; i < recordsToWrite.length; i += DB_BATCH_SIZE) {
|
for (let i = 0; i < recordsToWrite.length; i += DB_BATCH_SIZE) {
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ async function enrichWithDetails(
|
|||||||
>();
|
>();
|
||||||
const sessionIds = logs.map((log) => log.sessionId);
|
const sessionIds = logs.map((log) => log.sessionId);
|
||||||
if (sessionIds.length > 0) {
|
if (sessionIds.length > 0) {
|
||||||
const usageDetails = await primaryDb
|
const usageDetails = await logsDb
|
||||||
.select({
|
.select({
|
||||||
sessionId: aiUsageRecords.sessionId,
|
sessionId: aiUsageRecords.sessionId,
|
||||||
promptTokens: aiUsageRecords.promptTokens,
|
promptTokens: aiUsageRecords.promptTokens,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
db,
|
db,
|
||||||
|
logsDb,
|
||||||
aiUsageRecords,
|
aiUsageRecords,
|
||||||
aiProviders,
|
aiProviders,
|
||||||
resources,
|
resources,
|
||||||
@@ -78,22 +79,22 @@ async function query(data: Q) {
|
|||||||
uniqueUsers,
|
uniqueUsers,
|
||||||
uniqueVirtualApiKeys
|
uniqueVirtualApiKeys
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
db
|
logsDb
|
||||||
.selectDistinct({ id: aiUsageRecords.providerId })
|
.selectDistinct({ id: aiUsageRecords.providerId })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(baseConditions)
|
.where(baseConditions)
|
||||||
.limit(DISTINCT_LIMIT + 1),
|
.limit(DISTINCT_LIMIT + 1),
|
||||||
db
|
logsDb
|
||||||
.selectDistinct({ model: aiUsageRecords.requestedModel })
|
.selectDistinct({ model: aiUsageRecords.requestedModel })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(baseConditions)
|
.where(baseConditions)
|
||||||
.limit(DISTINCT_LIMIT + 1),
|
.limit(DISTINCT_LIMIT + 1),
|
||||||
db
|
logsDb
|
||||||
.selectDistinct({ id: aiUsageRecords.resourceId })
|
.selectDistinct({ id: aiUsageRecords.resourceId })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(and(baseConditions, not(isNull(aiUsageRecords.resourceId))))
|
.where(and(baseConditions, not(isNull(aiUsageRecords.resourceId))))
|
||||||
.limit(DISTINCT_LIMIT + 1),
|
.limit(DISTINCT_LIMIT + 1),
|
||||||
db
|
logsDb
|
||||||
.selectDistinct({ id: aiUsageRecords.siteResourceId })
|
.selectDistinct({ id: aiUsageRecords.siteResourceId })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(
|
.where(
|
||||||
@@ -104,12 +105,12 @@ async function query(data: Q) {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
.limit(DISTINCT_LIMIT + 1),
|
.limit(DISTINCT_LIMIT + 1),
|
||||||
db
|
logsDb
|
||||||
.selectDistinct({ userId: aiUsageRecords.userId })
|
.selectDistinct({ userId: aiUsageRecords.userId })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(and(baseConditions, not(isNull(aiUsageRecords.userId))))
|
.where(and(baseConditions, not(isNull(aiUsageRecords.userId))))
|
||||||
.limit(DISTINCT_LIMIT + 1),
|
.limit(DISTINCT_LIMIT + 1),
|
||||||
db
|
logsDb
|
||||||
.selectDistinct({ id: aiUsageRecords.virtualApiKeyId })
|
.selectDistinct({ id: aiUsageRecords.virtualApiKeyId })
|
||||||
.from(aiUsageRecords)
|
.from(aiUsageRecords)
|
||||||
.where(
|
.where(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { db, aiUsageRecords } from "@server/db";
|
import { logsDb, aiUsageRecords } from "@server/db";
|
||||||
import { registry } from "@server/openApi";
|
import { registry } from "@server/openApi";
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
@@ -29,7 +29,7 @@ async function query(data: Q) {
|
|||||||
const roleUserIds = await resolveRoleUserIds(data.orgId, data.roleId);
|
const roleUserIds = await resolveRoleUserIds(data.orgId, data.roleId);
|
||||||
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
||||||
|
|
||||||
const [totalsRow] = await db
|
const [totalsRow] = await logsDb
|
||||||
.select({
|
.select({
|
||||||
requests: count(),
|
requests: count(),
|
||||||
promptTokens: sql<number>`COALESCE(SUM(${aiUsageRecords.promptTokens}), 0)`,
|
promptTokens: sql<number>`COALESCE(SUM(${aiUsageRecords.promptTokens}), 0)`,
|
||||||
@@ -46,7 +46,7 @@ async function query(data: Q) {
|
|||||||
|
|
||||||
const dayExpr = dayBucketExpr();
|
const dayExpr = dayBucketExpr();
|
||||||
|
|
||||||
const requestsPerDay = await db
|
const requestsPerDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
requests: count()
|
requests: count()
|
||||||
@@ -56,7 +56,7 @@ async function query(data: Q) {
|
|||||||
.groupBy(dayExpr)
|
.groupBy(dayExpr)
|
||||||
.orderBy(dayExpr);
|
.orderBy(dayExpr);
|
||||||
|
|
||||||
const tokensPerDay = await db
|
const tokensPerDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
promptTokens: sql<number>`COALESCE(SUM(${aiUsageRecords.promptTokens}), 0)`,
|
promptTokens: sql<number>`COALESCE(SUM(${aiUsageRecords.promptTokens}), 0)`,
|
||||||
@@ -70,7 +70,7 @@ async function query(data: Q) {
|
|||||||
.groupBy(dayExpr)
|
.groupBy(dayExpr)
|
||||||
.orderBy(dayExpr);
|
.orderBy(dayExpr);
|
||||||
|
|
||||||
const costPerDay = await db
|
const costPerDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
cost: sql<number>`COALESCE(SUM(${aiUsageRecords.costUsd}), 0)`
|
cost: sql<number>`COALESCE(SUM(${aiUsageRecords.costUsd}), 0)`
|
||||||
@@ -80,7 +80,7 @@ async function query(data: Q) {
|
|||||||
.groupBy(dayExpr)
|
.groupBy(dayExpr)
|
||||||
.orderBy(dayExpr);
|
.orderBy(dayExpr);
|
||||||
|
|
||||||
const modelByDay = await db
|
const modelByDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
model: aiUsageRecords.requestedModel,
|
model: aiUsageRecords.requestedModel,
|
||||||
@@ -117,7 +117,7 @@ async function query(data: Q) {
|
|||||||
topModelsByTokens
|
topModelsByTokens
|
||||||
);
|
);
|
||||||
|
|
||||||
const topModelsRaw = await db
|
const topModelsRaw = await logsDb
|
||||||
.select({
|
.select({
|
||||||
model: aiUsageRecords.requestedModel,
|
model: aiUsageRecords.requestedModel,
|
||||||
requests: count(),
|
requests: count(),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { db, aiUsageRecords, aiProviders } from "@server/db";
|
import { db, logsDb, aiUsageRecords, aiProviders } from "@server/db";
|
||||||
import { registry } from "@server/openApi";
|
import { registry } from "@server/openApi";
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
@@ -29,7 +29,7 @@ async function query(data: Q) {
|
|||||||
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
||||||
const dayExpr = dayBucketExpr();
|
const dayExpr = dayBucketExpr();
|
||||||
|
|
||||||
const providerByDay = await db
|
const providerByDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
providerId: aiUsageRecords.providerId,
|
providerId: aiUsageRecords.providerId,
|
||||||
@@ -69,7 +69,7 @@ async function query(data: Q) {
|
|||||||
topByTokens
|
topByTokens
|
||||||
);
|
);
|
||||||
|
|
||||||
const topProvidersRaw = await db
|
const topProvidersRaw = await logsDb
|
||||||
.select({
|
.select({
|
||||||
providerId: aiUsageRecords.providerId,
|
providerId: aiUsageRecords.providerId,
|
||||||
requests: count(),
|
requests: count(),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { db, aiUsageRecords, resources, siteResources } from "@server/db";
|
import { db, logsDb, aiUsageRecords, resources, siteResources } from "@server/db";
|
||||||
import { registry } from "@server/openApi";
|
import { registry } from "@server/openApi";
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
@@ -39,7 +39,7 @@ async function query(data: Q) {
|
|||||||
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
||||||
const dayExpr = dayBucketExpr();
|
const dayExpr = dayBucketExpr();
|
||||||
|
|
||||||
const resourceByDay = await db
|
const resourceByDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
resourceId: aiUsageRecords.resourceId,
|
resourceId: aiUsageRecords.resourceId,
|
||||||
@@ -80,7 +80,7 @@ async function query(data: Q) {
|
|||||||
topByTokens
|
topByTokens
|
||||||
);
|
);
|
||||||
|
|
||||||
const topResourcesRaw = await db
|
const topResourcesRaw = await logsDb
|
||||||
.select({
|
.select({
|
||||||
resourceId: aiUsageRecords.resourceId,
|
resourceId: aiUsageRecords.resourceId,
|
||||||
siteResourceId: aiUsageRecords.siteResourceId,
|
siteResourceId: aiUsageRecords.siteResourceId,
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
import { db, aiUsageRecords, users, roles, userOrgRoles } from "@server/db";
|
import {
|
||||||
|
db,
|
||||||
|
logsDb,
|
||||||
|
aiUsageRecords,
|
||||||
|
users,
|
||||||
|
roles,
|
||||||
|
userOrgRoles
|
||||||
|
} from "@server/db";
|
||||||
import { registry } from "@server/openApi";
|
import { registry } from "@server/openApi";
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
@@ -36,7 +43,7 @@ async function query(data: Q) {
|
|||||||
// userId, so role totals are derived by expanding each user's usage into
|
// userId, so role totals are derived by expanding each user's usage into
|
||||||
// every role they hold in the org (per-role double counting for
|
// every role they hold in the org (per-role double counting for
|
||||||
// multi-role users is expected/accepted).
|
// multi-role users is expected/accepted).
|
||||||
const userByDay = await db
|
const userByDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
userId: aiUsageRecords.userId,
|
userId: aiUsageRecords.userId,
|
||||||
@@ -48,7 +55,7 @@ async function query(data: Q) {
|
|||||||
.groupBy(dayExpr, aiUsageRecords.userId)
|
.groupBy(dayExpr, aiUsageRecords.userId)
|
||||||
.orderBy(dayExpr);
|
.orderBy(dayExpr);
|
||||||
|
|
||||||
const userTotalsRaw = await db
|
const userTotalsRaw = await logsDb
|
||||||
.select({
|
.select({
|
||||||
userId: aiUsageRecords.userId,
|
userId: aiUsageRecords.userId,
|
||||||
requests: count(),
|
requests: count(),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { db, aiUsageRecords, virtualApiKeys } from "@server/db";
|
import { db, logsDb, aiUsageRecords, virtualApiKeys } from "@server/db";
|
||||||
import { registry } from "@server/openApi";
|
import { registry } from "@server/openApi";
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
@@ -31,7 +31,7 @@ async function query(data: Q) {
|
|||||||
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
const baseConditions = buildAiUsageWhere(data, roleUserIds);
|
||||||
const dayExpr = dayBucketExpr();
|
const dayExpr = dayBucketExpr();
|
||||||
|
|
||||||
const virtualApiKeyByDay = await db
|
const virtualApiKeyByDay = await logsDb
|
||||||
.select({
|
.select({
|
||||||
day: dayExpr.as("day"),
|
day: dayExpr.as("day"),
|
||||||
virtualApiKeyId: aiUsageRecords.virtualApiKeyId,
|
virtualApiKeyId: aiUsageRecords.virtualApiKeyId,
|
||||||
@@ -43,7 +43,7 @@ async function query(data: Q) {
|
|||||||
.groupBy(dayExpr, aiUsageRecords.virtualApiKeyId)
|
.groupBy(dayExpr, aiUsageRecords.virtualApiKeyId)
|
||||||
.orderBy(dayExpr);
|
.orderBy(dayExpr);
|
||||||
|
|
||||||
const virtualApiKeyTotalsRaw = await db
|
const virtualApiKeyTotalsRaw = await logsDb
|
||||||
.select({
|
.select({
|
||||||
virtualApiKeyId: aiUsageRecords.virtualApiKeyId,
|
virtualApiKeyId: aiUsageRecords.virtualApiKeyId,
|
||||||
requests: count(),
|
requests: count(),
|
||||||
|
|||||||
@@ -18,9 +18,6 @@ export default async function migration() {
|
|||||||
try {
|
try {
|
||||||
await db.execute(sql`BEGIN`);
|
await db.execute(sql`BEGIN`);
|
||||||
|
|
||||||
await db.execute(sql`
|
|
||||||
`);
|
|
||||||
|
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
CREATE TABLE "aiBudgetBreachEvents" (
|
CREATE TABLE "aiBudgetBreachEvents" (
|
||||||
"id" serial PRIMARY KEY NOT NULL,
|
"id" serial PRIMARY KEY NOT NULL,
|
||||||
@@ -454,14 +451,14 @@ export default async function migration() {
|
|||||||
throw new Error(fromZodError(parsedConfig.error).toString());
|
throw new Error(fromZodError(parsedConfig.error).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
traefikConfig.experimental.plugins.badger.version = "v1.6.0";
|
traefikConfig.experimental.plugins.badger.version = "v1.6.1";
|
||||||
|
|
||||||
const updatedTraefikYaml = yaml.dump(traefikConfig);
|
const updatedTraefikYaml = yaml.dump(traefikConfig);
|
||||||
|
|
||||||
fs.writeFileSync(traefikPath, updatedTraefikYaml, "utf8");
|
fs.writeFileSync(traefikPath, updatedTraefikYaml, "utf8");
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"Updated the version of Badger in your Traefik configuration to v1.6.0"
|
"Updated the version of Badger in your Traefik configuration to v1.6.1"
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(
|
console.log(
|
||||||
|
|||||||
@@ -456,14 +456,14 @@ export default async function migration() {
|
|||||||
throw new Error(fromZodError(parsedConfig.error).toString());
|
throw new Error(fromZodError(parsedConfig.error).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
traefikConfig.experimental.plugins.badger.version = "v1.6.0";
|
traefikConfig.experimental.plugins.badger.version = "v1.6.1";
|
||||||
|
|
||||||
const updatedTraefikYaml = yaml.dump(traefikConfig);
|
const updatedTraefikYaml = yaml.dump(traefikConfig);
|
||||||
|
|
||||||
fs.writeFileSync(traefikPath, updatedTraefikYaml, "utf8");
|
fs.writeFileSync(traefikPath, updatedTraefikYaml, "utf8");
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"Updated the version of Badger in your Traefik configuration to v1.6.0"
|
"Updated the version of Badger in your Traefik configuration to v1.6.1"
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(
|
console.log(
|
||||||
|
|||||||
Reference in New Issue
Block a user