rekuiper-server 1.0.0-rc4

REST API, OpenAPI routes, Prometheus metrics, and streaming hub for rekuiper
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
use crate::engine::{bootstrap_table_source, cancel_stream_sources, cancel_table_source};
use crate::state::{check_valid_name, AppState};
use axum::{
    body::Bytes,
    extract::{Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use indexmap::IndexMap;
use rekuiper_core::model::{StreamField, StreamRecord};
use rekuiper_core::{StreamDefinition, TableDefinition, MAX_HTTP_BATCH_RECORDS};
use rekuiper_sql::{Parser, StreamColumn};
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::HashMap;

#[derive(Deserialize)]
pub struct CreateStreamPayload {
    #[serde(default)]
    sql: Option<String>,
    #[serde(default)]
    name: Option<String>,
}

/// Map parsed `CREATE` columns onto stored stream/table fields.
pub fn to_stream_fields(cols: Vec<StreamColumn>) -> Vec<StreamField> {
    cols.into_iter()
        .map(|c| StreamField {
            name: c.name,
            field_type: c.data_type,
            default_value: c.default_value,
        })
        .collect()
}

/// eKuiper-style describe envelope shared by `GET` and `DESCRIBE` paths.
pub fn describe_stream(def: &StreamDefinition) -> Value {
    json!({
        "Name": def.name,
        "StreamFields": def.stream_fields,
        "Options": def.options,
        "StreamType": 0,
        "Statement": serde_json::Value::Null,
    })
}

/// eKuiper-style describe envelope for lookup tables.
pub fn describe_table(def: &TableDefinition) -> Value {
    json!({
        "Name": def.name,
        "StreamFields": def.stream_fields,
        "Options": def.options,
        "StreamType": 1,
        "Statement": serde_json::Value::Null,
    })
}

fn validate_video_options(
    options: &HashMap<String, String>,
    source_configs: &std::sync::Arc<parking_lot::RwLock<HashMap<String, Value>>>,
) -> Result<(), String> {
    if !options
        .iter()
        .any(|(k, v)| k.eq_ignore_ascii_case("type") && v.eq_ignore_ascii_case("video"))
    {
        return Ok(());
    }
    let mut vid_cfg = rekuiper_connectors::VideoSourceConfig::default();
    if let Some(conf_key) = options
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("conf_key") || k.eq_ignore_ascii_case("confkey"))
        .map(|(_, v)| v.trim())
        .filter(|s| !s.is_empty())
    {
        let lookup = format!("video/{}", conf_key);
        let configs_guard = source_configs.read();
        let conf_val = configs_guard
            .get(&lookup)
            .or_else(|| configs_guard.get(conf_key))
            .cloned();
        drop(configs_guard);
        if let Some(val) = conf_val {
            if let Ok(c) = serde_json::from_value::<rekuiper_connectors::VideoSourceConfig>(val) {
                vid_cfg = c;
            }
        }
    }
    if let Some(url) = options
        .iter()
        .find(|(k, _)| {
            k.eq_ignore_ascii_case("url")
                || k.eq_ignore_ascii_case("server")
                || k.eq_ignore_ascii_case("datasource")
        })
        .map(|(_, v)| v.trim())
        .filter(|s| !s.is_empty())
    {
        vid_cfg.url = url.to_string();
    }
    if let Some(interval) = options
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("interval"))
        .map(|(_, v)| v.trim())
        .filter(|s| !s.is_empty())
    {
        vid_cfg.set_interval_str(interval);
    }
    if let Some(fp) = options
        .iter()
        .find(|(k, _)| {
            k.eq_ignore_ascii_case("ffmpeg_path") || k.eq_ignore_ascii_case("ffmpegpath")
        })
        .map(|(_, v)| v.trim())
        .filter(|s| !s.is_empty())
    {
        vid_cfg.ffmpeg_path = fp.to_string();
    }
    vid_cfg.validate()
}

pub async fn list_streams(State(state): State<AppState>) -> impl IntoResponse {
    let streams = state.stream_manager.list_streams();
    Json(streams)
}

