Skip to content

Commit e2c0a19

Browse files
authored
feat: csm pagination api
feat: csm pagination api
2 parents 477a6f3 + 0ab46b5 commit e2c0a19

3 files changed

Lines changed: 92 additions & 34 deletions

File tree

packages/analysis-engine/src/index.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import "reflect-metadata";
33
import { container } from "tsyringe";
44

55
import { buildCommitDict } from "./commit.util";
6-
import { buildCSMDict } from "./csm";
6+
import { buildCSMDict, buildPaginatedCSMDict } from "./csm";
77
import getCommitRaws from "./parser";
88
import { PluginOctokit } from "./pluginOctokit";
99
import { buildStemDict } from "./stem";
1010
import { getSummary } from "./summary";
11+
import type { CSMNode } from "./types";
1112

1213
export { buildPaginatedCSMDict } from "./csm";
1314

@@ -50,12 +51,12 @@ export class AnalysisEngine {
5051
this.octokit = container.resolve(PluginOctokit);
5152
};
5253

53-
public analyzeGit = async () => {
54+
public analyzeGit = async (perPage?: number, lastCommitId?: string) => {
5455
let isPRSuccess = true;
5556
if (this.isDebugMode) console.log("baseBranchName: ", this.baseBranchName);
5657

5758
const commitRaws = getCommitRaws(this.gitLog);
58-
if (this.isDebugMode){
59+
if (this.isDebugMode) {
5960
console.log("commitRaws: ", commitRaws);
6061
}
6162

@@ -77,16 +78,44 @@ export class AnalysisEngine {
7778

7879
const stemDict = buildStemDict(commitDict, this.baseBranchName);
7980
if (this.isDebugMode) console.log("stemDict: ", stemDict);
80-
const csmDict = buildCSMDict(commitDict, stemDict, this.baseBranchName, pullRequests);
81-
if (this.isDebugMode) console.log("csmDict: ", csmDict);
82-
const nodes = stemDict.get(this.baseBranchName)?.nodes?.map(({ commit }) => commit);
83-
const geminiCommitSummary = await getSummary(nodes ? nodes?.slice(-10) : []);
84-
if (this.isDebugMode) console.log("GeminiCommitSummary: ", geminiCommitSummary);
85-
86-
return {
87-
isPRSuccess,
88-
csmDict,
89-
};
81+
82+
// Paginated CSM
83+
if (perPage) {
84+
const csmDict = buildPaginatedCSMDict(
85+
commitDict,
86+
stemDict,
87+
this.baseBranchName,
88+
perPage,
89+
lastCommitId,
90+
pullRequests
91+
);
92+
const list = csmDict[this.baseBranchName] ?? [];
93+
const lastNode: CSMNode | undefined = list.length > 0 ? list[list.length - 1] : undefined;
94+
95+
const isLastPage = list.length < perPage;
96+
const nextCommitId = !isLastPage && lastNode ? lastNode.base.commit.id : null;
97+
98+
return {
99+
isPRSuccess,
100+
csmDict,
101+
nextCommitId,
102+
isLastPage,
103+
};
104+
} else {
105+
// Non-paginated CSM
106+
const csmDict = buildCSMDict(commitDict, stemDict, this.baseBranchName, pullRequests);
107+
if (this.isDebugMode) console.log("csmDict: ", csmDict);
108+
const nodes = stemDict.get(this.baseBranchName)?.nodes?.map(({ commit }) => commit);
109+
const geminiCommitSummary = await getSummary(nodes ? nodes?.slice(-10) : []);
110+
if (this.isDebugMode) console.log("GeminiCommitSummary: ", geminiCommitSummary);
111+
112+
return {
113+
isPRSuccess,
114+
csmDict,
115+
nextCommitId: null,
116+
isLastPage: true,
117+
};
118+
}
90119
};
91120

92121
public updateArgs = (args: AnalysisEngineArgs) => {

packages/vscode/src/extension.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export async function activate(context: vscode.ExtensionContext) {
8282

8383
const initialBaseBranchName = await fetchCurrentBranch();
8484

85-
const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => {
85+
const fetchClusterNodes = async (
86+
baseBranchName = initialBaseBranchName,
87+
perPage?: number,
88+
lastCommitId?: string
89+
) => {
8690
const workerPath = vscode.Uri.joinPath(context.extensionUri, "dist", "worker.js").fsPath;
8791
const gitLog = await fetchGitLogInParallel(gitPath, currentWorkspacePath, workerPath);
8892
const { owner, repo: initialRepo } = getRepo(gitConfig);
@@ -96,9 +100,18 @@ export async function activate(context: vscode.ExtensionContext) {
96100
baseBranchName,
97101
});
98102

99-
const { isPRSuccess, csmDict } = await engine.analyzeGit();
100-
if (isPRSuccess) console.log("crawling PR Success");
101-
return mapClusterNodesFrom(csmDict);
103+
const analysisResult = await engine.analyzeGit(perPage, lastCommitId);
104+
105+
if (analysisResult.isPRSuccess) console.log("crawling PR Success");
106+
107+
const clusterNodes = mapClusterNodesFrom(analysisResult.csmDict);
108+
109+
return {
110+
clusterNodes: clusterNodes,
111+
isLastPage: analysisResult.isLastPage,
112+
nextCommitId: analysisResult.nextCommitId,
113+
isPRSuccess: analysisResult.isPRSuccess,
114+
};
102115
};
103116

104117
const webLoader = new WebviewLoader(extensionPath, context, {

packages/vscode/src/webview-loader.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,30 @@ export default class WebviewLoader implements vscode.Disposable {
3737

3838
if (command === "fetchAnalyzedData" || command === "refresh") {
3939
try {
40-
const baseBranchName = (payload && JSON.parse(payload)) ?? (await fetchCurrentBranch());
41-
const storedAnalyzedData = context.workspaceState.get<ClusterNode[]>(
42-
`${ANALYZE_DATA_KEY}_${baseBranchName}`
43-
);
44-
analyzedData = storedAnalyzedData;
45-
if (!storedAnalyzedData) {
46-
console.log("No cache Data");
47-
console.log("baseBranchName : ", baseBranchName);
48-
analyzedData = await fetchClusterNodes(baseBranchName);
49-
context.workspaceState.update(`${ANALYZE_DATA_KEY}_${baseBranchName}`, analyzedData);
50-
} else console.log("Cache data exists");
51-
52-
console.log("Current Stored data");
53-
context.workspaceState.keys().forEach((key) => {
54-
console.log(key);
55-
});
40+
const requestPayload = payload ? JSON.parse(payload) : {};
41+
const { baseBranchName, perPage, lastCommitId } = requestPayload;
42+
const currentBranch = baseBranchName ?? (await fetchCurrentBranch());
43+
44+
if (perPage) {
45+
console.log(`Paging request: perPage=${perPage}, lastCommitId=${lastCommitId}`);
46+
analyzedData = await fetchClusterNodes(currentBranch, perPage, lastCommitId);
47+
} else {
48+
const cacheKey = `${ANALYZE_DATA_KEY}_${currentBranch}`;
49+
const storedAnalyzedData = context.workspaceState.get<ClusterNode[]>(cacheKey);
50+
51+
if (storedAnalyzedData) {
52+
console.log("Cache data exists");
53+
analyzedData = storedAnalyzedData;
54+
} else {
55+
console.log("No cache Data");
56+
analyzedData = await fetchClusterNodes(currentBranch);
57+
context.workspaceState.update(cacheKey, analyzedData);
58+
}
59+
console.log("Current Stored data");
60+
context.workspaceState.keys().forEach((key) => {
61+
console.log(key);
62+
});
63+
}
5664

5765
const resMessage = {
5866
command,
@@ -158,7 +166,15 @@ export default class WebviewLoader implements vscode.Disposable {
158166

159167
type GithruFetcher<D = unknown, P extends unknown[] = []> = (...params: P) => Promise<D>;
160168
type GithruFetcherMap = {
161-
fetchClusterNodes: GithruFetcher<ClusterNode[], [string]>;
169+
fetchClusterNodes: GithruFetcher<
170+
{
171+
clusterNodes: ClusterNode[];
172+
isLastPage: boolean | undefined;
173+
nextCommitId: string | null | undefined;
174+
isPRSuccess: boolean;
175+
},
176+
[string?, number?, string?]
177+
>;
162178
fetchBranches: GithruFetcher<{ branchList: string[]; head: string | null }>;
163179
fetchCurrentBranch: GithruFetcher<string>;
164180
fetchGithubInfo: GithruFetcher<{ owner: string; repo: string }>;

0 commit comments

Comments
 (0)