Skip to content

Commit

Permalink
feat: store GraphViz query graphs in a separate folder (#4720)
Browse files Browse the repository at this point in the history
  • Loading branch information
laplab committed Feb 26, 2024
1 parent 45a92dc commit 58b338e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ package-lock.json

# Human readable Wasm output
*.wat

# Folder with GraphViz representation of query graphs.
.query_graphs
46 changes: 34 additions & 12 deletions query-engine/core/src/query_graph_builder/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
use std::{
fmt,
sync::atomic::{AtomicU32, Ordering},
};

use chrono::Local;
use once_cell::sync::Lazy;
use std::{fmt, fs::File, io::Write};

use super::*;
use crate::{query_document::*, query_graph::*, schema::*, IrSerializer};

pub static PRISMA_RENDER_DOT_FILE: Lazy<bool> = Lazy::new(|| match std::env::var("PRISMA_RENDER_DOT_FILE") {
Ok(enabled) => enabled == *("true") || enabled == *("1"),
Err(_) => false,
static PRISMA_DOT_PATH: Lazy<Option<String>> = Lazy::new(|| {
// Query graphs are saved only if `PRISMA_RENDER_DOT_FILE` env variable is set.
if !matches!(std::env::var("PRISMA_RENDER_DOT_FILE").as_deref(), Ok("true") | Ok("1")) {
return None;
}
// If `WORKSPACE_ROOT` env variable is defined, we save query graphs there. This ensures that
// there is a single central place to store query graphs, no matter which target is running.
// If this env variable is not defined, we save query graphs in the current directory.
let base_path = std::env::var("WORKSPACE_ROOT")
.ok()
.filter(|path| !path.is_empty())
.unwrap_or(".".into());
let time = Local::now().format("%Y-%m-%d %H:%M:%S");
let path = format!("{base_path}/.query_graphs/{time}");
std::fs::create_dir_all(&path).expect("Could not create directory to store query graphs");
Some(path)
});

pub struct QueryGraphBuilder<'a> {
Expand Down Expand Up @@ -57,6 +75,18 @@ impl<'a> QueryGraphBuilder<'a> {

if field_pair.schema_field.query_info().is_some() {
let graph = self.dispatch_build(field_pair)?;

// Used to debug generated graph.
if let Some(path) = &*PRISMA_DOT_PATH {
static COUNTER: AtomicU32 = AtomicU32::new(1);
let current = COUNTER.fetch_add(1, Ordering::Relaxed);
std::fs::write(
format!("{}/{}_{}.graphviz", path, current, serializer.key),
graph.to_graphviz(),
)
.unwrap();
}

Ok((graph, serializer))
} else {
Err(QueryGraphBuilderError::SchemaError(format!(
Expand Down Expand Up @@ -101,14 +131,6 @@ impl<'a> QueryGraphBuilder<'a> {
graph.finalize(self.query_schema.capabilities())?;
trace!("{}", graph);

// Used to debug generated graph.
if *PRISMA_RENDER_DOT_FILE {
let mut f = File::create("graph.dot").unwrap();
let output = graph.to_graphviz();

f.write_all(output.as_bytes()).unwrap();
}

Ok(graph)
}

Expand Down

0 comments on commit 58b338e

Please sign in to comment.