(
state: &DashboardState,
query: &str,
limit: i64,
)
| 171 | } |
| 172 | |
| 173 | pub(crate) async fn graph_payload( |
| 174 | state: &DashboardState, |
| 175 | query: &str, |
| 176 | limit: i64, |
| 177 | ) -> Result<Value, String> { |
| 178 | let fact_rows = fetch_facts(state, query, limit).await?; |
| 179 | |
| 180 | let mut nodes: Map<String, Value> = Map::new(); |
| 181 | let mut edges: Vec<Value> = Vec::new(); |
| 182 | let mut fact_ids: Vec<i64> = Vec::new(); |
| 183 | let mut category_counts: Map<String, Value> = Map::new(); |
| 184 | |
| 185 | for fact in &fact_rows { |
| 186 | let fact_id = fact.get("fact_id").and_then(Value::as_i64).unwrap_or(0); |
| 187 | let category = fact |
| 188 | .get("category") |
| 189 | .and_then(Value::as_str) |
| 190 | .unwrap_or("general") |
| 191 | .to_string(); |
| 192 | let has_hrr = fact.get("has_hrr").and_then(Value::as_i64).unwrap_or(0) != 0; |
| 193 | fact_ids.push(fact_id); |
| 194 | |
| 195 | let fact_node = format!("fact:{fact_id}"); |
| 196 | let category_node = format!("category:{category}"); |
| 197 | let bank_node = format!("bank:{category}"); |
| 198 | |
| 199 | nodes.entry(fact_node.clone()).or_insert_with(|| { |
| 200 | json!({ |
| 201 | "id": fact_node, |
| 202 | "kind": "fact", |
| 203 | "label": format!("#{fact_id}"), |
| 204 | "fact_id": fact_id, |
| 205 | "category": category, |
| 206 | "content": fact.get("content").cloned().unwrap_or(Value::Null), |
| 207 | "trust_score": fact.get("trust_score").cloned().unwrap_or(Value::Null), |
| 208 | "retrieval_count": fact.get("retrieval_count").cloned().unwrap_or(Value::Null), |
| 209 | "helpful_count": fact.get("helpful_count").cloned().unwrap_or(Value::Null), |
| 210 | "has_hrr": has_hrr, |
| 211 | }) |
| 212 | }); |
| 213 | nodes.entry(category_node.clone()).or_insert_with(|| { |
| 214 | json!({ "id": category_node, "kind": "category", "label": category, "category": category }) |
| 215 | }); |
| 216 | edges.push(json!({ "source": category_node, "target": fact_node, "kind": "contains" })); |
| 217 | if has_hrr { |
| 218 | nodes.entry(bank_node.clone()).or_insert_with(|| { |
| 219 | json!({ "id": bank_node, "kind": "bank", "label": category, "category": category }) |
| 220 | }); |
| 221 | edges.push(json!({ "source": bank_node, "target": fact_node, "kind": "bundles" })); |
| 222 | } |
| 223 | |
| 224 | let count = category_counts |
| 225 | .get(&category) |
| 226 | .and_then(Value::as_i64) |
| 227 | .unwrap_or(0); |
| 228 | category_counts.insert(category, json!(count + 1)); |
| 229 | } |
| 230 |
no test coverage detected