Rename some function and module

This commit is contained in:
DaLaw2 2024-12-13 12:25:04 +08:00
parent 63a13e6176
commit 8491c6b95e
4 changed files with 48 additions and 59 deletions

View File

@ -1,6 +1,6 @@
use crate::core::system::System;
use crate::model::flow_stats::FlowStats;
use crate::model::flow_type::{IPv4FlowType, IPv6FlowType};
use crate::model::flow_type::FlowType;
use crate::utils::log_entry::system::SystemEntry;
use aya::maps::{HashMap as AyaHashMap, MapData};
use aya::Pod;
@ -134,17 +134,15 @@ impl Statistics {
}
}
pub async fn get_ipv4_flow_data(
ipv4_flow_type: IPv4FlowType,
) -> StdHashMap<SocketAddrV4, FlowStats> {
pub async fn get_ipv4_flow_data(flow_type: FlowType) -> StdHashMap<SocketAddrV4, FlowStats> {
let monitor = Statistics::instance().await;
let iter = match ipv4_flow_type {
IPv4FlowType::Src1Min => monitor.ipv4_src_1min.iter(),
IPv4FlowType::Src10Min => monitor.ipv4_src_10min.iter(),
IPv4FlowType::Src1Hour => monitor.ipv4_src_1hour.iter(),
IPv4FlowType::Dst1Min => monitor.ipv4_dst_1min.iter(),
IPv4FlowType::Dst10Min => monitor.ipv4_dst_10min.iter(),
IPv4FlowType::Dst1Hour => monitor.ipv4_dst_1hour.iter(),
let iter = match flow_type {
FlowType::Src1Min => monitor.ipv4_src_1min.iter(),
FlowType::Src10Min => monitor.ipv4_src_10min.iter(),
FlowType::Src1Hour => monitor.ipv4_src_1hour.iter(),
FlowType::Dst1Min => monitor.ipv4_dst_1min.iter(),
FlowType::Dst10Min => monitor.ipv4_dst_10min.iter(),
FlowType::Dst1Hour => monitor.ipv4_dst_1hour.iter(),
};
iter.filter_map(Result::ok)
.map(|(key, value)| {
@ -155,17 +153,15 @@ impl Statistics {
.collect()
}
pub async fn get_ipv6_flow_data(
ipv6_flow_type: IPv6FlowType,
) -> StdHashMap<SocketAddrV6, FlowStats> {
pub async fn get_ipv6_flow_data(flow_type: FlowType) -> StdHashMap<SocketAddrV6, FlowStats> {
let monitor = Statistics::instance().await;
let iter = match ipv6_flow_type {
IPv6FlowType::Src1Min => monitor.ipv6_src_1min.iter(),
IPv6FlowType::Src10Min => monitor.ipv6_src_10min.iter(),
IPv6FlowType::Src1Hour => monitor.ipv6_src_1hour.iter(),
IPv6FlowType::Dst1Min => monitor.ipv6_dst_1min.iter(),
IPv6FlowType::Dst10Min => monitor.ipv6_dst_10min.iter(),
IPv6FlowType::Dst1Hour => monitor.ipv6_dst_1hour.iter(),
let iter = match flow_type {
FlowType::Src1Min => monitor.ipv6_src_1min.iter(),
FlowType::Src10Min => monitor.ipv6_src_10min.iter(),
FlowType::Src1Hour => monitor.ipv6_src_1hour.iter(),
FlowType::Dst1Min => monitor.ipv6_dst_1min.iter(),
FlowType::Dst10Min => monitor.ipv6_dst_10min.iter(),
FlowType::Dst1Hour => monitor.ipv6_dst_1hour.iter(),
};
iter.filter_map(Result::ok)
.map(|(key, value)| {

View File

@ -1,21 +1,18 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Debug, Copy, Clone)]
pub enum IPv4FlowType {
Src1Min,
Src10Min,
Src1Hour,
Dst1Min,
Dst10Min,
Dst1Hour,
}
#[derive(Deserialize, Debug, Copy, Clone)]
pub enum IPv6FlowType {
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
#[serde(rename_all = "lowercase")]
pub enum FlowType {
#[serde(rename = "src_1min")]
Src1Min,
#[serde(rename = "src_10min")]
Src10Min,
#[serde(rename = "src_1hour")]
Src1Hour,
#[serde(rename = "dst_1min")]
Dst1Min,
#[serde(rename = "dst_10min")]
Dst10Min,
#[serde(rename = "dst_1hour")]
Dst1Hour,
}

View File

@ -1,8 +1,8 @@
use crate::core::statistics::Statistics;
use crate::model::flow_type::{IPv4FlowType, IPv6FlowType};
use crate::model::flow_type::FlowType;
use crate::web::utils::flow_websocket::{IPv4FlowWebSocket, IPv6FlowWebSocket};
use actix_web::{get, web, Error, HttpRequest, HttpResponse, Responder, Scope};
use actix_web_actors::ws::start;
use crate::web::utils::flow_websocket::{IPv4FlowWebSocket, IPv6FlowWebSocket};
pub fn initialize() -> Scope {
web::scope("/statistics")
@ -24,73 +24,73 @@ pub fn initialize() -> Scope {
#[get("/get/ipv4/src/1min")]
async fn get_ipv4_src_1min() -> impl Responder {
let flow_data = Statistics::get_ipv4_flow_data(IPv4FlowType::Src1Min).await;
let flow_data = Statistics::get_ipv4_flow_data(FlowType::Src1Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv4/src/10min")]
async fn get_ipv4_src_10min() -> impl Responder {
let flow_data = Statistics::get_ipv4_flow_data(IPv4FlowType::Src10Min).await;
let flow_data = Statistics::get_ipv4_flow_data(FlowType::Src10Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv4/src/1hour")]
async fn get_ipv4_src_1hour() -> impl Responder {
let flow_data = Statistics::get_ipv4_flow_data(IPv4FlowType::Src1Hour).await;
let flow_data = Statistics::get_ipv4_flow_data(FlowType::Src1Hour).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv6/src/1min")]
async fn get_ipv6_src_1min() -> impl Responder {
let flow_data = Statistics::get_ipv6_flow_data(IPv6FlowType::Src1Min).await;
let flow_data = Statistics::get_ipv6_flow_data(FlowType::Src1Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv6/src/10min")]
async fn get_ipv6_src_10min() -> impl Responder {
let flow_data = Statistics::get_ipv6_flow_data(IPv6FlowType::Src10Min).await;
let flow_data = Statistics::get_ipv6_flow_data(FlowType::Src10Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv6/src/1hour")]
async fn get_ipv6_src_1hour() -> impl Responder {
let flow_data = Statistics::get_ipv6_flow_data(IPv6FlowType::Src1Hour).await;
let flow_data = Statistics::get_ipv6_flow_data(FlowType::Src1Hour).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv4/dst/1min")]
async fn get_ipv4_dst_1min() -> impl Responder {
let flow_data = Statistics::get_ipv4_flow_data(IPv4FlowType::Dst1Min).await;
let flow_data = Statistics::get_ipv4_flow_data(FlowType::Dst1Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv4/dst/10min")]
async fn get_ipv4_dst_10min() -> impl Responder {
let flow_data = Statistics::get_ipv4_flow_data(IPv4FlowType::Dst10Min).await;
let flow_data = Statistics::get_ipv4_flow_data(FlowType::Dst10Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv4/dst/1hour")]
async fn get_ipv4_dst_1hour() -> impl Responder {
let flow_data = Statistics::get_ipv4_flow_data(IPv4FlowType::Dst1Hour).await;
let flow_data = Statistics::get_ipv4_flow_data(FlowType::Dst1Hour).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv6/dst/1min")]
async fn get_ipv6_dst_1min() -> impl Responder {
let flow_data = Statistics::get_ipv6_flow_data(IPv6FlowType::Src1Min).await;
let flow_data = Statistics::get_ipv6_flow_data(FlowType::Src1Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv6/dst/10min")]
async fn get_ipv6_dst_10min() -> impl Responder {
let flow_data = Statistics::get_ipv6_flow_data(IPv6FlowType::Src10Min).await;
let flow_data = Statistics::get_ipv6_flow_data(FlowType::Src10Min).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
#[get("/get/ipv6/dst/1hour")]
async fn get_ipv6_dst_1hour() -> impl Responder {
let flow_data = Statistics::get_ipv6_flow_data(IPv6FlowType::Src1Hour).await;
let flow_data = Statistics::get_ipv6_flow_data(FlowType::Src1Hour).await;
HttpResponse::Ok().json(web::Json(flow_data))
}
@ -98,7 +98,7 @@ async fn get_ipv6_dst_1hour() -> impl Responder {
async fn websocket_ipv4(
req: HttpRequest,
stream: web::Payload,
path: web::Path<IPv4FlowType>,
path: web::Path<FlowType>,
) -> Result<HttpResponse, Error> {
let flow_type = path.into_inner();
let websocket = IPv4FlowWebSocket {
@ -112,7 +112,7 @@ async fn websocket_ipv4(
async fn websocket_ipv6(
req: HttpRequest,
stream: web::Payload,
path: web::Path<IPv6FlowType>,
path: web::Path<FlowType>,
) -> Result<HttpResponse, Error> {
let flow_type = path.into_inner();
let websocket = IPv6FlowWebSocket {

View File

@ -1,12 +1,12 @@
use crate::core::config_manager::ConfigManager;
use crate::core::statistics::Statistics;
use crate::model::flow_type::{IPv4FlowType, IPv6FlowType};
use crate::model::flow_type::FlowType;
use actix::prelude::*;
use actix_web_actors::ws;
use std::time::Duration;
pub struct IPv4FlowWebSocket {
pub flow_type: IPv4FlowType,
pub flow_type: FlowType,
pub interval: Option<SpawnHandle>,
}
@ -18,9 +18,7 @@ impl Actor for IPv4FlowWebSocket {
let refresh_interval = Duration::from_secs(config.refresh_interval);
let interval = ctx.run_interval(refresh_interval, |actor, ctx| {
let flow_type = actor.flow_type.clone();
let future = async move {
Statistics::get_ipv4_flow_data(flow_type).await
};
let future = async move { Statistics::get_ipv4_flow_data(flow_type).await };
ctx.wait(future.into_actor(actor).map(|flow_data, _, ctx| {
if let Ok(json) = serde_json::to_string(&flow_data) {
ctx.text(json);
@ -52,7 +50,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for IPv4FlowWebSocket
}
pub struct IPv6FlowWebSocket {
pub flow_type: IPv6FlowType,
pub flow_type: FlowType,
pub interval: Option<SpawnHandle>,
}
@ -64,9 +62,7 @@ impl Actor for IPv6FlowWebSocket {
let refresh_interval = Duration::from_secs(config.refresh_interval);
let interval = ctx.run_interval(refresh_interval, |actor, ctx| {
let flow_type = actor.flow_type.clone();
let future = async move {
Statistics::get_ipv6_flow_data(flow_type).await
};
let future = async move { Statistics::get_ipv6_flow_data(flow_type).await };
ctx.wait(future.into_actor(actor).map(|flow_data, _, ctx| {
if let Ok(json) = serde_json::to_string(&flow_data) {
ctx.text(json);