Path: blob/main/components/gitpod-db/src/workspace-db.ts
2498 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { DeepPartial } from "typeorm";78import {9Workspace,10WorkspaceInfo,11WorkspaceInstance,12WorkspaceInstanceUser,13Snapshot,14PrebuiltWorkspace,15PrebuiltWorkspaceUpdatable,16RunningWorkspaceInfo,17WorkspaceAndInstance,18WorkspaceType,19PrebuildInfo,20AdminGetWorkspacesQuery,21SnapshotState,22WorkspaceSession,23PrebuiltWorkspaceWithWorkspace,24PrebuildWithStatus,25WorkspaceInstanceMetrics,26} from "@gitpod/gitpod-protocol";2728export type MaybeWorkspace = Workspace | undefined;29export type MaybeWorkspaceInstance = WorkspaceInstance | undefined;3031export interface FindWorkspacesOptions {32userId: string;33organizationId?: string;34projectId?: string | string[];35includeWithoutProject?: boolean;36limit?: number;37searchString?: string;38includeHeadless?: boolean;39pinnedOnly?: boolean;40}4142export interface PrebuiltUpdatableAndWorkspace extends PrebuiltWorkspaceUpdatable {43prebuild: PrebuiltWorkspace;44workspace: Workspace;45}4647export type WorkspaceAuthData = Pick<Workspace, "id" | "ownerId" | "shareable">;48export type WorkspaceInstancePortsAuthData = Pick<WorkspaceInstance, "id" | "region">;49export interface WorkspacePortsAuthData {50instance: WorkspaceInstancePortsAuthData;51workspace: WorkspaceAuthData;52}5354export interface PrebuildWithWorkspace {55prebuild: PrebuiltWorkspace;56workspace: Workspace;57}5859export interface PrebuildWithWorkspaceAndInstances {60prebuild: PrebuiltWorkspace;61workspace: Workspace;62instances: WorkspaceInstance[];63}6465export type WorkspaceAndOwner = Pick<Workspace, "id" | "ownerId">;66export type WorkspaceOwnerAndSoftDeleted = Pick<Workspace, "id" | "ownerId" | "softDeleted">;67export type WorkspaceOwnerAndDeletionEligibility = Pick<Workspace, "id" | "ownerId" | "deletionEligibilityTime">;68export type WorkspaceOwnerAndContentDeletedTime = Pick<Workspace, "id" | "ownerId" | "contentDeletedTime">;6970export const WorkspaceDB = Symbol("WorkspaceDB");71export interface WorkspaceDB {72connect(maxTries: number, timeout: number): Promise<void>;7374transaction<T>(code: (db: WorkspaceDB) => Promise<T>): Promise<T>;7576store(workspace: Workspace): Promise<Workspace>;77updatePartial(workspaceId: string, partial: DeepPartial<Workspace>): Promise<void>;78findById(id: string): Promise<MaybeWorkspace>;79findByInstanceId(id: string): Promise<MaybeWorkspace>;80find(options: FindWorkspacesOptions): Promise<WorkspaceInfo[]>;81findWorkspacePortsAuthDataById(workspaceId: string): Promise<WorkspacePortsAuthData | undefined>;8283storeInstance(instance: WorkspaceInstance): Promise<WorkspaceInstance>;8485// Partial update: unconditional, single field updates. Enclose in a transaction if necessary86updateLastHeartbeat(instanceId: string, userId: string, newHeartbeat: Date, wasClosed?: boolean): Promise<void>;87getLastOwnerHeartbeatFor(instance: WorkspaceInstance): Promise<{ lastSeen: Date; wasClosed?: boolean } | undefined>;88getWorkspaceUsers(workspaceId: string, minLastSeen: number): Promise<WorkspaceInstanceUser[]>;89updateInstancePartial(instanceId: string, partial: DeepPartial<WorkspaceInstance>): Promise<WorkspaceInstance>;9091findInstanceById(workspaceInstanceId: string): Promise<MaybeWorkspaceInstance>;92findInstances(workspaceId: string): Promise<WorkspaceInstance[]>;93findWorkspacesByUser(userId: string): Promise<Workspace[]>;94findCurrentInstance(workspaceId: string): Promise<MaybeWorkspaceInstance>;95findRunningInstance(workspaceId: string): Promise<MaybeWorkspaceInstance>;96findSessionsInPeriod(97organizationId: string,98periodStart: Date,99periodEnd: Date,100limit: number,101offset: number,102): Promise<WorkspaceSession[]>;103findEligibleWorkspacesForSoftDeletion(104cutOffDate?: Date,105limit?: number,106type?: WorkspaceType,107): Promise<WorkspaceOwnerAndDeletionEligibility[]>;108findWorkspacesForContentDeletion(109minSoftDeletedTimeInDays: number,110limit: number,111): Promise<WorkspaceOwnerAndSoftDeleted[]>;112findWorkspacesForPurging(113minContentDeletionTimeInDays: number,114limit: number,115now: Date,116): Promise<WorkspaceOwnerAndContentDeletedTime[]>;117findAllWorkspaces(118offset: number,119limit: number,120orderBy: keyof Workspace,121orderDir: "ASC" | "DESC",122opts: {123ownerId?: string;124type?: WorkspaceType;125},126): Promise<{ total: number; rows: Workspace[] }>;127findAllWorkspaceAndInstances(128offset: number,129limit: number,130orderBy: keyof WorkspaceAndInstance,131orderDir: "ASC" | "DESC",132query?: AdminGetWorkspacesQuery,133): Promise<{ total: number; rows: WorkspaceAndInstance[] }>;134findWorkspaceAndInstance(id: string): Promise<WorkspaceAndInstance | undefined>;135findInstancesByPhase(phases: string[]): Promise<WorkspaceInstance[]>;136137getWorkspaceCount(type?: String): Promise<Number>;138getInstanceCount(type?: string): Promise<number>;139140findRegularRunningInstances(userId?: string): Promise<WorkspaceInstance[]>;141findRunningInstancesWithWorkspaces(142workspaceClusterName?: string,143userId?: string,144includeStopping?: boolean,145): Promise<RunningWorkspaceInfo[]>;146147findSnapshotById(snapshotId: string): Promise<Snapshot | undefined>;148findSnapshotsWithState(149state: SnapshotState,150offset: number,151limit: number,152): Promise<{ snapshots: Snapshot[]; total: number }>;153findSnapshotsByWorkspaceId(workspaceId: string): Promise<Snapshot[]>;154storeSnapshot(snapshot: Snapshot): Promise<Snapshot>;155deleteSnapshot(snapshotId: string): Promise<void>;156updateSnapshot(snapshot: DeepPartial<Snapshot> & Pick<Snapshot, "id">): Promise<void>;157158storePrebuiltWorkspace(pws: PrebuiltWorkspace): Promise<PrebuiltWorkspace>;159findPrebuiltWorkspaceByCommit(projectId: string, commit: string): Promise<PrebuiltWorkspace | undefined>;160findActivePrebuiltWorkspacesByBranch(161projectId: string,162branch: string,163): Promise<PrebuildWithWorkspaceAndInstances[]>;164findPrebuildsWithWorkspace(projectId: string): Promise<PrebuildWithWorkspace[]>;165findPrebuildWithStatus(prebuildId: string): Promise<PrebuildWithStatus | undefined>;166findPrebuildByWorkspaceID(wsid: string): Promise<PrebuiltWorkspace | undefined>;167findPrebuildByID(pwsid: string): Promise<PrebuiltWorkspace | undefined>;168countUnabortedPrebuildsSince(projectId: string, date: Date): Promise<number>;169attachUpdatableToPrebuild(pwsid: string, update: PrebuiltWorkspaceUpdatable): Promise<void>;170findUpdatablesForPrebuild(pwsid: string): Promise<PrebuiltWorkspaceUpdatable[]>;171markUpdatableResolved(updatableId: string): Promise<void>;172getUnresolvedUpdatables(limit?: number): Promise<PrebuiltUpdatableAndWorkspace[]>;173174hardDeleteWorkspace(workspaceID: string): Promise<void>;175176findPrebuiltWorkspacesByOrganization(177organizationId: string,178pagination: {179offset: number;180limit: number;181},182filter: {183configuration?: {184id: string;185branch?: string;186};187state?: "succeeded" | "failed" | "unfinished";188searchTerm?: string;189},190sort: {191field: string;192order: "ASC" | "DESC";193},194): Promise<PrebuiltWorkspaceWithWorkspace[]>;195findPrebuiltWorkspaceById(prebuildId: string): Promise<PrebuiltWorkspace | undefined>;196197storePrebuildInfo(prebuildInfo: PrebuildInfo): Promise<void>;198findPrebuildInfos(prebuildIds: string[]): Promise<PrebuildInfo[]>;199200storeMetrics(instanceId: string, metrics: WorkspaceInstanceMetrics): Promise<WorkspaceInstanceMetrics>;201getMetrics(instanceId: string): Promise<WorkspaceInstanceMetrics | undefined>;202updateMetrics(203instanceId: string,204update: WorkspaceInstanceMetrics,205merge: (current: WorkspaceInstanceMetrics, update: WorkspaceInstanceMetrics) => WorkspaceInstanceMetrics,206): Promise<WorkspaceInstanceMetrics>;207}208209210