Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Credits #51

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions aggregator/src/investor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::PgPool;

use crate::{empty_args, view_function_call, Completion, FetchAll, Image};
use crate::{empty_args, view_function_call, FetchAll, Image};

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Social {
Expand Down Expand Up @@ -42,26 +42,6 @@ pub struct Investor {
pub profile: Social,
}

impl Completion for Investor {
fn completion(&self) -> (u8, u8) {
let field_completion = [
self.profile.name.is_empty(),
self.profile.description.is_empty(),
self.profile.tagline.is_empty(),
self.profile.image.is_empty(),
self.profile.website.is_empty(),
self.profile.linktree.is_empty(),
self.profile.vertical.is_empty(),
self.profile.specialization.is_empty(),
self.profile.location.is_empty(),
];
(
field_completion.iter().filter(|&field| !field).count() as u8,
field_completion.len() as u8,
)
}
}

pub async fn sync_deleted(pool: &PgPool, investors: &HashSet<String>) -> anyhow::Result<()> {
let mut tx = pool.begin().await?;

Expand Down
30 changes: 0 additions & 30 deletions aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,6 @@ impl Image {
}
}

pub trait Completion
where
Self: Sized,
{
fn completion(&self) -> (u8, u8);

fn average_completion<I>(entries: I) -> f64
where
I: Iterator<Item = Self>,
{
let (sum, count) = entries
.into_iter()
.map(|entry| entry.completion())
.fold((0f64, 0usize), |(sum, count), (completed, total)| {
(sum + completed as f64 / total as f64, count + 1)
});
sum / count as f64
}

fn count_with_min_completion<I>(min: u8, entries: I) -> usize
where
I: Iterator<Item = Self>,
{
entries
.into_iter()
.filter(|entry| entry.completion().0 > min)
.count()
}
}

#[async_trait::async_trait]
pub trait FetchAll
where
Expand Down
53 changes: 40 additions & 13 deletions aggregator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use aggregator::{
project::{self, Project},
request::{self, FullRequest},
vendor::{self, Vendor},
Completion, FetchAll,
FetchAll,
};

#[tokio::main]
Expand All @@ -20,6 +20,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let social_account = std::env::var("SOCIAL_CONTRACT")
.unwrap_or("social.near".to_string())
.parse()?;
let signer_account = std::env::var("SIGNER_ACCOUNT")
.expect("SIGNER_ACCOUNT is not set")
.parse()?;
let secret_key = std::env::var("SECRET_KEY").expect("SECRET_KEY is not set");

let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(5)
Expand All @@ -36,28 +40,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

eprintln!("Projects:");
let projects = Project::fetch_all(&client, &horizon_account, &social_account).await?;
let filled_out = Project::count_with_min_completion(3, projects.values().cloned());
let filled = Project::average_completion(projects.values().cloned());
eprintln!("{:#?}/{:#?}", filled_out, projects.len());
eprintln!("Average completion: {:#?}", filled);
eprintln!("Fetching state of projects before update...");
let before_state =
project::get_state(&pool, projects.keys().cloned().collect_vec().clone()).await;
eprintln!("Inserting...");
project::insert_many(&pool, projects.values().cloned().collect_vec()).await?;

eprintln!("Vendors:");
let vendors = Vendor::fetch_all(&client, &horizon_account, &social_account).await?;
let filled_out = Vendor::count_with_min_completion(3, vendors.values().cloned());
let filled = Vendor::average_completion(vendors.values().cloned());
eprintln!("{:#?}/{:#?}", filled_out, vendors.len());
eprintln!("Average completion: {:#?}", filled);
eprintln!("Inserting...");
vendor::insert_many(&pool, vendors.values().cloned().collect_vec()).await?;

eprintln!("Investors:");
let investors = Investor::fetch_all(&client, &horizon_account, &social_account).await?;
let filled_out = Investor::count_with_min_completion(3, investors.values().cloned());
let filled = Investor::average_completion(investors.values().cloned());
eprintln!("{:#?}/{:#?}", filled_out, investors.len());
eprintln!("Average completion: {:#?}", filled);
eprintln!("Inserting...");
investor::insert_many(&pool, investors.values().cloned().collect_vec()).await?;

Expand Down Expand Up @@ -159,6 +154,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.await?;

eprintln!("Fetching state of projects after update...");
let after_state =
project::get_state(&pool, projects.keys().cloned().collect_vec().clone()).await;

eprintln!("Checking for changes...");
let for_achievements = after_state
.iter()
.filter_map(|project| {
let incentinves = project::check_incentives(
before_state.iter().find(|before| before.id == project.id),
project,
);

(!incentinves.is_empty()).then_some((project.id.clone(), incentinves))
})
.collect_vec();

if for_achievements.is_empty() {
eprintln!("No incentives to award");
} else {
eprintln!("Awarding {} incentives...", for_achievements.len());
project::award_incentives(
&client,
signer_account,
horizon_account,
&secret_key,
for_achievements,
)
.await;
eprintln!("Awarded incentives");
}

eprintln!("Done");

Ok(())
Expand Down
Loading