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