Skip to content

Commit

Permalink
feat(issues): initial flaw compat
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 25, 2024
1 parent 6244c4b commit dc0c131
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 25 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.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ rari-data = { path = "crates/rari-data" }
rari-templ-func = { path = "crates/rari-templ-func" }

tracing = "0.1"
tracing-subscriber = "0.3"
thiserror = "1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
Expand Down Expand Up @@ -100,6 +101,7 @@ rari-types.workspace = true
serde.workspace = true
serde_json.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
anyhow.workspace = true

self_update = { version = "0.41", default-features = false, features = [
Expand All @@ -109,7 +111,6 @@ self_update = { version = "0.41", default-features = false, features = [
] }
clap = { version = "4.5.1", features = ["derive"] }
clap-verbosity-flag = "2"
tracing-subscriber = "0.3"
tracing-log = "0.2"
tabwriter = "1"
axum = "0.7"
Expand Down
11 changes: 6 additions & 5 deletions crates/rari-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::thread::spawn;

use anyhow::{anyhow, Error};
use clap::{Args, Parser, Subcommand};
use issues::{issues_by, InMemoryLayer};
use rari_doc::build::{
build_blog_pages, build_contributor_spotlight_pages, build_curriculum_pages, build_docs,
build_generic_pages, build_spas,
};
use rari_doc::cached_readers::{read_and_cache_doc_pages, CACHED_DOC_PAGE_FILES};
use rari_doc::issues::{issues_by, InMemoryLayer};
use rari_doc::pages::types::doc::Doc;
use rari_doc::reader::read_docs_parallel;
use rari_doc::search_index::build_search_index;
Expand All @@ -35,7 +35,6 @@ use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{filter, Layer};

mod issues;
mod serve;

#[derive(Parser)]
Expand Down Expand Up @@ -167,9 +166,11 @@ fn main() -> Result<(), Error> {
.with_target("rari_doc", cli.verbose.log_level_filter().as_trace())
.with_target("rari", cli.verbose.log_level_filter().as_trace());

let memory_filter = filter::Targets::new().with_target("rari_doc", Level::WARN);
let memory_filter = filter::Targets::new()
.with_target("rari_doc", Level::WARN)
.with_target("rari", Level::WARN);

let memory_layer = InMemoryLayer::new();
let memory_layer = InMemoryLayer::default();
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
Expand Down Expand Up @@ -341,7 +342,7 @@ fn main() -> Result<(), Error> {
settings.deny_warnings = args.deny_warnings;
settings.cache_content = args.cache_content;
let _ = SETTINGS.set(settings);
serve::serve()?
serve::serve(memory_layer.clone())?
}
Commands::GitHistory => {
println!("Gathering history 📜");
Expand Down
35 changes: 30 additions & 5 deletions crates/rari-cli/serve.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
use axum::extract::Request;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;

use axum::extract::{Request, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Json, Router};
use rari_doc::error::DocError;
use rari_doc::issues::{to_display_issues, InMemoryLayer};
use rari_doc::pages::json::BuiltDocy;
use rari_doc::pages::page::{Page, PageBuilder, PageLike};
use tracing::{error, span, Level};

async fn get_json_handler(req: Request) -> Result<Json<BuiltDocy>, AppError> {
static REQ_COUNTER: AtomicU64 = AtomicU64::new(1);

async fn get_json_handler(
State(memory_layer): State<Arc<InMemoryLayer>>,
req: Request,
) -> Result<Json<BuiltDocy>, AppError> {
let req_id = REQ_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let span = span!(Level::WARN, "serve", req = req_id);
let _enter1 = span.enter();
let url = req.uri().path();
let json = get_json(url)?;
let mut json = get_json(url)?;
if let BuiltDocy::Doc(json_doc) = &mut json {
let m = memory_layer.get_events();
let mut issues = m.lock().unwrap();
let req_isses: Vec<_> = issues
.iter()
.filter(|issue| issue.req == req_id)
.cloned()
.collect();
issues.retain_mut(|i| i.req != req_id);
json_doc.doc.flaws = Some(to_display_issues(req_isses));
}
Ok(Json(json))
}

Expand Down Expand Up @@ -44,13 +67,15 @@ where
}
}

pub fn serve() -> Result<(), anyhow::Error> {
pub fn serve(memory_layer: InMemoryLayer) -> Result<(), anyhow::Error> {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let app = Router::new().fallback(get_json_handler);
let app = Router::new()
.fallback(get_json_handler)
.with_state(Arc::new(memory_layer));

let listener = tokio::net::TcpListener::bind("0.0.0.0:8083").await.unwrap();
axum::serve(listener, app).await.unwrap();
Expand Down
12 changes: 7 additions & 5 deletions crates/rari-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ license.workspace = true
rust-version.workspace = true

[dependencies]
rari-utils.workspace = true
rari-types.workspace = true
rari-md.workspace = true
rari-data.workspace = true
rari-templ-func.workspace = true

thiserror.workspace = true
serde.workspace = true
serde_json.workspace = true
Expand All @@ -17,6 +23,7 @@ itertools.workspace = true
constcat.workspace = true
indexmap.workspace = true
sha2.workspace = true
tracing-subscriber.workspace = true

serde_yaml_ng = "0.10"
yaml-rust = "0.4"
Expand Down Expand Up @@ -48,11 +55,6 @@ phf_macros = "0.11"
phf = "0.11"
unescaper = "0.1"

rari-utils.workspace = true
rari-types.workspace = true
rari-md.workspace = true
rari-data.workspace = true
rari-templ-func.workspace = true

css-syntax = { path = "../css-syntax", features = ["rari"] }

Expand Down
7 changes: 7 additions & 0 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ pub fn post_process_html<T: PageLike>(
if let Some(pos) = el.get_attribute("data-sourcepos") {
if let Some((start, _)) = pos.split_once('-') {
if let Some((line, col)) = start.split_once(':') {
let line_n =
line.parse::<usize>().map(|l| l + page.fm_offset()).ok();
let line = line_n
.map(|i| i.to_string())
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(line));
let line = line.as_ref();
tracing::warn!(
source = "redirected-link",
line = line,
Expand Down
108 changes: 99 additions & 9 deletions crates/rari-cli/issues.rs → crates/rari-doc/src/issues.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::sync::{Arc, Mutex};

Expand All @@ -9,14 +9,16 @@ use tracing::{Event, Subscriber};
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::Layer;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Issue {
pub req: u64,
pub fields: Vec<(&'static str, String)>,
pub spans: Vec<(&'static str, String)>,
}

#[derive(Debug, Default)]
pub struct IssueEntries {
req: u64,
entries: Vec<(&'static str, String)>,
}

Expand All @@ -29,6 +31,7 @@ pub struct Issues<'a> {

#[derive(Clone, Debug, Serialize)]
pub struct TemplIssue<'a> {
pub req: u64,
pub source: &'a str,
pub file: &'a str,
pub slug: &'a str,
Expand All @@ -40,6 +43,7 @@ pub struct TemplIssue<'a> {

static UNKNOWN: &str = "unknown";
static DEFAULT_TEMPL_ISSUE: TemplIssue<'static> = TemplIssue {
req: 0,
source: UNKNOWN,
file: UNKNOWN,
slug: UNKNOWN,
Expand Down Expand Up @@ -101,18 +105,12 @@ pub fn issues_by(issues: &[Issue]) -> Issues {
}
}

#[derive(Clone)]
#[derive(Clone, Default)]
pub struct InMemoryLayer {
events: Arc<Mutex<Vec<Issue>>>,
}

impl InMemoryLayer {
pub fn new() -> Self {
InMemoryLayer {
events: Arc::new(Mutex::new(Vec::new())),
}
}

pub fn get_events(&self) -> Arc<Mutex<Vec<Issue>>> {
Arc::clone(&self.events)
}
Expand All @@ -125,6 +123,11 @@ impl Visit for IssueEntries {
fn record_str(&mut self, field: &Field, value: &str) {
self.entries.push((field.name(), value.to_string()));
}
fn record_u64(&mut self, field: &Field, value: u64) {
if field.name() == "req" {
self.req = value;
}
}
}
impl Visit for Issue {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
Expand All @@ -133,6 +136,11 @@ impl Visit for Issue {
fn record_str(&mut self, field: &Field, value: &str) {
self.fields.push((field.name(), value.to_string()));
}
fn record_u64(&mut self, field: &Field, value: u64) {
if field.name() == "req" {
self.req = value;
}
}
}
impl<S> Layer<S> for InMemoryLayer
where
Expand All @@ -155,6 +163,7 @@ where
}
fn on_event(&self, event: &Event, ctx: tracing_subscriber::layer::Context<S>) {
let mut issue = Issue {
req: 0,
fields: vec![],
spans: vec![],
};
Expand All @@ -163,6 +172,9 @@ where
for span in scope {
let ext = span.extensions();
if let Some(entries) = ext.get::<IssueEntries>() {
if entries.req != 0 {
issue.req = entries.req
}
issue.spans.extend(entries.entries.iter().rev().cloned());
}
}
Expand All @@ -172,3 +184,81 @@ where
events.push(issue);
}
}

#[derive(Serialize, Debug, Default, Clone)]
#[serde(untagged)]
pub enum Additional {
BrokenLink {
href: String,
},
#[default]
None,
}

#[derive(Serialize, Debug, Default, Clone)]
pub struct DisplayIssue {
pub id: usize,
pub explanation: Option<String>,
pub suggestion: Option<String>,
pub fixable: Option<bool>,
pub fixed: bool,
pub name: String,
pub line: Option<usize>,
pub col: Option<usize>,
#[serde(flatten)]
pub additional: Additional,
}

pub type DisplayIssues = BTreeMap<&'static str, Vec<DisplayIssue>>;

impl From<Issue> for DisplayIssue {
fn from(value: Issue) -> Self {
let mut di = DisplayIssue::default();
let mut additional = HashMap::new();
for (key, value) in value.spans.into_iter().chain(value.fields.into_iter()) {
match key {
"line" => di.line = value.parse().ok(),
"col" => di.col = value.parse().ok(),
"source" => {
di.name = value;
}
"message" => di.explanation = Some(value),
"redirect" => di.suggestion = Some(value),

_ => {
additional.insert(key, value);
}
}
}
let additional = match di.name.as_str() {
"redirected-link" => {
di.fixed = false;
di.fixable = Some(true);
Additional::BrokenLink {
href: additional.remove("url").unwrap_or_default(),
}
}
_ => Additional::None,
};
di.additional = additional;
di
}
}

pub fn to_display_issues(issues: Vec<Issue>) -> DisplayIssues {
let mut map = BTreeMap::new();
for issue in issues {
let di = DisplayIssue::from(issue);
match &di.additional {
Additional::BrokenLink { .. } => {
let entry: &mut Vec<_> = map.entry("broken_links").or_default();
entry.push(di);
}
Additional::None => {
let entry: &mut Vec<_> = map.entry("unknown").or_default();
entry.push(di);
}
}
}
map
}
1 change: 1 addition & 0 deletions crates/rari-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod cached_readers;
pub mod error;
pub mod helpers;
pub mod html;
pub mod issues;
pub mod pages;
pub mod percent;
pub mod reader;
Expand Down
1 change: 1 addition & 0 deletions crates/rari-doc/src/pages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn build_doc(doc: &Doc) -> Result<BuiltDocy, DocError> {
browser_compat: doc.meta.browser_compat.clone(),
other_translations,
page_type: doc.meta.page_type,
flaws: None,
},
url: doc.meta.url.clone(),
..Default::default()
Expand Down
Loading

0 comments on commit dc0c131

Please sign in to comment.