-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add github popularity fetching
Signed-off-by: Guillaume Hivert <[email protected]>
- Loading branch information
Showing
12 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
apps/backend/db/migrations/20240521174525_alter_table_package_add_popularity.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- migrate:up | ||
alter table package | ||
add column popularity jsonb, | ||
drop column favorites; | ||
|
||
-- migrate:down | ||
alter table package | ||
drop column popularity, | ||
add column favorites int not null default 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import api/github/stargazer_count | ||
import backend/error | ||
import gleam/dynamic | ||
import gleam/function | ||
import gleam/http | ||
import gleam/http/request | ||
import gleam/httpc | ||
import gleam/json | ||
import gleam/list | ||
import gleam/option.{type Option, Some} | ||
import gleam/regex | ||
import gleam/result | ||
|
||
fn query( | ||
token: String, | ||
query: String, | ||
variables: Option(json.Json), | ||
decoder: dynamic.Decoder(a), | ||
) { | ||
let body = | ||
json.object([ | ||
#("query", json.string(query)), | ||
#("variables", json.nullable(variables, function.identity)), | ||
]) | ||
use response <- result.try( | ||
request.new() | ||
|> request.set_header("authorization", "Bearer " <> token) | ||
|> request.set_header("user-agent", "gloogle / 0.0.0") | ||
|> request.set_method(http.Post) | ||
|> request.set_scheme(http.Https) | ||
|> request.set_host("api.github.com") | ||
|> request.set_path("/graphql") | ||
|> request.set_body(json.to_string(body)) | ||
|> httpc.send() | ||
|> result.map_error(error.FetchError), | ||
) | ||
|
||
response.body | ||
|> json.decode(using: decoder) | ||
|> result.map_error(error.JsonError) | ||
} | ||
|
||
fn match_repository_name(repo_url: String) { | ||
let assert Ok(owner_name) = regex.from_string("https://github.com/(.+)/(.+)") | ||
regex.scan(with: owner_name, content: repo_url) | ||
|> list.first() | ||
|> result.replace_error(error.UnknownError( | ||
"No repository match for " <> repo_url, | ||
)) | ||
} | ||
|
||
pub fn get_stargazer_count(token: String, repo_url: String) { | ||
use match <- result.try(match_repository_name(repo_url)) | ||
case match.submatches { | ||
[Some(owner), Some(name)] -> | ||
stargazer_count.variables(name, owner) | ||
|> query(token, stargazer_count.query, _, stargazer_count.decoder) | ||
_ -> Error(error.UnknownError("")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import decipher | ||
import gleam/dynamic | ||
import gleam/json | ||
import gleam/option.{Some} | ||
|
||
pub const query = " | ||
query getStargazers($name: String!, $owner: String!) { | ||
repository(owner: $owner, name: $name) { | ||
stargazerCount | ||
} | ||
}" | ||
|
||
pub fn decoder(dyn) { | ||
decipher.at(["data", "repository", "stargazerCount"], dynamic.int)(dyn) | ||
} | ||
|
||
pub fn variables(name: String, owner: String) { | ||
Some( | ||
json.object([#("name", json.string(name)), #("owner", json.string(owner))]), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import api/github | ||
import backend/config.{type Context} | ||
import backend/postgres/queries | ||
import gleam/bool | ||
import gleam/dict | ||
import gleam/function | ||
import gleam/list | ||
import gleam/option | ||
import gleam/result | ||
import wisp | ||
|
||
pub fn compute_popularity(ctx: Context) { | ||
wisp.log_info("Syncing popularity") | ||
do_compute_popularity(ctx, offset: 0) | ||
|> function.tap(fn(_) { wisp.log_info("Syncing package ranks finished!") }) | ||
} | ||
|
||
fn do_compute_popularity(ctx: Context, offset offset: Int) { | ||
let db = ctx.db | ||
use repos <- result.try(queries.select_package_repository_address(db, offset)) | ||
use <- bool.guard(when: list.is_empty(repos), return: Ok(Nil)) | ||
list.map(repos, fn(repo) { | ||
repo | ||
|> option.map(update_repo_popularity(ctx, _)) | ||
|> option.unwrap(Ok(Nil)) | ||
|> result.try_recover(fn(_) { Ok(Nil) }) | ||
}) | ||
|> result.all() | ||
|> result.try(fn(_) { do_compute_popularity(ctx, offset: offset + 100) }) | ||
} | ||
|
||
fn update_repo_popularity(ctx: Context, repo: String) { | ||
wisp.log_debug("Syncing " <> repo) | ||
use count <- result.try(github.get_stargazer_count(ctx.github_token, repo)) | ||
dict.from_list([#("github", count)]) | ||
|> queries.update_package_popularity(ctx.db, repo, _) | ||
|> result.replace(Nil) | ||
|> function.tap(fn(_) { wisp.log_debug("Synced " <> repo) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters