Skip to content

Commit c315ded

Browse files
authored
Merge pull request #12 from armitage-labs/feat/store-cred-interval
Store interval data for an contributor
2 parents b437d63 + e58ab5e commit c315ded

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

cred-manager/packages/@types/sourcecred/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ declare module 'sourcecred' {
924924
export class CredGrainView {
925925
constructor(graph: CredGraph, ledger: Ledger);
926926
participants: () => ParticipantCredGrain[];
927-
927+
928928
totalCredPerInterval: () => number[];
929929

930930
totalGrainPerInterval: () => string[];
@@ -938,6 +938,11 @@ declare module 'sourcecred' {
938938
grainEarnedPerInterval: string[];
939939
}
940940

941+
export interface Interval {
942+
startTimeMs: number;
943+
endTimeMs: number;
944+
}
945+
941946
export interface LedgerManager {
942947
reloadLedger: () => Promise<ReloadResult>;
943948
ledger: Ledger;

cred-manager/prisma/schema.prisma

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,14 @@ model UserScore {
7373
user_type String @db.VarChar(255)
7474
score String @db.VarChar(255)
7575
created_at DateTime @default(now()) @db.Timestamp(6)
76+
UserScoreInterval UserScoreInterval[]
77+
}
78+
79+
model UserScoreInterval {
80+
id String @id @default(uuid())
81+
user_score_id String
82+
UserScore UserScore @relation(fields: [user_score_id], references: [id], onDelete: Cascade)
83+
score String @db.VarChar(255)
84+
interval_start String @db.VarChar(255)
85+
interval_end String @db.VarChar(255)
7686
}

cred-manager/src/sourcecred/service/calculationQueue.service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ export class CalculationQueueService {
2929
});
3030
if (unhandledCalculation) {
3131
this.logger.debug("Handling unhandled calculation request.");
32-
// update semaphore to unavailable
33-
await this.prismaService.calculationSemaphore.update({
34-
where: { id: semaphore.id },
35-
data: { available: false }
36-
})
37-
// calculate cred scores
38-
await this.sourceCredService.calculateCredScores(unhandledCalculation.team_id, unhandledCalculation.access_token, unhandledCalculation.email);
32+
try {
33+
// update semaphore to unavailable
34+
await this.prismaService.calculationSemaphore.update({
35+
where: { id: semaphore.id },
36+
data: { available: false }
37+
})
38+
// calculate cred scores
39+
await this.sourceCredService.calculateCredScores(unhandledCalculation.team_id, unhandledCalculation.access_token, unhandledCalculation.email);
40+
} catch(e) {
41+
this.logger.error(e);
42+
}
43+
3944
// delete the contribution request
4045
await this.prismaService.contributionRequest.delete({
4146
where: { id: unhandledCalculation.id }
@@ -45,7 +50,6 @@ export class CalculationQueueService {
4550
where: { id: semaphore.id },
4651
data: { available: true }
4752
});
48-
4953
}
5054
} else {
5155
this.logger.debug("Semaphore unavailable.");

cred-manager/src/sourcecred/service/sourcecred.service.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class SourceCredService {
5454
const pluginConfigString = this.craftPluginConfigString(
5555
userRegisteredRepos.map((repo) => repo.full_name),
5656
);
57+
await this.deleteContribution(teamId);
5758
const contribution = await this.saveContribution(teamId);
5859
await this.configureSourcecredGithubPlugin(pluginConfigString);
5960
await this.loadSourceCredPlugins(gitHubToken);
@@ -124,6 +125,8 @@ export class SourceCredService {
124125
totalCred: participant.cred,
125126
userName: participant.identity.name,
126127
type: participant.identity.subtype.toString(),
128+
credPerInterval: participant.credPerInterval,
129+
grainEarnedPerInterval: participant.grainEarnedPerInterval,
127130
});
128131
},
129132
);
@@ -134,6 +137,23 @@ export class SourceCredService {
134137
return userDtoArray.sort((a, b) => b.totalCred - a.totalCred);
135138
}
136139

140+
async deleteContribution(teamId: string) {
141+
const contribution = await this.prismaService.contributionCalculation.findFirst({
142+
where: {
143+
team_id: teamId,
144+
}
145+
});
146+
147+
148+
if (contribution != null) {
149+
await this.prismaService.contributionCalculation.deleteMany({
150+
where: {
151+
id : contribution.id
152+
}
153+
});
154+
}
155+
}
156+
137157
async saveContribution(teamId: string): Promise<ContributionCalculationDto> {
138158
try {
139159
const contribution =
@@ -153,17 +173,29 @@ export class SourceCredService {
153173
contributionCalculationId: string,
154174
userCredDtos: UserCredDto[],
155175
) {
176+
// need to delete the user score and interval for the team before we insert new one
156177
try {
157-
return await this.prismaService.userScore.createMany({
158-
data: userCredDtos.map((userCredDto) => {
159-
return {
160-
username: userCredDto.userName,
161-
user_type: userCredDto.type,
162-
score: userCredDto.totalCred.toString(),
163-
contribution_calculation_id: contributionCalculationId,
164-
};
165-
}),
166-
});
178+
for(var i = 0; i < userCredDtos.length; i++) {
179+
const userScore = await this.prismaService.userScore.create({
180+
data : {
181+
username: userCredDtos[i].userName,
182+
user_type: userCredDtos[i].type,
183+
score: userCredDtos[i].totalCred.toString(),
184+
contribution_calculation_id: contributionCalculationId,
185+
}
186+
});
187+
188+
await this.prismaService.userScoreInterval.createMany({
189+
data: userCredDtos[i].credPerInterval.map((interval) => {
190+
return {
191+
user_score_id: userScore.id,
192+
score: interval.toString(),
193+
interval_start: new Date().getTime().toString(), // TODO we need to get interval from graph
194+
interval_end: new Date().getTime().toString(), // TODO we need to get interval from graph
195+
};
196+
}),
197+
});
198+
}
167199
} catch (error) {
168200
console.error('Error creating users scores', error);
169201
throw error;

cred-manager/src/sourcecred/types/userCredDto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@ export interface UserCredJson {
22
totalCred: number;
33
userName: string;
44
type: string;
5+
credPerInterval?: number[];
6+
grainEarnedPerInterval?: string[];
57
}
68

79
export class UserCredDto {
810
constructor(
911
public totalCred: number,
1012
public userName: string,
1113
public type: string,
14+
public credPerInterval?: number[],
15+
public grainEarnedPerInterval?: string[]
1216
) {}
1317

1418
toJSON(): UserCredJson {
1519
return {
1620
totalCred: this.totalCred,
1721
userName: this.userName,
1822
type: this.type,
23+
credPerInterval: this.credPerInterval,
24+
grainEarnedPerInterval: this.grainEarnedPerInterval
1925
};
2026
}
2127

2228
static fromJSON(json: UserCredJson): UserCredDto {
23-
return new UserCredDto(json.totalCred, json.userName, json.type);
29+
return new UserCredDto(json.totalCred, json.userName, json.type, json.credPerInterval, json.grainEarnedPerInterval);
2430
}
2531
}

prisma/schema.prisma

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,14 @@ model UserScore {
7373
user_type String @db.VarChar(255)
7474
score String @db.VarChar(255)
7575
created_at DateTime @default(now()) @db.Timestamp(6)
76+
UserScoreInterval UserScoreInterval[]
77+
}
78+
79+
model UserScoreInterval {
80+
id String @id @default(uuid())
81+
user_score_id String
82+
UserScore UserScore @relation(fields: [user_score_id], references: [id], onDelete: Cascade)
83+
score String @db.VarChar(255)
84+
interval_start String @db.VarChar(255)
85+
interval_end String @db.VarChar(255)
7686
}

0 commit comments

Comments
 (0)