Skip to content

Commit

Permalink
add csv response to rewards endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill-K-1 committed Nov 26, 2024
1 parent a76a20e commit ef8526e
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gov-portal-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ futures-util = { workspace = true }
async-trait = { workspace = true }

# Ethereum
ethers = { workspace = true }
ethabi = { workspace = true }
ethereum-types = { workspace = true }
web3 = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions gov-portal-db/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
},
"rewardsManager": {
"moderators": [],
"maxCsvReportDateRange": 16070400,
"maxGetRewardsLimit": 100,
"collection": "Rewards"
},
"mongo": {
Expand Down
1 change: 1 addition & 0 deletions gov-portal-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod mongo_client;
pub mod quiz;
pub mod rewards_manager;
pub mod sbt;
pub mod server;
pub mod session_manager;
pub mod users_manager;
39 changes: 25 additions & 14 deletions gov-portal-db/src/rewards_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use futures_util::TryStreamExt;
use mongodb::options::{CountOptions, FindOptions, UpdateOptions};
use rewards_cache::{RewardsCache, RewardsDelta};
use serde::Deserialize;
use std::time::Duration;

use shared::common::{
BatchId, RewardDbEntry, RewardInfo, RewardStatus, Rewards, RewardsDbEntry, UpdateRewardKind,
Expand All @@ -16,7 +17,7 @@ use shared::common::{
use crate::mongo_client::{MongoClient, MongoConfig};
use client::RewardsDbClient;

const MAX_GET_REWARDS_LIMIT: u64 = 100;
const DEFAULT_MAX_GET_REWARDS_LIMIT: u64 = 100;
const DEFAULT_GET_REWARDS_LIMIT: u64 = 100;

/// Rewards manager's [`RewardsManager`] settings
Expand All @@ -26,6 +27,21 @@ pub struct RewardsManagerConfig {
pub moderators: Vec<Address>,
/// MongoDB rewards collection name
pub collection: String,
#[serde(
deserialize_with = "shared::utils::de_secs_duration",
default = "default_max_csv_report_date_range"
)]
pub max_csv_report_date_range: Duration,
#[serde(default = "default_max_get_rewards_limit")]
pub max_get_rewards_limit: u64,
}

pub fn default_max_get_rewards_limit() -> u64 {
DEFAULT_MAX_GET_REWARDS_LIMIT
}

pub fn default_max_csv_report_date_range() -> Duration {
Duration::from_secs(16_070_400) // 186 days
}

/// User profiles manager which provides read/write access to user profile data stored MongoDB
Expand All @@ -43,13 +59,13 @@ impl RewardsManager {
) -> anyhow::Result<Self> {
let db_client = RewardsDbClient::new(mongo_config, &config.collection).await?;

let mut rewards = Vec::with_capacity(MAX_GET_REWARDS_LIMIT as usize);
let mut rewards = Vec::with_capacity(config.max_get_rewards_limit as usize);

loop {
let fetched = Self::load_all_rewards(
&db_client,
Some(rewards.len() as u64),
Some(MAX_GET_REWARDS_LIMIT),
rewards.len() as u64,
config.max_get_rewards_limit,
)
.await?;

Expand Down Expand Up @@ -145,18 +161,13 @@ impl RewardsManager {

async fn load_all_rewards(
db_client: &RewardsDbClient,
start: Option<u64>,
limit: Option<u64>,
start: u64,
limit: u64,
) -> Result<Vec<RewardsDbEntry>, error::Error> {
let start = start.unwrap_or_default();
let limit = limit
.unwrap_or(DEFAULT_GET_REWARDS_LIMIT)
.clamp(1, MAX_GET_REWARDS_LIMIT) as i64;

let find_options = FindOptions::builder()
.max_time(db_client.req_timeout)
.skip(start)
.limit(limit)
.limit(limit.max(i64::MAX as u64) as i64)
.build();

tokio::time::timeout(db_client.req_timeout, async {
Expand Down Expand Up @@ -253,7 +264,7 @@ impl RewardsManager {
let start = start.unwrap_or_default();
let limit = limit
.unwrap_or(DEFAULT_GET_REWARDS_LIMIT)
.clamp(1, MAX_GET_REWARDS_LIMIT) as i64;
.clamp(1, self.config.max_get_rewards_limit) as i64;

if !self
.config
Expand Down Expand Up @@ -379,7 +390,7 @@ impl RewardsManager {
let start = start.unwrap_or_default();
let limit = limit
.unwrap_or(DEFAULT_GET_REWARDS_LIMIT)
.clamp(1, MAX_GET_REWARDS_LIMIT) as i64;
.clamp(1, self.config.max_get_rewards_limit) as i64;

let requires_moderator_access_rights = wallet != requestor;

Expand Down
Loading

0 comments on commit ef8526e

Please sign in to comment.