pub async fn create_stream(
    State(state): State<AppState>,
    Json(payload): Json<CreateStreamPayload>,
) -> Response {
    if let Some(sql) = payload.sql {
        // Stream management statements run inline: SHOW STREAMS lists names,
        // DESCRIBE STREAM reports one definition (both answer 201).
        let mut words = sql.split_whitespace();
        let head = (
            words.next().map(|w| w.to_ascii_uppercase()),
            words.next().map(|w| w.to_ascii_uppercase()),
        );
        if head == (Some("SHOW".to_string()), Some("STREAMS".to_string())) {
            return (
                StatusCode::CREATED,
                Json(state.stream_manager.list_streams()),
            )
                .into_response();
        }
        if head == (Some("DESCRIBE".to_string()), Some("STREAM".to_string())) {
            let target = words
                .next()
                .unwrap_or("")
                .trim_matches(['"', '\'', '`', ';']);
            if target.is_empty() {
                return (
                    StatusCode::BAD_REQUEST,
                    "Missing stream name in DESCRIBE STREAM",
                )
                    .into_response();
            }
            if let Some(def) = state.stream_manager.get_stream(target) {
                return (StatusCode::CREATED, Json(describe_stream(&def))).into_response();
            }
            return (
                StatusCode::BAD_REQUEST,
                Json(json!({
                    "error": 3000,
                    "message": format!(
                        "describe stream error: Describe stream fails, {} is not found.",
                        target
                    )
                })),
            )
                .into_response();
        }
        let mut parser = Parser::new(&sql);
        match parser.parse_create_stream() {
            Ok(stmt) => {
                for (k, v) in &stmt.options {
                    if k.eq_ignore_ascii_case("buffer_full_policy")
                        && !v.eq_ignore_ascii_case("block")
                        && !v.eq_ignore_ascii_case("dropOldest")
                    {
                        return (
                            StatusCode::BAD_REQUEST,
                            format!(
                                "Invalid buffer_full_policy: '{}', must be 'block' or 'dropOldest'",
                                v
                            ),
                        )
                            .into_response();
                    }
                    if (k.eq_ignore_ascii_case("offset_reset")
                        || k.eq_ignore_ascii_case("offsetreset"))
                        && !v.eq_ignore_ascii_case("earliest")
                        && !v.eq_ignore_ascii_case("latest")
                    {
                        return (
                            StatusCode::BAD_REQUEST,
                            format!(
                                "Invalid offset_reset: '{}', must be 'earliest' or 'latest'",
                                v
                            ),
                        )
                            .into_response();
                    }
                }
                if let Err(e) = validate_video_options(&stmt.options, &state.source_configs) {
                    return (
                        StatusCode::BAD_REQUEST,
                        Json(json!({
                            "error": 3000,
                            "message": format!("Stream command error: {}", e)
                        })),
                    )
                        .into_response();
                }
                let stream_def = StreamDefinition {
                    name: stmt.name.clone(),
                    sql: sql.clone(),
                    stream_fields: to_stream_fields(stmt.fields),
                    options: stmt.options,
                };
                if let Err(e) = state.stream_manager.create_stream(stream_def).await {
                    let msg = if e.to_string().contains("already exists") {
                        format!(
                            "Stream command error: Create stream fails: Item {} already exists.",
                            stmt.name
                        )
                    } else {
                        format!("Stream command error: {}", e)
                    };
                    return (
                        StatusCode::BAD_REQUEST,
                        Json(json!({
                            "error": 3000,
                            "message": msg
                        })),
                    )
                        .into_response();
                }
                state.stream_bus.get_or_create(&stmt.name);
                (
                    StatusCode::CREATED,
                    format!("Stream {} is created.", stmt.name),
                )
                    .into_response()
            }
            Err(e) => (
                StatusCode::BAD_REQUEST,
                Json(json!({
                    "error": 3000,
                    "message": format!("Stream command error: {}", e)
                })),
            )
                .into_response(),
        }
    } else if let Some(name) = payload.name {
        let stream_def = StreamDefinition {
            name: name.clone(),
            sql: "".to_string(),
            stream_fields: Vec::new(),
            options: HashMap::new(),
        };
        if let Err(e) = state.stream_manager.create_stream(stream_def).await {
            let msg = if e.to_string().contains("already exists") {
                format!(
                    "Stream command error: Create stream fails: Item {} already exists.",
                    name
                )
            } else {
                format!("Stream command error: {}", e)
            };
            return (
                StatusCode::BAD_REQUEST,
                Json(json!({
                    "error": 3000,
                    "message": msg
                })),
            )
                .into_response();
        }
        state.stream_bus.get_or_create(&name);
        (StatusCode::CREATED, format!("Stream {} is created.", name)).into_response()
    } else {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": "Missing sql or name in request"
            })),
        )
            .into_response()
    }
}

