Skip to content

Commit 204a028

Browse files
committed
feat: refactor DigestCron to use upsert method for search indexing and remove bulk deletion
1 parent 595e2fc commit 204a028

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

api/src/digest/cron.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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";
1211
import { SearchService } from "src/search/service";
1312
import { Service } from "typedi";
1413

@@ -70,17 +69,13 @@ export class DigestCron {
7069

7170
const projectsFromDataFolder = await this.dataService.listProjects();
7271

73-
const searchProjectItems: SearchItem[] = [];
74-
const searchContributorItems: SearchItem[] = [];
75-
const searchContributionItems: SearchItem[] = [];
76-
7772
for (const project of projectsFromDataFolder) {
7873
const [{ id: projectId }] = await this.projectsRepository.upsert({
7974
...project,
8075
runId,
8176
id: project.slug,
8277
});
83-
searchProjectItems.push({
78+
this.searchService.upsert("project", {
8479
id: project.slug.replace(/[.]/g, "-"), // MeiliSearch doesn't allow dots in ids
8580
title: project.name,
8681
type: "project",
@@ -130,7 +125,7 @@ export class DigestCron {
130125
runId,
131126
id: `${provider}-${githubUser.login}`,
132127
});
133-
searchContributorItems.push({
128+
this.searchService.upsert("contributor", {
134129
id: `${provider}-${githubUser.login}`,
135130
title: githubUser.name || githubUser.login,
136131
type: "contributor",
@@ -158,7 +153,7 @@ export class DigestCron {
158153
contributorId,
159154
id: `${provider}-${issue.id}`,
160155
});
161-
searchContributionItems.push({
156+
this.searchService.upsert("contribution", {
162157
id: `${provider}-${issue.id}`,
163158
title: issue.title,
164159
type: "contribution",
@@ -218,17 +213,6 @@ export class DigestCron {
218213
await this.contributorsRepository.deleteAllButWithRunId(runId);
219214
await this.repositoriesRepository.deleteAllButWithRunId(runId);
220215
await this.projectsRepository.deleteAllButWithRunId(runId);
221-
await this.searchService.deleteAllDocuments("project");
222-
await this.searchService.deleteAllDocuments("contribution");
223-
await this.searchService.deleteAllDocuments("contributor");
224-
} catch (error) {
225-
captureException(error, { tags: { type: "CRON" } });
226-
}
227-
228-
try {
229-
await this.searchService.index("project", searchProjectItems);
230-
await this.searchService.index("contribution", searchContributionItems);
231-
await this.searchService.index("contributor", searchContributorItems);
232216
} catch (error) {
233217
captureException(error, { tags: { type: "CRON" } });
234218
}

api/src/search/service.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,19 @@ export class SearchService {
3030
return [];
3131
};
3232

33-
public index = async (
33+
public upsert = async (
3434
index: SearchType,
35-
data: SearchItem[],
35+
data: SearchItem,
3636
): Promise<void> => {
3737
this.logger.info({
38-
message: `Indexing ${data.length} items in ${index}`,
38+
message: `Upserting ${data.title} in ${index}`,
3939
});
40-
await this.meilisearch.index(index).addDocuments(data);
40+
await this.meilisearch.index(index).updateDocuments([data]);
4141
this.logger.info({
42-
message: `Indexed ${data.length} items in ${index}`,
42+
message: `Upserted ${data.title} in ${index}`,
4343
});
4444
};
4545

46-
public deleteAllDocuments = async (index: SearchType): Promise<void> => {
47-
this.logger.info({ message: `Deleting all documents in ${index}` });
48-
await this.meilisearch.index(index).deleteAllDocuments();
49-
this.logger.info({ message: `Deleted all documents in ${index}` });
50-
};
51-
5246
public ensureIndexes = async (): Promise<void> => {
5347
await this.meilisearch.createIndex("project");
5448
this.logger.info({ message: "project index created" });

0 commit comments

Comments
 (0)