-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated endpoints, added job to load github metadata, expanded streak…
… table
- Loading branch information
Showing
16 changed files
with
397 additions
and
131 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,63 @@ | ||
BEGIN; | ||
|
||
-- Step 1: Create the new streak table | ||
CREATE TABLE IF NOT EXISTS streak ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT UNIQUE NOT NULL, | ||
period TEXT NOT NULL | ||
); | ||
|
||
-- Step 2: Insert any missing streak entries into the new streak table | ||
INSERT INTO | ||
streak (id, name, period) | ||
VALUES | ||
(0, 'Weekly Pull Request', 'Weekly'), | ||
( | ||
1, | ||
'Monthly Pull Request with score higher 8', | ||
'Monthly' | ||
); | ||
|
||
ALTER TABLE | ||
streak_user_data | ||
ADD | ||
COLUMN new_streak_id INTEGER; | ||
|
||
-- Step 4: Copy data from the old streak_id column to the new_streak_id column | ||
UPDATE | ||
streak_user_data | ||
SET | ||
new_streak_id = streak_user_data.streak_id; | ||
|
||
-- Step 5: Drop the old streak_id column | ||
ALTER TABLE | ||
streak_user_data DROP COLUMN streak_id; | ||
|
||
-- Step 6: Rename the new_streak_id column to streak_id | ||
ALTER TABLE | ||
streak_user_data RENAME COLUMN new_streak_id TO streak_id; | ||
|
||
-- Step 7: Add the foreign key constraint to the new streak_id column | ||
ALTER TABLE | ||
streak_user_data | ||
ADD | ||
CONSTRAINT fk_streak_id FOREIGN KEY (streak_id) REFERENCES streak(id) ON DELETE CASCADE; | ||
|
||
-- Step 8: Re-add the primary key constraint | ||
ALTER TABLE | ||
streak_user_data | ||
ADD | ||
PRIMARY KEY (user_id, streak_id); | ||
|
||
ALTER TABLE | ||
repos | ||
ADD | ||
COLUMN primary_language TEXT, | ||
ADD | ||
COLUMN open_issues INTEGER, | ||
ADD | ||
COLUMN stars INTEGER, | ||
ADD | ||
COLUMN forks INTEGER; | ||
|
||
COMMIT; |
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,7 @@ | ||
select | ||
r.id as repo_id, | ||
r.name as repo, | ||
o.name as organization | ||
from | ||
repos as r | ||
JOIN organizations o ON r.organization_id = o.id |
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,12 @@ | ||
SELECT | ||
streak.id as streak_id, | ||
name, | ||
period as streak_type, | ||
amount, | ||
best, | ||
latest_time_string | ||
FROM | ||
streak_user_data | ||
JOIN streak ON streak.id = streak_user_data.streak_id | ||
WHERE | ||
user_id = $1 |
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,86 @@ | ||
use std::{ | ||
sync::{atomic::AtomicBool, Arc}, | ||
time::Duration, | ||
}; | ||
|
||
use chrono::DateTime; | ||
use rocket::fairing::AdHoc; | ||
use rocket_db_pools::Database; | ||
use shared::{near::NearClient, TimePeriod}; | ||
|
||
use crate::db::DB; | ||
|
||
async fn fetch_and_store_users(near_client: &NearClient, db: &DB) -> anyhow::Result<()> { | ||
let timestamp = std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH)? | ||
.as_nanos(); | ||
let periods = [TimePeriod::Month, TimePeriod::Quarter, TimePeriod::AllTime] | ||
.into_iter() | ||
.map(|e| e.time_string(timestamp as u64)) | ||
.collect(); | ||
let users = near_client.users(periods).await?; | ||
for user in users { | ||
let user_id = db.upsert_user(&user.name).await?; | ||
for (period, data) in user.period_data { | ||
db.upsert_user_period_data(period, &data, user_id).await?; | ||
} | ||
for (streak_id, streak_data) in user.streaks { | ||
db.upsert_streak_user_data(&streak_data, streak_id as i32, user_id) | ||
.await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn fetch_and_store_prs(near_client: &NearClient, db: &DB) -> anyhow::Result<()> { | ||
let prs = near_client.prs().await?; | ||
for (pr, executed) in prs { | ||
let organization_id = db.upsert_organization(&pr.organization).await?; | ||
let repo_id = db.upsert_repo(organization_id, &pr.repo).await?; | ||
let author_id = db.upsert_user(&pr.author).await?; | ||
let _ = db | ||
.upsert_pull_request( | ||
repo_id, | ||
pr.number as i32, | ||
author_id, | ||
DateTime::from_timestamp_nanos(pr.created_at as i64).naive_utc(), | ||
pr.merged_at | ||
.map(|t| DateTime::from_timestamp_nanos(t as i64).naive_utc()), | ||
pr.score(), | ||
executed, | ||
) | ||
.await?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn fetch_and_store_all_data(near_client: &NearClient, db: &DB) -> anyhow::Result<()> { | ||
fetch_and_store_users(near_client, db).await?; | ||
fetch_and_store_prs(near_client, db).await?; | ||
Ok(()) | ||
} | ||
|
||
pub fn stage(client: NearClient, sleep_duration: Duration, atomic_bool: Arc<AtomicBool>) -> AdHoc { | ||
rocket::fairing::AdHoc::on_liftoff("Load users from Near every X minutes", move |rocket| { | ||
Box::pin(async move { | ||
// Get an actual DB connection | ||
let db = DB::fetch(rocket) | ||
.expect("Failed to get DB connection") | ||
.clone(); | ||
|
||
rocket::tokio::spawn(async move { | ||
let mut interval = rocket::tokio::time::interval(sleep_duration); | ||
let near_client = client; | ||
while atomic_bool.load(std::sync::atomic::Ordering::Relaxed) { | ||
interval.tick().await; | ||
|
||
// Execute a query of some kind | ||
if let Err(e) = fetch_and_store_all_data(&near_client, &db).await { | ||
rocket::error!("Failed to fetch and store data: {:#?}", e); | ||
} | ||
} | ||
}); | ||
}) | ||
}) | ||
} |
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
Oops, something went wrong.