pub async fn get_stream(State(state): State<AppState>, Path(name): Path<String>) -> Response {
    if let Err(resp) = check_valid_name(&name) {
        return resp;
    }
    if let Some(def) = state.stream_manager.get_stream(&name) {
        Json(describe_stream(&def)).into_response()
    } else {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": format!(
                    "describe stream error: Describe stream fails, {} is not found.",
                    name
                )
            })),
        )
            .into_response()
    }
}

pub async fn delete_stream(State(state): State<AppState>, Path(name): Path<String>) -> Response {
    if let Err(resp) = check_valid_name(&name) {
        return resp;
    }
    match state.stream_manager.delete_stream(&name).await {
        Ok(_) => {
            cancel_stream_sources(&state, &name);
            (StatusCode::OK, format!("Stream {} is dropped.", name)).into_response()
        }
        Err(e) => (StatusCode::NOT_FOUND, e.to_string()).into_response(),
    }
}

/// Replace a stream definition (eKuiper `PUT /streams/:name`). Accepts raw
/// `CREATE STREAM ...` DDL or a JSON envelope carrying `sql`; the path name
/// is canonical. Missing streams 404 instead of being silently created.
pub async fn update_stream(
    State(state): State<AppState>,
    Path(name): Path<String>,
    body: Bytes,
) -> Response {
    if let Err(resp) = check_valid_name(&name) {
        return resp;
    }
    if state.stream_manager.get_stream(&name).is_none() {
        return (StatusCode::NOT_FOUND, format!("Stream {} not found", name)).into_response();
    }
    if body.is_empty() {
        return (StatusCode::BAD_REQUEST, "Missing stream definition").into_response();
    }
    let sql = match serde_json::from_slice::<Value>(&body) {
        Ok(Value::Object(map)) => match map.get("sql").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => return (StatusCode::BAD_REQUEST, "Missing sql in request").into_response(),
        },
        Ok(_) => return (StatusCode::BAD_REQUEST, "Missing sql in request").into_response(),
        Err(_) => match String::from_utf8(body.to_vec()) {
            Ok(s) => s,
            Err(_) => {
                return (StatusCode::BAD_REQUEST, "Invalid stream definition").into_response();
            }
        },
    };
    let mut parser = Parser::new(&sql);
    let stmt = match parser.parse_create_stream() {
        Ok(s) => s,
        Err(e) => return (StatusCode::BAD_REQUEST, format!("Invalid SQL: {}", e)).into_response(),
    };
    for (k, v) in &stmt.options {
        if k.eq_ignore_ascii_case("buffer_full_policy")
            && !v.eq_ignore_ascii_case("block")
            && !v.eq_ignore_ascii_case("dropOldest")
        {
            return (
                StatusCode::BAD_REQUEST,
                format!(
                    "Invalid buffer_full_policy: '{}', must be 'block' or 'dropOldest'",
                    v
                ),
            )
                .into_response();
        }
        if (k.eq_ignore_ascii_case("offset_reset") || k.eq_ignore_ascii_case("offsetreset"))
            && !v.eq_ignore_ascii_case("earliest")
            && !v.eq_ignore_ascii_case("latest")
        {
            return (
                StatusCode::BAD_REQUEST,
                format!(
                    "Invalid offset_reset: '{}', must be 'earliest' or 'latest'",
                    v
                ),
            )
                .into_response();
        }
    }
    if let Err(e) = validate_video_options(&stmt.options, &state.source_configs) {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": format!("Stream command error: {}", e)
            })),
        )
            .into_response();
    }
    let stream_def = StreamDefinition {
        name: name.clone(),
        sql: sql.clone(),
        stream_fields: to_stream_fields(stmt.fields),
        options: stmt.options,
    };
    if let Err(e) = state.stream_manager.update_stream(stream_def).await {
        return (StatusCode::BAD_REQUEST, e.to_string()).into_response();
    }
    state.stream_bus.get_or_create(&name);
    (StatusCode::OK, format!("Stream {} is replaced.", name)).into_response()
}

