Skip to content

Commit

Permalink
feat: return snap_name in ratings data
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 11, 2024
1 parent 3856572 commit 7acd796
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/grpc/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use crate::{
conn,
db::VoteSummary,
Expand All @@ -9,17 +11,20 @@ use crate::{
common::Rating as PbRating,
},
ratings::Rating,
Context,
};
use tonic::{Request, Response, Status};
use tracing::error;

/// The general service governing retrieving ratings for the store app.
#[derive(Clone)]
pub struct RatingService;
pub struct RatingService {
ctx: Arc<Context>,
}

impl RatingService {
pub fn new_server() -> AppServer<RatingService> {
AppServer::new(RatingService)
pub fn new_server(ctx: Arc<Context>) -> AppServer<RatingService> {
AppServer::new(Self { ctx })
}
}

Expand All @@ -41,7 +46,9 @@ impl App for RatingService {
total_votes,
ratings_band,
snap_name,
} = Rating::from(votes);
} = Rating::new(votes, &self.ctx)
.await
.map_err(|_| Status::unknown("Internal server error"))?;

Ok(Response::new(GetRatingResponse {
rating: Some(PbRating {
Expand Down
2 changes: 1 addition & 1 deletion src/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn run_server(ctx: Context) -> Result<(), Box<dyn std::error::Error>>

builder
.layer(AuthLayer::new(verifier))
.add_service(RatingService::new_server())
.add_service(RatingService::new_server(ctx.clone()))
.add_service(ChartService::new_server(ctx.clone()))
.add_service(UserService::new_server(ctx.clone()))
.serve(addr)
Expand Down
19 changes: 14 additions & 5 deletions src/ratings/rating.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Calculations around snap ratings based on received votes
use crate::db::VoteSummary;
use crate::ratings::{get_snap_name, Error};
use crate::Context;

/// An arbitrary fixed number of votes we've determined is below the threshold to be meaningful.
const INSUFFICIENT_VOTES_QUANTITY: i64 = 25;
Expand Down Expand Up @@ -72,16 +74,23 @@ pub struct Rating {
pub snap_name: String,
}

impl From<VoteSummary> for Rating {
fn from(votes: VoteSummary) -> Self {
impl Rating {
pub async fn new(votes: VoteSummary, ctx: &Context) -> Result<Self, Error> {
let (_, ratings_band) = calculate_band(&votes);

Self {
let snap_name = get_snap_name(
&votes.snap_id,
&ctx.config.snapcraft_io_uri,
&ctx.http_client,
)
.await?;

Ok(Self {
snap_id: votes.snap_id,
total_votes: votes.total_votes as u64,
ratings_band,
snap_name: "".into(),
}
snap_name,
})
}
}

Expand Down

0 comments on commit 7acd796

Please sign in to comment.