Skip to content

Commit

Permalink
fix(api): set initial sync value
Browse files Browse the repository at this point in the history
  • Loading branch information
waltergalvao committed Jun 30, 2024
1 parent a7d60ab commit 2253b0b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { SweetQueue, addJob } from "../../../bull-mq/queues";
import { logger } from "../../../lib/logger";
import { octokit } from "../../../lib/octokit";
import { setInitialSyncProgress } from "../../workspaces/services/workspace.service";

export const syncGitHubInstallation = async (
gitInstallation: GitHubInstallation,
Expand All @@ -30,6 +31,8 @@ export const syncGitHubInstallation = async (

await connectUserToWorkspace(gitProfile, workspace);

await setInitialSyncProgress(workspace.id);

await addJob(SweetQueue.GITHUB_REPOSITORIES_SYNC, {
installation: { id: parseInt(installation.gitInstallationId) },
syncPullRequests: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getTimeToCode,
getTimeToMerge,
} from "./github-pull-request-tracking.service";
import { incrementInitialSync } from "../../workspaces/services/workspace.service";
import { incrementInitialSyncProgress } from "../../workspaces/services/workspace.service";

interface Author {
id: string;
Expand Down Expand Up @@ -82,7 +82,7 @@ export const syncPullRequest = async (
);

if (initialSync) {
incrementInitialSync(workspace.id, "done", 1);
incrementInitialSyncProgress(workspace.id, "done", 1);
}

if (syncReviews) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ResourceNotFoundException } from "../../errors/exceptions/resource-not-
import { logger } from "../../../lib/logger";
import {
getWorkspaceHandle,
incrementInitialSync,
incrementInitialSyncProgress,
} from "../../workspaces/services/workspace.service";
import { BusinessRuleException } from "../../errors/exceptions/business-rule.exception";
import { JobPriority, SweetQueue, addJobs } from "../../../bull-mq/queues";
Expand Down Expand Up @@ -38,7 +38,7 @@ export const syncGitHubRepositoryPullRequests = async (

if (!gitHubPullRequests.length) return;

await incrementInitialSync(
await incrementInitialSyncProgress(
workspace.id,
"waiting",
gitHubPullRequests.length
Expand Down
20 changes: 13 additions & 7 deletions apps/api/src/app/workspaces/services/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,34 @@ export const getWorkspaceUninstallGitUrl = (
return `https://github.com/settings/installations/${workspace.installation?.gitInstallationId}`;
};

export const incrementInitialSync = async (
workspaceId: number,
field: "waiting" | "done",
amount: number
) => {
export const setInitialSyncProgress = async (workspaceId: number) => {
const key = `workspace:${workspaceId}:sync`;
const sevenDaysInSeconds = 60 * 60 * 24 * 7;

await redisConnection
.multi()
.hincrby(key, field, amount)
.hset(key, { waiting: 0, done: 0 })
.expire(key, sevenDaysInSeconds)
.exec();
};

export const incrementInitialSyncProgress = async (
workspaceId: number,
field: "waiting" | "done",
amount: number
) => {
const key = `workspace:${workspaceId}:sync`;

await redisConnection.hincrby(key, field, amount);
};

export const getInitialSyncProgress = async (workspaceId: number) => {
try {
const progress = await redisConnection.hgetall(
`workspace:${workspaceId}:sync`
);

if (!progress) return 100;
if (!progress || !("waiting" in progress)) return 100;

const done = Number(progress.done);
const waiting = Number(progress.waiting);
Expand Down

0 comments on commit 2253b0b

Please sign in to comment.