/// HTTP push source following the eKuiper REST API.
///
/// Bounded lossless admission: the whole request is decoded and validated
/// BEFORE any record is admitted; the stream handle is resolved once; then
/// the batch is admitted with reserve-then-commit, accept-once semantics
/// (`StreamBus::publish_batch`): records stay in order and contiguous on
/// every subscriber, and the handler awaits downstream capacity. A `2xx`
/// means accepted into the in-memory pipeline, not durable delivery.
/// Batches larger than `MAX_HTTP_BATCH_RECORDS` are rejected with `413` and
/// malformed batches with `400`, both before admission. A `503` is returned
/// only when every subscriber closed before any record was committed, so
/// every non-2xx response admitted zero records and is safe to retry. There
/// is no timeout-based partial failure: overload manifests as slower `2xx`
/// responses, never silent loss with success acknowledgements.
pub async fn push_stream_data(
    State(state): State<AppState>,
    Path(name): Path<String>,
    req: axum::extract::Request,
) -> Response {
    let stream_def = match state.stream_manager.get_stream(&name) {
        Some(s) => s,
        None => {
            return (StatusCode::NOT_FOUND, format!("Stream {} not found", name)).into_response()
        }
    };

    let body_bytes = match axum::body::to_bytes(req.into_body(), 10 * 1024 * 1024).await {
        Ok(b) => b,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                format!("Fail to read request body: {}\n", e),
            )
                .into_response();
        }
    };

    let format = stream_def
        .options
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("FORMAT"))
        .map(|(_, v)| v.trim())
        .unwrap_or("json")
        .to_ascii_lowercase();

    let mut records = Vec::new();

    if format == "binary" {
        let val = if let Ok(json_val) = serde_json::from_slice::<Value>(&body_bytes) {
            match json_val {
                Value::Object(map) => {
                    let mut data: HashMap<String, Value> = map.into_iter().collect();
                    if !data.contains_key("self") {
                        if let Some((_, v)) = data.iter().next() {
                            data.insert("self".to_string(), v.clone());
                        }
                    }
                    data
                }
                Value::String(s) => {
                    let mut data = HashMap::new();
                    data.insert("self".to_string(), Value::String(s));
                    data
                }
                other => {
                    let mut data = HashMap::new();
                    data.insert("self".to_string(), other);
                    data
                }
            }
        } else {
            let str_val = match std::str::from_utf8(&body_bytes) {
                Ok(s) => s.to_string(),
                Err(_) => {
                    use base64::Engine as _;
                    base64::engine::general_purpose::STANDARD.encode(&body_bytes)
                }
            };
            let mut data = HashMap::new();
            data.insert("self".to_string(), Value::String(str_val));
            data
        };
        let mut data = val;
        if stream_def.stream_fields.len() == 1 {
            let col = &stream_def.stream_fields[0].name;
            if let Some(v) = data.get("self").cloned() {
                data.entry(col.clone()).or_insert(v);
            }
        }
        records.push(StreamRecord::new(data));
    } else if format == "delimited" || format == "csv" {
        let delimiter = stream_def
            .options
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("DELIMITER"))
            .map(|(_, v)| rekuiper_connectors::DelimitedCodec::delimiter_from_name(v))
            .unwrap_or(',');
        let headers: Vec<String> = stream_def
            .stream_fields
            .iter()
            .map(|f| f.name.clone())
            .collect();
        let codec = rekuiper_connectors::DelimitedCodec::new(delimiter, headers);

        if let Ok(Value::Array(items)) = serde_json::from_slice::<Value>(&body_bytes) {
            for item in items {
                if let Value::Object(map) = item {
                    records.push(StreamRecord::new(
                        map.into_iter().collect::<IndexMap<_, _>>(),
                    ));
                }
            }
        } else if let Ok(Value::Object(map)) = serde_json::from_slice::<Value>(&body_bytes) {
            records.push(StreamRecord::new(
                map.into_iter().collect::<IndexMap<_, _>>(),
            ));
        } else if let Ok(text) = std::str::from_utf8(&body_bytes) {
            for line in text.lines() {
                let line = line.trim();
                if !line.is_empty() {
                    let row = codec.decode_row(line);
                    records.push(StreamRecord::new(row));
                }
            }
        } else {
            return (
                StatusCode::BAD_REQUEST,
                "Fail to decode delimited payload as UTF-8 string or JSON",
            )
                .into_response();
        }
    } else {
        // Standard JSON format
        let payload: Value = match serde_json::from_slice(&body_bytes) {
            Ok(v) => v,
            Err(_) => {
                return (
                    StatusCode::BAD_REQUEST,
                    "Expected a JSON object or an array of JSON objects",
                )
                    .into_response();
            }
        };

        let objects: Vec<Value> = match payload {
            Value::Array(items) => items,
            Value::Object(_) => vec![payload],
            _ => {
                return (
                    StatusCode::BAD_REQUEST,
                    "Expected a JSON object or an array of JSON objects",
                )
                    .into_response();
            }
        };

        if objects.len() > MAX_HTTP_BATCH_RECORDS {
            return (
                StatusCode::PAYLOAD_TOO_LARGE,
                format!(
                    "Batch of {} records exceeds limit of {}",
                    objects.len(),
                    MAX_HTTP_BATCH_RECORDS
                ),
            )
                .into_response();
        }

        for item in objects {
            let Value::Object(map) = item else {
                return (
                    StatusCode::BAD_REQUEST,
                    "Expected a JSON object or an array of JSON objects",
                )
                    .into_response();
            };
            let data: HashMap<String, Value> = map.into_iter().collect();
            records.push(StreamRecord::new(data));
        }
    }

    if records.len() > MAX_HTTP_BATCH_RECORDS {
        return (
            StatusCode::PAYLOAD_TOO_LARGE,
            format!(
                "Batch of {} records exceeds limit of {}",
                records.len(),
                MAX_HTTP_BATCH_RECORDS
            ),
        )
            .into_response();
    }

    // Resolve the stream handle once per request
    let sender = state.stream_bus.get_or_create(&name);
    if records.is_empty() {
        return (StatusCode::OK, "Data ingested successfully.\n").into_response();
    }
    let _ = sender.send_batch(records).await;
    (StatusCode::OK, "Data ingested successfully.\n").into_response()
}

