Skip to content

Commit

Permalink
feat: return snap names in chart and ratings data
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 12, 2024
1 parent 95ba170 commit 2b74d7d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 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
11 changes: 10 additions & 1 deletion src/grpc/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
common::Rating as PbRating,
},
ratings::Rating,
ratings::{get_snap_name, Rating},
Context,
};
use std::sync::Arc;
Expand Down Expand Up @@ -46,11 +46,20 @@ impl App for RatingService {
ratings_band,
} = Rating::from(votes);

let snap_name = get_snap_name(
&snap_id,
&self.ctx.config.snapcraft_io_uri,
&self.ctx.http_client,
)
.await
.map_err(|_| Status::unknown("Internal server error"))?;

Ok(Response::new(GetRatingResponse {
rating: Some(PbRating {
snap_id,
total_votes,
ratings_band: ratings_band as i32,
snap_name,
}),
}))
}
Expand Down
43 changes: 31 additions & 12 deletions src/grpc/charts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::{
},
common::{Rating as PbRating, RatingsBand as PbRatingsBand},
},
ratings::{Chart, ChartData, Rating, RatingsBand},
ratings::{get_snap_name, Chart, ChartData, Error, Rating, RatingsBand},
Context,
};
use cached::proc_macro::cached;
use futures::future::try_join_all;
use std::sync::Arc;
use tonic::{Request, Response, Status};
use tracing::error;
Expand Down Expand Up @@ -55,7 +56,21 @@ impl chart_server::Chart for ChartService {
}

Ok(chart) => {
let ordered_chart_data = chart.data.into_iter().map(|cd| cd.into()).collect();
let ordered_chart_data: Vec<PbChartData> =
try_join_all(chart.data.into_iter().map(|chart_data| async {
let snap_name = get_snap_name(
&chart_data.rating.snap_id,
&self.ctx.config.snapcraft_io_uri,
&self.ctx.http_client,
)
.await?;

Result::<PbChartData, Error>::Ok(
PbChartData::from_chart_data_and_snap_name(chart_data, &snap_name),
)
}))
.await
.map_err(|_| Status::unknown("Internal server error"))?;

let payload = GetChartResponse {
timeframe: timeframe as i32,
Expand Down Expand Up @@ -84,27 +99,31 @@ impl chart_server::Chart for ChartService {
async fn get_chart_cached(
category: Option<Category>,
timeframe: Timeframe,
) -> Result<Chart, Box<dyn std::error::Error>> {
) -> Result<Chart, crate::db::Error> {
let summaries = VoteSummary::get_for_timeframe(timeframe, category, conn!()).await?;

Ok(Chart::new(timeframe, summaries))
}

impl From<ChartData> for PbChartData {
fn from(value: ChartData) -> Self {
impl PbChartData {
fn from_chart_data_and_snap_name(chart_data: ChartData, snap_name: &str) -> Self {
Self {
raw_rating: value.raw_rating,
rating: Some(value.rating.into()),
raw_rating: chart_data.raw_rating,
rating: Some(PbRating::from_rating_and_snap_name(
chart_data.rating,
snap_name,
)),
}
}
}

impl From<Rating> for PbRating {
fn from(r: Rating) -> Self {
impl PbRating {
fn from_rating_and_snap_name(rating: Rating, snap_name: &str) -> Self {
Self {
snap_id: r.snap_id,
total_votes: r.total_votes,
ratings_band: r.ratings_band as i32,
snap_id: rating.snap_id,
total_votes: rating.total_votes,
ratings_band: rating.ratings_band as i32,
snap_name: snap_name.into(),
}
}
}
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
2 changes: 1 addition & 1 deletion src/ratings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn get_json<T: DeserializeOwned>(
result = true
)
)]
async fn get_snap_name(
pub(crate) async fn get_snap_name(
snap_id: &str,
base: &str,
client: &reqwest::Client,
Expand Down

0 comments on commit 2b74d7d

Please sign in to comment.