/** * Copyright (c) 2020 Gitpod GmbH. All rights reserved. * Licensed under the GNU Affero General Public License (AGPL). * See License.AGPL.txt in the project root for license information. */ import { DeepPartial } from "typeorm"; import { Workspace, WorkspaceInfo, WorkspaceInstance, WorkspaceInstanceUser, Snapshot, PrebuiltWorkspace, PrebuiltWorkspaceUpdatable, RunningWorkspaceInfo, WorkspaceAndInstance, WorkspaceType, PrebuildInfo, AdminGetWorkspacesQuery, SnapshotState, WorkspaceSession, PrebuiltWorkspaceWithWorkspace, PrebuildWithStatus, WorkspaceInstanceMetrics, } from "@gitpod/gitpod-protocol"; export type MaybeWorkspace = Workspace | undefined; export type MaybeWorkspaceInstance = WorkspaceInstance | undefined; export interface FindWorkspacesOptions { userId: string; organizationId?: string; projectId?: string | string[]; includeWithoutProject?: boolean; limit?: number; searchString?: string; includeHeadless?: boolean; pinnedOnly?: boolean; } export interface PrebuiltUpdatableAndWorkspace extends PrebuiltWorkspaceUpdatable { prebuild: PrebuiltWorkspace; workspace: Workspace; } export type WorkspaceAuthData = Pick; export type WorkspaceInstancePortsAuthData = Pick; export interface WorkspacePortsAuthData { instance: WorkspaceInstancePortsAuthData; workspace: WorkspaceAuthData; } export interface PrebuildWithWorkspace { prebuild: PrebuiltWorkspace; workspace: Workspace; } export interface PrebuildWithWorkspaceAndInstances { prebuild: PrebuiltWorkspace; workspace: Workspace; instances: WorkspaceInstance[]; } export type WorkspaceAndOwner = Pick; export type WorkspaceOwnerAndSoftDeleted = Pick; export type WorkspaceOwnerAndDeletionEligibility = Pick; export type WorkspaceOwnerAndContentDeletedTime = Pick; export const WorkspaceDB = Symbol("WorkspaceDB"); export interface WorkspaceDB { connect(maxTries: number, timeout: number): Promise; transaction(code: (db: WorkspaceDB) => Promise): Promise; store(workspace: Workspace): Promise; updatePartial(workspaceId: string, partial: DeepPartial): Promise; findById(id: string): Promise; findByInstanceId(id: string): Promise; find(options: FindWorkspacesOptions): Promise; findWorkspacePortsAuthDataById(workspaceId: string): Promise; storeInstance(instance: WorkspaceInstance): Promise; // Partial update: unconditional, single field updates. Enclose in a transaction if necessary updateLastHeartbeat(instanceId: string, userId: string, newHeartbeat: Date, wasClosed?: boolean): Promise; getLastOwnerHeartbeatFor(instance: WorkspaceInstance): Promise<{ lastSeen: Date; wasClosed?: boolean } | undefined>; getWorkspaceUsers(workspaceId: string, minLastSeen: number): Promise; updateInstancePartial(instanceId: string, partial: DeepPartial): Promise; findInstanceById(workspaceInstanceId: string): Promise; findInstances(workspaceId: string): Promise; findWorkspacesByUser(userId: string): Promise; findCurrentInstance(workspaceId: string): Promise; findRunningInstance(workspaceId: string): Promise; findSessionsInPeriod( organizationId: string, periodStart: Date, periodEnd: Date, limit: number, offset: number, ): Promise; findEligibleWorkspacesForSoftDeletion( cutOffDate?: Date, limit?: number, type?: WorkspaceType, ): Promise; findWorkspacesForContentDeletion( minSoftDeletedTimeInDays: number, limit: number, ): Promise; findWorkspacesForPurging( minContentDeletionTimeInDays: number, limit: number, now: Date, ): Promise; findAllWorkspaces( offset: number, limit: number, orderBy: keyof Workspace, orderDir: "ASC" | "DESC", opts: { ownerId?: string; type?: WorkspaceType; }, ): Promise<{ total: number; rows: Workspace[] }>; findAllWorkspaceAndInstances( offset: number, limit: number, orderBy: keyof WorkspaceAndInstance, orderDir: "ASC" | "DESC", query?: AdminGetWorkspacesQuery, ): Promise<{ total: number; rows: WorkspaceAndInstance[] }>; findWorkspaceAndInstance(id: string): Promise; findInstancesByPhase(phases: string[]): Promise; getWorkspaceCount(type?: String): Promise; getInstanceCount(type?: string): Promise; findRegularRunningInstances(userId?: string): Promise; findRunningInstancesWithWorkspaces( workspaceClusterName?: string, userId?: string, includeStopping?: boolean, ): Promise; findSnapshotById(snapshotId: string): Promise; findSnapshotsWithState( state: SnapshotState, offset: number, limit: number, ): Promise<{ snapshots: Snapshot[]; total: number }>; findSnapshotsByWorkspaceId(workspaceId: string): Promise; storeSnapshot(snapshot: Snapshot): Promise; deleteSnapshot(snapshotId: string): Promise; updateSnapshot(snapshot: DeepPartial & Pick): Promise; storePrebuiltWorkspace(pws: PrebuiltWorkspace): Promise; findPrebuiltWorkspaceByCommit(projectId: string, commit: string): Promise; findActivePrebuiltWorkspacesByBranch( projectId: string, branch: string, ): Promise; findPrebuildsWithWorkspace(projectId: string): Promise; findPrebuildWithStatus(prebuildId: string): Promise; findPrebuildByWorkspaceID(wsid: string): Promise; findPrebuildByID(pwsid: string): Promise; countUnabortedPrebuildsSince(projectId: string, date: Date): Promise; attachUpdatableToPrebuild(pwsid: string, update: PrebuiltWorkspaceUpdatable): Promise; findUpdatablesForPrebuild(pwsid: string): Promise; markUpdatableResolved(updatableId: string): Promise; getUnresolvedUpdatables(limit?: number): Promise; hardDeleteWorkspace(workspaceID: string): Promise; findPrebuiltWorkspacesByOrganization( organizationId: string, pagination: { offset: number; limit: number; }, filter: { configuration?: { id: string; branch?: string; }; state?: "succeeded" | "failed" | "unfinished"; searchTerm?: string; }, sort: { field: string; order: "ASC" | "DESC"; }, ): Promise; findPrebuiltWorkspaceById(prebuildId: string): Promise; storePrebuildInfo(prebuildInfo: PrebuildInfo): Promise; findPrebuildInfos(prebuildIds: string[]): Promise; storeMetrics(instanceId: string, metrics: WorkspaceInstanceMetrics): Promise; getMetrics(instanceId: string): Promise; updateMetrics( instanceId: string, update: WorkspaceInstanceMetrics, merge: (current: WorkspaceInstanceMetrics, update: WorkspaceInstanceMetrics) => WorkspaceInstanceMetrics, ): Promise; }