/** * 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, WhitelistedRepository, Snapshot, PrebuiltWorkspace, PrebuiltWorkspaceUpdatable, RunningWorkspaceInfo, WorkspaceAndInstance, WorkspaceType, PrebuildInfo, AdminGetWorkspacesQuery, SnapshotState, } from "@gitpod/gitpod-protocol"; export type MaybeWorkspace = Workspace | undefined; export type MaybeWorkspaceInstance = WorkspaceInstance | undefined; export interface FindWorkspacesOptions { userId: string; projectId?: string | string[]; includeWithoutProject?: boolean; limit?: number; searchString?: string; includeHeadless?: boolean; pinnedOnly?: boolean; } export interface PrebuiltUpdatableAndWorkspace extends PrebuiltWorkspaceUpdatable { prebuild: PrebuiltWorkspace; workspace: Workspace; instance: WorkspaceInstance; } export type WorkspaceAuthData = Pick; export type WorkspaceInstancePortsAuthData = Pick; export interface WorkspacePortsAuthData { instance: WorkspaceInstancePortsAuthData; workspace: WorkspaceAuthData; } export type WorkspaceInstanceSession = Pick; export type WorkspaceSessionData = Pick; export interface WorkspaceInstanceSessionWithWorkspace { instance: WorkspaceInstanceSession; workspace: WorkspaceSessionData; } 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 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( userId: string, periodStart: string, periodEnd: string, ): Promise; findWorkspacesForGarbageCollection(minAgeInDays: number, limit: number): Promise; findWorkspacesForContentDeletion( minSoftDeletedTimeInDays: number, limit: number, ): Promise; findWorkspacesForPurging( minContentDeletionTimeInDays: number, limit: number, now: Date, ): Promise; findPrebuiltWorkspacesForGC(daysUnused: number, limit: number): Promise; findAllWorkspaces( offset: number, limit: number, orderBy: keyof Workspace, orderDir: "ASC" | "DESC", ownerId?: string, searchTerm?: string, minCreationTime?: Date, maxCreationDateTime?: Date, 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; findInstancesByPhaseAndRegion(phase: string, region: string): Promise; getWorkspaceCount(type?: String): Promise; getWorkspaceCountByCloneURL(cloneURL: string, sinceLastDays?: number, type?: string): Promise; getInstanceCount(type?: string): Promise; findAllWorkspaceInstances( offset: number, limit: number, orderBy: keyof WorkspaceInstance, orderDir: "ASC" | "DESC", ownerId?: string, minCreationTime?: Date, maxCreationTime?: Date, onlyRunning?: boolean, type?: WorkspaceType, ): Promise<{ total: number; rows: WorkspaceInstance[] }>; findRegularRunningInstances(userId?: string): Promise; findRunningInstancesWithWorkspaces( installation?: string, userId?: string, includeStopping?: boolean, ): Promise; isWhitelisted(repositoryUrl: string): Promise; getFeaturedRepositories(): 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(cloneURL: string, commit: string): Promise; findActivePrebuiltWorkspacesByBranch( projectId: string, branch: string, ): Promise; findPrebuildsWithWorkpace(cloneURL: string): Promise; findPrebuildByWorkspaceID(wsid: string): Promise; findPrebuildByID(pwsid: string): Promise; countRunningPrebuilds(cloneURL: string): Promise; countUnabortedPrebuildsSince(cloneURL: string, date: Date): Promise; findQueuedPrebuilds(cloneURL?: string): Promise; attachUpdatableToPrebuild(pwsid: string, update: PrebuiltWorkspaceUpdatable): Promise; findUpdatablesForPrebuild(pwsid: string): Promise; markUpdatableResolved(updatableId: string): Promise; getUnresolvedUpdatables(limit?: number): Promise; hardDeleteWorkspace(workspaceID: string): Promise; findPrebuiltWorkspacesByProject(projectId: string, branch?: string, limit?: number): Promise; findPrebuiltWorkspaceById(prebuildId: string): Promise; storePrebuildInfo(prebuildInfo: PrebuildInfo): Promise; findPrebuildInfos(prebuildIds: string[]): Promise; }