pub async fn list_tables(State(state): State<AppState>) -> impl IntoResponse {
    let tables = state.table_manager.list_tables();
    Json(tables)
}

/// Table lookup ingestion: accepts a single JSON object or an array of
/// objects and appends each as a lookup row for the table.
pub async fn push_table_data(
    State(state): State<AppState>,
    Path(name): Path<String>,
    Json(payload): Json<Value>,
) -> Response {
    let objects: Vec<Value> = match payload {
        Value::Array(items) => items,
        Value::Object(_) => vec![payload],
        _ => {
            return (
                StatusCode::BAD_REQUEST,
                "Expected a JSON object or an array of JSON objects",
            )
                .into_response();
        }
    };

    for item in objects {
        let Value::Object(map) = item else {
            return (
                StatusCode::BAD_REQUEST,
                "Expected a JSON object or an array of JSON objects",
            )
                .into_response();
        };
        let row: HashMap<String, Value> = map.into_iter().collect();
        state.table_manager.insert_table_row(&name, row);
    }

    (StatusCode::OK, "Table data ingested successfully.\n").into_response()
}

pub async fn create_table(
    State(state): State<AppState>,
    Json(payload): Json<CreateStreamPayload>,
) -> Response {
    let Some(sql) = payload.sql else {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": "Missing sql in request"
            })),
        )
            .into_response();
    };
    let mut parser = Parser::new(&sql);
    match parser.parse_create_table() {
        Ok(stmt) => {
            let table_def = TableDefinition {
                name: stmt.name.clone(),
                sql: sql.clone(),
                stream_fields: to_stream_fields(stmt.fields),
                options: stmt.options,
            };
            let table_name = stmt.name.clone();
            if let Err(e) = state.table_manager.create_table(table_def).await {
                let msg = if e.to_string().contains("already exists") {
                    format!(
                        "Table command error: Create table fails: Item {} already exists.",
                        table_name
                    )
                } else {
                    format!("Table command error: {}", e)
                };
                return (
                    StatusCode::BAD_REQUEST,
                    Json(json!({
                        "error": 3000,
                        "message": msg
                    })),
                )
                    .into_response();
            }
            bootstrap_table_source(&state, &table_name);
            (
                StatusCode::CREATED,
                format!("Table {} is created.", table_name),
            )
                .into_response()
        }
        Err(e) => (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": format!("Table command error: {}", e)
            })),
        )
            .into_response(),
    }
}

