Skip to content

Commit

Permalink
Add REST API for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Sing-Li authored Feb 11, 2022
2 parents 1305877 + f5059a5 commit 3f76f13
Show file tree
Hide file tree
Showing 5 changed files with 617 additions and 268 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ npm install pm2 -g # run this command on your server if pm2 is not installed.
pm2 start app.js --name "GSOC-Contribution-Leaderboard" # start the backend service
````

## Automated Data Fetch REST API
REST API endpoints are available to fetch important leaderboard data real-time.

The API uses the cached `data.json` file which is created and regularly updated on starting up the server. This means that if the API is called in the middle of a data fetch cycle, the data returned by it at any time would be based off the data present in the `data.json` at that time. Since the leaderboard itself displays data using the `data.json` file, the data fetched by the API and that displayed on the leaderboard will *always be consistent* at any given time.

Details about the API endpoints and their responses can be found in the [REST-API](/REST-API.md) file.

## Acknowledgement
This project is inspired by [GSOC-Contribution-Leaderboard](https://github.com/shubhsherl/GSoC-Contribution-Leaderboard/). Thanks to the Python team for the work.

Expand Down
149 changes: 149 additions & 0 deletions REST-API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
## Automated Data Fetch REST-API

The following endpoints can be used to fetch leaderboard data.

`/stats`

Gets important statistics about the leaderboard.

**Example Call**

```
curl --request GET 'http://localhost:8080/api/stats'
```

**Example Response**

```
{
totalContributors: 128,
totalOpenPRs: 58,
totalMergedPRs: 87,
totalIssues: 70,
}
```

---

`/rank`

Used to rank contributors by a parameter. Can be used to fetch the rank of a contributor based on a parameter (defaults to number of merged PRs).

Provides ranked list of contributor usernames sorted by `mergedprs` if no parameters provided.

**Query Parameters**

| Argument | Example | Required | Description
| ------------- | ------------- | ------------- | -------------
| parameter | openprs | Optional | Can take values `openprs`, `issues`, `mergedprs` to rank contributors by. Defaults to `mergedprs`.
| username | RonLek | Optional | Username of any contributor within the leaderboard. Returns error response if username not found. Case-insensitive.

**Example Call 1**

```
curl --request GET 'http://localhost:8080/api/rank?username=RonLek'
```

**Example Response 1**
```
{"username":"RonLek","rank":3}
```

**Example Call 2**

```
curl --request GET 'http://localhost:8080/api/rank?username=RonLek&parameter=openprs'
```

**Example Response 2**
```
{"username":"RonLek","rank":4}
```

**Example Call 3**

```
curl --request GET 'http://localhost:8080/api/rank?parameter=issues'
```

**Example Response 3**

```
{"ranks":["Darshilp326","yash-rajpal","RonLek","aditya-mitra","rodriq","djcruz93","lolimay"]}
```

---

`/contributor`

Used to fetch contributor details. Can be used to fetch contributor details by contributor rank sorted by parameter (defaults to number of merged PRs).

Provides all contributor data if no parameters provided.

**Query Parameters**

| Argument | Example | Required | Description
| ------------- | ------------- | ------------- | -------------
| parameter | openprs | Optional | Can take values `openprs`, `issues`, `mergedprs` to rank contributors by. Defaults to `mergedprs`.
| username | RonLek | Optional | Username of any contributor within the leaderboard. Returns error response if username not found. Case-sensitive.
| rank | 3 | Optional | Rank starting with 1.

**Example Call 1**

```
curl --request GET 'http://localhost:8080/api/contributor?username=RonLek'
```

**Example Response 1**

```
{
"home":"https://github.com/RonLek",
"avatarUrl":"https://avatars.githubusercontent.com/u/28918901?v=4",
"openPRsNumber": ...,
"openPRsLink": ...,
"mergedPRsNumber": ...,
"mergedPRsLink": ..,
"issuesNumber": ...,
"issuesLink": ...
}
```

**Example Call 2**
```
curl --request GET 'http://localhost:8080/api/contributor?rank=3'
```

**Example Response 2**
```
{
"home":"https://github.com/RonLek",
"avatarUrl":"https://avatars.githubusercontent.com/u/28918901?v=4",
"openPRsNumber": ...,
"openPRsLink": ...,
"mergedPRsNumber": ...,
"mergedPRsLink": ..,
"issuesNumber": ...,
"issuesLink": ...
}
```

**Example Call 3**

```
curl --request GET 'http://localhost:8080/api/contributor?rank=3&parameter=issues'
```

**Example Response 3**
```
{
"home":"https://github.com/RonLek",
"avatarUrl":"https://avatars.githubusercontent.com/u/28918901?v=4",
"openPRsNumber": ...,
"openPRsLink": ...,
"mergedPRsNumber": ...,
"mergedPRsLink": ..,
"issuesNumber": ...,
"issuesLink": ...
}
```
32 changes: 16 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ function refreshTable(newData) {
const queryString = window.location.search
const urlParams = new URLSearchParams(queryString)
switch (urlParams.get('sort')) { //assigns according to parameter-sort (default 'm')
case 'p':
pref1 = "openPRsNumber"
pref2 = "mergedPRsNumber"
pref3 = "issuesNumber"
break;
case 'i':
pref1 = "issuesNumber"
pref2 = "mergedPRsNumber"
pref3 = "openPRsNumber"
break;

default:
pref1 = "mergedPRsNumber"
pref2 = "openPRsNumber"
pref3 = "issuesNumber"
break;
case 'p':
pref1 = 'openPRsNumber'
pref2 = 'mergedPRsNumber'
pref3 = 'issuesNumber'
break
case 'i':
pref1 = 'issuesNumber'
pref2 = 'mergedPRsNumber'
pref3 = 'openPRsNumber'
break

default:
pref1 = 'mergedPRsNumber'
pref2 = 'openPRsNumber'
pref3 = 'issuesNumber'
break
}
if (a[pref1] < b[pref1]) {
return 1
Expand Down
Loading

0 comments on commit 3f76f13

Please sign in to comment.