Skip to content

Commit

Permalink
chore: Add new models for CPU count and peers count and display in da…
Browse files Browse the repository at this point in the history
…shboard
  • Loading branch information
aeltorio committed Jun 12, 2024
1 parent 0a23651 commit 2c6cbad
Show file tree
Hide file tree
Showing 17 changed files with 1,397 additions and 299 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@
"database": "${workspaceFolder:sctgdesk-api-server}/db_v2.sqlite3"
}
],
"sqltools.useNodeRuntime": true
"sqltools.useNodeRuntime": true,
"terminal.integrated.env.osx": {
"DATABASE_URL": "sqlite://${workspaceFolder}/db_v2.sqlite3"
},
"rust-analyzer.checkOnSave": true,
"rust-analyzer.runnables.extraEnv": {
"DATABASE_URL": "sqlite://${workspaceFolder}/db_v2.sqlite3"
}
}
74 changes: 67 additions & 7 deletions libs/state/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ use utils::types::AddressBook;
use utils::AbPeer;
use utils::AbRule;
use utils::AbTag;
use utils::CpuCount;
use utils::Group;
use utils::Peer;
use utils::Platform;
use utils::UpdateUserRequest;
use utils::UserListResponse;

Expand Down Expand Up @@ -1429,9 +1431,9 @@ impl Database {
let ab_guid = ab_guid.unwrap().as_bytes().to_vec();
let user_guid = if (rule.user.is_some()) {

Check warning on line 1432 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
let uuid = Uuid::parse_str(&rule.user.unwrap());
if (uuid.is_err()){
if (uuid.is_err()) {

Check warning on line 1434 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
None
}else{
} else {
Some(uuid.unwrap().as_bytes().to_vec())
}
} else {
Expand All @@ -1440,9 +1442,9 @@ impl Database {

let group_guid = if (rule.group.is_some()) {

Check warning on line 1443 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
let uuid = Uuid::parse_str(&rule.group.unwrap());
if (uuid.is_err()){
if (uuid.is_err()) {

Check warning on line 1445 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
None
}else{
} else {
Some(uuid.unwrap().as_bytes().to_vec())
}
} else {
Expand All @@ -1469,8 +1471,7 @@ impl Database {
Some(())
}

pub async fn add_shared_address_book(&self, name: &str, owner: UserId) -> Option<String>
{
pub async fn add_shared_address_book(&self, name: &str, owner: UserId) -> Option<String> {
let mut conn = self.pool.acquire().await.unwrap();
let ab_guid = Uuid::new_v4().as_bytes().to_vec();
let res = sqlx::query!(
Expand All @@ -1488,7 +1489,66 @@ impl Database {
log::error!("add_shared_address_book error: {:?}", res);
return None;
}

Some(guid_into_uuid(ab_guid)?)
}
pub async fn get_peers_count(&self, platform: Platform) -> u32 {
let mut conn = self.pool.acquire().await.unwrap();
let filter = match platform {
Platform::Windows => "windows%",
Platform::Linux => "linux%",
Platform::MacOS => "macos%",
Platform::Android => "android%",
Platform::All => "%",
_ => "unknown%",

Check warning on line 1503 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unreachable pattern
};
let res = sqlx::query!(
r#"
SELECT
COUNT(*) as count
FROM
peer
WHERE
json_extract(info,'$.os') like ?
"#,
filter
)
.fetch_one(&mut conn)
.await
.ok();
if res.is_none() {
return 0;
}
let res = res.unwrap();
res.count as u32
}

pub async fn get_cpus_count(&self) -> Vec<CpuCount> {
let mut conn = self.pool.acquire().await.unwrap();
let res = sqlx::query!(
r#"
SELECT
COALESCE(trim(json_extract(info,'$.cpu')),"unknown") as cpu,
COUNT(*) AS machine_count
FROM
peer
GROUP BY
cpu;
"#
)
.fetch_all(&mut conn)
.await
.ok();
if res.is_none() {
return Vec::new();
}
let res = res.unwrap();
let mut cpu_counts = Vec::new();
for row in res {
let cpu = row.cpu;
let total = row.machine_count as u32;
cpu_counts.push(CpuCount { cpu, total });
}
cpu_counts
}
}
11 changes: 10 additions & 1 deletion libs/state/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use oauth2::ProviderConfig;

use tokio::sync::RwLock;
use utils::{
AbPeer, AbRule, AbTag, AddUserRequest, AddressBook, Group, OidcState, Peer, Token, UpdateUserRequest, UserListResponse
AbPeer, AbRule, AbTag, AddUserRequest, AddressBook, CpuCount, Group, OidcState, Peer, Platform, Token, UpdateUserRequest, UserListResponse
};

pub struct ApiState {
Expand Down Expand Up @@ -692,4 +692,13 @@ impl ApiState {
pub async fn add_shared_address_book(&self, name: &str, owner: UserId) -> Option<String> {
self.db.add_shared_address_book(name, owner).await
}

pub async fn get_peers_count(&self, platform: Platform) -> u32 {
self.db.get_peers_count(platform).await
}

pub async fn get_cpus_count(&self) -> Vec<CpuCount>{
self.db.get_cpus_count().await
}
}

14 changes: 14 additions & 0 deletions libs/utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ pub struct PeersResponse {
pub data: Vec<Peer>,
}

#[derive(Serialize, Deserialize, Clone, JsonSchema)]
pub struct PeersCountResponse {
pub total: u32,
}
#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug, Default)]
pub struct PeerInfo {
pub cpu: Option<String>,
Expand Down Expand Up @@ -746,4 +750,14 @@ pub struct AbRuleAddRequest {
#[derive(Serialize, Deserialize, Clone, JsonSchema)]
pub struct AbRuleDeleteRequest {
pub guid: String, // rule guid
}

pub enum Platform {
Windows,MacOS,Linux,Android,All
}

#[derive(Serialize, Deserialize, Clone, JsonSchema)]
pub struct CpuCount {
pub cpu: String,
pub total: u32,
}
Loading

0 comments on commit 2c6cbad

Please sign in to comment.