Skip to content

Commit

Permalink
Merge pull request #3 from coreyja/ca/main/checkin-duration
Browse files Browse the repository at this point in the history
Record Duration and Rebrand as UpGuradian
  • Loading branch information
coreyja committed Mar 14, 2024
2 parents 37af5fc + 9ca775a commit b57ab62
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,2 +1,4 @@
/target
.envrc

/public/frontend

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

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

116 changes: 85 additions & 31 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
@@ -1,8 +1,9 @@
[package]
name = "status"
name = "up_guardian"
version = "0.1.0"
edition = "2021"
license = "private"
build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down Expand Up @@ -53,3 +54,6 @@ futures = "0.3.30"
sqlx = "0.7.3"
uuid = { version = "1.6.1", features = ["v4"] }
tower-cookies = { version = "0.10.0", features = ["private", "signed"] }
mime_guess = "2.0.4"
include_dir = { version = "0.7.3", features = ["metadata", "glob"] }
humantime = "2.1.0"
10 changes: 7 additions & 3 deletions Dockerfile
Expand Up @@ -20,7 +20,11 @@ COPY . .
COPY tailwind.config.js .
RUN ./tailwindcss -i src/tailwind.css -o target/tailwind.css

RUN cargo build --release --locked --bin status
RUN curl -fsSL https://bun.sh/install | BUN_INSTALL=/app bash

ENV PATH="/app/bin:${PATH}"

RUN cargo build --release --locked --bin up_guardian

# Start building the final image
FROM debian:stable-slim as final
Expand All @@ -31,8 +35,8 @@ RUN apt-get update && apt-get install -y \
&& rm -rf /var/lib/apt/lists/* \
&& update-ca-certificates

COPY --from=builder /app/target/release/status .
COPY --from=builder /app/target/release/up_guardian .

EXPOSE 3001

ENTRYPOINT ["./status"]
ENTRYPOINT ["./up_guardian"]
9 changes: 9 additions & 0 deletions build.rs
@@ -0,0 +1,9 @@
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=frontend/**/*");
Command::new("bun")
.args(["build", "frontend/index.ts", "--outdir", "public/frontend/"])
.status()
.expect("Failed to build frontend");
}
19 changes: 19 additions & 0 deletions frontend/index.ts
@@ -0,0 +1,19 @@
document.addEventListener("click", function (e) {
if (!e.target) return;

const clickedElement = (e.target as HTMLElement).closest(
"[data-app='ToggleClass']"
);
if (clickedElement) {
const className = clickedElement.getAttribute("data-class")!;
const targetSelector = clickedElement.getAttribute("data-target")!;

const targetElement =
clickedElement.closest(targetSelector) ||
document.querySelector(targetSelector);

if (targetElement) {
targetElement.classList.toggle(className);
}
}
});
1 change: 1 addition & 0 deletions migrations/20240313231211_CreateCron.down.sql
@@ -0,0 +1 @@
DROP TABLE Crons;
16 changes: 16 additions & 0 deletions migrations/20240313231211_CreateCron.up.sql
@@ -0,0 +1,16 @@
CREATE TABLE
Crons (
cron_id UUID PRIMARY KEY,
name TEXT NOT NULL,
last_run_at TIMESTAMP
WITH
TIME ZONE NOT NULL,
created_at TIMESTAMP
WITH
TIME ZONE NOT NULL,
updated_at TIMESTAMP
WITH
TIME ZONE NOT NULL
);

CREATE UNIQUE INDEX idx_crons_name ON Crons (name);
5 changes: 5 additions & 0 deletions migrations/20240314020654_AddDurationToCheckin.down.sql
@@ -0,0 +1,5 @@
-- Add migration script here
-- Add a new column to the checkin table
-- `duration_ms` stores the duration of the checkin in milliseconds
ALTER TABLE Checkins
DROP COLUMN duration_nanos;
5 changes: 5 additions & 0 deletions migrations/20240314020654_AddDurationToCheckin.up.sql
@@ -0,0 +1,5 @@
-- Add migration script here
-- Add a new column to the checkin table
-- `duration_ms` stores the duration of the checkin in milliseconds
ALTER TABLE Checkins
ADD COLUMN duration_nanos BIGINT;
Binary file added public/Logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/Logomark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 13 additions & 7 deletions src/jobs/create_checkin.rs
@@ -1,6 +1,9 @@

use cja::{app_state::AppState as _, jobs::Job};
use miette::IntoDiagnostic;
use serde::{Deserialize, Serialize};

use tokio::time::Instant;
use uuid::Uuid;

use crate::app_state::AppState;
Expand Down Expand Up @@ -32,30 +35,33 @@ impl Job<AppState> for CreateCheckin {
let path = page.path;

let url = format!("https://{domain}{path}");
let now = Instant::now();
let resp = reqwest::get(&url).await;

let (status, outcome) = match resp {
Err(_) => (None, "error"),
Ok(resp) => (
Some(resp.status()),
Ok(resp) => (Some(resp.status()), {
if resp.status().is_success() {
"success"
} else {
"failure"
},
),
}
}),
};
let duration = now.elapsed();
let duration: i64 = duration.as_nanos().try_into().unwrap();

let status: Option<i32> = status.map(|s| s.as_u16().into());

sqlx::query!(
r#"
INSERT INTO Checkins (page_id, status_code, outcome)
VALUES ($1, $2, $3)
INSERT INTO Checkins (page_id, status_code, outcome, duration_nanos)
VALUES ($1, $2, $3, $4)
"#,
self.page_id,
status,
outcome
outcome,
duration
)
.execute(app_state.db())
.await
Expand Down
8 changes: 7 additions & 1 deletion src/routes/current_user/pages.rs
@@ -1,3 +1,5 @@
use std::time::Duration;

use axum::{
extract::{Path, State},
response::{IntoResponse, Redirect},
Expand Down Expand Up @@ -125,10 +127,14 @@ pub async fn show(
ul {
@for checkin in checkins {
li {
(checkin.created_at) " - " (checkin.outcome)
(checkin.created_at.format("%d/%m/%Y %H:%M:%S")) " - " (checkin.outcome)
@if let Some(status) = checkin.status_code {
" - " (status)
}
@if let Some(duration) = checkin.duration_nanos {
@let duration = Duration::from_nanos(duration as u64);
" - " (humantime::format_duration(duration))
}
}
}
}
Expand Down

0 comments on commit b57ab62

Please sign in to comment.