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
use std::fmt;

use graphql_client::GraphQLQuery;

use crate::{BoxedSubscription, QueryResult};

/// Components query for returning sources, transforms, and sinks
#[derive(GraphQLQuery, Debug, Copy, Clone)]
#[graphql(
    schema_path = "graphql/schema.json",
    query_path = "graphql/queries/components.graphql",
    response_derives = "Debug"
)]
pub struct ComponentsQuery;

/// Components subscription for notification when a component has been added
#[derive(GraphQLQuery, Debug, Copy, Clone)]
#[graphql(
    schema_path = "graphql/schema.json",
    query_path = "graphql/subscriptions/component_added.graphql",
    response_derives = "Debug"
)]
pub struct ComponentAddedSubscription;

/// Components subscription for notification when a component has been removed
#[derive(GraphQLQuery, Debug, Copy, Clone)]
#[graphql(
    schema_path = "graphql/schema.json",
    query_path = "graphql/subscriptions/component_removed.graphql",
    response_derives = "Debug"
)]
pub struct ComponentRemovedSubscription;

pub trait ComponentsQueryExt {
    async fn components_query(&self, first: i64) -> crate::QueryResult<ComponentsQuery>;
}

impl ComponentsQueryExt for crate::Client {
    async fn components_query(&self, first: i64) -> QueryResult<ComponentsQuery> {
        let request_body = ComponentsQuery::build_query(components_query::Variables { first });
        self.query::<ComponentsQuery>(&request_body).await
    }
}

pub trait ComponentsSubscriptionExt {
    fn component_added(&self) -> crate::BoxedSubscription<ComponentAddedSubscription>;
    fn component_removed(&self) -> crate::BoxedSubscription<ComponentRemovedSubscription>;
}

impl ComponentsSubscriptionExt for crate::SubscriptionClient {
    /// Subscription for when a component has been added
    fn component_added(&self) -> BoxedSubscription<ComponentAddedSubscription> {
        let request_body =
            ComponentAddedSubscription::build_query(component_added_subscription::Variables);

        self.start::<ComponentAddedSubscription>(&request_body)
    }

    /// Subscription for when a component has been removed
    fn component_removed(&self) -> BoxedSubscription<ComponentRemovedSubscription> {
        let request_body =
            ComponentRemovedSubscription::build_query(component_removed_subscription::Variables);

        self.start::<ComponentRemovedSubscription>(&request_body)
    }
}

impl components_query::ComponentsQueryComponentsEdgesNodeOn {
    pub fn received_bytes_total(&self) -> i64 {
        // This is network bytes received, and only sources can receive events.
        match self {
            components_query::ComponentsQueryComponentsEdgesNodeOn::Source(s) => s
                .metrics
                .received_bytes_total
                .as_ref()
                .map(|p| p.received_bytes_total as i64)
                .unwrap_or(0),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Transform(_) => 0,
            components_query::ComponentsQueryComponentsEdgesNodeOn::Sink(_) => 0,
        }
    }

    pub fn received_events_total(&self) -> i64 {
        match self {
            components_query::ComponentsQueryComponentsEdgesNodeOn::Source(s) => s
                .metrics
                .received_events_total
                .as_ref()
                .map(|p| p.received_events_total as i64)
                .unwrap_or(0),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Transform(t) => t
                .metrics
                .received_events_total
                .as_ref()
                .map(|p| p.received_events_total as i64)
                .unwrap_or(0),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Sink(s) => s
                .metrics
                .received_events_total
                .as_ref()
                .map(|p| p.received_events_total as i64)
                .unwrap_or(0),
        }
    }

    pub fn sent_bytes_total(&self) -> i64 {
        // This is network bytes sent, and only sinks can send out events.
        match self {
            components_query::ComponentsQueryComponentsEdgesNodeOn::Source(_) => 0,
            components_query::ComponentsQueryComponentsEdgesNodeOn::Transform(_) => 0,
            components_query::ComponentsQueryComponentsEdgesNodeOn::Sink(s) => s
                .metrics
                .sent_bytes_total
                .as_ref()
                .map(|p| p.sent_bytes_total as i64)
                .unwrap_or(0),
        }
    }

    pub fn sent_events_total(&self) -> i64 {
        match self {
            components_query::ComponentsQueryComponentsEdgesNodeOn::Source(s) => s
                .metrics
                .sent_events_total
                .as_ref()
                .map(|p| p.sent_events_total as i64)
                .unwrap_or(0),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Transform(t) => t
                .metrics
                .sent_events_total
                .as_ref()
                .map(|p| p.sent_events_total as i64)
                .unwrap_or(0),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Sink(s) => s
                .metrics
                .sent_events_total
                .as_ref()
                .map(|p| p.sent_events_total as i64)
                .unwrap_or(0),
        }
    }

    pub fn outputs(&self) -> Vec<(String, i64)> {
        match self {
            components_query::ComponentsQueryComponentsEdgesNodeOn::Source(s) => s
                .outputs
                .iter()
                .map(|o| {
                    (
                        o.output_id.clone(),
                        o.sent_events_total
                            .as_ref()
                            .map(|p| p.sent_events_total as i64)
                            .unwrap_or(0),
                    )
                })
                .collect(),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Transform(t) => t
                .outputs
                .iter()
                .map(|o| {
                    (
                        o.output_id.clone(),
                        o.sent_events_total
                            .as_ref()
                            .map(|p| p.sent_events_total as i64)
                            .unwrap_or(0),
                    )
                })
                .collect(),
            components_query::ComponentsQueryComponentsEdgesNodeOn::Sink(_) => vec![],
        }
    }
}

impl fmt::Display for components_query::ComponentsQueryComponentsEdgesNodeOn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let res = match self {
            components_query::ComponentsQueryComponentsEdgesNodeOn::Source(_) => "source",
            components_query::ComponentsQueryComponentsEdgesNodeOn::Transform(_) => "transform",
            components_query::ComponentsQueryComponentsEdgesNodeOn::Sink(_) => "sink",
        };

        write!(f, "{}", res)
    }
}

impl fmt::Display for component_added_subscription::ComponentAddedSubscriptionComponentAddedOn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let res = match self {
            component_added_subscription::ComponentAddedSubscriptionComponentAddedOn::Source => {
                "source"
            }
            component_added_subscription::ComponentAddedSubscriptionComponentAddedOn::Transform => {
                "transform"
            }
            component_added_subscription::ComponentAddedSubscriptionComponentAddedOn::Sink => {
                "sink"
            }
        };

        write!(f, "{}", res)
    }
}

impl fmt::Display
    for component_removed_subscription::ComponentRemovedSubscriptionComponentRemovedOn
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let res = match self {
            component_removed_subscription::ComponentRemovedSubscriptionComponentRemovedOn::Source => {
                "source"
            }
            component_removed_subscription::ComponentRemovedSubscriptionComponentRemovedOn::Transform => {
                "transform"
            }
            component_removed_subscription::ComponentRemovedSubscriptionComponentRemovedOn::Sink => {
                "sink"
            }
        };

        write!(f, "{}", res)
    }
}