Skip to content

Commit

Permalink
feat: return snap_name in chart data
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 11, 2024
1 parent cec3d3e commit 3856572
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ skip_cache = []
cached = { version = "0.54.0", features = ["async"] }
dotenvy = "0.15"
envy = "0.4"
futures = "0.3"
http = "1.1.0"
jsonwebtoken = "9.2"
prost = "0.13.3"
Expand Down
1 change: 1 addition & 0 deletions proto/ratings_features_common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ message Rating {
string snap_id = 1;
uint64 total_votes = 2;
RatingsBand ratings_band = 3;
string snap_name = 4;
}

enum RatingsBand {
Expand Down
2 changes: 2 additions & 0 deletions src/grpc/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ impl App for RatingService {
snap_id,
total_votes,
ratings_band,
snap_name,
} = Rating::from(votes);

Ok(Response::new(GetRatingResponse {
rating: Some(PbRating {
snap_id,
total_votes,
ratings_band: ratings_band as i32,
snap_name,
}),
}))
}
Expand Down
4 changes: 3 additions & 1 deletion src/grpc/charts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async fn get_chart_cached(
) -> Result<Chart, Box<dyn std::error::Error>> {
let summaries = VoteSummary::get_for_timeframe(timeframe, category, conn!()).await?;

Ok(Chart::new(timeframe, summaries))
Ok(Chart::new(timeframe, summaries, ctx).await?)
}

impl From<ChartData> for PbChartData {
Expand All @@ -107,6 +107,7 @@ impl From<Rating> for PbRating {
snap_id: r.snap_id,
total_votes: r.total_votes,
ratings_band: r.ratings_band as i32,
snap_name: r.snap_name,
}
}
}
Expand All @@ -117,6 +118,7 @@ impl From<PbRating> for Rating {
snap_id: r.snap_id,
total_votes: r.total_votes,
ratings_band: RatingsBand::from_repr(r.ratings_band).unwrap(),
snap_name: r.snap_name,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/proto/ratings.features.common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub struct Rating {
pub total_votes: u64,
#[prost(enumeration = "RatingsBand", tag = "3")]
pub ratings_band: i32,
#[prost(string, tag = "4")]
pub snap_name: ::prost::alloc::string::String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
Expand Down
36 changes: 29 additions & 7 deletions src/ratings/charts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
//! Struct definitions for the charting feature for ratings.
use futures::future::try_join_all;

use crate::{
db::{Timeframe, VoteSummary},
ratings::rating::{calculate_band, Rating},
ratings::{
get_snap_name,
rating::{calculate_band, Rating},
Error,
},
Context,
};
use std::cmp::Ordering;

Expand All @@ -12,8 +19,22 @@ pub struct Chart {
}

impl Chart {
pub fn new(timeframe: Timeframe, data: Vec<VoteSummary>) -> Self {
let mut data: Vec<ChartData> = data.into_iter().map(Into::into).collect();
pub async fn new(
timeframe: Timeframe,
data: Vec<VoteSummary>,
ctx: &Context,
) -> Result<Self, Error> {
let mut data: Vec<ChartData> = try_join_all(data.into_iter().map(|vote_summary| async {
let snap_name = get_snap_name(
&vote_summary.snap_id,
&ctx.config.snapcraft_io_uri,
&ctx.http_client,
)
.await?;

Result::<ChartData, Error>::Ok(ChartData::new(vote_summary, &snap_name))
}))
.await?;

data.sort_by(|a, b| {
b.raw_rating
Expand All @@ -22,10 +43,10 @@ impl Chart {
});

// Take only the first 20 elements from the sorted chart_data
Chart {
Ok(Self {
timeframe,
data: data.into_iter().take(20).collect(),
}
})
}
}

Expand All @@ -35,13 +56,14 @@ pub struct ChartData {
pub rating: Rating,
}

impl From<VoteSummary> for ChartData {
fn from(vote_summary: VoteSummary) -> Self {
impl ChartData {
pub fn new(vote_summary: VoteSummary, snap_name: &str) -> Self {
let (raw_rating, ratings_band) = calculate_band(&vote_summary);
let rating = Rating {
snap_id: vote_summary.snap_id,
total_votes: vote_summary.total_votes as u64,
ratings_band,
snap_name: snap_name.into(),
};
let raw_rating = raw_rating.unwrap_or(0.0) as f32;

Expand Down
3 changes: 3 additions & 0 deletions src/ratings/rating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct Rating {
/// The descriptive indicator of "how good" this snap is based
/// on aggregated ratings.
pub ratings_band: RatingsBand,
/// The unique name of the snap.
pub snap_name: String,
}

impl From<VoteSummary> for Rating {
Expand All @@ -78,6 +80,7 @@ impl From<VoteSummary> for Rating {
snap_id: votes.snap_id,
total_votes: votes.total_votes as u64,
ratings_band,
snap_name: "".into(),
}
}
}
Expand Down

0 comments on commit 3856572

Please sign in to comment.