|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use serde_json::Value; |
| 3 | + |
| 4 | +use crate::error::Error; |
| 5 | +use crate::http::{construct_request, submit_request, RequestPrep}; |
| 6 | +use crate::jobs; |
| 7 | + |
| 8 | +byond_fn!( |
| 9 | + fn influxdb2_publish(data, endpoint, token) { |
| 10 | + let data = data.to_owned(); |
| 11 | + let endpoint = endpoint.to_owned(); |
| 12 | + let token = token.to_owned(); |
| 13 | + Some(jobs::start(move || { |
| 14 | + fn handle(data: &str, endpoint: &str, token: &str) -> Result<RequestPrep, Error> { |
| 15 | + let mut lines = vec!(); |
| 16 | + |
| 17 | + let data: Value = serde_json::from_str(data)?; |
| 18 | + for entry in data.as_array().unwrap() { |
| 19 | + let entry = entry.as_object().ok_or(Error::InvalidMetrics)?; |
| 20 | + |
| 21 | + let measurement = entry.get("@measurement").ok_or(Error::InvalidMetrics)?.as_str().ok_or(Error::InvalidMetrics)?.to_owned(); |
| 22 | + let mut measurement_tags = vec!{measurement}; |
| 23 | + |
| 24 | + let tags = entry.get("@tags").ok_or(Error::InvalidMetrics)?.as_object().ok_or(Error::InvalidMetrics)?; |
| 25 | + for (key, val) in tags { |
| 26 | + measurement_tags.push(concat_string!(key, "=", val.as_str().ok_or(Error::InvalidMetrics)?)) |
| 27 | + }; |
| 28 | + |
| 29 | + let mut fields = vec!{}; |
| 30 | + for (key, val) in entry { |
| 31 | + if key.starts_with('@') { |
| 32 | + continue; |
| 33 | + } |
| 34 | + fields.push(concat_string!(key, "=", val.to_string())) |
| 35 | + }; |
| 36 | + |
| 37 | + let timestamp = entry.get("@timestamp").ok_or(Error::InvalidMetrics)?.as_str().ok_or(Error::InvalidMetrics)?; |
| 38 | + lines.push(concat_string!(measurement_tags.join(","), " ", fields.join(",") , " ", timestamp)); |
| 39 | + } |
| 40 | + |
| 41 | + construct_request( |
| 42 | + "post", |
| 43 | + endpoint, |
| 44 | + lines.join("\n").as_str(), |
| 45 | + concat_string!("{\"Authorization\":\"Token ", token ,"\"}").as_str(), |
| 46 | + "" |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + let req = match handle(data.as_str(), endpoint.as_str(), token.as_str()) { |
| 51 | + Ok(r) => r, |
| 52 | + Err(e) => return e.to_string() |
| 53 | + }; |
| 54 | + match submit_request(req) { |
| 55 | + Ok(r) => r, |
| 56 | + Err(e) => e.to_string() |
| 57 | + } |
| 58 | + })) |
| 59 | + } |
| 60 | +); |
| 61 | + |
| 62 | +#[derive(Serialize, Deserialize)] |
| 63 | +struct ProfileProcEntry { |
| 64 | + name: String, |
| 65 | + #[serde(rename = "self")] |
| 66 | + self_: f32, |
| 67 | + total: f32, |
| 68 | + real: f32, |
| 69 | + over: f32, |
| 70 | + calls: f32, |
| 71 | +} |
| 72 | +byond_fn!( |
| 73 | + fn influxdb2_publish_profile(data, endpoint, token, round_id) { |
| 74 | + let data = data.to_owned(); |
| 75 | + let endpoint = endpoint.to_owned(); |
| 76 | + let token = token.to_owned(); |
| 77 | + let round_id = round_id.to_owned(); |
| 78 | + Some(jobs::start(move || { |
| 79 | + fn handle(data: &str, endpoint: &str, token: &str, round_id: &str) -> Result<RequestPrep, Error> { |
| 80 | + let mut lines = vec!(); |
| 81 | + |
| 82 | + let data: Vec<ProfileProcEntry> = serde_json::from_str(data)?; |
| 83 | + let timestamp = std::time::SystemTime::now() |
| 84 | + .duration_since(std::time::UNIX_EPOCH) |
| 85 | + .unwrap() |
| 86 | + .as_secs() |
| 87 | + .to_string(); |
| 88 | + for entry in data { |
| 89 | + let mut name = entry.name; |
| 90 | + if name.is_empty() { |
| 91 | + name = String::from("(no_name)"); |
| 92 | + } |
| 93 | + lines.push(concat_string!("profile,proc=", name, " self=", entry.self_.to_string(), ",total=", entry.total.to_string(), ",real=", entry.real.to_string(), ",over=", entry.over.to_string(), ",calls=", entry.calls.to_string(), ",round_id=", round_id.to_string(), " ", timestamp)); |
| 94 | + } |
| 95 | + |
| 96 | + construct_request( |
| 97 | + "post", |
| 98 | + endpoint, |
| 99 | + lines.join("\n").as_str(), |
| 100 | + concat_string!("{\"Authorization\":\"Token ", token ,"\"}").as_str(), |
| 101 | + "" |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + let req = match handle(data.as_str(), endpoint.as_str(), token.as_str(), round_id.as_str()) { |
| 106 | + Ok(r) => r, |
| 107 | + Err(e) => return e.to_string() |
| 108 | + }; |
| 109 | + match submit_request(req) { |
| 110 | + Ok(r) => r, |
| 111 | + Err(e) => e.to_string() |
| 112 | + } |
| 113 | + })) |
| 114 | + } |
| 115 | +); |
0 commit comments