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

Update trademark_disclaimer to allow for new URL #797

Closed
wants to merge 11 commits into from
Closed
73 changes: 37 additions & 36 deletions Cargo.lock

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

21 changes: 13 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ edition = "2021"
rust-version = "1.65"

[workspace.dependencies]
anyhow = "1.0.66"
anyhow = "1.0.68"
askalono = "0.4.6"
askama = { git = "https://github.com/djc/askama", rev = "eeec6f0654f32270aec4e4a0d0f42e4ad39bc28e" }
askama_axum = { git = "https://github.com/djc/askama", rev = "eeec6f0654f32270aec4e4a0d0f42e4ad39bc28e" }
async-trait = "0.1.59"
async-trait = "0.1.60"
axum = { version = "0.6.1", features = ["macros"] }
bincode = "1.3.3"
clap = { version = "4.0.29", features = ["derive"] }
clap = { version = "4.0.30", features = ["derive"] }
clomonitor-core = { path = "../clomonitor-core" }
comfy-table = "6.1.3"
config = "0.13.3"
Expand All @@ -40,21 +40,26 @@ metrics = "0.20.1"
metrics-exporter-prometheus = "0.11.0"
mime = "0.3.16"
mockall = "0.11.3"
openssl = { version = "0.10.44", features = ["vendored"] }
openssl = { version = "0.10.45", features = ["vendored"] }
postgres-openssl = "0.5.0"
postgres-types = { version = "0.2.4", features = ["derive"] }
predicates = "2.1.4"
regex = "1.7.0"
reqwest = "0.11.13"
resvg = "0.27.0"
serde = { version = "1.0.150", features = ["derive"] }
serde_json = "1.0.89"
serde_yaml = "0.9.14"
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
serde_yaml = "0.9.16"
serde_qs = "0.10.1"
sha2 = "0.10.6"
tempfile = "3.3.0"
tera = { version = "1.17.1", default-features = false }
time = { version = "0.3.17", features = ["macros", "parsing", "serde"] }
time = { version = "0.3.17", features = [
"formatting",
"macros",
"parsing",
"serde",
] }
tiny-skia = "0.8.2"
tokio = { version = "1.23.0", features = [
"macros",
Expand Down
25 changes: 22 additions & 3 deletions clomonitor-apiserver/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::handlers::RepositoryReportMDTemplate;
use crate::{
handlers::RepositoryReportMDTemplate,
views::{ProjectId, Total},
};
use anyhow::Result;
use async_trait::async_trait;
use clomonitor_core::score::Score;
Expand All @@ -10,6 +13,9 @@ use std::sync::Arc;
use time::Date;
use tokio_postgres::types::Json;

// Lock key used when updating the projects views in the database.
const LOCK_KEY_UPDATE_PROJECTS_VIEWS: i64 = 1;

/// Type alias to represent a DB trait object.
pub(crate) type DynDB = Arc<dyn DB + Send + Sync>;

Expand Down Expand Up @@ -59,7 +65,10 @@ pub(crate) trait DB {
async fn search_projects(&self, input: &SearchProjectsInput) -> Result<(Count, JsonString)>;

/// Get some general stats.
async fn stats(&self, foundation: Option<&String>) -> Result<JsonString>;
async fn stats(&self, foundation: Option<&str>) -> Result<JsonString>;

/// Update the number of views of the projects provided.
async fn update_projects_views(&self, data: Vec<(ProjectId, Date, Total)>) -> Result<()>;
}

/// DB implementation backed by PostgreSQL.
Expand Down Expand Up @@ -195,14 +204,24 @@ impl DB for PgDB {
Ok((count, projects))
}

async fn stats(&self, foundation: Option<&String>) -> Result<JsonString> {
async fn stats(&self, foundation: Option<&str>) -> Result<JsonString> {
let db = self.pool.get().await?;
let stats = db
.query_one("select get_stats($1::text)::text", &[&foundation])
.await?
.get(0);
Ok(stats)
}

async fn update_projects_views(&self, data: Vec<(ProjectId, Date, Total)>) -> Result<()> {
let db = self.pool.get().await?;
db.execute(
"select update_projects_views($1::bigint, $2::jsonb)",
&[&LOCK_KEY_UPDATE_PROJECTS_VIEWS, &Json(&data)],
)
.await?;
Ok(())
}
}

/// Query input used when searching for projects.
Expand Down
19 changes: 17 additions & 2 deletions clomonitor-apiserver/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::filters;
use crate::db::{DynDB, SearchProjectsInput};
use crate::{
db::{DynDB, SearchProjectsInput},
views::DynVT,
};
use anyhow::Error;
use askama_axum::Template;
use axum::{
Expand All @@ -23,6 +26,7 @@ use std::{collections::HashMap, fmt::Display, sync::Arc};
use tera::{Context, Tera};
use time::{format_description, Date};
use tracing::error;
use uuid::Uuid;

/// Index HTML document cache duration.
pub const INDEX_CACHE_MAX_AGE: usize = 300;
Expand Down Expand Up @@ -361,7 +365,7 @@ pub(crate) async fn stats(
) -> impl IntoResponse {
// Get stats from database
let stats = db
.stats(params.get("foundation"))
.stats(params.get("foundation").map(|p| p.as_str()))
.await
.map_err(internal_error)?;

Expand All @@ -373,6 +377,17 @@ pub(crate) async fn stats(
.map_err(internal_error)
}

/// Handler used to track a project view.
pub(crate) async fn track_view(
State(vt): State<DynVT>,
Path(project_id): Path<Uuid>,
) -> impl IntoResponse {
match vt.read().await.track_view(project_id).await {
Ok(_) => StatusCode::NO_CONTENT,
Err(err) => internal_error(err),
}
}

/// Helper for mapping any error into a `500 Internal Server Error` response.
fn internal_error<E>(err: E) -> StatusCode
where
Expand Down
Loading