Skip to main content

couchbase_core/analyticsx/
query_result.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use std::fmt::{Display, Formatter};
20use std::time::Duration;
21
22use serde::Deserialize;
23use serde_json::value::RawValue;
24
25#[derive(Debug, Clone, Copy, Deserialize, Ord, PartialOrd, Eq, PartialEq, Hash)]
26#[serde(rename_all = "lowercase")]
27#[non_exhaustive]
28pub enum Status {
29    Running,
30    Success,
31    Timeout,
32    Fatal,
33    Failed,
34    Unknown,
35}
36
37impl Display for Status {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Status::Running => write!(f, "running"),
41            Status::Success => write!(f, "success"),
42            Status::Timeout => write!(f, "timeout"),
43            Status::Fatal => write!(f, "fatal"),
44            Status::Failed => write!(f, "failed"),
45            Status::Unknown => write!(f, "unknown"),
46        }
47    }
48}
49
50#[derive(Debug, Clone)]
51pub struct MetadataPlans {
52    pub logical_plan: Option<Box<RawValue>>,
53    pub optimized_logical_plan: Option<Box<RawValue>>,
54    pub rewritten_expression_tree: Option<String>,
55    pub expression_tree: Option<String>,
56    pub job: Option<Box<RawValue>>,
57}
58
59#[derive(Debug, Clone)]
60pub struct MetaData {
61    pub request_id: Option<String>,
62    pub client_context_id: Option<String>,
63    pub status: Status,
64    pub warnings: Vec<Warning>,
65    pub metrics: Option<Metrics>,
66    pub signature: Option<Box<RawValue>>,
67    pub plans: Option<MetadataPlans>,
68}
69
70impl Display for MetaData {
71    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
72        write!(
73            f,
74            "request_id: {:?}, client_context_id: {:?}, status: {}, metrics: {:?}, signature: {:?}, warnings: {:?}, plans: {:?}",
75            self.request_id,
76            self.client_context_id,
77            self.status,
78            self.metrics,
79            self.signature,
80            self.warnings,
81            self.plans
82        )
83    }
84}
85
86#[derive(Debug, Clone)]
87pub struct Warning {
88    pub code: u32,
89    pub message: String,
90}
91
92impl Display for Warning {
93    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
94        write!(f, "code: {}, message: {}", self.code, self.message)
95    }
96}
97
98#[derive(Default, Debug, Clone)]
99pub struct Metrics {
100    pub elapsed_time: Duration,
101    pub execution_time: Duration,
102    pub result_count: u64,
103    pub result_size: u64,
104    pub error_count: u64,
105    pub warning_count: u64,
106    pub processed_objects: i64,
107}
108
109impl Display for Metrics {
110    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
111        write!(
112            f,
113            "elapsed_time: {:?}, execution_time: {:?}, result_count: {}, result_size: {},  error_count: {}, warning_count: {}, processed_objects: {}",
114            self.elapsed_time,
115            self.execution_time,
116            self.result_count,
117            self.result_size,
118            self.error_count,
119            self.warning_count,
120            self.processed_objects,
121        )
122    }
123}