Added RTT function

This commit is contained in:
Edward Shen 2019-05-02 18:47:59 -04:00
parent 01757eb4da
commit 39d932c949
Signed by: edward
GPG key ID: F350507060ED6C90
5 changed files with 23 additions and 9 deletions

View file

@ -35,6 +35,7 @@
endpoints:[ endpoints:[
(label: "The code doesn't match!", endpoint: "http://example.com/", code: 204), (label: "The code doesn't match!", endpoint: "http://example.com/", code: 204),
(label: "The body doesn't match!", endpoint: "http://example.com/", body: "asdf"), (label: "The body doesn't match!", endpoint: "http://example.com/", body: "asdf"),
(label: "Slow reponse time", endpoint: "http://slowwly.robertomurray.co.uk/delay/2000/url/"),
(label: "Here's an error:", endpoint: "https://some-invalid-website.arpa") (label: "Here's an error:", endpoint: "https://some-invalid-website.arpa")
] ]
), ),

View file

@ -68,8 +68,9 @@
<div class="info"> <div class="info">
<h3>{{ status.endpoint }}</h3> <h3>{{ status.endpoint }}</h3>
<div class="spacer"></div> <div class="spacer"></div>
<a href="{{ status.location }}" target="_blank">{{ status.location }}</a> {% if status.status != 2 %}<p>{{ status.rtt }}</p>{% endif %}
</div> </div>
<a href="{{ status.location }}" target="_blank">{{ status.location }}</a>
{% if status.error %}<p class="error-msg">{{ status.error }}</p>{% endif %} {% if status.error %}<p class="error-msg">{{ status.error }}</p>{% endif %}
</main> </main>
</section> </section>

View file

@ -12,24 +12,27 @@ pub struct EndpointStatus {
pub status: u8, pub status: u8,
pub location: String, pub location: String,
pub endpoint: String, pub endpoint: String,
pub rtt: Option<String>,
pub error: Option<String>, pub error: Option<String>,
} }
impl EndpointStatus { impl EndpointStatus {
pub fn ok(location: String, endpoint: String) -> Self { pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
EndpointStatus { EndpointStatus {
status: 0, status: 0,
location, location,
endpoint, endpoint,
rtt: Some(rtt),
error: None, error: None,
} }
} }
pub fn warn(location: String, endpoint: String, error: Option<String>) -> Self { pub fn warn(location: String, endpoint: String, rtt: String, error: Option<String>) -> Self {
EndpointStatus { EndpointStatus {
status: 1, status: 1,
location, location,
endpoint, endpoint,
rtt: Some(rtt),
error, error,
} }
} }
@ -39,6 +42,7 @@ impl EndpointStatus {
status: 2, status: 2,
location, location,
endpoint, endpoint,
rtt: None,
error, error,
} }
} }

View file

@ -1,3 +1,5 @@
#![forbid(unsafe_code)]
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate chrono; extern crate chrono;

View file

@ -3,7 +3,7 @@ use crate::{
handlers::{EndpointStatus, StatusGroup}, handlers::{EndpointStatus, StatusGroup},
State, State,
}; };
use chrono::prelude::Utc; use chrono::Utc;
use reqwest::{Client, RedirectPolicy, Url, UrlError}; use reqwest::{Client, RedirectPolicy, Url, UrlError};
use ring::{ use ring::{
digest::{digest, SHA256}, digest::{digest, SHA256},
@ -51,8 +51,14 @@ fn get_result(
) -> EndpointStatus { ) -> EndpointStatus {
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone()); let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
let url = get_url(&website_conf.base, &path, port).expect("reading config"); let url = get_url(&website_conf.base, &path, port).expect("reading config");
let ping_start = Utc::now();
let ping_result = client.get(&url).send(); let ping_result = client.get(&url).send();
let duration = Utc::now() - ping_start;
let duration = if duration.num_seconds() > 0 {
format!("{}s", duration.num_milliseconds() as f64 / 1000.)
} else {
format!("{}ms", duration.num_milliseconds())
};
match ping_result { match ping_result {
Ok(mut res) => { Ok(mut res) => {
let res_body = res.text().expect("could not get body of request"); let res_body = res.text().expect("could not get body of request");
@ -90,14 +96,14 @@ fn get_result(
} }
if error.is_some() { if error.is_some() {
EndpointStatus::warn(url, label, error) EndpointStatus::warn(url, label, duration, error)
} else { } else {
EndpointStatus::ok(url, label) EndpointStatus::ok(url, label, duration)
} }
} }
Err(e) => { Err(e) => {
if let Some(true) = endpoint.should_err { if let Some(true) = endpoint.should_err {
EndpointStatus::ok(url, label) EndpointStatus::ok(url, label, duration)
} else { } else {
EndpointStatus::error(url, label, Some(format!("{}", e))) EndpointStatus::error(url, label, Some(format!("{}", e)))
} }