Skip to content

Commit a131b12

Browse files
committed
feat: refactor DigestCron to streamline upsert operations and remove unused search item arrays
1 parent e4bb276 commit a131b12

File tree

4 files changed

+35
-46
lines changed

4 files changed

+35
-46
lines changed

api/src/digest/cron.ts

Lines changed: 24 additions & 40 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,23 +69,15 @@ export class DigestCron {
7069

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

73-
const projectSearchItems: SearchItem[] = [];
74-
const contributionSearchItems: SearchItem[] = [];
75-
const contributorSearchItems: SearchItem[] = [];
76-
7772
for (const project of projectsFromDataFolder) {
78-
const [{ id: projectId }] = await this.projectsRepository.upsert({
73+
const projectEntity = {
7974
...project,
8075
runId,
81-
id: project.slug,
82-
});
83-
const sanitizedSlug = project.slug.replace(/[.]/g, "-"); // MeiliSearch doesn't allow dots in ids
84-
projectSearchItems.push({
85-
id: `${runId}--${sanitizedSlug}`,
86-
title: project.name,
87-
type: "project",
88-
runId,
89-
});
76+
id: project.slug.replace(/[.]/g, "-"), // MeiliSearch doesn't allow dots in ids,
77+
};
78+
const [{ id: projectId }] =
79+
await this.projectsRepository.upsert(projectEntity);
80+
await this.searchService.upsert("project", projectEntity);
9081

9182
let addedRepositoryCount = 0;
9283
try {
@@ -123,21 +114,18 @@ export class DigestCron {
123114

124115
if (githubUser.type !== "User") continue;
125116

126-
const [{ id: contributorId }] =
127-
await this.contributorsRepository.upsert({
128-
name: githubUser.name || githubUser.login,
129-
username: githubUser.login,
130-
url: githubUser.html_url,
131-
avatarUrl: githubUser.avatar_url,
132-
runId,
133-
id: `${provider}-${githubUser.login}`,
134-
});
135-
contributorSearchItems.push({
136-
id: `${runId}--${provider}-${githubUser.login}`,
137-
title: githubUser.name || githubUser.login,
138-
type: "contributor",
117+
const contributorEntity = {
118+
name: githubUser.name || githubUser.login,
119+
username: githubUser.login,
120+
url: githubUser.html_url,
121+
avatarUrl: githubUser.avatar_url,
139122
runId,
140-
});
123+
id: `${provider}-${githubUser.login}`,
124+
};
125+
126+
const [{ id: contributorId }] =
127+
await this.contributorsRepository.upsert(contributorEntity);
128+
await this.searchService.upsert("contributor", contributorEntity);
141129

142130
await this.contributorsRepository.upsertRelationWithRepository({
143131
contributorId,
@@ -147,7 +135,7 @@ export class DigestCron {
147135
});
148136

149137
const type = issue.pull_request ? "PULL_REQUEST" : "ISSUE";
150-
await this.contributionsRepository.upsert({
138+
const contributionEntity = {
151139
title: issue.title,
152140
type,
153141
updatedAt: issue.updated_at,
@@ -160,13 +148,12 @@ export class DigestCron {
160148
repositoryId,
161149
contributorId,
162150
id: `${provider}-${issue.id}`,
163-
});
164-
contributionSearchItems.push({
165-
id: `${runId}--${provider}-${issue.id}`,
166-
title: issue.title,
167-
type: "contribution",
168-
runId,
169-
});
151+
} as const;
152+
await this.contributionsRepository.upsert(contributionEntity);
153+
await this.searchService.upsert(
154+
"contribution",
155+
contributionEntity,
156+
);
170157
}
171158

172159
const repoContributors =
@@ -222,9 +209,6 @@ export class DigestCron {
222209
await this.contributorsRepository.deleteAllButWithRunId(runId);
223210
await this.repositoriesRepository.deleteAllButWithRunId(runId);
224211
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);
228212
await this.searchService.deleteAllButWithRunId("project", runId);
229213
await this.searchService.deleteAllButWithRunId("contribution", runId);
230214
await this.searchService.deleteAllButWithRunId("contributor", runId);

api/src/search/service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SearchItem, SearchType } from "./types";
22

3+
import { BaseSearchItem } from "@dzcode.io/models/dist/_base";
34
import { ConfigService } from "src/config/service";
45
import { LoggerService } from "src/logger/service";
56
import { MeiliSearch } from "meilisearch";
@@ -30,15 +31,15 @@ export class SearchService {
3031
return [];
3132
};
3233

33-
public upsert = async (
34+
public upsert = async <T extends BaseSearchItem>(
3435
index: SearchType,
35-
data: SearchItem[],
36+
data: T,
3637
): Promise<void> => {
3738
this.logger.info({
38-
message: `Upserting ${data.length} items to ${index}`,
39+
message: `Upserting "${data.id}" item to ${index}`,
3940
});
40-
await this.meilisearch.index(index).updateDocuments(data);
41-
this.logger.info({ message: `Upserted ${data.length} items to ${index}` });
41+
await this.meilisearch.index(index).updateDocuments([data]);
42+
this.logger.info({ message: `Upserted "${data.id}" item to ${index}` });
4243
};
4344

4445
public deleteAllButWithRunId = async (

api/src/search/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export interface GetSearchResponse extends GeneralResponse {
77
export interface SearchItem {
88
id: string;
99
title: string;
10-
type: SearchType;
1110
runId: string;
1211
}
1312

packages/models/src/_base/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ export type BaseEntity = {
22
id: string;
33
runId: string;
44
};
5+
6+
export type BaseSearchItem = {
7+
id: string;
8+
runId: string;
9+
};

0 commit comments

Comments
 (0)