Skip to content

Commit 5d203a1

Browse files
committed
logs
1 parent d484739 commit 5d203a1

2 files changed

Lines changed: 179 additions & 5 deletions

File tree

packages/app/server/api/repos.get.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,65 @@ export default defineEventHandler(async (event) => {
77
// Use R2 service
88
const r2Service = useR2GitHubService(event);
99

10-
// Get all repositories from R2
10+
// First, dump all storage keys for debugging
11+
console.log("[R2API] Dumping R2 storage structure for debugging");
12+
const storageKeys = await r2Service.dumpStorageKeys();
13+
console.log(`[R2API] R2 storage has ${storageKeys.total_keys} total keys`);
14+
15+
// Get sample values to understand the data structure
16+
const sampleValues = await r2Service.getSampleValues();
17+
console.log(
18+
`[R2API] Got ${sampleValues.sample_count || 0} sample values from R2`,
19+
);
20+
21+
// Try to get all repositories directly from storage keys
22+
console.log("[R2API] Attempting to scan all keys for repositories");
23+
// Use the getStorageAccess method to access storage safely
24+
const storageAccess = r2Service.getStorageAccess();
25+
const allKeys = await storageAccess.getKeys();
26+
const repoKeys = allKeys.filter((key) => key.startsWith("repo:"));
27+
console.log(`[R2API] Found ${repoKeys.length} repository keys`);
28+
29+
// Directly load repositories from keys
30+
const loadedRepositories = [];
31+
for (const key of repoKeys) {
32+
try {
33+
const data = await storageAccess.getItem(key);
34+
if (data) {
35+
const repo = typeof data === "string" ? JSON.parse(data) : data;
36+
console.log(
37+
`[R2API] Successfully loaded repository from key: ${key}`,
38+
);
39+
loadedRepositories.push(repo);
40+
}
41+
} catch (error) {
42+
console.error(
43+
`[R2API] Error loading repository from key ${key}:`,
44+
error,
45+
);
46+
}
47+
}
48+
49+
console.log(
50+
`[R2API] Directly loaded ${loadedRepositories.length} repositories from keys`,
51+
);
52+
53+
// Also try the original method
1154
const repositories = await r2Service.listAllRepositories();
55+
console.log(
56+
`[R2API] Found ${repositories.length} repositories using listAllRepositories()`,
57+
);
1258

59+
// Determine which set of repositories to use
60+
const effectiveRepositories =
61+
loadedRepositories.length > 0 ? loadedRepositories : repositories;
1362
console.log(
14-
`[R2API] Found ${repositories.length} repositories in R2 storage`,
63+
`[R2API] Using ${effectiveRepositories.length} repositories for response`,
1564
);
1665

1766
// Get latest commit for each repository
1867
const reposWithLatestCommit = await Promise.all(
19-
repositories.map(async (repo) => {
68+
effectiveRepositories.map(async (repo) => {
2069
try {
2170
const commits = await r2Service.listCommits(
2271
repo.owner.login,
@@ -79,10 +128,16 @@ export default defineEventHandler(async (event) => {
79128
const clientDebugInfo = {
80129
timestamp: new Date().toISOString(),
81130
storage_info: r2Service.getStorageInfo(),
82-
repository_count: repositories.length,
83-
repository_names: repositories.map(
131+
repository_count: effectiveRepositories.length,
132+
repository_names: effectiveRepositories.map(
84133
(repo) => `${repo.owner.login}/${repo.name}`,
85134
),
135+
storage_structure: {
136+
total_keys: storageKeys.total_keys,
137+
key_counts: storageKeys.key_counts,
138+
key_samples: storageKeys.key_samples,
139+
},
140+
sample_values: sampleValues,
86141
};
87142

88143
return {

packages/app/server/utils/r2-service.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,125 @@ export class R2GitHubService {
459459
}
460460
}
461461

462+
// Get full details of all keys in the storage for debugging
463+
async dumpStorageKeys(): Promise<{
464+
total_keys: number;
465+
key_counts: Record<string, number>;
466+
key_samples: Record<string, string[]>;
467+
all_keys: string[];
468+
}> {
469+
try {
470+
this.log("Dumping all storage keys for debugging");
471+
const keys = await this.storage.getKeys();
472+
this.log(`Found ${keys.length} total keys in storage`);
473+
474+
// Organize keys by prefix for better debugging
475+
const keysByPrefix: Record<string, string[]> = {};
476+
477+
keys.forEach((key) => {
478+
const prefix = key.split(":")[0] || "unknown";
479+
if (!keysByPrefix[prefix]) {
480+
keysByPrefix[prefix] = [];
481+
}
482+
keysByPrefix[prefix].push(key);
483+
});
484+
485+
// Count each type
486+
const counts: Record<string, number> = {};
487+
Object.keys(keysByPrefix).forEach((prefix) => {
488+
counts[prefix] = keysByPrefix[prefix].length;
489+
});
490+
491+
this.log(`Key counts by prefix: ${JSON.stringify(counts)}`);
492+
493+
// Sample a few keys from each prefix (max 5)
494+
const samples: Record<string, string[]> = {};
495+
Object.keys(keysByPrefix).forEach((prefix) => {
496+
samples[prefix] = keysByPrefix[prefix].slice(0, 5);
497+
});
498+
499+
return {
500+
total_keys: keys.length,
501+
key_counts: counts,
502+
key_samples: samples,
503+
all_keys: keys,
504+
};
505+
} catch (error) {
506+
this.log(`Error dumping storage keys: ${error}`, true);
507+
return {
508+
total_keys: 0,
509+
key_counts: {},
510+
key_samples: {},
511+
all_keys: [],
512+
};
513+
}
514+
}
515+
516+
// Get a few sample values to see the structure
517+
async getSampleValues(): Promise<{
518+
sample_count: number;
519+
samples: Record<string, any>;
520+
error?: string;
521+
}> {
522+
try {
523+
this.log("Fetching sample values from storage for debugging");
524+
const keys = await this.storage.getKeys();
525+
526+
if (keys.length === 0) {
527+
return {
528+
sample_count: 0,
529+
samples: {},
530+
error: "No keys found in storage",
531+
};
532+
}
533+
534+
// Sample up to 5 keys of different types
535+
const sampleValues: Record<string, any> = {};
536+
const prefixes = ["repo:", "commit:", "check-run:", "search-index"];
537+
538+
for (const prefix of prefixes) {
539+
const matchingKeys = keys
540+
.filter((key) => key.startsWith(prefix))
541+
.slice(0, 2);
542+
if (matchingKeys.length > 0) {
543+
sampleValues[prefix] = {};
544+
for (const key of matchingKeys) {
545+
try {
546+
const value = await this.storage.getItem(key);
547+
if (value) {
548+
sampleValues[prefix][key] =
549+
typeof value === "string" ? JSON.parse(value) : value;
550+
}
551+
} catch (error) {
552+
sampleValues[prefix][key] = `Error parsing value: ${error}`;
553+
}
554+
}
555+
}
556+
}
557+
558+
return {
559+
sample_count: Object.keys(sampleValues).length,
560+
samples: sampleValues,
561+
};
562+
} catch (error) {
563+
this.log(`Error getting sample values: ${error}`, true);
564+
return {
565+
sample_count: 0,
566+
samples: {},
567+
error: `Could not get sample values: ${error}`,
568+
};
569+
}
570+
}
571+
572+
// Expose the storage for debugging purposes
573+
// This is normally not recommended but useful for debugging R2 issues
574+
getStorageAccess() {
575+
return {
576+
getKeys: async () => await this.storage.getKeys(),
577+
getItem: async (key: string) => await this.storage.getItem(key),
578+
};
579+
}
580+
462581
// Index methods - fetching data from GitHub and storing in R2
463582
async indexRepository(owner: string, repo: string): Promise<void> {
464583
try {

0 commit comments

Comments
 (0)