pub async fn get_table(State(state): State<AppState>, Path(name): Path<String>) -> Response {
    if let Err(resp) = check_valid_name(&name) {
        return resp;
    }
    if let Some(def) = state.table_manager.get_table(&name) {
        Json(describe_table(&def)).into_response()
    } else {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": format!(
                    "describe table error: Describe table fails, {} is not found.",
                    name
                )
            })),
        )
            .into_response()
    }
}

pub async fn delete_table(State(state): State<AppState>, Path(name): Path<String>) -> Response {
    if let Err(resp) = check_valid_name(&name) {
        return resp;
    }
    cancel_table_source(&state, &name);
    match state.table_manager.delete_table(&name).await {
        Ok(_) => (StatusCode::OK, format!("Table {} is dropped.", name)).into_response(),
        Err(e) => (StatusCode::NOT_FOUND, e.to_string()).into_response(),
    }
}

/// Replace a table definition (eKuiper `PUT /tables/:name`). Accepts raw
/// `CREATE TABLE ...` DDL or a JSON envelope carrying `sql`; the path name
/// is canonical. Missing tables 404 instead of being silently created.
pub async fn update_table(
    State(state): State<AppState>,
    Path(name): Path<String>,
    body: Bytes,
) -> Response {
    if let Err(resp) = check_valid_name(&name) {
        return resp;
    }
    if state.table_manager.get_table(&name).is_none() {
        return (StatusCode::NOT_FOUND, format!("Table {} not found", name)).into_response();
    }
    if body.is_empty() {
        return (StatusCode::BAD_REQUEST, "Missing table definition").into_response();
    }
    let sql = match serde_json::from_slice::<Value>(&body) {
        Ok(Value::Object(map)) => match map.get("sql").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => return (StatusCode::BAD_REQUEST, "Missing sql in request").into_response(),
        },
        Ok(_) => return (StatusCode::BAD_REQUEST, "Missing sql in request").into_response(),
        Err(_) => match String::from_utf8(body.to_vec()) {
            Ok(s) => s,
            Err(_) => {
                return (StatusCode::BAD_REQUEST, "Invalid table definition").into_response();
            }
        },
    };
    let mut parser = Parser::new(&sql);
    let stmt = match parser.parse_create_table() {
        Ok(s) => s,
        Err(e) => return (StatusCode::BAD_REQUEST, format!("Invalid SQL: {}", e)).into_response(),
    };
    let table_def = TableDefinition {
        name: name.clone(),
        sql: sql.clone(),
        stream_fields: to_stream_fields(stmt.fields),
        options: stmt.options,
    };
    cancel_table_source(&state, &name);
    if let Err(e) = state.table_manager.update_table(table_def).await {
        return (StatusCode::BAD_REQUEST, e.to_string()).into_response();
    }
    bootstrap_table_source(&state, &name);
    (StatusCode::OK, format!("Table {} is replaced.", name)).into_response()
}

