mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-29 15:31:25 +02:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c411a1a5b9 | |||
| a4d9365563 | |||
| e9f7678b90 | |||
| a904c915d8 | |||
| cb84c2954b | |||
| a02d16fd58 | |||
| 331fee24d4 | |||
| e57826d6e0 | |||
| 3d4e143c1f | |||
| 10a25c184d | |||
| 906099d1e1 | |||
| 9a5824900d | |||
| 72d2c79793 | |||
| 23764feb4f | |||
| d2809fbfd1 |
@@ -465,6 +465,8 @@
|
|||||||
"apiKeysDelete": "Delete API Key",
|
"apiKeysDelete": "Delete API Key",
|
||||||
"apiKeysManage": "Manage API Keys",
|
"apiKeysManage": "Manage API Keys",
|
||||||
"apiKeysDescription": "API keys are used to authenticate with the integration API",
|
"apiKeysDescription": "API keys are used to authenticate with the integration API",
|
||||||
|
"orgsManage": "Manage Organizations",
|
||||||
|
"orgsDescription": "View and manage all organizations on this instance",
|
||||||
"provisioningKeysTitle": "Provisioning Key",
|
"provisioningKeysTitle": "Provisioning Key",
|
||||||
"provisioningKeysManage": "Manage Provisioning Keys",
|
"provisioningKeysManage": "Manage Provisioning Keys",
|
||||||
"provisioningKeysDescription": "Provisioning keys are used to authenticate automated site provisioning for your organization.",
|
"provisioningKeysDescription": "Provisioning keys are used to authenticate automated site provisioning for your organization.",
|
||||||
@@ -2089,6 +2091,7 @@
|
|||||||
"resourceBudgetSettings": "Budget",
|
"resourceBudgetSettings": "Budget",
|
||||||
"resourceBudgetSettingsDescription": "Configure how this AI gateway restricts usage based on spending or token limits",
|
"resourceBudgetSettingsDescription": "Configure how this AI gateway restricts usage based on spending or token limits",
|
||||||
"sidebarApiKeys": "API Keys",
|
"sidebarApiKeys": "API Keys",
|
||||||
|
"sidebarOrgs": "Organizations",
|
||||||
"sidebarProvisioning": "Provisioning",
|
"sidebarProvisioning": "Provisioning",
|
||||||
"sidebarSettings": "Settings",
|
"sidebarSettings": "Settings",
|
||||||
"sidebarAllUsers": "All Users",
|
"sidebarAllUsers": "All Users",
|
||||||
|
|||||||
@@ -1984,7 +1984,7 @@ export const aiSessionLog = pgTable(
|
|||||||
// were cut short at AI_SESSION_LOG_MAX_BODY_CHARS before storage.
|
// were cut short at AI_SESSION_LOG_MAX_BODY_CHARS before storage.
|
||||||
truncated: boolean("truncated").notNull().default(false),
|
truncated: boolean("truncated").notNull().default(false),
|
||||||
statusCode: integer("statusCode"),
|
statusCode: integer("statusCode"),
|
||||||
createdAt: bigint("createdAt", { mode: "number" }).notNull() // epoch ms
|
createdAt: bigint("createdAt", { mode: "number" }).notNull() // epoch seconds
|
||||||
},
|
},
|
||||||
(t) => [
|
(t) => [
|
||||||
index("idx_ai_session_log_org_created").on(t.orgId, t.createdAt),
|
index("idx_ai_session_log_org_created").on(t.orgId, t.createdAt),
|
||||||
|
|||||||
@@ -1980,7 +1980,7 @@ export const aiSessionLog = sqliteTable(
|
|||||||
.notNull()
|
.notNull()
|
||||||
.default(false),
|
.default(false),
|
||||||
statusCode: integer("statusCode"),
|
statusCode: integer("statusCode"),
|
||||||
createdAt: integer("createdAt").notNull() // epoch ms
|
createdAt: integer("createdAt").notNull() // epoch seconds
|
||||||
},
|
},
|
||||||
(t) => [
|
(t) => [
|
||||||
index("idx_ai_session_log_org_created").on(t.orgId, t.createdAt),
|
index("idx_ai_session_log_org_created").on(t.orgId, t.createdAt),
|
||||||
|
|||||||
@@ -580,6 +580,8 @@ export async function recordUsage(input: UsageRecordInput): Promise<void> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timestamp = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
usageRecordBuffer.push({
|
usageRecordBuffer.push({
|
||||||
orgId: input.orgId,
|
orgId: input.orgId,
|
||||||
providerId: input.providerId,
|
providerId: input.providerId,
|
||||||
@@ -597,7 +599,7 @@ export async function recordUsage(input: UsageRecordInput): Promise<void> {
|
|||||||
totalTokens,
|
totalTokens,
|
||||||
costUsd: input.costUsd,
|
costUsd: input.costUsd,
|
||||||
estimated: usage.estimated,
|
estimated: usage.estimated,
|
||||||
createdAt: input.createdAt ?? Date.now()
|
createdAt: input.createdAt ?? timestamp
|
||||||
});
|
});
|
||||||
|
|
||||||
// Flush immediately if buffer is full, otherwise schedule a flush
|
// Flush immediately if buffer is full, otherwise schedule a flush
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
db,
|
db,
|
||||||
|
primaryDb,
|
||||||
newts,
|
newts,
|
||||||
blueprints,
|
blueprints,
|
||||||
Blueprint,
|
Blueprint,
|
||||||
@@ -80,93 +81,103 @@ export async function applyBlueprint({
|
|||||||
trx,
|
trx,
|
||||||
siteId
|
siteId
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// We need to update the targets on the newts from the successfully updated information
|
// Push updates to newts/clients only after the transaction has
|
||||||
for (const result of publicResourcesResults) {
|
// committed. Doing this while the transaction is still open can
|
||||||
for (const target of result.targetsToUpdate) {
|
// race with the writes (e.g. newts requesting config before the
|
||||||
const [site] = await trx
|
// new targets/resources are actually visible), leaving them out
|
||||||
.select()
|
// of sync until manually toggled.
|
||||||
.from(sites)
|
|
||||||
.innerJoin(newts, eq(sites.siteId, newts.siteId))
|
// We need to update the targets on the newts from the successfully updated information
|
||||||
.where(
|
for (const result of publicResourcesResults) {
|
||||||
and(
|
for (const target of result.targetsToUpdate) {
|
||||||
eq(sites.siteId, target.siteId),
|
// read from the primary: this determines whether/how we push
|
||||||
eq(sites.orgId, orgId),
|
// the just-created target to the newt, so a lagging replica
|
||||||
eq(sites.type, "newt"),
|
// returning stale or missing data here would silently skip
|
||||||
isNotNull(sites.pubKey)
|
// the push
|
||||||
)
|
const [site] = await primaryDb
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.innerJoin(newts, eq(sites.siteId, newts.siteId))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(sites.siteId, target.siteId),
|
||||||
|
eq(sites.orgId, orgId),
|
||||||
|
eq(sites.type, "newt"),
|
||||||
|
isNotNull(sites.pubKey)
|
||||||
)
|
)
|
||||||
.limit(1);
|
)
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
if (site) {
|
if (site) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Updating target ${target.targetId} on site ${site.sites.siteId}`
|
`Updating target ${target.targetId} on site ${site.sites.siteId}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// see if you can find a matching target health check from the healthchecksToUpdate array
|
||||||
|
const matchingHealthcheck =
|
||||||
|
result.healthchecksToUpdate.find(
|
||||||
|
(hc) => hc.targetId === target.targetId
|
||||||
);
|
);
|
||||||
|
|
||||||
// see if you can find a matching target health check from the healthchecksToUpdate array
|
if (["http", "tcp", "udp"].includes(target.mode)) {
|
||||||
const matchingHealthcheck =
|
await addProxyTargets(
|
||||||
result.healthchecksToUpdate.find(
|
site.newt.newtId,
|
||||||
(hc) => hc.targetId === target.targetId
|
[target],
|
||||||
);
|
matchingHealthcheck
|
||||||
|
? [matchingHealthcheck]
|
||||||
if (["http", "tcp", "udp"].includes(target.mode)) {
|
: [],
|
||||||
await addProxyTargets(
|
result.proxyResource.mode === "udp"
|
||||||
site.newt.newtId,
|
? "udp"
|
||||||
[target],
|
: "tcp",
|
||||||
matchingHealthcheck
|
site.newt.version
|
||||||
? [matchingHealthcheck]
|
);
|
||||||
: [],
|
} else if (
|
||||||
result.proxyResource.mode === "udp"
|
["ssh", "rdp", "vnc"].includes(target.mode)
|
||||||
? "udp"
|
) {
|
||||||
: "tcp",
|
await sendBrowserGatewayTargets(
|
||||||
site.newt.version
|
site.newt.newtId,
|
||||||
);
|
[target],
|
||||||
} else if (
|
site.newt.version
|
||||||
["ssh", "rdp", "vnc"].includes(target.mode)
|
);
|
||||||
) {
|
|
||||||
await sendBrowserGatewayTargets(
|
|
||||||
site.newt.newtId,
|
|
||||||
[target],
|
|
||||||
site.newt.version
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Successfully updated public resources for org ${orgId}: ${JSON.stringify(publicResourcesResults)}`
|
`Successfully updated public resources for org ${orgId}: ${JSON.stringify(publicResourcesResults)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
// We need to update the targets on the newts from the successfully updated information
|
// We need to update the targets on the newts from the successfully updated information
|
||||||
for (const result of privateResourcesResults) {
|
for (const result of privateResourcesResults) {
|
||||||
rebuildClientAssociationsFromSiteResource(
|
rebuildClientAssociationsFromSiteResource(
|
||||||
result.newSiteResource
|
result.newSiteResource
|
||||||
|
)
|
||||||
|
.then(() =>
|
||||||
|
waitForSiteResourceRebuildIdle(
|
||||||
|
result.newSiteResource.siteResourceId
|
||||||
|
)
|
||||||
)
|
)
|
||||||
.then(() =>
|
.then(() =>
|
||||||
waitForSiteResourceRebuildIdle(
|
handleMessagingForUpdatedSiteResource(
|
||||||
result.newSiteResource.siteResourceId
|
result.oldSiteResource,
|
||||||
)
|
result.newSiteResource,
|
||||||
|
result.oldSites.map((s) => s.siteId),
|
||||||
|
result.newSites.map((s) => s.siteId)
|
||||||
)
|
)
|
||||||
.then(() =>
|
)
|
||||||
handleMessagingForUpdatedSiteResource(
|
.catch((e) => {
|
||||||
result.oldSiteResource,
|
logger.error(
|
||||||
result.newSiteResource,
|
`Failed to rebuild and handle messaging for site resource ${result.newSiteResource.siteResourceId}. Error: ${e}`
|
||||||
result.oldSites.map((s) => s.siteId),
|
);
|
||||||
result.newSites.map((s) => s.siteId)
|
});
|
||||||
)
|
}
|
||||||
)
|
|
||||||
.catch((e) => {
|
|
||||||
logger.error(
|
|
||||||
`Failed to rebuild and handle messaging for site resource ${result.newSiteResource.siteResourceId}. Error: ${e}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Successfully updated private resources for org ${orgId}: ${JSON.stringify(privateResourcesResults)}`
|
`Successfully updated private resources for org ${orgId}: ${JSON.stringify(privateResourcesResults)}`
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
blueprintSucceeded = true;
|
blueprintSucceeded = true;
|
||||||
blueprintMessage = "Blueprint applied successfully";
|
blueprintMessage = "Blueprint applied successfully";
|
||||||
|
|||||||
@@ -52,8 +52,7 @@ export async function validateAndConstructDomain(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if organization has access to domain
|
if (!domainRes.orgDomains) {
|
||||||
if (domainRes.orgDomains && domainRes.orgDomains.orgId !== orgId) {
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `Organization does not have access to domain with ID ${domainId}`
|
error: `Organization does not have access to domain with ID ${domainId}`
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { gzipSync, gunzipSync } from "zlib";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gzip a string and return it as base64 so it can be stored in a TEXT column.
|
||||||
|
*/
|
||||||
|
export function compressText(value: string): string {
|
||||||
|
return gzipSync(Buffer.from(value, "utf8")).toString("base64");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse of compressText - base64-decode and gunzip back to the original string.
|
||||||
|
*/
|
||||||
|
export function decompressText(value: string): string {
|
||||||
|
return gunzipSync(Buffer.from(value, "base64")).toString("utf8");
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ import {
|
|||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { and, eq, gt, desc, max, sql } from "drizzle-orm";
|
import { and, eq, gt, desc, max, sql } from "drizzle-orm";
|
||||||
import { decrypt } from "@server/lib/crypto";
|
import { decrypt } from "@server/lib/crypto";
|
||||||
|
import { decompressText } from "@server/lib/textCompression";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import {
|
import {
|
||||||
LogType,
|
LogType,
|
||||||
@@ -680,8 +681,8 @@ export class LogStreamingManager {
|
|||||||
Record<string, unknown> & { id: number }
|
Record<string, unknown> & { id: number }
|
||||||
>;
|
>;
|
||||||
|
|
||||||
case "aiSession":
|
case "aiSession": {
|
||||||
return (await logsDb
|
const rows = (await logsDb
|
||||||
.select()
|
.select()
|
||||||
.from(aiSessionLog)
|
.from(aiSessionLog)
|
||||||
.where(
|
.where(
|
||||||
@@ -694,6 +695,33 @@ export class LogStreamingManager {
|
|||||||
.limit(limit)) as Array<
|
.limit(limit)) as Array<
|
||||||
Record<string, unknown> & { id: number }
|
Record<string, unknown> & { id: number }
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
const compressedFields = [
|
||||||
|
"requestBody",
|
||||||
|
"responseBody",
|
||||||
|
"normalizedRequest",
|
||||||
|
"normalizedResponse"
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
for (const field of compressedFields) {
|
||||||
|
const value = row[field];
|
||||||
|
if (typeof value !== "string") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
row[field] = decompressText(value);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(
|
||||||
|
`Failed to decompress AI session log field ${field}`,
|
||||||
|
{ error }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import { and, eq, lt } from "drizzle-orm";
|
|||||||
import cache from "#private/lib/cache";
|
import cache from "#private/lib/cache";
|
||||||
import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs";
|
import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs";
|
||||||
import { sanitizeString } from "@server/lib/sanitize";
|
import { sanitizeString } from "@server/lib/sanitize";
|
||||||
|
import { compressText } from "@server/lib/textCompression";
|
||||||
import type { AiCapability } from "@server/lib/aiCapabilities";
|
import type { AiCapability } from "@server/lib/aiCapabilities";
|
||||||
import {
|
import {
|
||||||
normalizeAiRequest,
|
normalizeAiRequest,
|
||||||
@@ -151,17 +152,14 @@ async function getRetentionDays(orgId: string): Promise<number> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function cleanUpOldLogs(orgId: string, retentionDays: number) {
|
export async function cleanUpOldLogs(orgId: string, retentionDays: number) {
|
||||||
// calculateCutoffTimestamp returns a seconds-epoch cutoff (built for
|
const cutoffTimestamp = calculateCutoffTimestamp(retentionDays);
|
||||||
// requestAuditLog.timestamp), but aiSessionLog.createdAt is ms-epoch to
|
|
||||||
// match aiUsageRecords - convert before comparing.
|
|
||||||
const cutoffTimestampMs = calculateCutoffTimestamp(retentionDays) * 1000;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await logsDb
|
await logsDb
|
||||||
.delete(aiSessionLog)
|
.delete(aiSessionLog)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
lt(aiSessionLog.createdAt, cutoffTimestampMs),
|
lt(aiSessionLog.createdAt, cutoffTimestamp),
|
||||||
eq(aiSessionLog.orgId, orgId)
|
eq(aiSessionLog.orgId, orgId)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -243,6 +241,8 @@ export function logAiSession(data: {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timestamp = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
sessionLogBuffer.push({
|
sessionLogBuffer.push({
|
||||||
sessionId: data.sessionId,
|
sessionId: data.sessionId,
|
||||||
orgId: sanitizeString(data.orgId),
|
orgId: sanitizeString(data.orgId),
|
||||||
@@ -256,13 +256,19 @@ export function logAiSession(data: {
|
|||||||
),
|
),
|
||||||
requestedModel: sanitizeString(data.requestedModel),
|
requestedModel: sanitizeString(data.requestedModel),
|
||||||
isStream: data.isStream,
|
isStream: data.isStream,
|
||||||
requestBody: sanitizeString(requestBodyText.value),
|
requestBody: compressText(
|
||||||
responseBody: sanitizeString(responseBodyText.value),
|
sanitizeString(requestBodyText.value)
|
||||||
|
),
|
||||||
|
responseBody: compressText(
|
||||||
|
sanitizeString(responseBodyText.value)
|
||||||
|
),
|
||||||
normalizedRequest: normalizedRequestText
|
normalizedRequest: normalizedRequestText
|
||||||
? sanitizeString(normalizedRequestText.value)
|
? compressText(sanitizeString(normalizedRequestText.value))
|
||||||
: undefined,
|
: undefined,
|
||||||
normalizedResponse: normalizedResponseText
|
normalizedResponse: normalizedResponseText
|
||||||
? sanitizeString(normalizedResponseText.value)
|
? compressText(
|
||||||
|
sanitizeString(normalizedResponseText.value)
|
||||||
|
)
|
||||||
: undefined,
|
: undefined,
|
||||||
truncated:
|
truncated:
|
||||||
requestBodyText.truncated ||
|
requestBodyText.truncated ||
|
||||||
@@ -270,7 +276,7 @@ export function logAiSession(data: {
|
|||||||
(normalizedRequestText?.truncated ?? false) ||
|
(normalizedRequestText?.truncated ?? false) ||
|
||||||
(normalizedResponseText?.truncated ?? false),
|
(normalizedResponseText?.truncated ?? false),
|
||||||
statusCode: data.statusCode,
|
statusCode: data.statusCode,
|
||||||
createdAt: Date.now()
|
createdAt: timestamp
|
||||||
});
|
});
|
||||||
|
|
||||||
// Flush immediately if buffer is full, otherwise schedule a flush
|
// Flush immediately if buffer is full, otherwise schedule a flush
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export const aiUsageAnalyticsFiltersQuery = z.object({
|
|||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
.refine((val) => !isNaN(Date.parse(val)), {
|
||||||
error: "timeStart must be a valid ISO date string"
|
error: "timeStart must be a valid ISO date string"
|
||||||
})
|
})
|
||||||
.transform((val) => new Date(val).getTime())
|
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||||
.prefault(() => getSevenDaysAgo().toISOString())
|
.prefault(() => getSevenDaysAgo().toISOString())
|
||||||
.openapi({
|
.openapi({
|
||||||
type: "string",
|
type: "string",
|
||||||
@@ -31,7 +31,7 @@ export const aiUsageAnalyticsFiltersQuery = z.object({
|
|||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
.refine((val) => !isNaN(Date.parse(val)), {
|
||||||
error: "timeEnd must be a valid ISO date string"
|
error: "timeEnd must be a valid ISO date string"
|
||||||
})
|
})
|
||||||
.transform((val) => new Date(val).getTime())
|
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||||
.prefault(() => new Date().toISOString())
|
.prefault(() => new Date().toISOString())
|
||||||
.openapi({
|
.openapi({
|
||||||
type: "string",
|
type: "string",
|
||||||
@@ -122,12 +122,12 @@ export function buildAiUsageWhere(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buckets createdAt (epoch ms) down to a per-day string, dialect-aware, same
|
// Buckets createdAt (epoch seconds) down to a per-day string, dialect-aware,
|
||||||
// approach as the DATE_TRUNC/DATE branch in queryRequestAnalytics.ts.
|
// same approach as the DATE_TRUNC/DATE branch in queryRequestAnalytics.ts.
|
||||||
export function dayBucketExpr() {
|
export function dayBucketExpr() {
|
||||||
return driver === "pg"
|
return driver === "pg"
|
||||||
? sql<string>`DATE_TRUNC('day', TO_TIMESTAMP(${aiUsageRecords.createdAt} / 1000.0))`
|
? sql<string>`DATE_TRUNC('day', TO_TIMESTAMP(${aiUsageRecords.createdAt}))`
|
||||||
: sql<string>`DATE(${aiUsageRecords.createdAt} / 1000, 'unixepoch')`;
|
: sql<string>`DATE(${aiUsageRecords.createdAt}, 'unixepoch')`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DailyMetricRow<K extends string> = {
|
export type DailyMetricRow<K extends string> = {
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ import {
|
|||||||
queryAiSessionLogsQuery,
|
queryAiSessionLogsQuery,
|
||||||
queryAiSessionLogsParams,
|
queryAiSessionLogsParams,
|
||||||
queryAiSession,
|
queryAiSession,
|
||||||
countAiSessionQuery
|
countAiSessionQuery,
|
||||||
|
decompressAiSessionLogRow
|
||||||
} from "./queryAiSessionLog";
|
} from "./queryAiSessionLog";
|
||||||
import { generateCSV } from "./generateCSV";
|
import { generateCSV } from "./generateCSV";
|
||||||
|
|
||||||
@@ -87,7 +88,9 @@ export async function exportAiSessionLogs(
|
|||||||
|
|
||||||
const baseQuery = queryAiSession(data);
|
const baseQuery = queryAiSession(data);
|
||||||
|
|
||||||
const log = await baseQuery.limit(MAX_EXPORT_LIMIT);
|
const log = (await baseQuery.limit(MAX_EXPORT_LIMIT)).map(
|
||||||
|
decompressAiSessionLogRow
|
||||||
|
);
|
||||||
|
|
||||||
const csvData = generateCSV(log);
|
const csvData = generateCSV(log);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import { AI_CAPABILITIES } from "@server/lib/aiCapabilities";
|
|||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { getSevenDaysAgo } from "@app/lib/getSevenDaysAgo";
|
import { getSevenDaysAgo } from "@app/lib/getSevenDaysAgo";
|
||||||
|
import { decompressText } from "@server/lib/textCompression";
|
||||||
|
|
||||||
export const queryAiSessionLogsQuery = z.strictObject({
|
export const queryAiSessionLogsQuery = z.strictObject({
|
||||||
// iso string just validate its a parseable date
|
// iso string just validate its a parseable date
|
||||||
@@ -32,7 +33,7 @@ export const queryAiSessionLogsQuery = z.strictObject({
|
|||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
.refine((val) => !isNaN(Date.parse(val)), {
|
||||||
error: "timeStart must be a valid ISO date string"
|
error: "timeStart must be a valid ISO date string"
|
||||||
})
|
})
|
||||||
.transform((val) => new Date(val).getTime())
|
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||||
.prefault(() => getSevenDaysAgo().toISOString())
|
.prefault(() => getSevenDaysAgo().toISOString())
|
||||||
.openapi({
|
.openapi({
|
||||||
type: "string",
|
type: "string",
|
||||||
@@ -45,7 +46,7 @@ export const queryAiSessionLogsQuery = z.strictObject({
|
|||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
.refine((val) => !isNaN(Date.parse(val)), {
|
||||||
error: "timeEnd must be a valid ISO date string"
|
error: "timeEnd must be a valid ISO date string"
|
||||||
})
|
})
|
||||||
.transform((val) => new Date(val).getTime())
|
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||||
.optional()
|
.optional()
|
||||||
.prefault(() => new Date().toISOString())
|
.prefault(() => new Date().toISOString())
|
||||||
.openapi({
|
.openapi({
|
||||||
@@ -166,6 +167,35 @@ export function queryAiSession(data: Q) {
|
|||||||
.orderBy(desc(aiSessionLog.createdAt));
|
.orderBy(desc(aiSessionLog.createdAt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function decompressField(value: string | null): string | null {
|
||||||
|
if (value == null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return decompressText(value);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Failed to decompress AI session log field", { error });
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decompressAiSessionLogRow<
|
||||||
|
T extends {
|
||||||
|
requestBody: string | null;
|
||||||
|
responseBody: string | null;
|
||||||
|
normalizedRequest: string | null;
|
||||||
|
normalizedResponse: string | null;
|
||||||
|
}
|
||||||
|
>(row: T): T {
|
||||||
|
return {
|
||||||
|
...row,
|
||||||
|
requestBody: decompressField(row.requestBody),
|
||||||
|
responseBody: decompressField(row.responseBody),
|
||||||
|
normalizedRequest: decompressField(row.normalizedRequest),
|
||||||
|
normalizedResponse: decompressField(row.normalizedResponse)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function enrichWithDetails(
|
async function enrichWithDetails(
|
||||||
logs: Awaited<ReturnType<typeof queryAiSession>>
|
logs: Awaited<ReturnType<typeof queryAiSession>>
|
||||||
) {
|
) {
|
||||||
@@ -620,7 +650,9 @@ export async function queryAiSessionLogs(
|
|||||||
|
|
||||||
const baseQuery = queryAiSession(data);
|
const baseQuery = queryAiSession(data);
|
||||||
|
|
||||||
const logsRaw = await baseQuery.limit(data.limit).offset(data.offset);
|
const logsRaw = (
|
||||||
|
await baseQuery.limit(data.limit).offset(data.offset)
|
||||||
|
).map(decompressAiSessionLogRow);
|
||||||
|
|
||||||
const log = await enrichWithDetails(logsRaw);
|
const log = await enrichWithDetails(logsRaw);
|
||||||
|
|
||||||
|
|||||||
@@ -30,14 +30,14 @@ const queryAiUsageFilterOptionsQuery = z.object({
|
|||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
.refine((val) => !isNaN(Date.parse(val)), {
|
||||||
error: "timeStart must be a valid ISO date string"
|
error: "timeStart must be a valid ISO date string"
|
||||||
})
|
})
|
||||||
.transform((val) => new Date(val).getTime())
|
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||||
.prefault(() => getSevenDaysAgo().toISOString()),
|
.prefault(() => getSevenDaysAgo().toISOString()),
|
||||||
timeEnd: z
|
timeEnd: z
|
||||||
.string()
|
.string()
|
||||||
.refine((val) => !isNaN(Date.parse(val)), {
|
.refine((val) => !isNaN(Date.parse(val)), {
|
||||||
error: "timeEnd must be a valid ISO date string"
|
error: "timeEnd must be a valid ISO date string"
|
||||||
})
|
})
|
||||||
.transform((val) => new Date(val).getTime())
|
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||||
.prefault(() => new Date().toISOString())
|
.prefault(() => new Date().toISOString())
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+16
-11
@@ -66,6 +66,10 @@ import * as aiBudget from "@server/routers/aiBudget";
|
|||||||
import * as virtualApiKey from "@server/routers/virtualApiKey";
|
import * as virtualApiKey from "@server/routers/virtualApiKey";
|
||||||
import * as certificates from "@server/routers/certificates";
|
import * as certificates from "@server/routers/certificates";
|
||||||
|
|
||||||
|
function rateLimitIdentityKey(value: unknown): string {
|
||||||
|
return typeof value === "string" ? value.trim().toLowerCase() : "";
|
||||||
|
}
|
||||||
|
|
||||||
// Root routes
|
// Root routes
|
||||||
export const unauthenticated = Router();
|
export const unauthenticated = Router();
|
||||||
|
|
||||||
@@ -83,6 +87,7 @@ authenticated.get("/org/checkId", org.checkId);
|
|||||||
authenticated.put("/org", getUserOrgs, org.createOrg);
|
authenticated.put("/org", getUserOrgs, org.createOrg);
|
||||||
|
|
||||||
authenticated.get("/orgs", verifyUserIsServerAdmin, org.listOrgs);
|
authenticated.get("/orgs", verifyUserIsServerAdmin, org.listOrgs);
|
||||||
|
authenticated.get("/admin/orgs", verifyUserIsServerAdmin, org.adminListOrgs);
|
||||||
authenticated.get("/user/:userId/orgs", verifyIsLoggedInUser, org.listUserOrgs);
|
authenticated.get("/user/:userId/orgs", verifyIsLoggedInUser, org.listUserOrgs);
|
||||||
|
|
||||||
authenticated.get(
|
authenticated.get(
|
||||||
@@ -1927,7 +1932,7 @@ authRouter.put(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`signup:${ipKeyGenerator(req.ip || "")}:${req.body.email}`,
|
`signup:${ipKeyGenerator(req.ip || "")}:${rateLimitIdentityKey(req.body.email)}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only sign up ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only sign up ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -1942,7 +1947,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`login:${req.body.email || ipKeyGenerator(req.ip || "")}`,
|
`login:${rateLimitIdentityKey(req.body.email) || ipKeyGenerator(req.ip || "")}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only log in ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only log in ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -1959,7 +1964,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`lookupUser:${req.body.identifier || ipKeyGenerator(req.ip || "")}`,
|
`lookupUser:${rateLimitIdentityKey(req.body.identifier) || ipKeyGenerator(req.ip || "")}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only lookup users ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only lookup users ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -2037,7 +2042,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) => {
|
keyGenerator: (req) => {
|
||||||
return `signup:${req.body.email || req.user?.userId || ipKeyGenerator(req.ip || "")}`;
|
return `signup:${rateLimitIdentityKey(req.body.email) || req.user?.userId || ipKeyGenerator(req.ip || "")}`;
|
||||||
},
|
},
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only enable 2FA ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only enable 2FA ${15} times every ${15} minutes. Please try again later.`;
|
||||||
@@ -2053,7 +2058,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) => {
|
keyGenerator: (req) => {
|
||||||
return `signup:${req.body.email || req.user?.userId || ipKeyGenerator(req.ip || "")}`;
|
return `signup:${rateLimitIdentityKey(req.body.email) || req.user?.userId || ipKeyGenerator(req.ip || "")}`;
|
||||||
},
|
},
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only request a 2FA code ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request a 2FA code ${15} times every ${15} minutes. Please try again later.`;
|
||||||
@@ -2085,7 +2090,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`signup:${req.body.email || ipKeyGenerator(req.ip || "")}`,
|
`signup:${rateLimitIdentityKey(req.body.email) || ipKeyGenerator(req.ip || "")}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only sign up ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only sign up ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -2103,7 +2108,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`requestEmailVerificationCode:${req.user?.email || ipKeyGenerator(req.ip || "")}`,
|
`requestEmailVerificationCode:${rateLimitIdentityKey(req.user?.email) || ipKeyGenerator(req.ip || "")}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only request an email verification code ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request an email verification code ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -2125,7 +2130,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`requestPasswordReset:${req.body.email || ipKeyGenerator(req.ip || "")}`,
|
`requestPasswordReset:${rateLimitIdentityKey(req.body.email) || ipKeyGenerator(req.ip || "")}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only request a password reset ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request a password reset ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -2141,7 +2146,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`resetPassword:${req.body.email || ipKeyGenerator(req.ip || "")}`,
|
`resetPassword:${rateLimitIdentityKey(req.body.email) || ipKeyGenerator(req.ip || "")}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only request a password reset ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request a password reset ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -2188,7 +2193,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
max: 15,
|
max: 15,
|
||||||
keyGenerator: (req) =>
|
keyGenerator: (req) =>
|
||||||
`authWithWhitelist:${ipKeyGenerator(req.ip || "")}:${req.body.email}:${req.params.resourceId}`,
|
`authWithWhitelist:${ipKeyGenerator(req.ip || "")}:${rateLimitIdentityKey(req.body.email)}:${req.params.resourceId}`,
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only request an email OTP ${15} times every ${15} minutes. Please try again later.`;
|
const message = `You can only request an email OTP ${15} times every ${15} minutes. Please try again later.`;
|
||||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||||
@@ -2240,7 +2245,7 @@ authRouter.post(
|
|||||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||||
max: 10, // Allow 10 authentication attempts per 15 minutes per IP
|
max: 10, // Allow 10 authentication attempts per 15 minutes per IP
|
||||||
keyGenerator: (req) => {
|
keyGenerator: (req) => {
|
||||||
return `securityKeyAuth:${req.body.email || ipKeyGenerator(req.ip || "")}`;
|
return `securityKeyAuth:${rateLimitIdentityKey(req.body.email) || ipKeyGenerator(req.ip || "")}`;
|
||||||
},
|
},
|
||||||
handler: (req, res, next) => {
|
handler: (req, res, next) => {
|
||||||
const message = `You can only attempt security key authentication ${10} times every ${15} minutes. Please try again later.`;
|
const message = `You can only attempt security key authentication ${10} times every ${15} minutes. Please try again later.`;
|
||||||
|
|||||||
@@ -0,0 +1,241 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db, users } from "@server/db";
|
||||||
|
import { orgs, resources, sites, userOrgs } from "@server/db";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import { and, asc, desc, eq, like, or, sql, type SQL } from "drizzle-orm";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
import { createApiResponseSchema } from "@server/lib/openapi/createApiResponseSchema";
|
||||||
|
import type { PaginatedResponse } from "@server/types/Pagination";
|
||||||
|
|
||||||
|
const adminListOrgsSchema = z.strictObject({
|
||||||
|
pageSize: z.coerce
|
||||||
|
.number<string>()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.optional()
|
||||||
|
.catch(20)
|
||||||
|
.default(20)
|
||||||
|
.openapi({
|
||||||
|
type: "integer",
|
||||||
|
default: 20,
|
||||||
|
description: "Number of items per page"
|
||||||
|
}),
|
||||||
|
page: z.coerce
|
||||||
|
.number<string>()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.optional()
|
||||||
|
.catch(1)
|
||||||
|
.default(1)
|
||||||
|
.openapi({
|
||||||
|
type: "integer",
|
||||||
|
default: 1,
|
||||||
|
description: "Page number to retrieve"
|
||||||
|
}),
|
||||||
|
query: z.string().optional(),
|
||||||
|
sort_by: z
|
||||||
|
.enum(["name", "createdAt"])
|
||||||
|
.optional()
|
||||||
|
.catch(undefined)
|
||||||
|
.openapi({
|
||||||
|
type: "string",
|
||||||
|
enum: ["name", "createdAt"],
|
||||||
|
description: "Field to sort by"
|
||||||
|
}),
|
||||||
|
order: z
|
||||||
|
.enum(["asc", "desc"])
|
||||||
|
.optional()
|
||||||
|
.default("asc")
|
||||||
|
.catch("asc")
|
||||||
|
.openapi({
|
||||||
|
type: "string",
|
||||||
|
enum: ["asc", "desc"],
|
||||||
|
default: "asc",
|
||||||
|
description: "Sort order"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AdminOrgRow = {
|
||||||
|
orgId: string;
|
||||||
|
name: string;
|
||||||
|
subnet: string | null;
|
||||||
|
utilitySubnet: string | null;
|
||||||
|
createdAt: string | null;
|
||||||
|
userCount: number;
|
||||||
|
siteCount: number;
|
||||||
|
resourceCount: number;
|
||||||
|
owner: {
|
||||||
|
userId: string;
|
||||||
|
username: string;
|
||||||
|
} | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AdminListOrgsResponse = PaginatedResponse<{
|
||||||
|
orgs: AdminOrgRow[];
|
||||||
|
}>;
|
||||||
|
|
||||||
|
const AdminListOrgsResponseDataSchema = z.object({
|
||||||
|
orgs: z.array(
|
||||||
|
z.object({
|
||||||
|
orgId: z.string(),
|
||||||
|
name: z.string(),
|
||||||
|
subnet: z.string().nullable(),
|
||||||
|
createdAt: z.string().nullable(),
|
||||||
|
userCount: z.number(),
|
||||||
|
siteCount: z.number(),
|
||||||
|
resourceCount: z.number()
|
||||||
|
})
|
||||||
|
),
|
||||||
|
pagination: z.object({
|
||||||
|
total: z.number(),
|
||||||
|
page: z.number(),
|
||||||
|
pageSize: z.number()
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
registry.registerPath({
|
||||||
|
method: "get",
|
||||||
|
path: "/admin/orgs",
|
||||||
|
description:
|
||||||
|
"List all organizations in the system with usage counts (server admin).",
|
||||||
|
tags: [OpenAPITags.Org],
|
||||||
|
request: {
|
||||||
|
query: adminListOrgsSchema
|
||||||
|
},
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
description: "Successful response",
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: createApiResponseSchema(
|
||||||
|
AdminListOrgsResponseDataSchema
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function adminListOrgs(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedQuery = adminListOrgsSchema.safeParse(req.query);
|
||||||
|
if (!parsedQuery.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedQuery.error)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { pageSize, page, query, sort_by, order } = parsedQuery.data;
|
||||||
|
|
||||||
|
let conditions: (SQL<unknown> | undefined)[] = [];
|
||||||
|
if (query) {
|
||||||
|
const q = "%" + query.toLowerCase() + "%";
|
||||||
|
conditions.push(
|
||||||
|
or(
|
||||||
|
like(sql`LOWER(${orgs.name})`, q),
|
||||||
|
like(sql`LOWER(${orgs.orgId})`, q),
|
||||||
|
like(sql`LOWER(${orgs.subnet})`, q)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortColumns = {
|
||||||
|
name: orgs.name,
|
||||||
|
createdAt: orgs.createdAt
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
const orderBy = sort_by
|
||||||
|
? order === "asc"
|
||||||
|
? asc(sortColumns[sort_by])
|
||||||
|
: desc(sortColumns[sort_by])
|
||||||
|
: asc(orgs.name);
|
||||||
|
|
||||||
|
// Drizzle renders bare column references in the select list without their
|
||||||
|
// table prefix, which would make a correlated subquery compare a column to
|
||||||
|
// itself, so the outer `orgs` side is qualified explicitly.
|
||||||
|
const orgIdRef = sql`${sql.identifier("orgs")}.${sql.identifier("orgId")}`;
|
||||||
|
|
||||||
|
const [countRows, rows] = await Promise.all([
|
||||||
|
db
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(orgs)
|
||||||
|
.where(and(...conditions)),
|
||||||
|
db
|
||||||
|
.selectDistinct({
|
||||||
|
orgId: orgs.orgId,
|
||||||
|
name: orgs.name,
|
||||||
|
subnet: orgs.subnet,
|
||||||
|
utilitySubnet: orgs.utilitySubnet,
|
||||||
|
createdAt: orgs.createdAt,
|
||||||
|
userCount: sql<number>`(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ${userOrgs}
|
||||||
|
WHERE ${userOrgs.orgId} = ${orgIdRef}
|
||||||
|
)`.as("userCount"),
|
||||||
|
siteCount: sql<number>`(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ${sites}
|
||||||
|
WHERE ${sites.orgId} = ${orgIdRef}
|
||||||
|
)`.as("siteCount"),
|
||||||
|
resourceCount: sql<number>`(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ${resources}
|
||||||
|
WHERE ${resources.orgId} = ${orgIdRef}
|
||||||
|
)`.as("resourceCount"),
|
||||||
|
owner: {
|
||||||
|
userId: users.userId,
|
||||||
|
username: users.username
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.from(orgs)
|
||||||
|
.where(and(...conditions, eq(userOrgs.isOwner, true)))
|
||||||
|
.leftJoin(userOrgs, eq(userOrgs.orgId, orgs.orgId))
|
||||||
|
.leftJoin(users, eq(userOrgs.userId, users.userId))
|
||||||
|
.limit(pageSize)
|
||||||
|
.offset(pageSize * (page - 1))
|
||||||
|
.orderBy(orderBy)
|
||||||
|
]);
|
||||||
|
|
||||||
|
const totalCount = Number(countRows[0]?.count ?? 0);
|
||||||
|
|
||||||
|
return response<AdminListOrgsResponse>(res, {
|
||||||
|
data: {
|
||||||
|
orgs: rows.map((row) => ({
|
||||||
|
...row,
|
||||||
|
userCount: Number(row.userCount ?? 0),
|
||||||
|
siteCount: Number(row.siteCount ?? 0),
|
||||||
|
resourceCount: Number(row.resourceCount ?? 0)
|
||||||
|
})),
|
||||||
|
pagination: {
|
||||||
|
total: totalCount,
|
||||||
|
page,
|
||||||
|
pageSize
|
||||||
|
}
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Organizations retrieved successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"An error occurred..."
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,13 +16,12 @@ const getOrgSchema = z.strictObject({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export type GetOrgResponse = {
|
export type GetOrgResponse = {
|
||||||
org: Org;
|
org: Omit<Org, "sshCaPrivateKey">;
|
||||||
};
|
};
|
||||||
const GetOrgResponseDataSchema = z.object({
|
const GetOrgResponseDataSchema = z.object({
|
||||||
org: z.object({}).passthrough()
|
org: z.object({}).passthrough()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
registry.registerPath({
|
registry.registerPath({
|
||||||
method: "get",
|
method: "get",
|
||||||
path: "/org/{orgId}",
|
path: "/org/{orgId}",
|
||||||
@@ -76,9 +75,12 @@ export async function getOrg(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sshCaPrivateKey is encrypted anyway but just to be safe
|
||||||
|
const { sshCaPrivateKey: _, ...orgWithoutPrivateKey } = org;
|
||||||
|
|
||||||
return response<GetOrgResponse>(res, {
|
return response<GetOrgResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
org
|
org: orgWithoutPrivateKey
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
error: false,
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ export * from "./listOrgs";
|
|||||||
export * from "./pickOrgDefaults";
|
export * from "./pickOrgDefaults";
|
||||||
export * from "./checkOrgUserAccess";
|
export * from "./checkOrgUserAccess";
|
||||||
export * from "./resetOrgBandwidth";
|
export * from "./resetOrgBandwidth";
|
||||||
|
export * from "./adminListOrgs";
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ const getSiteResourceParamsSchema = z.strictObject({
|
|||||||
.pipe(z.int().positive().optional())
|
.pipe(z.int().positive().optional())
|
||||||
.optional(),
|
.optional(),
|
||||||
niceId: z.string().optional(),
|
niceId: z.string().optional(),
|
||||||
orgId: z.string()
|
orgId: z.string().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
async function query(siteResourceId?: number, niceId?: string, orgId?: string) {
|
async function query(siteResourceId?: number, niceId?: string, orgId?: string) {
|
||||||
@@ -34,6 +34,13 @@ async function query(siteResourceId?: number, niceId?: string, orgId?: string) {
|
|||||||
)
|
)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
return siteResource;
|
return siteResource;
|
||||||
|
} else if (siteResourceId) {
|
||||||
|
const [siteResource] = await db
|
||||||
|
.select()
|
||||||
|
.from(siteResources)
|
||||||
|
.where(eq(siteResources.siteResourceId, siteResourceId))
|
||||||
|
.limit(1);
|
||||||
|
return siteResource;
|
||||||
} else if (niceId && orgId) {
|
} else if (niceId && orgId) {
|
||||||
const [siteResource] = await db
|
const [siteResource] = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -60,9 +67,7 @@ registry.registerPath({
|
|||||||
tags: [OpenAPITags.PrivateResourceLegacy],
|
tags: [OpenAPITags.PrivateResourceLegacy],
|
||||||
request: {
|
request: {
|
||||||
params: z.object({
|
params: z.object({
|
||||||
siteResourceId: z.number(),
|
siteResourceId: z.number()
|
||||||
siteId: z.number(),
|
|
||||||
orgId: z.string()
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
responses: {
|
responses: {
|
||||||
@@ -90,9 +95,7 @@ registry.registerPath({
|
|||||||
tags: [OpenAPITags.PrivateResource],
|
tags: [OpenAPITags.PrivateResource],
|
||||||
request: {
|
request: {
|
||||||
params: z.object({
|
params: z.object({
|
||||||
siteResourceId: z.number(),
|
siteResourceId: z.number()
|
||||||
siteId: z.number(),
|
|
||||||
orgId: z.string()
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
responses: {
|
responses: {
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ export default async function migration() {
|
|||||||
sql`ALTER TABLE "subscriptions" ADD COLUMN "override" boolean DEFAULT false;`
|
sql`ALTER TABLE "subscriptions" ADD COLUMN "override" boolean DEFAULT false;`
|
||||||
);
|
);
|
||||||
await db.execute(
|
await db.execute(
|
||||||
sql`ALTER TABLE "orgs" ADD COLUMN "settingsLogRetentionDaysAISessions" integer DEFAULT 7 NOT NULL;`
|
sql`ALTER TABLE "orgs" ADD COLUMN "settingsLogRetentionDaysAISessions" integer DEFAULT 0 NOT NULL;`
|
||||||
);
|
);
|
||||||
await db.execute(
|
await db.execute(
|
||||||
sql`ALTER TABLE "siteResources" ADD COLUMN "requiresExitNodeConnection" boolean DEFAULT false NOT NULL;`
|
sql`ALTER TABLE "siteResources" ADD COLUMN "requiresExitNodeConnection" boolean DEFAULT false NOT NULL;`
|
||||||
@@ -345,6 +345,9 @@ export default async function migration() {
|
|||||||
await db.execute(
|
await db.execute(
|
||||||
sql`ALTER TABLE "virtualApiKeys" ADD CONSTRAINT "virtualApiKeys_createdByUserId_user_id_fk" FOREIGN KEY ("createdByUserId") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;`
|
sql`ALTER TABLE "virtualApiKeys" ADD CONSTRAINT "virtualApiKeys_createdByUserId_user_id_fk" FOREIGN KEY ("createdByUserId") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;`
|
||||||
);
|
);
|
||||||
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "eventStreamingDestinations" ADD "sendAISessionLogs" boolean DEFAULT false NOT NULL;`
|
||||||
|
);
|
||||||
await db.execute(
|
await db.execute(
|
||||||
sql`CREATE INDEX "idx_ai_budget_breach_events_budget_created" ON "aiBudgetBreachEvents" USING btree ("budgetId","createdAt");`
|
sql`CREATE INDEX "idx_ai_budget_breach_events_budget_created" ON "aiBudgetBreachEvents" USING btree ("budgetId","createdAt");`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -397,11 +397,14 @@ export default async function migration() {
|
|||||||
`ALTER TABLE 'clients' ADD 'exitNodeSubnet' text;`
|
`ALTER TABLE 'clients' ADD 'exitNodeSubnet' text;`
|
||||||
).run();
|
).run();
|
||||||
db.prepare(
|
db.prepare(
|
||||||
`ALTER TABLE 'orgs' ADD 'settingsLogRetentionDaysAISessions' integer DEFAULT 7 NOT NULL;`
|
`ALTER TABLE 'orgs' ADD 'settingsLogRetentionDaysAISessions' integer DEFAULT 0 NOT NULL;`
|
||||||
).run();
|
).run();
|
||||||
db.prepare(
|
db.prepare(
|
||||||
`ALTER TABLE 'siteResources' ADD 'requiresExitNodeConnection' integer DEFAULT false NOT NULL;`
|
`ALTER TABLE 'siteResources' ADD 'requiresExitNodeConnection' integer DEFAULT false NOT NULL;`
|
||||||
).run();
|
).run();
|
||||||
|
db.prepare(
|
||||||
|
`ALTER TABLE 'eventStreamingDestinations' ADD 'sendAISessionLogs' integer DEFAULT false NOT NULL;`
|
||||||
|
).run();
|
||||||
|
|
||||||
const insertRoleAction = db.prepare(`
|
const insertRoleAction = db.prepare(`
|
||||||
INSERT INTO 'roleActions' ("roleId", "actionId", "orgId")
|
INSERT INTO 'roleActions' ("roleId", "actionId", "orgId")
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import SettingsSectionTitle from "@app/components/SettingsSectionTitle";
|
||||||
|
import OrgsTable from "@app/components/OrgsTable";
|
||||||
|
import { internal } from "@app/lib/api";
|
||||||
|
import { authCookieHeader } from "@app/lib/api/cookies";
|
||||||
|
import type { AdminListOrgsResponse } from "@server/routers/org";
|
||||||
|
import type { AxiosResponse } from "axios";
|
||||||
|
import type { Metadata } from "next";
|
||||||
|
import { getTranslations } from "next-intl/server";
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: "Organizations"
|
||||||
|
};
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
type OrganizationsPageProps = {
|
||||||
|
searchParams: Promise<Record<string, string>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function OrganizationsPage(props: OrganizationsPageProps) {
|
||||||
|
const searchParams = new URLSearchParams(await props.searchParams);
|
||||||
|
|
||||||
|
let orgs: AdminListOrgsResponse["orgs"] = [];
|
||||||
|
let pagination: AdminListOrgsResponse["pagination"] = {
|
||||||
|
total: 0,
|
||||||
|
page: 1,
|
||||||
|
pageSize: 20
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await internal.get<AxiosResponse<AdminListOrgsResponse>>(
|
||||||
|
`/admin/orgs?${searchParams.toString()}`,
|
||||||
|
await authCookieHeader()
|
||||||
|
);
|
||||||
|
const responseData = res.data.data;
|
||||||
|
orgs = responseData.orgs;
|
||||||
|
pagination = responseData.pagination;
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
const t = await getTranslations();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<SettingsSectionTitle
|
||||||
|
title={t("orgsManage")}
|
||||||
|
description={t("orgsDescription")}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<OrgsTable
|
||||||
|
orgs={orgs}
|
||||||
|
rowCount={pagination.total}
|
||||||
|
pagination={{
|
||||||
|
pageIndex: pagination.page - 1,
|
||||||
|
pageSize: pagination.pageSize
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
Bot,
|
Bot,
|
||||||
Boxes,
|
Boxes,
|
||||||
Building2,
|
Building2,
|
||||||
|
Building2Icon,
|
||||||
Cable,
|
Cable,
|
||||||
ChartLine,
|
ChartLine,
|
||||||
Coins,
|
Coins,
|
||||||
@@ -377,6 +378,11 @@ export const adminNavSections = (env?: Env): SidebarNavSection[] => [
|
|||||||
href: "/admin/api-keys",
|
href: "/admin/api-keys",
|
||||||
icon: <KeyRound className="size-4 flex-none" />
|
icon: <KeyRound className="size-4 flex-none" />
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: "sidebarOrgs",
|
||||||
|
href: "/admin/organizations",
|
||||||
|
icon: <Building2Icon className="size-4 flex-none" />
|
||||||
|
},
|
||||||
...(build === "oss" ||
|
...(build === "oss" ||
|
||||||
env?.app.identityProviderMode === "global" ||
|
env?.app.identityProviderMode === "global" ||
|
||||||
env?.app.identityProviderMode === undefined
|
env?.app.identityProviderMode === undefined
|
||||||
@@ -388,7 +394,7 @@ export const adminNavSections = (env?: Env): SidebarNavSection[] => [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
: []),
|
: []),
|
||||||
...(build == "enterprise"
|
...(build === "enterprise"
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
title: "sidebarLicense",
|
title: "sidebarLicense",
|
||||||
|
|||||||
@@ -0,0 +1,305 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Button } from "@app/components/ui/button";
|
||||||
|
import {
|
||||||
|
ControlledDataTable,
|
||||||
|
type ExtendedColumnDef
|
||||||
|
} from "@app/components/ui/controlled-data-table";
|
||||||
|
import { useNavigationContext } from "@app/hooks/useNavigationContext";
|
||||||
|
import { toast } from "@app/hooks/useToast";
|
||||||
|
import { getNextSortOrder, getSortDirection } from "@app/lib/sortColumn";
|
||||||
|
import type { AdminOrgRow, DeleteOrgResponse } from "@server/routers/org";
|
||||||
|
|
||||||
|
import { type PaginationState } from "@tanstack/react-table";
|
||||||
|
import {
|
||||||
|
ArrowDown01Icon,
|
||||||
|
ArrowUp10Icon,
|
||||||
|
ArrowUpRight,
|
||||||
|
ChevronsUpDownIcon
|
||||||
|
} from "lucide-react";
|
||||||
|
import moment from "moment";
|
||||||
|
import { useTranslations } from "next-intl";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { useMemo, useState, useTransition } from "react";
|
||||||
|
import { useDebouncedCallback } from "use-debounce";
|
||||||
|
import ConfirmDeleteDialog from "./ConfirmDeleteDialog";
|
||||||
|
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
||||||
|
import type { AxiosResponse } from "axios";
|
||||||
|
import api from "gpt-tokenizer";
|
||||||
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
|
|
||||||
|
type OrgTableProps = {
|
||||||
|
orgs: AdminOrgRow[];
|
||||||
|
pagination: PaginationState;
|
||||||
|
rowCount: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function OrgsTable({
|
||||||
|
orgs,
|
||||||
|
pagination,
|
||||||
|
rowCount
|
||||||
|
}: OrgTableProps) {
|
||||||
|
const router = useRouter();
|
||||||
|
const t = useTranslations();
|
||||||
|
const {
|
||||||
|
navigate: filter,
|
||||||
|
isNavigating: isFiltering,
|
||||||
|
searchParams
|
||||||
|
} = useNavigationContext();
|
||||||
|
|
||||||
|
const [isRefreshing, startTransition] = useTransition();
|
||||||
|
|
||||||
|
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
||||||
|
const [selectedOrg, setSelectedOrg] = useState<AdminOrgRow | null>();
|
||||||
|
const api = createApiClient(useEnvContext());
|
||||||
|
|
||||||
|
function refreshData() {
|
||||||
|
startTransition(async () => {
|
||||||
|
try {
|
||||||
|
router.refresh();
|
||||||
|
} catch (error) {
|
||||||
|
toast({
|
||||||
|
title: t("error"),
|
||||||
|
description: t("refreshError"),
|
||||||
|
variant: "destructive"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleSort(column: string) {
|
||||||
|
const newSearch = getNextSortOrder(column, searchParams);
|
||||||
|
|
||||||
|
filter({
|
||||||
|
searchParams: newSearch
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortableHeader(column: string, label: string) {
|
||||||
|
const sortOrder = getSortDirection(column, searchParams);
|
||||||
|
const Icon =
|
||||||
|
sortOrder === "asc"
|
||||||
|
? ArrowDown01Icon
|
||||||
|
: sortOrder === "desc"
|
||||||
|
? ArrowUp10Icon
|
||||||
|
: ChevronsUpDownIcon;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
className="p-3"
|
||||||
|
onClick={() => toggleSort(column)}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
<Icon className="ml-2 h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const columns = useMemo<ExtendedColumnDef<AdminOrgRow>[]>(() => {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
accessorKey: "name",
|
||||||
|
friendlyName: t("name"),
|
||||||
|
enableHiding: false,
|
||||||
|
header: () => sortableHeader("name", t("name"))
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "createdAt",
|
||||||
|
friendlyName: t("createdAt"),
|
||||||
|
header: () => sortableHeader("createdAt", t("createdAt")),
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const createdAt = row.original.createdAt;
|
||||||
|
return (
|
||||||
|
<span>
|
||||||
|
{createdAt ? moment(createdAt).format("lll") : "-"}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "owner",
|
||||||
|
friendlyName: t("accessRoleOwner"),
|
||||||
|
header: () => (
|
||||||
|
<span className="p-3">{t("accessRoleOwner")}</span>
|
||||||
|
),
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const owner = row.original.owner;
|
||||||
|
return owner ? (
|
||||||
|
<Button
|
||||||
|
className="tabular-nums"
|
||||||
|
asChild
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
>
|
||||||
|
<Link href={`/admin/users/${owner.userId}`}>
|
||||||
|
{owner.username}
|
||||||
|
<ArrowUpRight className="ml-2 h-3 w-3" />
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<code>-</code>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "orgId",
|
||||||
|
friendlyName: t("orgId"),
|
||||||
|
header: () => <span className="p-3">{t("orgId")}</span>
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "subnet",
|
||||||
|
friendlyName: t("subnet"),
|
||||||
|
header: () => <span className="p-3">{t("subnet")}</span>,
|
||||||
|
cell: ({ row }) => <span>{row.original.subnet || "-"}</span>
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "utilitySubnet",
|
||||||
|
friendlyName: t("utilitySubnet"),
|
||||||
|
header: () => <span className="p-3">{t("utilitySubnet")}</span>,
|
||||||
|
cell: ({ row }) => (
|
||||||
|
<span>{row.original.utilitySubnet || "-"}</span>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "userCount",
|
||||||
|
friendlyName: t("users"),
|
||||||
|
header: () => <span className="p-3">{t("users")}</span>,
|
||||||
|
cell: ({ row }) => (
|
||||||
|
<span className="tabular-nums">
|
||||||
|
{row.original.userCount}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "siteCount",
|
||||||
|
friendlyName: t("sites"),
|
||||||
|
header: () => <span className="p-3">{t("sites")}</span>,
|
||||||
|
cell: ({ row }) => (
|
||||||
|
<span className="tabular-nums">
|
||||||
|
{row.original.siteCount}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "resourceCount",
|
||||||
|
friendlyName: t("resources"),
|
||||||
|
header: () => <span className="p-3">{t("resources")}</span>,
|
||||||
|
cell: ({ row }) => (
|
||||||
|
<span className="tabular-nums">
|
||||||
|
{row.original.resourceCount}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
id: "actions",
|
||||||
|
enableHiding: false,
|
||||||
|
header: () => <span className="p-3"></span>,
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const orgRow = row.original;
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-2 justify-end">
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
setSelectedOrg(orgRow);
|
||||||
|
setIsDeleteModalOpen(true);
|
||||||
|
}}
|
||||||
|
variant="outline"
|
||||||
|
className="text-red-400 focus:text-destructive "
|
||||||
|
>
|
||||||
|
{t("delete")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}, [t, searchParams]);
|
||||||
|
|
||||||
|
const handlePaginationChange = (newPage: PaginationState) => {
|
||||||
|
searchParams.set("page", (newPage.pageIndex + 1).toString());
|
||||||
|
searchParams.set("pageSize", newPage.pageSize.toString());
|
||||||
|
filter({
|
||||||
|
searchParams
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSearchChange = useDebouncedCallback((query: string) => {
|
||||||
|
searchParams.set("query", query);
|
||||||
|
searchParams.delete("page");
|
||||||
|
filter({
|
||||||
|
searchParams
|
||||||
|
});
|
||||||
|
}, 300);
|
||||||
|
|
||||||
|
async function deleteOrg(orgId: string) {
|
||||||
|
try {
|
||||||
|
// TODO
|
||||||
|
// const res = await api.delete<AxiosResponse<DeleteOrgResponse>>(
|
||||||
|
// `/org/${orgId}`
|
||||||
|
// );
|
||||||
|
toast({
|
||||||
|
title: t("orgDeleted"),
|
||||||
|
description: t("orgDeletedMessage")
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: t("orgErrorDelete"),
|
||||||
|
description: formatAxiosError(err, t("orgErrorDeleteMessage"))
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{selectedOrg && (
|
||||||
|
<ConfirmDeleteDialog
|
||||||
|
open={isDeleteModalOpen}
|
||||||
|
setOpen={(val) => {
|
||||||
|
setIsDeleteModalOpen(val);
|
||||||
|
setSelectedOrg(null);
|
||||||
|
}}
|
||||||
|
dialog={
|
||||||
|
<div className="space-y-2">
|
||||||
|
<p>{t("orgQuestionRemove")}</p>
|
||||||
|
<p>{t("orgMessageRemove")}</p>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
buttonText={t("orgDeleteConfirm")}
|
||||||
|
onConfirm={async () => {
|
||||||
|
startTransition(() => deleteOrg(selectedOrg.orgId));
|
||||||
|
}}
|
||||||
|
string={selectedOrg.name}
|
||||||
|
title={t("orgDelete")}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<ControlledDataTable
|
||||||
|
columns={columns}
|
||||||
|
rows={orgs}
|
||||||
|
tableId="admin-orgs-table"
|
||||||
|
searchPlaceholder={t("orgSearch")}
|
||||||
|
pagination={pagination}
|
||||||
|
onPaginationChange={handlePaginationChange}
|
||||||
|
searchQuery={searchParams.get("query")?.toString()}
|
||||||
|
onSearch={handleSearchChange}
|
||||||
|
onRefresh={refreshData}
|
||||||
|
isRefreshing={isRefreshing || isFiltering}
|
||||||
|
rowCount={rowCount}
|
||||||
|
columnVisibility={{
|
||||||
|
subnet: false,
|
||||||
|
utilitySubnet: false,
|
||||||
|
orgId: false
|
||||||
|
}}
|
||||||
|
enableColumnVisibility
|
||||||
|
stickyLeftColumn="name"
|
||||||
|
stickyRightColumn="actions"
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -21,14 +21,12 @@ import { Switch } from "@app/components/ui/switch";
|
|||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
import { useNavigationContext } from "@app/hooks/useNavigationContext";
|
import { useNavigationContext } from "@app/hooks/useNavigationContext";
|
||||||
import { useOptimisticLabels } from "@app/hooks/useOptimisticLabels";
|
import { useOptimisticLabels } from "@app/hooks/useOptimisticLabels";
|
||||||
import { usePaidStatus } from "@app/hooks/usePaidStatus";
|
|
||||||
import { toast } from "@app/hooks/useToast";
|
import { toast } from "@app/hooks/useToast";
|
||||||
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
||||||
import { orgQueries } from "@app/lib/queries";
|
import { orgQueries } from "@app/lib/queries";
|
||||||
import { getNextSortOrder, getSortDirection } from "@app/lib/sortColumn";
|
import { getNextSortOrder, getSortDirection } from "@app/lib/sortColumn";
|
||||||
import { build } from "@server/build";
|
|
||||||
import { UpdateResourceResponse } from "@server/routers/resource";
|
|
||||||
import type { GetBatchedCertificateResponse } from "@server/routers/certificates/types";
|
import type { GetBatchedCertificateResponse } from "@server/routers/certificates/types";
|
||||||
|
import { UpdateResourceResponse } from "@server/routers/resource";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import type { PaginationState } from "@tanstack/react-table";
|
import type { PaginationState } from "@tanstack/react-table";
|
||||||
import { AxiosResponse } from "axios";
|
import { AxiosResponse } from "axios";
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ import {
|
|||||||
} from "./ui/controlled-data-table";
|
} from "./ui/controlled-data-table";
|
||||||
|
|
||||||
import { useOptimisticLabels } from "@app/hooks/useOptimisticLabels";
|
import { useOptimisticLabels } from "@app/hooks/useOptimisticLabels";
|
||||||
import { durationToMs } from "@app/lib/durationToMs";
|
|
||||||
import { orgQueries, productUpdatesQueries } from "@app/lib/queries";
|
import { orgQueries, productUpdatesQueries } from "@app/lib/queries";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
|
|||||||
Reference in New Issue
Block a user