Skip to content

Commit e4bb276

Browse files
committed
feat: enhance DigestCron and SearchService to support bulk upsert and deletion of search items with runId
1 parent 204a028 commit e4bb276

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

api/src/digest/cron.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { GithubService } from "src/github/service";
88
import { LoggerService } from "src/logger/service";
99
import { ProjectRepository } from "src/project/repository";
1010
import { RepositoryRepository } from "src/repository/repository";
11+
import { SearchItem } from "src/search/types";
1112
import { SearchService } from "src/search/service";
1213
import { Service } from "typedi";
1314

@@ -69,16 +70,22 @@ export class DigestCron {
6970

7071
const projectsFromDataFolder = await this.dataService.listProjects();
7172

73+
const projectSearchItems: SearchItem[] = [];
74+
const contributionSearchItems: SearchItem[] = [];
75+
const contributorSearchItems: SearchItem[] = [];
76+
7277
for (const project of projectsFromDataFolder) {
7378
const [{ id: projectId }] = await this.projectsRepository.upsert({
7479
...project,
7580
runId,
7681
id: project.slug,
7782
});
78-
this.searchService.upsert("project", {
79-
id: project.slug.replace(/[.]/g, "-"), // MeiliSearch doesn't allow dots in ids
83+
const sanitizedSlug = project.slug.replace(/[.]/g, "-"); // MeiliSearch doesn't allow dots in ids
84+
projectSearchItems.push({
85+
id: `${runId}--${sanitizedSlug}`,
8086
title: project.name,
8187
type: "project",
88+
runId,
8289
});
8390

8491
let addedRepositoryCount = 0;
@@ -125,10 +132,11 @@ export class DigestCron {
125132
runId,
126133
id: `${provider}-${githubUser.login}`,
127134
});
128-
this.searchService.upsert("contributor", {
129-
id: `${provider}-${githubUser.login}`,
135+
contributorSearchItems.push({
136+
id: `${runId}--${provider}-${githubUser.login}`,
130137
title: githubUser.name || githubUser.login,
131138
type: "contributor",
139+
runId,
132140
});
133141

134142
await this.contributorsRepository.upsertRelationWithRepository({
@@ -153,10 +161,11 @@ export class DigestCron {
153161
contributorId,
154162
id: `${provider}-${issue.id}`,
155163
});
156-
this.searchService.upsert("contribution", {
157-
id: `${provider}-${issue.id}`,
164+
contributionSearchItems.push({
165+
id: `${runId}--${provider}-${issue.id}`,
158166
title: issue.title,
159167
type: "contribution",
168+
runId,
160169
});
161170
}
162171

@@ -213,6 +222,12 @@ export class DigestCron {
213222
await this.contributorsRepository.deleteAllButWithRunId(runId);
214223
await this.repositoriesRepository.deleteAllButWithRunId(runId);
215224
await this.projectsRepository.deleteAllButWithRunId(runId);
225+
await this.searchService.upsert("project", projectSearchItems);
226+
await this.searchService.upsert("contribution", contributionSearchItems);
227+
await this.searchService.upsert("contributor", contributorSearchItems);
228+
await this.searchService.deleteAllButWithRunId("project", runId);
229+
await this.searchService.deleteAllButWithRunId("contribution", runId);
230+
await this.searchService.deleteAllButWithRunId("contributor", runId);
216231
} catch (error) {
217232
captureException(error, { tags: { type: "CRON" } });
218233
}

api/src/search/service.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,58 @@ export class SearchService {
3232

3333
public upsert = async (
3434
index: SearchType,
35-
data: SearchItem,
35+
data: SearchItem[],
3636
): Promise<void> => {
3737
this.logger.info({
38-
message: `Upserting ${data.title} in ${index}`,
38+
message: `Upserting ${data.length} items to ${index}`,
39+
});
40+
await this.meilisearch.index(index).updateDocuments(data);
41+
this.logger.info({ message: `Upserted ${data.length} items to ${index}` });
42+
};
43+
44+
public deleteAllButWithRunId = async (
45+
index: SearchType,
46+
runId: string,
47+
): Promise<void> => {
48+
this.logger.info({
49+
message: `Deleting all ${index} but with runId ${runId}`,
50+
});
51+
await this.meilisearch.index(index).deleteDocuments({
52+
filter: `NOT runId=${runId}`,
3953
});
40-
await this.meilisearch.index(index).updateDocuments([data]);
4154
this.logger.info({
42-
message: `Upserted ${data.title} in ${index}`,
55+
message: `Deleted all ${index} but with runId ${runId}`,
4356
});
4457
};
4558

4659
public ensureIndexes = async (): Promise<void> => {
47-
await this.meilisearch.createIndex("project");
60+
await this.meilisearch.createIndex("project", {
61+
primaryKey: "id",
62+
});
4863
this.logger.info({ message: "project index created" });
4964

50-
await this.meilisearch.createIndex("contribution");
65+
await this.meilisearch.createIndex("contribution", {
66+
primaryKey: "id",
67+
});
5168
this.logger.info({ message: "contribution index created" });
5269

53-
await this.meilisearch.createIndex("contributor");
70+
await this.meilisearch.createIndex("contributor", {
71+
primaryKey: "id",
72+
});
5473
this.logger.info({ message: "contributor index created" });
74+
75+
await this.updateFilterableAttributes();
5576
};
77+
78+
private async updateFilterableAttributes(): Promise<void> {
79+
await this.meilisearch
80+
.index("project")
81+
.updateFilterableAttributes(["runId"]);
82+
await this.meilisearch
83+
.index("contribution")
84+
.updateFilterableAttributes(["runId"]);
85+
await this.meilisearch
86+
.index("contributor")
87+
.updateFilterableAttributes(["runId"]);
88+
}
5689
}

api/src/search/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface SearchItem {
88
id: string;
99
title: string;
1010
type: SearchType;
11+
runId: string;
1112
}
1213

1314
export type SearchType = "project" | "contribution" | "contributor";

0 commit comments

Comments
 (0)