Skip to content

Commit 222b07a

Browse files
feedback
1 parent 5c5271e commit 222b07a

6 files changed

Lines changed: 37 additions & 6 deletions

File tree

packages/db/prisma/migrations/20260618182127_add_service_ping_event_table/migration.sql renamed to packages/db/prisma/migrations/20260618210032_add_service_ping_event_table/migration.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ CREATE TABLE "ServicePingEvent" (
33
"id" TEXT NOT NULL,
44
"payload" JSONB NOT NULL,
55
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
6+
"orgId" INTEGER NOT NULL,
67

78
CONSTRAINT "ServicePingEvent_pkey" PRIMARY KEY ("id")
89
);
10+
11+
-- AddForeignKey
12+
ALTER TABLE "ServicePingEvent" ADD CONSTRAINT "ServicePingEvent_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "Org"("id") ON DELETE CASCADE ON UPDATE CASCADE;

packages/db/prisma/schema.prisma

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ model Org {
318318
mcpServers McpServer[]
319319
320320
license License?
321+
servicePingEvents ServicePingEvent[]
321322
}
322323

323324
model License {
@@ -362,6 +363,9 @@ model ServicePingEvent {
362363
id String @id @default(cuid())
363364
payload Json
364365
createdAt DateTime @default(now())
366+
367+
org Org @relation(fields: [orgId], references: [id], onDelete: Cascade)
368+
orgId Int
365369
}
366370

367371
enum OrgRole {

packages/shared/src/entitlements.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,18 @@ const getValidOnlineLicense = (_license: License | null): License | null => {
120120
return null;
121121
}
122122

123+
export const isValidOfflineLicenseActive = (): boolean => {
124+
return getValidOfflineLicense() !== null;
125+
}
126+
127+
export const isValidOnlineLicenseActive = (_license: License | null): boolean => {
128+
return getValidOnlineLicense(_license) !== null;
129+
}
130+
123131
export const isValidLicenseActive = (_license: License | null): boolean => {
124132
return (
125-
getValidOfflineLicense() !== null ||
126-
getValidOnlineLicense(_license) !== null
133+
isValidOfflineLicenseActive() ||
134+
isValidOnlineLicenseActive(_license)
127135
);
128136
}
129137

packages/shared/src/index.server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export {
66
getEntitlements as _getEntitlements,
77
isAnonymousAccessAvailable as _isAnonymousAccessAvailable,
88
isValidLicenseActive as _isValidLicenseActive,
9+
isValidOfflineLicenseActive,
10+
isValidOnlineLicenseActive as _isValidOnlineLicenseActive,
911
getSeatCap,
1012
getOfflineLicenseMetadata,
1113
STALE_ONLINE_LICENSE_THRESHOLD_MS,

packages/web/src/app/(app)/settings/license/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export interface ServicePingHistoryEntry {
1414
// Returns the recorded Service Ping history so offline deployments can export
1515
// it and send it back to us out-of-band (they can't reach Lighthouse directly).
1616
export const getServicePingHistory = async (): Promise<ServicePingHistoryEntry[] | ServiceError> => sew(() =>
17-
withAuth(async ({ role, prisma }) =>
17+
withAuth(async ({ org, role, prisma }) =>
1818
withMinimumOrgRole(role, OrgRole.OWNER, async () => {
1919
const events = await prisma.servicePingEvent.findMany({
20+
where: { orgId: org.id },
2021
orderBy: { createdAt: 'asc' },
2122
});
2223

packages/web/src/features/billing/servicePing.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { existsSync } from "fs";
22
import { SINGLE_TENANT_ORG_ID } from "@/lib/constants";
33
import { isServiceError } from "@/lib/utils";
44
import { __unsafePrisma } from "@/prisma";
5-
import { createLogger, decryptActivationCode, env, SOURCEBOT_VERSION } from "@sourcebot/shared";
5+
import {
6+
createLogger,
7+
decryptActivationCode,
8+
env,
9+
SOURCEBOT_VERSION,
10+
isValidOfflineLicenseActive
11+
} from "@sourcebot/shared";
612
import { client } from "./client";
713
import { ServicePingRequest } from "./types";
814
import { ServiceErrorException } from "@/lib/serviceError";
@@ -83,7 +89,12 @@ export const syncWithLighthouse = async (orgId: number) => {
8389
...(activationCode && { activationCode }),
8490
};
8591

86-
await recordServicePingInDB(payload);
92+
await recordServicePingInDB(orgId, payload);
93+
94+
if (isValidOfflineLicenseActive()) {
95+
logger.debug('Skipping service ping: active offline license detected.');
96+
return;
97+
}
8798

8899
const response = await client.ping(payload);
89100
if (isServiceError(response)) {
@@ -175,13 +186,14 @@ const inferDeploymentType = (): string => {
175186
return 'other';
176187
};
177188

178-
const recordServicePingInDB = async (payload: ServicePingRequest) => {
189+
const recordServicePingInDB = async (orgId: number, payload: ServicePingRequest) => {
179190
// Strip the activation code before persisting.
180191
const { activationCode: _activationCode, ...sanitizedPayload } = payload;
181192

182193
try {
183194
await __unsafePrisma.servicePingEvent.create({
184195
data: {
196+
orgId,
185197
payload: sanitizedPayload,
186198
},
187199
});

0 commit comments

Comments
 (0)