Oberfläche nach Durchsicht: Kalender, Suchen, eigene Verwaltungsseiten, gemeinsame Gruppenkontingente

- Übersicht mit Anfragen diesen Monat; Aufzeichnungen mit Key-Name und Präfix
- Ollama-API zusätzlich unter /ollama; Endpunkte in der Oberfläche einheitlich
- Gruppen & Kontingente nur eigene; Alle Gruppen & Kontingente und Alle Keys
  für Administratoren, Keys meiner Kontingente für Lecturer, jeweils mit Suche
- Kontingentformular: Modelle alle oder ausgewählte (nach Schnittstelle, aus
  GET /omega/api/models), Wochentage zum Anklicken, Uhrzeit- und Datumsfelder,
  je Person oder für die Gruppe gemeinsam (Spalte quotas.shared)
- Auswahl beim Key-Anlegen zeigt, von wem ein Kontingent kommt
- Zeitplan als Wochenkalender wie in den Mockups
- Benutzer mit Suche und Ausblenden gesperrter Konten
- Audit-Log mit deutschen Bezeichnungen, verständlichen Details und Suche
- Gültigkeit um Mitternacht als "bis einschließlich"; start.sh wartet auf die Engines

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stephan RehfeldandClaude Opus 5.5 committed 2026-11-20 18:31:05 +01:00
1 parent d753fd9285
commit 11dbc1c781
18 files changed
+805 -164

No files matched your search