pub async fn get_table_details(State(state): State<AppState>) -> impl IntoResponse {
    let defs = state.table_manager.list_table_definitions();
    let summaries: Vec<Value> = defs
        .into_iter()
        .map(|def| {
            let table_type = def
                .options
                .iter()
                .find(|(k, _)| k.eq_ignore_ascii_case("TYPE"))
                .map(|(_, v)| v.to_ascii_lowercase())
                .unwrap_or_else(|| "memory".to_string());
            let format = def
                .options
                .iter()
                .find(|(k, _)| k.eq_ignore_ascii_case("FORMAT"))
                .map(|(_, v)| v.to_ascii_lowercase())
                .unwrap_or_else(|| "json".to_string());
            json!({
                "name": def.name,
                "type": table_type,
                "format": format,
            })
        })
        .collect();
    Json(summaries)
}

pub async fn get_stream_details(State(state): State<AppState>) -> impl IntoResponse {
    let names = state.stream_manager.list_streams();
    let summaries: Vec<Value> = names
        .iter()
        .filter_map(|n| state.stream_manager.get_stream(n))
        .map(|def| {
            let stream_type = def
                .options
                .iter()
                .find(|(k, _)| k.eq_ignore_ascii_case("TYPE"))
                .map(|(_, v)| v.to_ascii_lowercase())
                .unwrap_or_else(|| "mqtt".to_string());
            let format = def
                .options
                .iter()
                .find(|(k, _)| k.eq_ignore_ascii_case("FORMAT"))
                .map(|(_, v)| v.to_ascii_lowercase())
                .unwrap_or_else(|| "json".to_string());
            json!({
                "name": def.name,
                "type": stream_type,
                "format": format,
            })
        })
        .collect();
    Json(summaries)
}

/// Field-type map for a describe subject: `{name: {type, index}}`.
pub fn field_schema_map(fields: &[StreamField]) -> Value {
    let mut schema_map = serde_json::Map::new();
    for (idx, field) in fields.iter().enumerate() {
        schema_map.insert(
            field.name.clone(),
            json!({
                "type": field.field_type.to_ascii_lowercase(),
                "index": idx
            }),
        );
    }
    Value::Object(schema_map)
}

pub async fn get_stream_schema(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Response {
    if let Some(def) = state.stream_manager.get_stream(&name) {
        Json(field_schema_map(&def.stream_fields)).into_response()
    } else {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": format!(
                    "describe stream error: Describe stream fails, {} is not found.",
                    name
                )
            })),
        )
            .into_response()
    }
}

pub async fn get_table_schema(State(state): State<AppState>, Path(name): Path<String>) -> Response {
    if let Some(def) = state.table_manager.get_table(&name) {
        Json(field_schema_map(&def.stream_fields)).into_response()
    } else {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": 3000,
                "message": format!(
                    "describe table error: Describe table fails, {} is not found.",
                    name
                )
            })),
        )
            .into_response()
    }
}

