Skip to content

Commit

Permalink
Merge pull request #65 from Rajdip019/dashboard-setup
Browse files Browse the repository at this point in the history
Dashboard setup
  • Loading branch information
Rajdip019 authored Jul 24, 2024
2 parents 53befac + c18465d commit 5bd29b1
Show file tree
Hide file tree
Showing 23 changed files with 1,264 additions and 29 deletions.
Binary file modified .DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ jsonwebtoken = "9.3.0"
openssl = "0.10.64"
regex = "1.10.4"
uuid = "1.8.0"
woothee = "0.13.0"
2 changes: 2 additions & 0 deletions src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl User {
created_at: decrypted_user.created_at,
updated_at: decrypted_user.updated_at,
email_verified: decrypted_user.email_verified,
blocked_until: decrypted_user.blocked_until,
is_active: decrypted_user.is_active,
uid: decrypted_user.uid,
});
Expand Down Expand Up @@ -222,6 +223,7 @@ impl User {
name: decrypted_user.name,
email: decrypted_user.email,
role: decrypted_user.role,
blocked_until: decrypted_user.blocked_until,
created_at: decrypted_user.created_at,
updated_at: decrypted_user.updated_at,
email_verified: decrypted_user.email_verified,
Expand Down
1 change: 1 addition & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth_handler;
pub mod health_check_handler;
pub mod overview_handler;
pub mod password_handler;
pub mod session_handler;
pub mod user_handler;
93 changes: 93 additions & 0 deletions src/handlers/overview_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use axum::{extract::State, Json};
use axum_macros::debug_handler;
use bson::doc;
use bson::DateTime;
use woothee::parser::{Parser, WootheeResult};

use crate::core::session::Session;
use crate::errors::Result;
use crate::models::overview_model::OverviewResponse;
use crate::{core::user::User, AppState};

#[debug_handler]
pub async fn get_all_overview_handler(
State(state): State<AppState>,
) -> Result<Json<OverviewResponse>> {
println!(">> HANDLER: get_all_overview_handler called");

let users = User::get_all(&state.mongo_client).await.unwrap();
let user_count = users.len();
let active_user_count = users.iter().filter(|u| u.is_active).count();
let inactive_user_count = users.iter().filter(|u| !u.is_active).count();
let blocked_user_count = users
.iter()
.filter(|u| u.blocked_until.map_or(false, |time| time > DateTime::now()))
.count();

let all_sessions = Session::get_all(&state.mongo_client).await.unwrap();
println!(">> all_sessions Length: {:?}", all_sessions.len());

let active_session_count = all_sessions.iter().filter(|s| !s.is_revoked).count();
let revoked_session_count = all_sessions.iter().filter(|s| s.is_revoked).count();

// create a user-agent map from all_sessions where is_revoked = false
let user_agents: Vec<String> = all_sessions
.iter()
.filter(|s| !s.is_revoked)
.map(|s| s.user_agent.clone())
.collect();

println!(">> user_agents: {:?}", user_agents);

let parser = Parser::new();

// find out os_types, device_types, browser_types from all_sessions using user-agent-parser
let results: Vec<Option<WootheeResult>> =
user_agents.iter().map(|ua| parser.parse(ua)).collect();

// get os_types as a string[] from results
let os_types: Vec<String> = results
.iter()
.map(|r| {
r.as_ref()
.map_or_else(String::new, |result| result.os.to_string())
})
.collect();

// get device_types as a string[] from results
let device_types: Vec<String> = results
.iter()
.map(|r| {
r.as_ref()
.map_or_else(String::new, |result| result.category.to_string())
})
.collect();

// get browser_types as a string[] from results
let browser_types: Vec<String> = results
.iter()
.map(|r| {
r.as_ref()
.map_or_else(String::new, |result| result.name.to_string())
})
.collect();

println!(">> results USER AGENTSSS: {:?}", results);
println!(">> os_types: {:?}", os_types);
println!(">> device_types: {:?}", device_types);
println!(">> browser_types: {:?}", browser_types);

let response = OverviewResponse {
user_count,
active_user_count,
inactive_user_count,
blocked_user_count,
revoked_session_count,
active_session_count,
os_types,
device_types,
browser_types,
};

Ok(Json(response))
}
2 changes: 2 additions & 0 deletions src/handlers/user_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub async fn get_user_email_handler(
role: user.role,
is_active: user.is_active,
email_verified: user.email_verified,
blocked_until: user.blocked_until,
created_at: user.created_at,
updated_at: user.updated_at,
}))
Expand Down Expand Up @@ -223,6 +224,7 @@ pub async fn get_user_id_handler(
role: user.role,
is_active: user.is_active,
email_verified: user.email_verified,
blocked_until: user.blocked_until,
created_at: user.created_at,
updated_at: user.updated_at,
}))
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.merge(routes::user_routes::routes(State(app_state.clone())))
.merge(routes::password_routes::routes(State(app_state.clone())))
.merge(routes::session_routes::routes(State(app_state.clone())))
.merge(routes::overview_routes::routes(State(app_state.clone())))
.layer(middleware::map_response(main_response_mapper))
.layer(middleware::from_fn(with_api_key));

Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod auth_model;
pub mod overview_model;
pub mod password_model;
pub mod session_model;
pub mod user_model;
14 changes: 14 additions & 0 deletions src/models/overview_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OverviewResponse {
pub user_count: usize,
pub active_user_count: usize,
pub inactive_user_count: usize,
pub blocked_user_count: usize,
pub revoked_session_count: usize,
pub active_session_count: usize,
pub os_types: Vec<String>,
pub device_types: Vec<String>,
pub browser_types: Vec<String>,
}
7 changes: 4 additions & 3 deletions src/models/user_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct ToggleUserActivationStatusPayload {
pub struct ToggleUserActivationStatusResponse {
pub message: String,
pub email: String,
pub is_active: bool
pub is_active: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -76,6 +76,7 @@ pub struct UserResponse {
pub email: String,
pub email_verified: bool,
pub is_active: bool,
pub blocked_until: Option<DateTime>,
pub created_at: Option<DateTime>,
pub updated_at: Option<DateTime>,
}
Expand All @@ -98,10 +99,10 @@ pub struct EmailVerificationRequest {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EmailVerificationPayload {
pub req_id: String
pub req_id: String,
}
#[derive(Serialize, Debug, Clone)]
pub struct EmailVerificationResponse {
pub message: String,
pub req_id: String,
}
}
1 change: 1 addition & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth_routes;
pub mod health_check_routes;
pub mod overview_routes;
pub mod password_routes;
pub mod session_routes;
pub mod user_routes;
11 changes: 11 additions & 0 deletions src/routes/overview_routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use axum::{extract::State, routing::get, Router};

use crate::{handlers::overview_handler::get_all_overview_handler, AppState};

pub fn routes(State(state): State<AppState>) -> Router {
let overview_routes = Router::new().route("/get-all", get(get_all_overview_handler));

Router::new()
.nest("/overview", overview_routes)
.with_state(state)
}
19 changes: 19 additions & 0 deletions ui/app/api/overview/get-all/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export async function GET(req: Request) {
const endPoint: (string | undefined) = `${process.env.NEXT_PUBLIC_API_BASE_URL}/api/overview/get-all`

if (endPoint) {
try {
const res = await fetch(endPoint, {
headers: {
'Content-Type': 'application/json', // Set the appropriate content type for your request
'x-api-key': process.env.X_API_KEY!,
},
cache: 'no-cache',
});
const data = await res.json();
return Response.json({ data })
} catch (error) {
console.error('Error during request:', error);
}
}
}
18 changes: 18 additions & 0 deletions ui/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@
--input: 217.2 32.6% 17.5%;
--ring: 224.3 76.3% 48%;
--radius: 0.5rem;

--chart-1-1: 221.2 83.2% 53.3%;
--chart-2-1: 212 95% 68%;
--chart-3-1: 216 92% 60%;
--chart-4-1: 210 98% 78%;
--chart-5-1: 212 97% 87%;

--chart-1-2: 359 2% 90%;
--chart-2-2: 240 1% 74%;
--chart-3-2: 240 1% 58%;
--chart-4-2: 240 1% 42%;
--chart-5-2: 240 2% 26%;

--chart-1-3: 139 65% 20%;
--chart-2-3: 140 74% 44%;
--chart-3-3: 142 88% 28%;
--chart-4-3: 137 55% 15%;
--chart-5-3: 141 40% 9%;
}
}

Expand Down
Loading

0 comments on commit 5bd29b1

Please sign in to comment.