Skip to content

Commit

Permalink
Merge pull request #67 from Rajdip019/dashboard-setup
Browse files Browse the repository at this point in the history
Dashboard setup
  • Loading branch information
Rajdip019 authored Jul 26, 2024
2 parents 698cca3 + a36b11f commit 0a769f3
Show file tree
Hide file tree
Showing 16 changed files with 1,232 additions and 149 deletions.
58 changes: 58 additions & 0 deletions src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
},
};
use bson::{doc, DateTime};
use woothee::parser::Parser;
use futures::StreamExt;
use mongodb::{Client, Collection};
use serde::{Deserialize, Serialize};
Expand All @@ -22,6 +23,12 @@ pub struct Session {
pub id_token: String,
pub refresh_token: String,
pub user_agent: String,
pub os: String,
pub os_version: String,
pub vendor: String,
pub device: String,
pub browser: String,
pub browser_version: String,
pub is_revoked: bool,
pub created_at: DateTime,
pub updated_at: DateTime,
Expand All @@ -39,13 +46,46 @@ impl Session {
Err(_) => "".to_string(),
};

let parser = Parser::new();

let user_agent_data = parser.parse(user_agent);

let os = user_agent_data.as_ref().map_or_else(String::new, |result| result.os.to_string());

let os_version = user_agent_data
.as_ref()
.map_or_else(String::new, |result| result.os_version.to_string());

let vendor = user_agent_data
.as_ref()
.map_or_else(String::new, |result| result.vendor.to_string());

let device = user_agent_data
.as_ref()
.map_or_else(String::new, |result| result.category.to_string());

let browser = user_agent_data
.as_ref()
.map_or_else(String::new, |result| result.name.to_string());

let browser_version = user_agent_data
.as_ref()
.map_or_else(String::new, |result| result.version.to_string());


Self {
uid: user.uid.to_string(),
session_id: Uuid::new_v4().to_string(),
email: user.email.to_string(),
id_token,
refresh_token,
user_agent: user_agent.to_string(),
os,
os_version,
vendor,
device,
browser,
browser_version,
is_revoked: false,
created_at: DateTime::now(),
updated_at: DateTime::now(),
Expand Down Expand Up @@ -297,6 +337,12 @@ impl Session {
session_id: decrypted_session.session_id,
email: decrypted_session.email,
user_agent: decrypted_session.user_agent,
os: decrypted_session.os,
os_version: decrypted_session.os_version,
vendor: decrypted_session.vendor,
device: decrypted_session.device,
browser: decrypted_session.browser,
browser_version: decrypted_session.browser_version,
is_revoked: decrypted_session.is_revoked,
created_at: decrypted_session.created_at,
updated_at: decrypted_session.updated_at,
Expand Down Expand Up @@ -396,6 +442,12 @@ impl Session {
session_id: decrypted_session.session_id,
email: decrypted_session.email,
user_agent: decrypted_session.user_agent,
os: decrypted_session.os,
os_version: decrypted_session.os_version,
vendor: decrypted_session.vendor,
device: decrypted_session.device,
browser: decrypted_session.browser,
browser_version: decrypted_session.browser_version,
is_revoked: decrypted_session.is_revoked,
created_at: decrypted_session.created_at,
updated_at: decrypted_session.updated_at,
Expand Down Expand Up @@ -452,6 +504,12 @@ impl Session {
session_id: data.session_id,
email: data.email,
user_agent: data.user_agent,
os: data.os,
os_version: data.os_version,
vendor: data.vendor,
device: data.device,
browser: data.browser,
browser_version: data.browser_version,
is_revoked: data.is_revoked,
created_at: data.created_at,
updated_at: data.updated_at,
Expand Down
41 changes: 6 additions & 35 deletions src/handlers/overview_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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;
Expand Down Expand Up @@ -30,49 +29,21 @@ pub async fn get_all_overview_handler(
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
let os_types: Vec<String> = all_sessions
.iter()
.filter(|s| !s.is_revoked)
.map(|s| s.user_agent.clone())
.map(|session| session.os.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
let device_types: Vec<String> = all_sessions
.iter()
.map(|r| {
r.as_ref()
.map_or_else(String::new, |result| result.category.to_string())
})
.map(|session| session.device.clone())
.collect();

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

println!(">> results USER AGENTSSS: {:?}", results);
println!(">> os_types: {:?}", os_types);
println!(">> device_types: {:?}", device_types);
println!(">> browser_types: {:?}", browser_types);
Expand Down
23 changes: 14 additions & 9 deletions src/models/session_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ pub struct VerifySession {

#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct SessionResponse {
pub uid : String,
pub session_id : String,
pub email : String,
pub user_agent : String,
pub is_revoked : bool,
pub created_at : DateTime,
pub updated_at : DateTime,
pub uid: String,
pub session_id: String,
pub email: String,
pub user_agent: String,
pub os: String,
pub os_version: String,
pub vendor: String,
pub device: String,
pub browser: String,
pub browser_version: String,
pub is_revoked: bool,
pub created_at: DateTime,
pub updated_at: DateTime,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -54,7 +60,6 @@ pub struct RevokeSessionsResult {
pub message: String,
}


#[derive(Deserialize, Debug, Clone)]
pub struct DeleteAllSessionsPayload {
pub uid: String,
Expand All @@ -80,4 +85,4 @@ pub struct DeleteSessionsResult {
pub struct SessionDetailsPayload {
pub uid: String,
pub session_id: String,
}
}
25 changes: 25 additions & 0 deletions ui/app/api/user/get-recent/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export async function POST(req: Request) {
const endPoint: (string | undefined) = `${process.env.NEXT_PUBLIC_API_BASE_URL}/api/user/get-recent`;

const { limit } = await req.json();

if (endPoint) {
try {
const res = await fetch(endPoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json', // Set the appropriate content type for your request
'x-api-key': process.env.X_API_KEY!,
},
cache: 'no-cache',
body: JSON.stringify({
limit
}),
});
const data = await res.json();
return Response.json({ data })
} catch (error) {
console.error('Error during request:', error);
}
}
}
2 changes: 1 addition & 1 deletion ui/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function RootLayout({
<TooltipProvider>
<main className="w-screen">
<Navbar />
<div className="flex items-start h-[calc(100vh-4rem)]">
<div className="flex items-start min-h-[calc(100vh-4rem)]">
<Sidebar items={AppPages} />
<div className="p-4 ml-56 min-h-[calc(100vh-5rem)] w-[calc(100vw-14rem)] mt-20">
{children}
Expand Down
Loading

0 comments on commit 0a769f3

Please sign in to comment.