neumann_server 0.4.0

gRPC server exposing Neumann database via QueryRouter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Handlers for relational engine browsing with Memoria design system.

use std::sync::Arc;

use axum::extract::{Path, Query, State};
use maud::{html, Markup};
use serde::Deserialize;

use relational_engine::{Condition, Value};

use crate::web::templates::layout;
use crate::web::templates::layout::{
    format_number, m_breadcrumb, m_empty, m_expandable_json, m_expandable_string, m_header,
};
use crate::web::AdminContext;
use crate::web::NavItem;

/// Query parameters for pagination.
#[derive(Debug, Deserialize)]
pub struct PaginationParams {
    /// Page number (zero-indexed).
    #[serde(default)]
    pub page: usize,
    /// Number of items per page.
    #[serde(default = "default_page_size")]
    pub page_size: usize,
}

const fn default_page_size() -> usize {
    50
}

/// List all tables.
pub async fn tables_list(State(ctx): State<Arc<AdminContext>>) -> Markup {
    let tables = ctx.relational.list_tables();

    let content = html! {
        (m_header("TABLES", Some("Browse relational data structures")))

        @if tables.is_empty() {
            (m_empty("NO TABLES", "Create a table to initialize storage"))
        } @else {
            div class="m-card" {
                div class="m-card-header" { "TABLE REGISTRY" }
                div class="m-card-content p-0" {
                    table class="m-table" {
                        thead {
                            tr {
                                th { "TABLE" }
                                th { "ROWS" }
                                th { "COLUMNS" }
                            }
                        }
                        tbody {
                            @for table in &tables {
                                @let row_count = ctx.relational.row_count(table).unwrap_or(0);
                                @let schema = ctx.relational.get_schema(table);
                                @let col_count = schema.as_ref().map_or(0, |s| s.columns.len());
                                tr {
                                    td {
                                        a href=(format!("/relational/{table}")) class="text-white hover:text-neutral-300" {
                                            (table)
                                        }
                                    }
                                    td class="font-mono text-neutral-300" { (format_number(row_count)) }
                                    td class="font-mono" { (col_count) }
                                }
                            }
                        }
                    }
                }
            }

            div class="mt-4 text-sm text-neutral-400" {
                "[ " (tables.len()) " TABLE(S) REGISTERED ]"
            }
        }
    };

    layout("Tables", NavItem::Relational, content)
}

