Skip to content

Commit 6708203

Browse files
cjrkoadyc3
andauthored
Make ott-vis-datasource and ott-collector communicate (#1397)
* system state * serve sample state * http request + minor changes to collector * changed Vec<Balancer> to Box<Balancer> and get request change (did not test any of this yet) * changes * display data * cargo fmt * fmt (again) * fmt * prettier * move rocket import * Update packages/ott-vis-datasource/src/datasource.ts Co-authored-by: Carson McManus <[email protected]> * requested changes * remove unused type * remove unused type --------- Co-authored-by: Carson McManus <[email protected]>
1 parent be6b25a commit 6708203

File tree

5 files changed

+156
-85
lines changed

5 files changed

+156
-85
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pin-project = "1.0.12"
2424
prometheus = { version = "0.13.3", features = ["process"] }
2525
rand = "0.8.5"
2626
reqwest = { version = "0.11.17", features = ["json", "stream", "rustls-tls"] }
27-
rocket = "0.5.0"
27+
rocket = {version = "0.5.0", features = ["json"]}
2828
serde = { version = "1", features = ["derive", "rc"] }
2929
serde_json = { version = "1", features = ["raw_value"] }
3030
serde_path_to_error = "0.1.15"

crates/ott-collector/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ edition = "2021"
88
anyhow.workspace = true
99
ott-common.workspace = true
1010
reqwest.workspace = true
11-
rocket.workspace = true
1211
serde.workspace = true
1312
serde_json.workspace = true
1413
tokio.workspace = true
1514
tracing.workspace = true
16-
tracing-subscriber.workspace = true
15+
rocket.workspace = true
16+
tracing-subscriber.workspace = true

crates/ott-collector/src/main.rs

+146-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,153 @@
11
#[macro_use]
22
extern crate rocket;
33

4+
use rocket::serde::json::Json;
5+
use serde::Serialize;
6+
7+
#[derive(Serialize)]
8+
struct SystemState(Vec<Balancer>);
9+
10+
#[derive(Serialize)]
11+
struct Balancer {
12+
id: String,
13+
region: String,
14+
monoliths: Vec<Monolith>,
15+
}
16+
17+
#[derive(Serialize)]
18+
struct Monolith {
19+
id: String,
20+
region: String,
21+
rooms: Vec<Room>,
22+
}
23+
24+
#[derive(Serialize)]
25+
struct Room {
26+
name: String,
27+
clients: i32,
28+
}
29+
30+
fn return_sample_state() -> SystemState {
31+
SystemState({
32+
vec![
33+
Balancer {
34+
id: "154d9d41-128c-45ab-83d8-28661882c9e3".to_string(),
35+
region: "ewr".to_string(),
36+
monoliths: vec![
37+
Monolith {
38+
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf".to_string(),
39+
region: "ewr".to_string(),
40+
rooms: vec![
41+
Room {
42+
name: "foo".to_string(),
43+
clients: 2,
44+
},
45+
Room {
46+
name: "bar".to_string(),
47+
clients: 0,
48+
},
49+
],
50+
},
51+
Monolith {
52+
id: "419580cb-f576-4314-8162-45340c94bae1".to_string(),
53+
region: "ewr".to_string(),
54+
rooms: vec![Room {
55+
name: "baz".to_string(),
56+
clients: 3,
57+
}],
58+
},
59+
Monolith {
60+
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac".to_string(),
61+
region: "cdg".to_string(),
62+
rooms: vec![Room {
63+
name: "qux".to_string(),
64+
clients: 0,
65+
}],
66+
},
67+
],
68+
},
69+
Balancer {
70+
id: "c91d183c-980e-4160-b196-43658148f469".to_string(),
71+
region: "ewr".to_string(),
72+
monoliths: vec![
73+
Monolith {
74+
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf".to_string(),
75+
region: "ewr".to_string(),
76+
rooms: vec![
77+
Room {
78+
name: "foo".to_string(),
79+
clients: 1,
80+
},
81+
Room {
82+
name: "bar".to_string(),
83+
clients: 2,
84+
},
85+
],
86+
},
87+
Monolith {
88+
id: "419580cb-f576-4314-8162-45340c94bae1".to_string(),
89+
region: "ewr".to_string(),
90+
rooms: vec![Room {
91+
name: "baz".to_string(),
92+
clients: 0,
93+
}],
94+
},
95+
Monolith {
96+
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac".to_string(),
97+
region: "cdg".to_string(),
98+
rooms: vec![Room {
99+
name: "qux".to_string(),
100+
clients: 0,
101+
}],
102+
},
103+
],
104+
},
105+
Balancer {
106+
id: "5a2e3b2d-f27b-4e3d-9b59-c921442f7ff0".to_string(),
107+
region: "cdg".to_string(),
108+
monoliths: vec![
109+
Monolith {
110+
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf".to_string(),
111+
region: "ewr".to_string(),
112+
rooms: vec![
113+
Room {
114+
name: "foo".to_string(),
115+
clients: 0,
116+
},
117+
Room {
118+
name: "bar".to_string(),
119+
clients: 0,
120+
},
121+
],
122+
},
123+
Monolith {
124+
id: "419580cb-f576-4314-8162-45340c94bae1".to_string(),
125+
region: "ewr".to_string(),
126+
rooms: vec![Room {
127+
name: "baz".to_string(),
128+
clients: 0,
129+
}],
130+
},
131+
Monolith {
132+
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac".to_string(),
133+
region: "cdg".to_string(),
134+
rooms: vec![Room {
135+
name: "qux".to_string(),
136+
clients: 4,
137+
}],
138+
},
139+
],
140+
},
141+
]
142+
})
143+
}
144+
4145
mod cors;
5146

6147
/// Serve the current system state
7148
#[get("/state")]
8-
fn serve_state() {
9-
todo!("Serve the current system state")
149+
fn serve_state() -> Json<SystemState> {
150+
Json(return_sample_state())
10151
}
11152

12153
#[launch]
@@ -22,3 +163,6 @@ fn rocket() -> _ {
22163
fn status() -> &'static str {
23164
"OK"
24165
}
166+
167+
#[cfg(test)]
168+
mod test {}

packages/ott-vis-datasource/src/datasource.ts

+6-80
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
} from "@grafana/data";
99

1010
import { MyQuery, MyDataSourceOptions } from "./types";
11-
import type { SystemState } from "ott-vis-common";
1211
import { getBackendSrv } from "@grafana/runtime";
12+
import type { SystemState } from "ott-vis-common";
1313
import { lastValueFrom } from "rxjs";
1414

1515
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
@@ -21,15 +21,16 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
2121
}
2222

