Skip to main content

gen_diff/
sample.rs

1//! Builds a graph diff between two samples.
2//!
3//! The base is the reference state and the query is compared against it. Graph
4//! elements found only in the query are additions, while elements found only
5//! in the base are removals.
6
7use gen_models::{
8    block_group::{BlockGroup, BlockGroupError},
9    block_group_edge::BlockGroupEdge,
10    db::GraphConnection,
11};
12
13use crate::graph::{DiffGenGraph, EdgeDiffInput, build_diff_graph_from_edges};
14
15/// The two sample block groups and their unified, annotated graph.
16#[derive(Clone, Debug)]
17pub struct SampleDiff {
18    /// Graph selected as the query for the comparison.
19    pub query_block_group: BlockGroup,
20    /// Graph selected as the base for the comparison.
21    pub base_block_group: BlockGroup,
22    /// Unified graph annotated relative to the base.
23    pub graph: DiffGenGraph,
24}
25
26/// Builds one graph showing the difference between matching query and base
27/// block groups.
28///
29/// The base is the comparison baseline. Query-only graph elements are added,
30/// while base-only elements are removed. The shared graph-diff builder aligns
31/// sequence boundaries before constructing either diff input, then unifies
32/// their explicitly annotated nodes and edges.
33pub fn build_sample_diff(
34    conn: &GraphConnection,
35    collection_name: &str,
36    graph_name: &str,
37    query_name: &str,
38    base_name: &str,
39    history_ref: Option<&str>,
40) -> Result<SampleDiff, BlockGroupError> {
41    let query_block_group =
42        BlockGroup::get_by_name(conn, collection_name, query_name, graph_name, history_ref)?;
43    let base_block_group =
44        BlockGroup::get_by_name(conn, collection_name, base_name, graph_name, history_ref)?;
45    let query_edges =
46        BlockGroupEdge::edges_for_block_group(conn, &query_block_group.id, history_ref);
47    let base_edges = BlockGroupEdge::edges_for_block_group(conn, &base_block_group.id, history_ref);
48    let graph = build_diff_graph_from_edges(
49        EdgeDiffInput::unattributed(&base_edges),
50        EdgeDiffInput::unattributed(&query_edges),
51        None,
52    );
53
54    Ok(SampleDiff {
55        query_block_group,
56        base_block_group,
57        graph,
58    })
59}