/// Show table detail with schema.
pub async fn table_detail(
    State(ctx): State<Arc<AdminContext>>,
    Path(table): Path<String>,
) -> Markup {
    let schema = ctx.relational.get_schema(&table);
    let row_count = ctx.relational.row_count(&table).unwrap_or(0);

    let content = html! {
        (m_breadcrumb(&[("/relational", "TABLES"), ("", &table)]))

        (m_header(&table.to_uppercase(), Some(&format!("{} records", format_number(row_count)))))

        @if let Ok(schema) = schema {
            // Schema section
            div class="mb-6" {
                div class="m-card" {
                    div class="m-card-header" { "SCHEMA DEFINITION" }
                    div class="m-card-content p-0" {
                        table class="m-table" {
                            thead {
                                tr {
                                    th { "COLUMN" }
                                    th { "TYPE" }
                                    th { "NULLABLE" }
                                }
                            }
                            tbody {
                                @for col in &schema.columns {
                                    tr {
                                        td class="text-white" { (col.name.clone()) }
                                        td {
                                            span class="text-neutral-300" {
                                                (format!("{:?}", col.column_type))
                                            }
                                        }
                                        td class="text-neutral-400" {
                                            @if col.nullable { "YES" } @else { "NO" }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Indexes section
            @let hash_indexes = ctx.relational.get_indexed_columns(&table);
            @let btree_indexes = ctx.relational.get_btree_indexed_columns(&table);
            div class="mb-6" {
                div class="m-card" {
                    div class="m-card-header" { "INDEXES" }
                    div class="m-card-content" {
                        @if hash_indexes.is_empty() && btree_indexes.is_empty() {
                            span class="text-neutral-500 text-sm" { "No indexes defined" }
                        } @else {
                            ul class="space-y-2" {
                                @for col in &hash_indexes {
                                    li class="flex justify-between" {
                                        span class="text-white font-mono" { (col) }
                                        span class="text-neutral-400 text-xs" { "HASH" }
                                    }
                                }
                                @for col in &btree_indexes {
                                    li class="flex justify-between" {
                                        span class="text-white font-mono" { (col) }
                                        span class="text-neutral-400 text-xs" { "BTREE" }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Browse rows link
            div {
                a href=(format!("/relational/{}/rows", table)) class="m-btn" {
                    "[ BROWSE ROWS ]"
                }
            }
        } @else {
            (m_empty("TABLE NOT FOUND", "The requested table does not exist in storage"))
        }
    };

    layout(&format!("Table: {table}"), NavItem::Relational, content)
}

/// Browse table rows with pagination.
pub async fn table_rows(
    State(ctx): State<Arc<AdminContext>>,
    Path(table): Path<String>,
    Query(params): Query<PaginationParams>,
) -> Markup {
    let schema = ctx.relational.get_schema(&table);
    let total_rows = ctx.relational.row_count(&table).unwrap_or(0);

    let page = params.page;
    let page_size = params.page_size.min(100);
    let offset = page * page_size;

    let content = html! {
        (m_breadcrumb(&[("/relational", "TABLES"), (&format!("/relational/{table}"), &table), ("", "ROWS")]))

        (m_header(&format!("{} - ROWS", table.to_uppercase()), Some(&format!("{} total records", format_number(total_rows)))))

        @if let Ok(schema) = schema {
            @let rows = ctx.relational.select(&table, Condition::True)
                .map(|r| r.into_iter().skip(offset).take(page_size).collect::<Vec<_>>())
                .unwrap_or_default();

            @if rows.is_empty() && total_rows == 0 {
                (m_empty("NO RECORDS", "This table contains no data"))
            } @else {
                div class="m-card" {
                    div class="m-card-header" { "DATA RECORDS" }
                    div class="m-card-content p-0 overflow-x-auto" {
                        table class="m-table min-w-max" {
                            thead {
                                tr {
                                    th { "#" }
                                    @for col in &schema.columns {
                                        th { (col.name.to_uppercase()) }
                                    }
                                }
                            }
                            tbody {
                                @for (idx, row) in rows.iter().enumerate() {
                                    tr {
                                        td class="text-neutral-500 font-mono" { (offset + idx + 1) }
                                        @for col in &schema.columns {
                                            td {
                                                (render_value(row.get(&col.name)))
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Pagination
                (pagination(page, page_size, total_rows, &format!("/relational/{table}/rows")))
            }
        } @else {
            (m_empty("TABLE NOT FOUND", "The requested table does not exist"))
        }
    };

    layout(&format!("{table} Rows"), NavItem::Relational, content)
}

fn render_value(value: Option<&Value>) -> Markup {
    match value {
        None => html! { span class="text-neutral-500 italic" { "null" } },
        Some(Value::Null) => html! { span class="text-neutral-500 italic" { "null" } },
        Some(Value::Int(v)) => html! { span class="text-white font-mono" { (v) } },
        Some(Value::Float(v)) => {
            html! { span class="text-white font-mono" { (format!("{v:.4}")) } }
        },
        Some(Value::String(v)) => m_expandable_string(v, 80),
        Some(Value::Bool(v)) => html! { span class="text-neutral-300" { (v) } },
        Some(Value::Bytes(v)) => {
            html! { span class="text-neutral-400" { "[" (v.len()) " bytes]" } }
        },
        Some(Value::Json(v)) => {
            let s = v.to_string();
            m_expandable_json(&s, 80)
        },
        Some(_) => html! { span class="text-neutral-400" { "?" } },
    }
}

fn pagination(page: usize, page_size: usize, total: usize, base_url: &str) -> Markup {
    let total_pages = total.div_ceil(page_size);
    let has_prev = page > 0;
    let has_next = page + 1 < total_pages;

    html! {
        div class="mt-4 flex items-center justify-between" {
            div class="text-sm text-neutral-400" {
                "SHOWING " (page * page_size + 1) " - " (((page + 1) * page_size).min(total)) " OF " (format_number(total))
            }
            div class="flex items-center gap-2" {
                @if has_prev {
                    a href=(format!("{base_url}?page={}&page_size={page_size}", page - 1))
                      class="m-btn text-sm" {
                        "[ PREV ]"
                    }
                }
                span class="px-3 py-1 text-sm text-neutral-400" {
                    "PAGE " (page + 1) " / " (total_pages.max(1))
                }
                @if has_next {
                    a href=(format!("{base_url}?page={}&page_size={page_size}", page + 1))
                      class="m-btn text-sm" {
                        "[ NEXT ]"
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_page_size() {
        assert_eq!(default_page_size(), 50);
    }

    #[test]
    fn test_pagination_params_default() {
        let params: PaginationParams = serde_json::from_str("{}").unwrap();
        assert_eq!(params.page, 0);
        assert_eq!(params.page_size, 50);
    }

    #[test]
    fn test_pagination_params_custom() {
        let params: PaginationParams =
            serde_json::from_str(r#"{"page": 5, "page_size": 25}"#).unwrap();
        assert_eq!(params.page, 5);
        assert_eq!(params.page_size, 25);
    }

    #[test]
    fn test_render_value_none() {
        let html = render_value(None).into_string();
        assert!(html.contains("null"));
    }

    #[test]
    fn test_render_value_null() {
        let html = render_value(Some(&Value::Null)).into_string();
        assert!(html.contains("null"));
    }

    #[test]
    fn test_render_value_int() {
        let html = render_value(Some(&Value::Int(42))).into_string();
        assert!(html.contains("42"));
    }

    #[test]
    fn test_render_value_negative_int() {
        let html = render_value(Some(&Value::Int(-100))).into_string();
        assert!(html.contains("-100"));
    }

    #[test]
    fn test_render_value_float() {
        let html = render_value(Some(&Value::Float(3.1415))).into_string();
        assert!(html.contains("3.1415"));
    }

    #[test]
    fn test_render_value_string() {
        let html = render_value(Some(&Value::String("hello world".to_string()))).into_string();
        assert!(html.contains("hello world"));
    }

    #[test]
    fn test_render_value_bool_true() {
        let html = render_value(Some(&Value::Bool(true))).into_string();
        assert!(html.contains("true"));
    }

    #[test]
    fn test_render_value_bool_false() {
        let html = render_value(Some(&Value::Bool(false))).into_string();
        assert!(html.contains("false"));
    }

    #[test]
    fn test_render_value_bytes() {
        let html = render_value(Some(&Value::Bytes(vec![1, 2, 3, 4, 5]))).into_string();
        assert!(html.contains("5 bytes"));
    }

    #[test]
    fn test_render_value_json() {
        let json = serde_json::json!({"key": "value"});
        let html = render_value(Some(&Value::Json(json))).into_string();
        assert!(html.contains("key"));
    }

    #[test]
    fn test_pagination_first_page() {
        let html = pagination(0, 10, 100, "/test").into_string();
        assert!(html.contains("PAGE 1"));
        assert!(html.contains("SHOWING 1 - 10"));
        assert!(!html.contains("PREV")); // No prev on first page
        assert!(html.contains("NEXT"));
    }

    #[test]
    fn test_pagination_middle_page() {
        let html = pagination(5, 10, 100, "/test").into_string();
        assert!(html.contains("PAGE 6"));
        assert!(html.contains("PREV"));
        assert!(html.contains("NEXT"));
    }

    #[test]
    fn test_pagination_last_page() {
        let html = pagination(9, 10, 100, "/test").into_string();
        assert!(html.contains("PAGE 10"));
        assert!(html.contains("PREV"));
        assert!(!html.contains("NEXT")); // No next on last page
    }

    #[test]
    fn test_pagination_single_page() {
        let html = pagination(0, 10, 5, "/test").into_string();
        assert!(html.contains("PAGE 1 / 1"));
        assert!(!html.contains("PREV"));
        assert!(!html.contains("NEXT"));
    }

    #[test]
    fn test_pagination_empty() {
        let html = pagination(0, 10, 0, "/test").into_string();
        // Should handle edge case of 0 items
        assert!(html.contains("PAGE 1"));
    }

    // ========== Handler integration tests ==========

    fn create_populated_relational_context() -> Arc<AdminContext> {
        use relational_engine::{Column, ColumnType, Schema};
        use std::collections::HashMap;

        let relational = Arc::new(relational_engine::RelationalEngine::new());
        let vector = Arc::new(vector_engine::VectorEngine::new());
        let graph = Arc::new(graph_engine::GraphEngine::new());

        let schema = Schema::new(vec![
            Column::new("id", ColumnType::Int),
            Column::new("name", ColumnType::String),
            Column::new("email", ColumnType::String),
        ]);
        relational.create_table("users", schema).unwrap();

        for i in 1..=5 {
            let mut row = HashMap::new();
            row.insert("id".to_string(), Value::Int(i));
            row.insert("name".to_string(), Value::String(format!("user_{i}")));
            row.insert(
                "email".to_string(),
                Value::String(format!("user_{i}@test.com")),
            );
            relational.insert("users", row).unwrap();
        }

        // Second table
        let schema2 = Schema::new(vec![
            Column::new("key", ColumnType::String),
            Column::new("value", ColumnType::Float),
        ]);
        relational.create_table("metrics", schema2).unwrap();

        Arc::new(AdminContext::new(relational, vector, graph))
    }

    #[tokio::test]
    async fn test_tables_list_with_data() {
        let ctx = create_populated_relational_context();
        let result = tables_list(State(ctx)).await;
        let html = result.into_string();
        assert!(html.contains("TABLE REGISTRY"));
        assert!(html.contains("users"));
        assert!(html.contains("metrics"));
    }

    #[tokio::test]
    async fn test_tables_list_empty() {
        let ctx = Arc::new(AdminContext::new(
            Arc::new(relational_engine::RelationalEngine::new()),
            Arc::new(vector_engine::VectorEngine::new()),
            Arc::new(graph_engine::GraphEngine::new()),
        ));
        let result = tables_list(State(ctx)).await;
        let html = result.into_string();
        assert!(html.contains("NO TABLES"));
    }

    #[tokio::test]
    async fn test_table_detail_with_schema() {
        let ctx = create_populated_relational_context();
        let result = table_detail(State(ctx), Path("users".to_string())).await;
        let html = result.into_string();
        assert!(html.contains("SCHEMA DEFINITION"));
        assert!(html.contains("COLUMN"));
        assert!(html.contains("TYPE"));
        assert!(html.contains("NULLABLE"));
        assert!(html.contains("id"));
        assert!(html.contains("name"));
        assert!(html.contains("email"));
        assert!(html.contains("BROWSE ROWS"));
    }

    #[tokio::test]
    async fn test_table_detail_not_found() {
        let ctx = create_populated_relational_context();
        let result = table_detail(State(ctx), Path("nonexistent".to_string())).await;
        let html = result.into_string();
        assert!(html.contains("TABLE NOT FOUND"));
    }

    #[tokio::test]
    async fn test_table_rows_with_data() {
        let ctx = create_populated_relational_context();
        let params = Query(PaginationParams {
            page: 0,
            page_size: 50,
        });
        let result = table_rows(State(ctx), Path("users".to_string()), params).await;
        let html = result.into_string();
        assert!(html.contains("ROWS"));
        assert!(html.contains("user_1"));
        assert!(html.contains("user_5"));
    }

    #[tokio::test]
    async fn test_table_rows_pagination() {
        let ctx = create_populated_relational_context();
        let params = Query(PaginationParams {
            page: 0,
            page_size: 2,
        });
        let result = table_rows(State(ctx), Path("users".to_string()), params).await;
        let html = result.into_string();
        assert!(html.contains("ROWS"));
        assert!(html.contains("NEXT"));
    }

    #[tokio::test]
    async fn test_table_rows_empty_table() {
        let ctx = create_populated_relational_context();
        let params = Query(PaginationParams {
            page: 0,
            page_size: 50,
        });
        let result = table_rows(State(ctx), Path("metrics".to_string()), params).await;
        let html = result.into_string();
        assert!(html.contains("NO RECORDS"));
    }

    #[tokio::test]
    async fn test_table_detail_no_indexes() {
        let ctx = create_populated_relational_context();
        let result = table_detail(State(ctx), Path("users".to_string())).await;
        let html = result.into_string();
        assert!(html.contains("INDEXES"));
        assert!(html.contains("No indexes defined"));
    }

    #[tokio::test]
    async fn test_table_detail_with_indexes() {
        use relational_engine::{Column, ColumnType, Schema};
        use std::collections::HashMap;

        let relational = Arc::new(relational_engine::RelationalEngine::new());
        let schema = Schema::new(vec![
            Column::new("id", ColumnType::Int),
            Column::new("name", ColumnType::String),
        ]);
        relational
            .create_table("indexed_tbl", schema)
            .expect("create");

        let mut row = HashMap::new();
        row.insert("id".to_string(), Value::Int(1));
        row.insert("name".to_string(), Value::String("alice".into()));
        relational.insert("indexed_tbl", row).expect("insert");

        relational
            .create_index("indexed_tbl", "name")
            .expect("create index");

        let mut ctx = AdminContext::new(
            relational,
            Arc::new(vector_engine::VectorEngine::new()),
            Arc::new(graph_engine::GraphEngine::new()),
        );
        ctx.blob = None;
        ctx.checkpoint = None;
        ctx.store = None;
        let ctx = Arc::new(ctx);

        let result = table_detail(State(ctx), Path("indexed_tbl".to_string())).await;
        let html = result.into_string();
        assert!(html.contains("INDEXES"));
        assert!(html.contains("name"));
        assert!(html.contains("HASH"));
    }
}