+55 -21
View File
@@ -158,7 +158,8 @@ impl Db {
requests_hour INTEGER, requests_day INTEGER, requests_week INTEGER,
requests_month INTEGER, requests_total INTEGER,
note TEXT NOT NULL,
created_at INTEGER NOT NULL
created_at INTEGER NOT NULL,
shared INTEGER NOT NULL DEFAULT 0
);
-- Verbrauch je Key bzw. Kontingent und Zeitraum (z. B. day:2026-11-20). Die Zähler
-- bleiben erhalten, wenn alte Einträge in usage gelöscht werden.
@@ -296,7 +297,16 @@ impl Db {
)?;
let id = conn.last_insert_rowid();
drop(conn);
self.audit(actor, "key.create", &format!("id={id} name={name} super={is_super} quota={quota_id:?} user={user_id:?}"))?;
let owner = match user_id {
Some(u) => self.user(u)?.map(|u| u.username).unwrap_or_else(|| format!("#{u}")),
None => "den Betreiber".into(),
};
let kind = match (is_super, quota_id) {
(true, _) => "Super-Key".to_string(),
(false, Some(q)) => format!("Kontingent »{}«", self.quota(q)?.map(|q| q.name).unwrap_or_default()),
(false, None) => "unbegrenzt".into(),
};
self.audit(actor, "key.create", &format!("Key »{name}« (#{id}) für {owner}, {kind}"))?;
Ok((id, key))
}
@@ -323,12 +333,13 @@ impl Db {
}
pub fn revoke_key(&self, actor: &str, id: i64) -> Result<bool> {
let name = self.key(id)?.map(|k| k.name).unwrap_or_default();
let changed = self.conn().execute(
"UPDATE api_keys SET revoked_at = ?1 WHERE id = ?2 AND revoked_at IS NULL",
params![now(), id],
)?;
if changed > 0 {
self.audit(actor, "key.revoke", &format!("id={id}"))?;
self.audit(actor, "key.revoke", &format!("Key »{name}« (#{id})"))?;
}
Ok(changed > 0)
}
@@ -462,7 +473,7 @@ impl Db {
self.audit(
actor,
"schedule.add",
&format!("id={id} {} {} {}", e.kind_name(), e.subject(), e.time.describe()),
&format!("{} {}, {} (#{id})", e.kind_label(), e.subject(), e.time.describe()),
)?;
Ok(id)
}
@@ -546,7 +557,7 @@ impl Db {
pub fn remove_schedule(&self, actor: &str, id: i64) -> Result<bool> {
let changed = self.conn().execute("DELETE FROM schedule WHERE id = ?1", params![id])?;
if changed > 0 {
self.audit(actor, "schedule.remove", &format!("id={id}"))?;
self.audit(actor, "schedule.remove", &format!("Eintrag #{id}"))?;
}
Ok(changed > 0)
}
@@ -564,7 +575,11 @@ impl Db {
if changed == 0 {
bail!("kein Key mit ID {key_id}, oder es ist ein Super-Key");
}
self.audit(actor, "key.quota", &format!("id={key_id} quota={quota_id:?}"))
let quota = match quota_id {
Some(q) => format!("Kontingent »{}«", self.quota(q)?.map(|q| q.name).unwrap_or_default()),
None => "unbegrenzt".into(),
};
self.audit(actor, "key.quota", &format!("Key #{key_id}: {quota}"))
}
pub fn create_quota(&self, actor: &str, q: &Quota) -> Result<i64> {
@@ -586,22 +601,22 @@ impl Db {
conn.execute(
"INSERT INTO quotas (name, valid_from, valid_until, days, time_start, time_end, models,
tokens_hour, tokens_day, tokens_week, tokens_month, tokens_total, requests_hour, requests_day, requests_week, requests_month, requests_total, note, created_at,
owner_id, group_id, user_id)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22)",
owner_id, group_id, user_id, shared)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22, ?23)",
params![
q.name, q.time.from, q.time.until, days, start, end, models, limits[0], limits[1], limits[2],
limits[3], limits[4], limits[5], limits[6], limits[7], limits[8], limits[9], q.note, now(),
q.owner_id, q.group_id, q.user_id
q.owner_id, q.group_id, q.user_id, q.shared
],
)?;
let id = conn.last_insert_rowid();
drop(conn);
let target = match (q.group_id, q.user_id) {
(Some(g), _) => format!(" group={g}"),
(_, Some(u)) => format!(" user={u}"),
(Some(g), _) => format!(" für Gruppe {}", self.group(g)?.map(|g| g.name).unwrap_or_default()),
(_, Some(u)) => format!(" für {}", self.user(u)?.map(|u| u.username).unwrap_or_default()),
_ => String::new(),
};
self.audit(actor, "quota.create", &format!("id={id} {}{target}: {}", q.name, q.describe()))?;
self.audit(actor, "quota.create", &format!("Kontingent »{}« (#{id}){target}: {}", q.name, q.describe()))?;
Ok(id)
}
@@ -609,7 +624,7 @@ impl Db {
let conn = self.conn();
let mut stmt = conn.prepare(&format!(
"SELECT id, name, valid_from, valid_until, days, time_start, time_end, models, tokens_hour, tokens_day, tokens_week, tokens_month, tokens_total, requests_hour, requests_day, requests_week, requests_month, requests_total, note,
owner_id, group_id, user_id
owner_id, group_id, user_id, shared
FROM quotas {filter} ORDER BY id"
))?;
let mut rows = stmt.query(args)?;
@@ -639,6 +654,7 @@ impl Db {
owner_id: r.get(19)?,
group_id: r.get(20)?,
user_id: r.get(21)?,
shared: r.get(22)?,
});
}
Ok(list)
@@ -654,6 +670,7 @@ impl Db {
/// Nur ein Kontingent, an dem keine Keys mehr hängen, kann entfernt werden.
pub fn remove_quota(&self, actor: &str, id: i64) -> Result<()> {
let name = self.quota(id)?.map(|q| q.name).unwrap_or_default();
let conn = self.conn();
let keys: i64 = conn.query_row("SELECT count(*) FROM api_keys WHERE quota_id = ?1", params![id], |r| r.get(0))?;
if keys > 0 {
@@ -664,7 +681,7 @@ impl Db {
}
conn.execute("DELETE FROM usage_counters WHERE scope = 'quota' AND scope_id = ?1", params![id])?;
drop(conn);
self.audit(actor, "quota.remove", &format!("id={id}"))
self.audit(actor, "quota.remove", &format!("Kontingent »{name}« (#{id})"))
}
/// Verbrauch in den Zeiträumen, in die t fällt
@@ -724,9 +741,22 @@ impl Db {
Ok(())
}
fn scopes(key_id: i64, user_id: Option<i64>, quota_id: Option<i64>) -> Vec<Scope> {
/// Wo eine Anfrage gezählt wird: im System, beim Key und beim Kontingent je Person. Ein
/// gemeinsames Gruppenkontingent zählt zusätzlich im Topf der Gruppe (Benutzer 0).
fn scopes(conn: &Connection, key_id: i64, user_id: Option<i64>, quota_id: Option<i64>) -> Result<Vec<Scope>> {
let user = user_id.unwrap_or(0);
[Scope::System, Scope::Key(key_id)].into_iter().chain(quota_id.map(|q| Scope::Quota(q, user))).collect()
let mut scopes = vec![Scope::System, Scope::Key(key_id)];
if let Some(q) = quota_id {
scopes.push(Scope::Quota(q, user));
let shared: bool = conn
.query_row("SELECT shared FROM quotas WHERE id = ?1", params![q], |r| r.get(0))
.optional()?
.unwrap_or(false);
if shared && user != 0 {
scopes.push(Scope::Quota(q, 0));
}
}
Ok(scopes)
}
/// Prüft das Kontingent und zählt die Anfrage, in einer Transaktion, damit gleichzeitige
@@ -742,11 +772,12 @@ impl Db {
let mut conn = self.conn();
let tx = conn.transaction()?;
if let Some(q) = quota
&& let Err(denial) = q.check_usage(&Self::counters(&tx, Scope::Quota(q.id, user_id.unwrap_or(0)), t)?, t)
&& let Err(denial) =
q.check_usage(&Self::counters(&tx, Scope::Quota(q.id, if q.shared { 0 } else { user_id.unwrap_or(0) }), t)?, t)
{
return Ok(Err(denial));
}
for scope in Self::scopes(key_id, user_id, quota.map(|q| q.id)) {
for scope in Self::scopes(&tx, key_id, user_id, quota.map(|q| q.id))? {
Self::add(&tx, scope, t, 1, 0, 0)?;
}
tx.commit()?;
@@ -757,7 +788,7 @@ impl Db {
pub fn refund_request(&self, key_id: i64, user_id: Option<i64>, quota_id: Option<i64>, t: DateTime<Local>) -> Result<()> {
let mut conn = self.conn();
let tx = conn.transaction()?;
for scope in Self::scopes(key_id, user_id, quota_id) {
for scope in Self::scopes(&tx, key_id, user_id, quota_id)? {
Self::add(&tx, scope, t, -1, 0, 0)?;
}
Ok(tx.commit()?)
@@ -775,7 +806,7 @@ impl Db {
) -> Result<()> {
let mut conn = self.conn();
let tx = conn.transaction()?;
for scope in Self::scopes(key_id, user_id, quota_id) {
for scope in Self::scopes(&tx, key_id, user_id, quota_id)? {
Self::add(&tx, scope, t, 0, prompt as i64, completion as i64)?;
}
Ok(tx.commit()?)
@@ -786,7 +817,7 @@ impl Db {
pub fn prune_usage(&self, actor: &str, days: u32) -> Result<usize> {
let cutoff = now() - i64::from(days) * 86_400;
let deleted = self.conn().execute("DELETE FROM usage WHERE ts < ?1", params![cutoff])?;
self.audit(actor, "usage.prune", &format!("older_than_days={days} deleted={deleted}"))?;
self.audit(actor, "usage.prune", &format!("{deleted} Einträge älter als {days} Tage gelöscht"))?;
Ok(deleted)
}
@@ -838,6 +869,9 @@ fn migrate(conn: &Connection) -> Result<()> {
if !has_column(conn, "api_keys", "user_id")? {
conn.execute("ALTER TABLE api_keys ADD COLUMN user_id INTEGER REFERENCES users(id)", [])?;
}
if !has_column(conn, "quotas", "shared")? {
conn.execute("ALTER TABLE quotas ADD COLUMN shared INTEGER NOT NULL DEFAULT 0", [])?;
}
for column in ["owner_id", "group_id", "user_id"] {
if !has_column(conn, "quotas", column)? {
conn.execute(&format!("ALTER TABLE quotas ADD COLUMN {column} INTEGER"), [])?;