stat page and cache working

This commit is contained in:
2025-06-25 18:03:44 +02:00
parent 0878b7dcec
commit 2d373da4c5
6 changed files with 248 additions and 5 deletions

View File

@@ -25,6 +25,8 @@ enum Commands {
}, },
/// Watch for changes in the posts directory /// Watch for changes in the posts directory
Watch, Watch,
/// Show Rust parser statistics
Rsparseinfo,
} }
fn main() { fn main() {
@@ -65,5 +67,8 @@ fn main() {
std::thread::sleep(std::time::Duration::from_secs(60)); std::thread::sleep(std::time::Duration::from_secs(60));
} }
} }
Commands::Rsparseinfo => {
println!("{}", markdown::rsparseinfo());
}
} }
} }

View File

@@ -11,11 +11,14 @@ use ammonia::clean;
use slug::slugify; use slug::slugify;
use notify::{RecursiveMode, RecommendedWatcher, Watcher, Config}; use notify::{RecursiveMode, RecommendedWatcher, Watcher, Config};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::time::Duration; use std::time::{Duration, Instant};
use syntect::highlighting::{ThemeSet, Style}; use syntect::highlighting::{ThemeSet, Style};
use syntect::parsing::SyntaxSet; use syntect::parsing::SyntaxSet;
use syntect::html::{highlighted_html_for_string, IncludeBackground}; use syntect::html::{highlighted_html_for_string, IncludeBackground};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::RwLock;
use serde_json;
#[derive(Debug, Deserialize, Clone, serde::Serialize)] #[derive(Debug, Deserialize, Clone, serde::Serialize)]
pub struct PostFrontmatter { pub struct PostFrontmatter {
@@ -37,6 +40,19 @@ pub struct Post {
pub author: String, pub author: String,
} }
#[derive(Debug, Clone, serde::Serialize, Default)]
pub struct PostStats {
pub slug: String,
pub cache_hits: u64,
pub cache_misses: u64,
pub last_interpret_time_ms: u128,
pub last_compile_time_ms: u128,
}
static POST_CACHE: Lazy<RwLock<HashMap<String, Post>>> = Lazy::new(|| RwLock::new(HashMap::new()));
static ALL_POSTS_CACHE: Lazy<RwLock<Option<Vec<Post>>>> = Lazy::new(|| RwLock::new(None));
static POST_STATS: Lazy<RwLock<HashMap<String, PostStats>>> = Lazy::new(|| RwLock::new(HashMap::new()));
fn get_posts_directory() -> PathBuf { fn get_posts_directory() -> PathBuf {
let candidates = [ let candidates = [
"./posts", "./posts",
@@ -111,7 +127,27 @@ static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
builder builder
}); });
pub fn rsparseinfo() -> String {
let stats = POST_STATS.read().unwrap();
serde_json::to_string(&stats.values().collect::<Vec<_>>()).unwrap_or_else(|_| "[]".to_string())
}
pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>> { pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>> {
let start = Instant::now();
let mut stats = POST_STATS.write().unwrap();
let entry = stats.entry(slug.to_string()).or_insert_with(|| PostStats {
slug: slug.to_string(),
..Default::default()
});
// Try cache first
if let Some(post) = POST_CACHE.read().unwrap().get(slug).cloned() {
entry.cache_hits += 1;
entry.last_interpret_time_ms = 0;
entry.last_compile_time_ms = 0;
return Ok(post);
}
entry.cache_misses += 1;
drop(stats); // Release lock before heavy work
let posts_dir = get_posts_directory(); let posts_dir = get_posts_directory();
let file_path = posts_dir.join(format!("{}.md", slug)); let file_path = posts_dir.join(format!("{}.md", slug));
let file_content = fs::read_to_string(&file_path)?; let file_content = fs::read_to_string(&file_path)?;
@@ -202,7 +238,9 @@ pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>>
let sanitized_html = AMMONIA.clean(&html_output).to_string(); let sanitized_html = AMMONIA.clean(&html_output).to_string();
Ok(Post { let interpret_time = start.elapsed();
let compile_start = Instant::now();
let post = Post {
slug: slug.to_string(), slug: slug.to_string(),
title: front.title, title: front.title,
date: front.date, date: front.date,
@@ -211,10 +249,26 @@ pub fn get_post_by_slug(slug: &str) -> Result<Post, Box<dyn std::error::Error>>
content: sanitized_html, content: sanitized_html,
created_at: created_at.to_rfc3339(), created_at: created_at.to_rfc3339(),
author: std::env::var("BLOG_OWNER").unwrap_or_else(|_| "Anonymous".to_string()), author: std::env::var("BLOG_OWNER").unwrap_or_else(|_| "Anonymous".to_string()),
}) };
let compile_time = compile_start.elapsed();
// Insert into cache
POST_CACHE.write().unwrap().insert(slug.to_string(), post.clone());
// Update stats
let mut stats = POST_STATS.write().unwrap();
let entry = stats.entry(slug.to_string()).or_insert_with(|| PostStats {
slug: slug.to_string(),
..Default::default()
});
entry.last_interpret_time_ms = interpret_time.as_millis();
entry.last_compile_time_ms = compile_time.as_millis();
Ok(post)
} }
pub fn get_all_posts() -> Result<Vec<Post>, Box<dyn std::error::Error>> { pub fn get_all_posts() -> Result<Vec<Post>, Box<dyn std::error::Error>> {
// Try cache first
if let Some(posts) = ALL_POSTS_CACHE.read().unwrap().clone() {
return Ok(posts);
}
let posts_dir = get_posts_directory(); let posts_dir = get_posts_directory();
let mut posts = Vec::new(); let mut posts = Vec::new();
for entry in fs::read_dir(posts_dir)? { for entry in fs::read_dir(posts_dir)? {
@@ -228,6 +282,8 @@ pub fn get_all_posts() -> Result<Vec<Post>, Box<dyn std::error::Error>> {
} }
} }
posts.sort_by(|a, b| b.created_at.cmp(&a.created_at)); posts.sort_by(|a, b| b.created_at.cmp(&a.created_at));
// Cache the result
*ALL_POSTS_CACHE.write().unwrap() = Some(posts.clone());
Ok(posts) Ok(posts)
} }
@@ -240,11 +296,13 @@ pub fn watch_posts<F: Fn() + Send + 'static>(on_change: F) -> notify::Result<Rec
let (tx, rx) = channel(); let (tx, rx) = channel();
let mut watcher = RecommendedWatcher::new(tx, Config::default())?; let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
watcher.watch(get_posts_directory().as_path(), RecursiveMode::Recursive)?; watcher.watch(get_posts_directory().as_path(), RecursiveMode::Recursive)?;
std::thread::spawn(move || { std::thread::spawn(move || {
loop { loop {
match rx.recv() { match rx.recv() {
Ok(_event) => { Ok(_event) => {
// Invalidate caches on any change
POST_CACHE.write().unwrap().clear();
*ALL_POSTS_CACHE.write().unwrap() = None;
on_change(); on_change();
}, },
Err(e) => { Err(e) => {
@@ -254,6 +312,5 @@ pub fn watch_posts<F: Fn() + Send + 'static>(on_change: F) -> notify::Result<Rec
} }
} }
}); });
Ok(watcher) Ok(watcher)
} }

