diff --git a/docs/readme_fr.md b/docs/readme_fr.md index 427f2971451c8..ddd003a46dfd3 100644 --- a/docs/readme_fr.md +++ b/docs/readme_fr.md @@ -90,18 +90,6 @@ Pour masquer des statistiques spécifiques, vous pouvez passer un paramètre de ![Les Stats GitHub de Anurag](https://github-readme-stats.vercel.app/api?username=anuraghazra&hide=contribs,prs) ``` -### Ajouter le compte des contributions privées au compte des commits totaux - -Vous pouvez ajouter le compte de toutes vos contributions privées au compte total des engagements en utilisant le paramètre de requête `?count_private=true`. - -_Note: Si vous déployez vous-même ce projet, les contributions privées seront comptées par défaut ; sinon, vous devez choisir de partager les comptes de vos contributions privées._ - -> Options: `&count_private=true` - -```md -![Les Stats GitHub de Anurag](https://github-readme-stats.vercel.app/api?username=anuraghazra&count_private=true) -``` - ### Afficher les icônes Pour activer les icônes, vous pouvez passer `show_icons=true` dans le paramètre de requête, comme ceci : @@ -160,7 +148,7 @@ Vous pouvez fournir plusieurs valeurs (suivie d'une virgule) dans l'option bg_co - `hide_rank` - Masquer le rang _(boolean)_ - `show_icons` - Afficher les icônes _(boolean)_ - `include_all_commits` - Compter le total de commits au lieu de ne compter que les commits de l'année en cours _(boolean)_ -- `count_private` - Compter les commits privés _(boolean)_ +- `count_private` - Compter les contributions privées _(boolean)_ - `line_height` - Fixer la hauteur de la ligne entre les textes _(number)_ #### Repo Card Exclusive Options: diff --git a/readme.md b/readme.md index f59342f3ab9f4..5a430769b22f7 100644 --- a/readme.md +++ b/readme.md @@ -120,19 +120,6 @@ You can pass a query parameter `&hide=` to hide any specific stats with comma-se ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&hide=contribs,prs) ``` -### Adding private contributions count to total commits count - -You can add the count of all your private contributions to the total commits count by using the query parameter `&count_private=true`. - -> **Note** -> If you are deploying this project yourself, the private contributions will be counted by default. If you are using the public Vercel instance, you need to choose to [share your private contributions](https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/managing-contribution-settings-on-your-profile/showing-your-private-contributions-and-achievements-on-your-profile). - -> Options: `&count_private=true` - -```md -![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&count_private=true) -``` - ### Showing icons To enable icons, you can pass `&show_icons=true` in the query param, like so: @@ -283,7 +270,7 @@ You can provide multiple comma-separated values in the bg_color option to render - `rank_icon` - Shows alternative rank icon (i.e. `github` or `default`). Default: `default`. - `show_icons` - _(boolean)_. Default: `false`. - `include_all_commits` - Count total commits instead of just the current year commits _(boolean)_. Default: `false`. -- `count_private` - Count private commits _(boolean)_. Default: `false`. +- `count_private` - Count private contributions _(boolean)_. Default: `false`. - `line_height` - Sets the line height between text _(number)_. Default: `25`. - `exclude_repo` - Exclude stars from specified repositories _(Comma-separated values)_. Default: `[] (blank array)`. - `custom_title` - Sets a custom title for the card. Default: ` GitHub Stats`. diff --git a/src/calculateRank.js b/src/calculateRank.js index 215c24d848c34..7648ad412ed67 100644 --- a/src/calculateRank.js +++ b/src/calculateRank.js @@ -1,105 +1,72 @@ -/** - * Calculates the probability of x taking on x or a value less than x in a normal distribution - * with mean and standard deviation. - * - * @see https://stackoverflow.com/a/5263759/10629172 - * - * @param {string} mean The mean of the normal distribution. - * @param {number} sigma The standard deviation of the normal distribution. - * @param {number} to The value to calculate the probability for. - * @returns {number} Probability. - */ -const normalcdf = (mean, sigma, to) => { - var z = (to - mean) / Math.sqrt(2 * sigma * sigma); - var t = 1 / (1 + 0.3275911 * Math.abs(z)); - var a1 = 0.254829592; - var a2 = -0.284496736; - var a3 = 1.421413741; - var a4 = -1.453152027; - var a5 = 1.061405429; - var erf = - 1 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-z * z); - var sign = 1; - if (z < 0) { - sign = -1; - } - return (1 / 2) * (1 + sign * erf); -}; +function expsf(x, lambda = 1) { + return 2 ** (-lambda * x); +} /** * Calculates the users rank. * * @param {object} params Parameters on which the user's rank depends. - * @param {number} params.totalRepos Total number of repos. - * @param {number} params.totalCommits Total number of commits. - * @param {number} params.contributions The number of contributions. - * @param {number} params.followers The number of followers. + * @param {boolean} params.all_commits Whether `include_all_commits` was used. + * @param {number} params.commits Number of commits. * @param {number} params.prs The number of pull requests. * @param {number} params.issues The number of issues. - * @param {number} params.stargazers The number of stars. + * @param {number} params.repos Total number of repos. + * @param {number} params.stars The number of stars. + * @param {number} params.followers The number of followers. * @returns {{level: string, score: number}}} The users rank. */ -const calculateRank = ({ - totalRepos, - totalCommits, - contributions, - followers, +function calculateRank({ + all_commits, + commits, prs, issues, - stargazers, -}) => { - const COMMITS_OFFSET = 1.65; - const CONTRIBS_OFFSET = 1.65; - const ISSUES_OFFSET = 1; - const STARS_OFFSET = 0.75; - const PRS_OFFSET = 0.5; - const FOLLOWERS_OFFSET = 0.45; - const REPO_OFFSET = 1; - - const ALL_OFFSETS = - CONTRIBS_OFFSET + - ISSUES_OFFSET + - STARS_OFFSET + - PRS_OFFSET + - FOLLOWERS_OFFSET + - REPO_OFFSET; - - const RANK_S_VALUE = 1; - const RANK_DOUBLE_A_VALUE = 25; - const RANK_A2_VALUE = 45; - const RANK_A3_VALUE = 60; - const RANK_B_VALUE = 100; + repos, // unused + stars, + followers, +}) { + const COMMITS_MEAN = all_commits ? 1000 : 250, + COMMITS_WEIGHT = 2; + const PRS_MEAN = 50, + PRS_WEIGHT = 3; + const ISSUES_MEAN = 25, + ISSUES_WEIGHT = 1; + const STARS_MEAN = 250, + STARS_WEIGHT = 4; + const FOLLOWERS_MEAN = 25, + FOLLOWERS_WEIGHT = 1; - const TOTAL_VALUES = - RANK_S_VALUE + - RANK_DOUBLE_A_VALUE + - RANK_A2_VALUE + - RANK_A3_VALUE + - RANK_B_VALUE; + const TOTAL_WEIGHT = + COMMITS_WEIGHT + + PRS_WEIGHT + + ISSUES_WEIGHT + + STARS_WEIGHT + + FOLLOWERS_WEIGHT; - // prettier-ignore - const score = ( - totalCommits * COMMITS_OFFSET + - contributions * CONTRIBS_OFFSET + - issues * ISSUES_OFFSET + - stargazers * STARS_OFFSET + - prs * PRS_OFFSET + - followers * FOLLOWERS_OFFSET + - totalRepos * REPO_OFFSET - ) / 100; + const rank = + (COMMITS_WEIGHT * expsf(commits, 1 / COMMITS_MEAN) + + PRS_WEIGHT * expsf(prs, 1 / PRS_MEAN) + + ISSUES_WEIGHT * expsf(issues, 1 / ISSUES_MEAN) + + STARS_WEIGHT * expsf(stars, 1 / STARS_MEAN) + + FOLLOWERS_WEIGHT * expsf(followers, 1 / FOLLOWERS_MEAN)) / + TOTAL_WEIGHT; - const normalizedScore = normalcdf(score, TOTAL_VALUES, ALL_OFFSETS) * 100; + const RANK_S_PLUS = 0.025; + const RANK_S = 0.1; + const RANK_A_PLUS = 0.25; + const RANK_A = 0.5; + const RANK_B_PLUS = 0.75; const level = (() => { - if (normalizedScore < RANK_S_VALUE) return "S+"; - if (normalizedScore < RANK_DOUBLE_A_VALUE) return "S"; - if (normalizedScore < RANK_A2_VALUE) return "A++"; - if (normalizedScore < RANK_A3_VALUE) return "A+"; - return "B+"; + if (rank <= RANK_S_PLUS) return "S+"; + if (rank <= RANK_S) return "S"; + if (rank <= RANK_A_PLUS) return "A+"; + if (rank <= RANK_A) return "A"; + if (rank <= RANK_B_PLUS) return "B+"; + return "B"; })(); - return { level, score: normalizedScore }; -}; + return { level, score: rank * 100 }; +} export { calculateRank }; export default calculateRank; diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 8603e38bbf59d..8fecffa466f8d 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -192,7 +192,7 @@ const fetchStats = async ( totalIssues: 0, totalStars: 0, contributedTo: 0, - rank: { level: "C", score: 0 }, + rank: { level: "B", score: 0 }, }; let res = await statsFetcher(username); @@ -220,53 +220,44 @@ const fetchStats = async ( const user = res.data.data.user; - // populate repoToHide map for quick lookup - // while filtering out - let repoToHide = {}; - if (exclude_repo) { - exclude_repo.forEach((repoName) => { - repoToHide[repoName] = true; - }); - } - stats.name = user.name || user.login; - stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount; - - // normal commits - stats.totalCommits = user.contributionsCollection.totalCommitContributions; - // if include_all_commits then just get that, - // since totalCommitsFetcher already sends totalCommits no need to += + // if include_all_commits, fetch all commits using the REST API. if (include_all_commits) { stats.totalCommits = await totalCommitsFetcher(username); + } else { + stats.totalCommits = user.contributionsCollection.totalCommitContributions; } - // if count_private then add private commits to totalCommits so far. + // if count_private, add private contributions to totalCommits. if (count_private) { stats.totalCommits += user.contributionsCollection.restrictedContributionsCount; } stats.totalPRs = user.pullRequests.totalCount; + stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount; stats.contributedTo = user.repositoriesContributedTo.totalCount; - // Retrieve stars while filtering out repositories to be hidden + // Retrieve stars while filtering out repositories to be hidden. + let repoToHide = new Set(exclude_repo); + stats.totalStars = user.repositories.nodes .filter((data) => { - return !repoToHide[data.name]; + return !repoToHide.has(data.name); }) .reduce((prev, curr) => { return prev + curr.stargazers.totalCount; }, 0); stats.rank = calculateRank({ - totalCommits: stats.totalCommits, - totalRepos: user.repositories.totalCount, - followers: user.followers.totalCount, - contributions: stats.contributedTo, - stargazers: stats.totalStars, + all_commits: include_all_commits, + commits: stats.totalCommits, prs: stats.totalPRs, issues: stats.totalIssues, + repos: user.repositories.totalCount, + stars: stats.totalStars, + followers: user.followers.totalCount, }); return stats; diff --git a/tests/api.test.js b/tests/api.test.js index f11832ef9141c..0f14378312435 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -12,17 +12,18 @@ const stats = { totalCommits: 200, totalIssues: 300, totalPRs: 400, - contributedTo: 500, + contributedTo: 50, rank: null, }; + stats.rank = calculateRank({ - totalCommits: stats.totalCommits, - totalRepos: 1, - followers: 0, - contributions: stats.contributedTo, - stargazers: stats.totalStars, + all_commits: false, + commits: stats.totalCommits, prs: stats.totalPRs, issues: stats.totalIssues, + repos: 1, + stars: stats.totalStars, + followers: 0, }); const data_stats = { @@ -229,38 +230,6 @@ describe("Test /api/", () => { } }); - it("should add private contributions", async () => { - const { req, res } = faker( - { - username: "anuraghazra", - count_private: true, - }, - data_stats, - ); - - await api(req, res); - - expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); - expect(res.send).toBeCalledWith( - renderStatsCard( - { - ...stats, - totalCommits: stats.totalCommits + 100, - rank: calculateRank({ - totalCommits: stats.totalCommits + 100, - totalRepos: 1, - followers: 0, - contributions: stats.contributedTo, - stargazers: stats.totalStars, - prs: stats.totalPRs, - issues: stats.totalIssues, - }), - }, - {}, - ), - ); - }); - it("should allow changing ring_color", async () => { const { req, res } = faker( { diff --git a/tests/calculateRank.test.js b/tests/calculateRank.test.js index 235b1b5f20b04..3bfd7f4376248 100644 --- a/tests/calculateRank.test.js +++ b/tests/calculateRank.test.js @@ -2,17 +2,87 @@ import "@testing-library/jest-dom"; import { calculateRank } from "../src/calculateRank.js"; describe("Test calculateRank", () => { - it("should calculate rank correctly", () => { + it("new user gets B rank", () => { expect( calculateRank({ - totalCommits: 100, - totalRepos: 5, + all_commits: false, + commits: 0, + prs: 0, + issues: 0, + repos: 0, + stars: 0, + followers: 0, + }), + ).toStrictEqual({ level: "B", score: 100 }); + }); + + it("average user gets A rank", () => { + expect( + calculateRank({ + all_commits: false, + commits: 250, + prs: 50, + issues: 25, + repos: 0, + stars: 250, + followers: 25, + }), + ).toStrictEqual({ level: "A", score: 50 }); + }); + + it("average user gets A rank (include_all_commits)", () => { + expect( + calculateRank({ + all_commits: true, + commits: 1000, + prs: 50, + issues: 25, + repos: 0, + stars: 250, + followers: 25, + }), + ).toStrictEqual({ level: "A", score: 50 }); + }); + + it("more than average user gets A+ rank", () => { + expect( + calculateRank({ + all_commits: false, + commits: 500, + prs: 100, + issues: 50, + repos: 0, + stars: 500, + followers: 50, + }), + ).toStrictEqual({ level: "A+", score: 25 }); + }); + + it("expert user gets S rank", () => { + expect( + calculateRank({ + all_commits: false, + commits: 1000, + prs: 200, + issues: 100, + repos: 0, + stars: 1000, followers: 100, - contributions: 61, - stargazers: 400, - prs: 300, - issues: 200, }), - ).toStrictEqual({ level: "A+", score: 49.25629684876535 }); + ).toStrictEqual({ level: "S", score: 6.25 }); + }); + + it("ezyang gets S+ rank", () => { + expect( + calculateRank({ + all_commits: false, + commits: 1000, + prs: 4000, + issues: 2000, + repos: 0, + stars: 5000, + followers: 2000, + }), + ).toStrictEqual({ level: "S+", score: 1.1363983154296875 }); }); }); diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index 08523f3362e0e..3b27e8a6bc356 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -102,13 +102,13 @@ describe("Test fetchStats", () => { it("should fetch correct stats", async () => { let stats = await fetchStats("anuraghazra"); const rank = calculateRank({ - totalCommits: 100, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 300, + all_commits: false, + commits: 100, prs: 300, issues: 200, + repos: 5, + stars: 300, + followers: 100, }); expect(stats).toStrictEqual({ @@ -132,13 +132,13 @@ describe("Test fetchStats", () => { let stats = await fetchStats("anuraghazra"); const rank = calculateRank({ - totalCommits: 100, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 300, + all_commits: false, + commits: 100, prs: 300, issues: 200, + repos: 5, + stars: 300, + followers: 100, }); expect(stats).toStrictEqual({ @@ -161,49 +161,26 @@ describe("Test fetchStats", () => { ); }); - it("should fetch and add private contributions", async () => { - let stats = await fetchStats("anuraghazra", true); - const rank = calculateRank({ - totalCommits: 150, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 300, - prs: 300, - issues: 200, - }); - - expect(stats).toStrictEqual({ - contributedTo: 61, - name: "Anurag Hazra", - totalCommits: 150, - totalIssues: 200, - totalPRs: 300, - totalStars: 300, - rank, - }); - }); - it("should fetch total commits", async () => { mock .onGet("https://api.github.com/search/commits?q=author:anuraghazra") .reply(200, { total_count: 1000 }); - let stats = await fetchStats("anuraghazra", true, true); + let stats = await fetchStats("anuraghazra", false, true); const rank = calculateRank({ - totalCommits: 1050, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 300, + all_commits: true, + commits: 1000, prs: 300, issues: 200, + repos: 5, + stars: 300, + followers: 100, }); expect(stats).toStrictEqual({ contributedTo: 61, name: "Anurag Hazra", - totalCommits: 1050, + totalCommits: 1000, totalIssues: 200, totalPRs: 300, totalStars: 300, @@ -216,21 +193,21 @@ describe("Test fetchStats", () => { .onGet("https://api.github.com/search/commits?q=author:anuraghazra") .reply(200, { total_count: 1000 }); - let stats = await fetchStats("anuraghazra", true, true, ["test-repo-1"]); + let stats = await fetchStats("anuraghazra", false, true, ["test-repo-1"]); const rank = calculateRank({ - totalCommits: 1050, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 200, + all_commits: true, + commits: 1000, prs: 300, issues: 200, + repos: 5, + stars: 200, + followers: 100, }); expect(stats).toStrictEqual({ contributedTo: 61, name: "Anurag Hazra", - totalCommits: 1050, + totalCommits: 1000, totalIssues: 200, totalPRs: 300, totalStars: 200, @@ -243,13 +220,13 @@ describe("Test fetchStats", () => { let stats = await fetchStats("anuraghazra"); const rank = calculateRank({ - totalCommits: 100, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 400, + all_commits: false, + commits: 100, prs: 300, issues: 200, + repos: 5, + stars: 400, + followers: 100, }); expect(stats).toStrictEqual({ @@ -268,13 +245,13 @@ describe("Test fetchStats", () => { let stats = await fetchStats("anuraghazra"); const rank = calculateRank({ - totalCommits: 100, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 300, + all_commits: false, + commits: 100, prs: 300, issues: 200, + repos: 5, + stars: 300, + followers: 100, }); expect(stats).toStrictEqual({ @@ -293,13 +270,13 @@ describe("Test fetchStats", () => { let stats = await fetchStats("anuraghazra"); const rank = calculateRank({ - totalCommits: 100, - totalRepos: 5, - followers: 100, - contributions: 61, - stargazers: 300, + all_commits: false, + commits: 100, prs: 300, issues: 200, + repos: 5, + stars: 300, + followers: 100, }); expect(stats).toStrictEqual({