pub async fn push_otlp_data(
    State(state): State<AppState>,
    req: axum::extract::Request,
) -> Response {
    let uri = req.uri();
    let query_stream = uri.query().and_then(|q| {
        q.split('&').find_map(|pair| {
            let (k, v) = pair.split_once('=')?;
            if k.eq_ignore_ascii_case("stream") {
                Some(v.to_string())
            } else {
                None
            }
        })
    });
    let header_stream = req
        .headers()
        .get("x-rekuiper-stream")
        .and_then(|v| v.to_str().ok().map(|s| s.to_string()));

    let target_stream = query_stream.or(header_stream).unwrap_or_else(|| {
        for name in state.stream_manager.list_streams() {
            if let Some(def) = state.stream_manager.get_stream(&name) {
                if def.options.iter().any(|(k, v)| {
                    k.eq_ignore_ascii_case("TYPE")
                        && (v.eq_ignore_ascii_case("otlp")
                            || v.eq_ignore_ascii_case("opentelemetry"))
                }) {
                    return name;
                }
            }
        }
        "otlp".to_string()
    });

    let body_bytes = match axum::body::to_bytes(req.into_body(), 10 * 1024 * 1024).await {
        Ok(b) => b,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                format!("Fail to read request body: {}\n", e),
            )
                .into_response();
        }
    };

    let stream_tx = state.stream_bus.get_or_create(&target_stream);

    match rekuiper_connectors::parse_otlp_payload(&body_bytes) {
        Ok(records) => {
            for rec in records {
                let _ = stream_tx.send(rec).await;
            }
            (
                StatusCode::OK,
                [("content-type", "application/json")],
                "{\"partialSuccess\":{}}\n",
            )
                .into_response()
        }
        Err(e) => {
            // Push synthetic error record so rules with onError: dlq capture it under DlqStage::CodecDecode
            let mut err_data = HashMap::new();
            err_data.insert("__raw_error__".to_string(), json!(e.to_string()));
            err_data.insert(
                "__raw_payload__".to_string(),
                json!(String::from_utf8_lossy(&body_bytes).to_string()),
            );
            let _ = stream_tx
                .send(rekuiper_core::model::StreamRecord::new(err_data))
                .await;

            (
                StatusCode::BAD_REQUEST,
                [("content-type", "application/json")],
                format!("{{\"error\":\"{}\"}}\n", e),
            )
                .into_response()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::AppState;
    use rekuiper_core::{RuleManager, StreamBus, StreamManager, TableManager};

    #[tokio::test]
    async fn test_create_stream_rejects_disallowed_video_url() {
        let bus = StreamBus::new();
        let state = AppState::new(
            "test".to_string(),
            rekuiper_conf::KuiperConfig::default(),
            StreamManager::new(),
            TableManager::new(),
            RuleManager::new(bus.clone()),
            bus,
        );
        let payload = CreateStreamPayload {
            sql: Some(
                "CREATE STREAM v_bad () WITH (TYPE=\"video\", URL=\"/etc/passwd\")".to_string(),
            ),
            name: None,
        };
        let resp = create_stream(State(state), Json(payload)).await;
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn test_create_stream_accepts_valid_video_url() {
        let bus = StreamBus::new();
        let state = AppState::new(
            "test".to_string(),
            rekuiper_conf::KuiperConfig::default(),
            StreamManager::new(),
            TableManager::new(),
            RuleManager::new(bus.clone()),
            bus,
        );
        let payload = CreateStreamPayload {
            sql: Some(
                "CREATE STREAM v_good () WITH (TYPE=\"video\", URL=\"rtsp://127.0.0.1:8554/live\")"
                    .to_string(),
            ),
            name: None,
        };
        let resp = create_stream(State(state), Json(payload)).await;
        assert_eq!(resp.status(), StatusCode::CREATED);
    }

    #[tokio::test]
    async fn test_update_stream_rejects_disallowed_video_url() {
        let bus = StreamBus::new();
        let state = AppState::new(
            "test".to_string(),
            rekuiper_conf::KuiperConfig::default(),
            StreamManager::new(),
            TableManager::new(),
            RuleManager::new(bus.clone()),
            bus,
        );
        let create_payload = CreateStreamPayload {
            sql: Some(
                "CREATE STREAM v_up () WITH (TYPE=\"video\", URL=\"rtsp://127.0.0.1:8554/live\")"
                    .to_string(),
            ),
            name: None,
        };
        let resp = create_stream(State(state.clone()), Json(create_payload)).await;
        assert_eq!(resp.status(), StatusCode::CREATED);

        let update_body = axum::body::Bytes::from(
            json!({
                "sql": "CREATE STREAM v_up () WITH (TYPE=\"video\", URL=\"/etc/shadow\")"
            })
            .to_string(),
        );
        let update_resp = update_stream(State(state), Path("v_up".to_string()), update_body).await;
        assert_eq!(update_resp.status(), StatusCode::BAD_REQUEST);
    }
}