Discover your GitHub ranking with Metrics Insights!
Tags: github, showdev, javascript, node
Published: 2021-04-19 17:11:14

Ever wondered how high is ranked your most starred/forked repository across all [GitHub](https://github.com)? Or whether if you're one of most followed user account 🙂 ?

Well, this is possible thanks to [Metrics Insights](https://metrics.lecoq.io/about)! 

![Preview image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbq3ewr0ymy8erb40h4s.png)
 
There's also a few additional data, like the number of bytes you've written for each languages you used up until now!

![Preview image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz6urdyh1v1vsquu8wqa.png)
 
Best of it is that you can use [Metrics Insights](https://metrics.lecoq.io/about) in your browser without any account login nor cookies!

## 🤔 How does it work though?

I'm currently working on [Metrics](https://github.com/lowlighter/metrics), a project designed to gather data that you can then render as SVG to embed on your GitHub user profile, like below:

![Preview image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ivu4p47r55v9tvyib5x.png)

Currently, there's hundreds of options, including 4 templates and 30 plugins, one of them being the `🏆 Achievements` one:

![Preview image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gv663em79s3q66ryqgmo.png)
 
As you can see, some of them include ranking. You may wonder of these are computed? Well it's actually a lot simpler that it may looks, using [GitHub GraphQL API](https://docs.github.com/en/graphql) and [Puppeteer](https://github.com/puppeteer/puppeteer).

The trick is to fetch the number of `followers`, `created repositories` of said user, along with their most `starred repository` and most `forked repository`.

Then using `search` GitHub GraphQL API, we can count how many users and repositories are above using the following query (replacing each `$` prefixed values with what we found earlier):

```graphql
query AchievementsRanking {
  repo_rank:search(query: "stars:>$stars", type: REPOSITORY, first: 0) {
    repositoryCount
  }
  user_rank:search(query: "followers:>$followers", type: USER, first: 0) {
    userCount
  }
  forks_rank:search(query: "forks:>$forks", type: REPOSITORY, first: 0) {
    repositoryCount
  }
  created_rank:search(query: "repos:>$created", type: USER, first: 0) {
    userCount
  }
}
```

As you can see, we only retrieve the total count of each categorie, we don't even try to fetch any content. So basically, if an user has `100` followers and `user_rank.userCount` send back `999` users, we can deduce that user is 1000<sup>th</sup> (`999+1`) since 999 users has more followers. We do the same for each categorie.

One thing to note is that GitHub API may return incomplete count below some thresholds as there's too many results to compute for even GitHub to handle it. But if you have created a respository at least once, or having a repository which has been starred 5+ times or forked 1+ time or if you have 3+ followers, you can compete within each categorie 😃 !

I guess it also means that there's a lot of inactive users and unused account, so if you're somewhat active on GitHub you'll be able to see your stats pretty easily with this tool. 

Since it's not possible to estimate how many repositories and users exists through GitHub's own API, you may wonder how I got total repositories and users count?

Well, actually these are displayed on [GitHub search](https://github.com/search):

![GitHub search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7vb5hibwsr83s2k62ej.png)
 
This is why we needed puppeteer 🙂 We just crawl a few times (displayed stats is random between users, repositories and issues) and extract these. Once we got each categorie at least once, no more need for puppeteer, we're ready to go!

___

If you're interested, don't hesitate to take a look at this project source code and support it. There's even a plugin to update your profile readme with your latest post from **dev.to**!

{% github lowlighter/metrics no-readme %}

You can provide some feedback like bug reports and suggestion for this feature on [GitHub discussions](https://github.com/lowlighter/metrics/discussions/229) 🙂

Thanks a lot for reading, and don't hesitate to share your scores in comments section!

_One last bonus: can you guess **who** is the most followed GitHub user? [Click here to know!](https://metrics.lecoq.io/about/torvalds)_