forked from tgstation/rust-g
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alexkar598
committed
Jul 18, 2023
1 parent
daeeedf
commit 858e560
Showing
9 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#define rustg_influxdb2_publish(data, endpoint, token) RUSTG_CALL(RUST_G, "influxdb2_publish")(data, endpoint, token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use serde_json::Value; | ||
|
||
use crate::error::Error; | ||
use crate::http::{construct_request, submit_request, RequestPrep}; | ||
use crate::jobs; | ||
|
||
byond_fn!( | ||
fn influxdb2_publish(data, endpoint, token) { | ||
let data = data.to_owned(); | ||
let endpoint = endpoint.to_owned(); | ||
let token = token.to_owned(); | ||
Some(jobs::start(move || { | ||
fn handle(data: &str, endpoint: &str, token: &str) -> Result<RequestPrep, Error> { | ||
let mut lines = vec!(); | ||
|
||
let data: Value = serde_json::from_str(data)?; | ||
for entry in data.as_array().unwrap() { | ||
let entry = entry.as_object().ok_or(Error::InvalidMetrics)?; | ||
|
||
let measurement = entry.get("@measurement").ok_or(Error::InvalidMetrics)?.as_str().ok_or(Error::InvalidMetrics)?.to_owned(); | ||
let mut measurement_tags = vec!{measurement}; | ||
|
||
let tags = entry.get("@tags").ok_or(Error::InvalidMetrics)?.as_object().ok_or(Error::InvalidMetrics)?; | ||
for (key, val) in tags { | ||
measurement_tags.push(concat_string!(key, "=", val.as_str().ok_or(Error::InvalidMetrics)?)) | ||
}; | ||
|
||
let mut fields = vec!{}; | ||
for (key, val) in entry { | ||
if key.starts_with('@') { | ||
continue; | ||
} | ||
fields.push(concat_string!(key, "=", val.to_string())) | ||
}; | ||
|
||
let timestamp = entry.get("@timestamp").ok_or(Error::InvalidMetrics)?.as_str().ok_or(Error::InvalidMetrics)?; | ||
lines.push(concat_string!(measurement_tags.join(","), " ", fields.join(",") , " ", timestamp)); | ||
} | ||
|
||
construct_request( | ||
"post", | ||
endpoint, | ||
lines.join("\n").as_str(), | ||
concat_string!("{\"Authorization\":\"Token ", token ,"\"}").as_str(), | ||
"" | ||
) | ||
} | ||
|
||
let req = match handle(data.as_str(), endpoint.as_str(), token.as_str()) { | ||
Ok(r) => r, | ||
Err(e) => return e.to_string() | ||
}; | ||
match submit_request(req) { | ||
Ok(r) => r, | ||
Err(e) => e.to_string() | ||
} | ||
})) | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters