sysmonk/routes/
monitor.rs

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
use crate::{constant, legacy, resources, routes, squire};
use actix_web::http::StatusCode;
use actix_web::{web, HttpRequest, HttpResponse};
use fernet::Fernet;
use std::sync::Arc;
use sysinfo::Disks;

/// Handles the monitor endpoint and rendering the appropriate HTML page.
///
/// # Arguments
///
/// * `request` - A reference to the Actix web `HttpRequest` object.
/// * `fernet` - Fernet object to encrypt the auth payload that will be set as `session_token` cookie.
/// * `session` - Session struct that holds the `session_mapping` to handle sessions.
/// * `metadata` - Struct containing metadata of the application.
/// * `config` - Configuration data for the application.
/// * `template` - Configuration container for the loaded templates.
///
/// # Returns
///
/// Returns an `HTTPResponse` with the cookie for `session_token` reset if available.
#[get("/monitor")]
pub async fn monitor(request: HttpRequest,
                     fernet: web::Data<Arc<Fernet>>,
                     session: web::Data<Arc<constant::Session>>,
                     metadata: web::Data<Arc<constant::MetaData>>,
                     config: web::Data<Arc<squire::settings::Config>>,
                     template: web::Data<Arc<minijinja::Environment<'static>>>) -> HttpResponse {
    let auth_response = squire::authenticator::verify_token(&request, &config, &fernet, &session);
    if !auth_response.ok {
        return routes::auth::failed_auth(auth_response);
    }
    let monitor_template = template.get_template("monitor").unwrap();
    let mut response = HttpResponse::build(StatusCode::OK);
    response.content_type("text/html; charset=utf-8");
    log::debug!("Session Validation Response: {}", auth_response.detail);

    // Refresh all disks during startup and re-use it
    let disks = Disks::new_with_refreshed_list();

    let sys_info_map = resources::info::get_sys_info(&disks);
    let legacy_disk_info = legacy::disks::get_all_disks();

    // legacy functions have a mechanism to check for physical devices, so it takes precedence
    let has_name_and_size = !legacy_disk_info.is_empty() &&
        legacy_disk_info.iter().all(|disk| {
            disk.contains_key("Name") && disk.contains_key("Size")
        });
    let sys_info_disks = if has_name_and_size {
        log::debug!("Using legacy methods for disks!");
        legacy_disk_info
    } else {
        resources::info::get_disks(&disks)
    };

    let sys_info_network = resources::network::get_network_info().await;

    let sys_info_basic = sys_info_map.get("basic").unwrap();
    let sys_info_mem_storage = sys_info_map.get("mem_storage").unwrap();

    let rendered = monitor_template.render(minijinja::context!(
        version => metadata.pkg_version,
        logout => "/logout",
        sys_info_basic => sys_info_basic,
        sys_info_mem_storage => sys_info_mem_storage,
        sys_info_network => sys_info_network,
        sys_info_disks => sys_info_disks
    )).unwrap();
    response.body(rendered)
}