2323
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
24-
// const { range } = options;
25-
// const from = range!.from.valueOf();
26-
// const to = range!.to.valueOf();
24+
const response = getBackendSrv().fetch<SystemState>({
25+
url: `${this.baseUrl}/state`,
26+
});
27+
const systemState = (await lastValueFrom(response)).data;
2728

2829
// Return a constant for each query.
2930
const data = options.targets.map(target => {
3031
return new MutableDataFrame({
3132
refId: target.refId,
32-
fields: [{ name: "Balancers", values: [sampleSystemState], type: FieldType.other }],
33+
fields: [{ name: "Balancers", values: [systemState], type: FieldType.other }],
3334
});
3435
});
3536

@@ -55,78 +56,3 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
5556
};
5657
}
5758
}
58-
59-
const sampleSystemState: SystemState = [
60-
{
61-
id: "154d9d41-128c-45ab-83d8-28661882c9e3",
62-
region: "ewr",
63-
monoliths: [
64-
{
65-
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf",
66-
region: "ewr",
67-
rooms: [
68-
{ name: "foo", clients: 2 },
69-
{ name: "bar", clients: 0 },
70-
],
71-
},
72-
{
73-
id: "419580cb-f576-4314-8162-45340c94bae1",
74-
region: "ewr",
75-
rooms: [{ name: "baz", clients: 3 }],
76-
},
77-
{
78-
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac",
79-
region: "cdg",
80-
rooms: [{ name: "qux", clients: 0 }],
81-
},
82-
],
83-
},
84-
{
85-
id: "c91d183c-980e-4160-b196-43658148f469",
86-
region: "ewr",
87-
monoliths: [
88-
{
89-
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf",
90-
region: "ewr",
91-
rooms: [
92-
{ name: "foo", clients: 1 },
93-
{ name: "bar", clients: 2 },
94-
],
95-
},
96-
{
97-
id: "419580cb-f576-4314-8162-45340c94bae1",
98-
region: "ewr",
99-
rooms: [{ name: "baz", clients: 0 }],
100-
},
101-
{
102-
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac",
103-
region: "cdg",
104-
rooms: [{ name: "qux", clients: 0 }],
105-
},
106-
],
107-
},
108-
{
109-
id: "5a2e3b2d-f27b-4e3d-9b59-c921442f7ff0",
110-
region: "cdg",
111-
monoliths: [
112-
{
113-
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf",
114-
region: "ewr",
115-
rooms: [
116-
{ name: "foo", clients: 0 },
117-
{ name: "bar", clients: 0 },
118-
],
119-
},
120-
{
121-
id: "419580cb-f576-4314-8162-45340c94bae1",
122-
region: "ewr",
123-
rooms: [{ name: "baz", clients: 0 }],
124-
},
125-
{
126-
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac",
127-
region: "cdg",
128-
rooms: [{ name: "qux", clients: 4 }],
129-
},
130-
],
131-
},
132-
];

0 commit comments

Comments
 (0)