@@ -8,12 +8,13 @@ import { inject, injectable } from "inversify";
8
8
import { TypeORM } from "./typeorm" ;
9
9
import { Repository } from "typeorm" ;
10
10
import { v4 as uuidv4 } from "uuid" ;
11
- import { PartialProject , Project , ProjectEnvVar , ProjectEnvVarWithValue } from "@gitpod/gitpod-protocol" ;
11
+ import { PartialProject , Project , ProjectEnvVar , ProjectEnvVarWithValue , ProjectUsage } from "@gitpod/gitpod-protocol" ;
12
12
import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service" ;
13
13
import { ProjectDB } from "../project-db" ;
14
14
import { DBProject } from "./entity/db-project" ;
15
15
import { DBProjectEnvVar } from "./entity/db-project-env-vars" ;
16
16
import { DBProjectInfo } from "./entity/db-project-info" ;
17
+ import { DBProjectUsage } from "./entity/db-project-usage" ;
17
18
18
19
function toProjectEnvVar ( envVarWithValue : ProjectEnvVarWithValue ) : ProjectEnvVar {
19
20
const envVar = { ...envVarWithValue } ;
@@ -42,6 +43,10 @@ export class ProjectDBImpl implements ProjectDB {
42
43
return ( await this . getEntityManager ( ) ) . getRepository < DBProjectInfo > ( DBProjectInfo ) ;
43
44
}
44
45
46
+ protected async getProjectUsageRepo ( ) : Promise < Repository < DBProjectUsage > > {
47
+ return ( await this . getEntityManager ( ) ) . getRepository < DBProjectUsage > ( DBProjectUsage ) ;
48
+ }
49
+
45
50
public async findProjectById ( projectId : string ) : Promise < Project | undefined > {
46
51
const repo = await this . getRepo ( ) ;
47
52
return repo . findOne ( { id : projectId , markedDeleted : false } ) ;
@@ -146,6 +151,11 @@ export class ProjectDBImpl implements ProjectDB {
146
151
if ( info ) {
147
152
await projectInfoRepo . update ( projectId , { deleted : true } ) ;
148
153
}
154
+ const projectUsageRepo = await this . getProjectUsageRepo ( ) ;
155
+ const usage = await projectUsageRepo . findOne ( { projectId, deleted : false } ) ;
156
+ if ( usage ) {
157
+ await projectUsageRepo . update ( projectId , { deleted : true } ) ;
158
+ }
149
159
}
150
160
151
161
public async setProjectEnvironmentVariable (
@@ -229,4 +239,23 @@ export class ProjectDBImpl implements ProjectDB {
229
239
creationTime : new Date ( ) . toISOString ( ) ,
230
240
} ) ;
231
241
}
242
+
243
+ public async getProjectUsage ( projectId : string ) : Promise < ProjectUsage | undefined > {
244
+ const projectUsageRepo = await this . getProjectUsageRepo ( ) ;
245
+ const usage = await projectUsageRepo . findOne ( { projectId } ) ;
246
+ if ( usage ) {
247
+ return {
248
+ lastWebhookReceived : usage . lastWebhookReceived ,
249
+ lastWorkspaceStart : usage . lastWorkspaceStart ,
250
+ } ;
251
+ }
252
+ }
253
+
254
+ public async updateProjectUsage ( projectId : string , usage : Partial < ProjectUsage > ) : Promise < void > {
255
+ const projectUsageRepo = await this . getProjectUsageRepo ( ) ;
256
+ await projectUsageRepo . save ( {
257
+ projectId,
258
+ ...usage ,
259
+ } ) ;
260
+ }
232
261
}
0 commit comments