View File

@@ -256,6 +256,16 @@ export default function ManagePage() {
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /> <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg> </svg>
</button> </button>
<Link
href="/admin/manage/rust-status"
className="px-4 py-3 sm:py-2 bg-teal-600 text-white rounded hover:bg-teal-700 transition-colors text-base font-medium flex items-center"
title="Rust Parser Status"
>
<svg className="h-5 w-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M12 20a8 8 0 100-16 8 8 0 000 16z" />
</svg>
Rust Parser Status
</Link>
<button <button
onClick={handleLogout} onClick={handleLogout}
className="px-4 py-3 sm:py-2 bg-red-600 text-white rounded hover:bg-red-700 text-base font-medium" className="px-4 py-3 sm:py-2 bg-red-600 text-white rounded hover:bg-red-700 text-base font-medium"

View File

@@ -0,0 +1,74 @@
import React, { useEffect, useState } from 'react';
interface PostStats {
slug: string;
cache_hits: number;
cache_misses: number;
last_interpret_time_ms: number;
last_compile_time_ms: number;
}
export default function RustStatusPage() {
const [stats, setStats] = useState<PostStats[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchStats = async () => {
setLoading(true);
setError(null);
try {
const res = await fetch('/api/admin/posts?rsparseinfo=1');
if (!res.ok) throw new Error('Failed to fetch stats');
const data = await res.json();
setStats(data);
} catch (e: any) {
setError(e.message || 'Unknown error');
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchStats();
const interval = setInterval(fetchStats, 5000);
return () => clearInterval(interval);
}, []);
return (
<div className="p-8 max-w-4xl mx-auto">
<h1 className="text-2xl font-bold mb-6">Rust Parser Status</h1>
{loading && <div>Loading...</div>}
{error && <div className="text-red-500">{error}</div>}
{!loading && !error && (
<div className="overflow-x-auto">
<table className="min-w-full border border-gray-300 bg-white shadow-md rounded">
<thead>
<tr className="bg-gray-100">
<th className="px-4 py-2 text-left">Slug</th>
<th className="px-4 py-2 text-right">Cache Hits</th>
<th className="px-4 py-2 text-right">Cache Misses</th>
<th className="px-4 py-2 text-right">Last Interpret Time (ms)</th>
<th className="px-4 py-2 text-right">Last Compile Time (ms)</th>
</tr>
</thead>
<tbody>
{stats.length === 0 ? (
<tr><td colSpan={5} className="text-center py-4">No stats available.</td></tr>
) : (
stats.map(stat => (
<tr key={stat.slug} className="border-t">
<td className="px-4 py-2 font-mono">{stat.slug}</td>
<td className="px-4 py-2 text-right">{stat.cache_hits}</td>
<td className="px-4 py-2 text-right">{stat.cache_misses}</td>
<td className="px-4 py-2 text-right">{stat.last_interpret_time_ms}</td>
<td className="px-4 py-2 text-right">{stat.last_compile_time_ms}</td>
</tr>
))
)}
</tbody>
</table>
</div>
)}
</div>
);
}

View File

@@ -0,0 +1,75 @@
'use client';
import React, { useEffect, useState } from 'react';
interface PostStats {
slug: string;
cache_hits: number;
cache_misses: number;
last_interpret_time_ms: number;
last_compile_time_ms: number;
}
export default function RustStatusPage() {
const [stats, setStats] = useState<PostStats[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchStats = async () => {
setLoading(true);
setError(null);
try {
const res = await fetch('/api/admin/posts?rsparseinfo=1');
if (!res.ok) throw new Error('Failed to fetch stats');
const data = await res.json();
setStats(data);
} catch (e: any) {
setError(e.message || 'Unknown error');
} finally {
setLoading(false);
}
};
React.useEffect(() => {
fetchStats();
const interval = setInterval(fetchStats, 5000);
return () => clearInterval(interval);
}, []);
return (
<div className="p-8 max-w-4xl mx-auto">
<h1 className="text-2xl font-bold mb-6">Rust Parser Status</h1>
{loading && <div>Loading...</div>}
{error && <div className="text-red-500">{error}</div>}
{!loading && !error && (
<div className="overflow-x-auto">
<table className="min-w-full border border-gray-300 bg-white shadow-md rounded">
<thead>
<tr className="bg-gray-100">
<th className="px-4 py-2 text-left">Slug</th>
<th className="px-4 py-2 text-right">Cache Hits</th>
<th className="px-4 py-2 text-right">Cache Misses</th>
<th className="px-4 py-2 text-right">Last Interpret Time (ms)</th>
<th className="px-4 py-2 text-right">Last Compile Time (ms)</th>
</tr>
</thead>
<tbody>
{stats.length === 0 ? (
<tr><td colSpan={5} className="text-center py-4">No stats available.</td></tr>
) : (
stats.map(stat => (
<tr key={stat.slug} className="border-t">
<td className="px-4 py-2 font-mono">{stat.slug}</td>
<td className="px-4 py-2 text-right">{stat.cache_hits}</td>
<td className="px-4 py-2 text-right">{stat.cache_misses}</td>
<td className="px-4 py-2 text-right">{stat.last_interpret_time_ms}</td>
<td className="px-4 py-2 text-right">{stat.last_compile_time_ms}</td>
</tr>
))
)}
</tbody>
</table>
</div>
)}
</div>
);
}

View File

@@ -3,6 +3,7 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import matter from 'gray-matter'; import matter from 'gray-matter';
import { getPostsDirectory } from '@/lib/postsDirectory'; import { getPostsDirectory } from '@/lib/postsDirectory';
import { spawnSync } from 'child_process';
const postsDirectory = getPostsDirectory(); const postsDirectory = getPostsDirectory();
@@ -48,6 +49,27 @@ export async function POST(request: Request) {
} }
export async function GET(request: Request) { export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const info = searchParams.get('rsparseinfo');
if (info === '1') {
// Call the Rust backend for parser stats
const rustResult = spawnSync(
process.cwd() + '/markdown_backend/target/release/markdown_backend',
['rsparseinfo'],
{ encoding: 'utf-8' }
);
if (rustResult.status === 0 && rustResult.stdout) {
return new Response(rustResult.stdout, {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} else {
return new Response(JSON.stringify({ error: rustResult.stderr || rustResult.error }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
// Return the current pinned.json object // Return the current pinned.json object
try { try {
const pinnedPath = path.join(process.cwd(), 'posts', 'pinned.json'); const pinnedPath = path.join(process.cwd(), 'posts', 'pinned.json');