-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wipfeat: format + botstats query * feat: bot statistics on home page
- Loading branch information
1 parent
79abdb8
commit dde69ad
Showing
7 changed files
with
165 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::error::Error; | ||
|
||
use crate::{models::{Test, User, Prompt}, Context}; | ||
use actix_web::{get, web, HttpResponse, Responder}; | ||
use mongodb::{bson::doc, Collection}; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize)] | ||
struct BotStats { | ||
total_tests: u64, | ||
total_users: u64, | ||
total_prompts: u64, | ||
} | ||
|
||
#[get("/botstats")] | ||
async fn botstats(context: web::Data<Context>) -> impl Responder { | ||
|
||
match botstats_query(context).await { | ||
Ok(stats) => HttpResponse::Ok().json(stats), | ||
Err(e) => HttpResponse::InternalServerError().body(e.to_string()), | ||
} | ||
|
||
} | ||
|
||
async fn botstats_query(context: web::Data<Context>) -> Result<BotStats, Box<dyn Error>> { | ||
let tests: Collection<Test> = context.db.collection("testsv2"); | ||
let users: Collection<User> = context.db.collection("usersv2"); | ||
let prompts: Collection<Prompt> = context.db.collection("prompts"); | ||
|
||
let total_tests = tests.count_documents(doc! {}, None).await?; | ||
let total_users = users.count_documents(doc! {}, None).await?; | ||
let total_prompts = prompts.count_documents(doc! {}, None).await?; | ||
|
||
Ok(BotStats { | ||
total_tests, | ||
total_users, | ||
total_prompts, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
mod prompts; | ||
mod botstats; | ||
|
||
use actix_web::web; | ||
|
||
pub fn api_config(cfg: &mut web::ServiceConfig) { | ||
cfg | ||
.service(prompts::prompts); | ||
.service(prompts::prompts) | ||
.service(botstats::botstats); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use client" | ||
|
||
import axios from "axios" | ||
import {useEffect, useState} from "react" | ||
|
||
type BotStats = { | ||
total_tests: number, | ||
total_users: number, | ||
total_prompts: number | ||
} | ||
|
||
const BotStats = ({className}: {className: string}) => { | ||
const [botStats, setBotStats] = useState<BotStats>() | ||
useEffect(() => { | ||
async function query() { | ||
const res = await axios.get("/api/botstats") | ||
setBotStats(res.data as BotStats) | ||
|
||
} | ||
query() | ||
}, []) | ||
|
||
return ( | ||
<div className={className}> | ||
<div className="border border-zinc-500 my-3 hidden md:block md:w-[600px]" /> | ||
<div className={`flex flex-col items-center gap-6 md:flex-row md:gap-[100px] justify-center`}> | ||
<div className="flex flex-col gap-2 items-start"> | ||
<div className="text-[#F51A1F] text-3xl">{botStats?.total_tests}+</div> | ||
<div className="text-zinc-300 text-md">Tests Served</div> | ||
</div> | ||
|
||
<div className="flex flex-col gap-2 items-start"> | ||
<div className="text-orange-400 text-3xl">{botStats?.total_users}</div> | ||
<div className="text-zinc-300 text-md">Active Users</div> | ||
</div> | ||
|
||
<div className="flex flex-col gap-2 items-start"> | ||
<div className="text-amber-400 text-3xl">{botStats?.total_prompts}</div> | ||
<div className="text-zinc-300 text-md">Typing Prompts</div> | ||
</div> | ||
</div> | ||
<div className="border border-zinc-500 my-3 hidden md:block md:w-[600px]" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default BotStats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters