Skip to content

Commit df2da20

Browse files
AslemammadAmirSa12
andauthored
chore: stats github workflow (#296)
Co-authored-by: AmirSa12 <[email protected]>
1 parent 51afc2a commit df2da20

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

.github/workflows/stats.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: pkg.pr.new stats
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
stats:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Set up Node.js
11+
uses: actions/setup-node@v3
12+
with:
13+
node-version: '16'
14+
15+
- name: Fetch and Count Data Iteratively
16+
uses: actions/github-script@v6
17+
with:
18+
script: |
19+
async function fetchAndCountData(url) {
20+
const counts = {
21+
templates: 0,
22+
packages: 0,
23+
orgs: new Set(),
24+
repos: new Set(),
25+
commits: 0,
26+
prsAndBranches: 0,
27+
};
28+
let cursor = null;
29+
30+
do {
31+
try {
32+
// Construct the URL with cursor if available
33+
const fetchUrl = cursor ? `${url}?cursor=${encodeURIComponent(cursor)}` : url;
34+
35+
// Fetch the data from the API
36+
const response = await fetch(fetchUrl);
37+
38+
if (!response.ok) {
39+
throw new Error(`HTTP error! status: ${response.status}`);
40+
}
41+
42+
const responseData = await response.json();
43+
44+
// Process the current page of data
45+
if (responseData.data) {
46+
for (const item of responseData.data) {
47+
if (item.type === "template") {
48+
counts.templates += 1;
49+
} else if (item.type === "package") {
50+
counts.packages += 1;
51+
counts.orgs.add(item.org);
52+
counts.repos.add(item.repo);
53+
counts.commits += 1; // Count one commit per package
54+
} else if (item.type === "cursor") {
55+
counts.orgs.add(item.org);
56+
counts.repos.add(item.repo);
57+
58+
// Count PR numbers and branch names combined
59+
counts.prsAndBranches += 1;
60+
}
61+
}
62+
}
63+
64+
// Update the cursor for the next page
65+
cursor = responseData.nextCursor || null;
66+
} catch (error) {
67+
console.error(`Error fetching data: ${error.message}`);
68+
cursor = null; // Exit the loop on error
69+
}
70+
} while (cursor);
71+
72+
return {
73+
templates: counts.templates,
74+
packages: counts.packages,
75+
orgs: counts.orgs.size,
76+
repos: counts.repos.size,
77+
commits: counts.commits,
78+
prsAndBranches: counts.prsAndBranches,
79+
};
80+
}
81+
82+
const url = "https://pkg.pr.new/stats";
83+
const counts = await fetchAndCountData(url);
84+
85+
console.log("Counts:", counts);

packages/backend/server/routes/stats.get.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ export default eventHandler(async (event) => {
4444
ref: sha256(ref),
4545
};
4646
} else if (key.startsWith(templatesPrefix)) {
47-
const trimmedKey = key.slice(useTemplatesBucket.base.length);
48-
const [org, repo, ...templateNameParts] = trimmedKey.split(":");
49-
const template = templateNameParts.join(":");
47+
const trimmedKey = key.slice(templatesPrefix.length);
48+
const template = trimmedKey;
5049

5150
result = {
5251
type: "template",
53-
org: sha256(org),
54-
repo: sha256(repo),
5552
template: sha256(template),
5653
};
5754
}

0 commit comments

Comments
 (0)