Files
ilkerandClaude Opus 5 d67af5d6c3 hpcod: projects, and what a cluster cost
"How much did the protein run use" is a question about a piece of
work, not about c003-15. A tenant asks for clusters over months;
individually each one is an address, some hardware and a lease, and
nothing joined them up.

A project groups them, and is optional throughout. Somebody who wants
one cluster for an afternoon should not have to invent a project to
get it, and requiring one would mean every tenant carrying a
"default" that means nothing. The form does not even ask when there
are none: a field whose only option is "none" is a question with one
answer.

The cost side needed no new columns. The cluster record already said
when it was created and destroyed, and cluster_vm said how many cores
and how much memory it held; what was missing was anything that read
them. cluster_usage is that, and it is a view rather than a table
because a derived number stored is a number that can disagree with
what it was derived from. It works retroactively - every cluster this
estate has ever built now has a line.

Core-hours are reserved, not executed, and the view says so. That is
the honest unit for an on-demand cluster: the hardware was yours and
nobody else could have it, whether or not a job ran. What was
actually executed is a Slurm accounting question and there is no
slurmdbd.

The project is attached inside the allocating transaction, for the
same reason the placement and the applications are, and it is checked
there to be the tenant's own and still open - a cluster filed against
somebody else's project leaks what they are working on. Closing keeps
history and stops it being offered. There is no delete: the reference
from cluster is RESTRICT, because a project removed from under its
clusters leaves their cost attributed to nothing, and afterwards is
when the question gets asked.

A release now writes manifest.json beside home.tgz: the nodes, the
wall time, the core-hours. The database knows all of it and outlives
the cluster, but the tarball can be moved off this host and the
description travels with it. Not fatal if it fails - the data is
already safe by then, and holding hardware because a note could not
be written is the wrong trade.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 13:05:35 +02:00

144 lines
4.1 KiB
Go

package httpapi
import (
"errors"
"log"
"net/http"
"strconv"
"strings"
"github.com/ilkermanap/hpcod/internal/store"
)
// Projects, and what clusters have cost.
//
// A cluster on its own is an address and a lease. A project is the thing
// somebody is accountable for, and until now nothing joined the two: the
// record said a cluster existed for two hours and held six cores, and
// there was nowhere to ask what it had been for.
//
// Both pages are filtered the same way as everything else here. A tenant
// sees their own; an administrator sees the estate. A project name says
// what somebody is working on, which is not public within a site.
// scope returns the tenant a page should be limited to, and empty for an
// administrator - which the store reads as "everything".
func scope(r *http.Request) string {
if isAdmin(r.Context()) {
return ""
}
return userFromContext(r.Context())
}
// openProjects is what the New cluster form offers. Closed ones are left
// out: a closed project is history, and offering it would invite filing
// new work against something somebody has finished with.
func (s *Server) openProjects(r *http.Request) []store.Project {
if s.store == nil {
return nil
}
who := userFromContext(r.Context())
if who == "" {
return nil
}
// The tenant's own, even for an administrator: the form creates a
// cluster for whoever is named in it, and the default is themselves.
all, err := s.store.Projects(r.Context(), who)
if err != nil {
log.Printf("projects for %s: %v", who, err)
return nil
}
open := make([]store.Project, 0, len(all))
for _, p := range all {
if p.Open() {
open = append(open, p)
}
}
return open
}
func (s *Server) pageProjects(w http.ResponseWriter, r *http.Request) {
d := s.base("projects", r)
if s.store == nil {
d.Err = "No database is configured, so nothing can be recorded or shown here. Set -db or $HPCOD_DB_DSN."
s.render(w, "projects.html", d)
return
}
if r.Method == http.MethodPost {
if err := s.projectAction(r); err != nil {
d.Err = err.Error()
} else {
http.Redirect(w, r, "/projects", http.StatusSeeOther)
return
}
}
list, err := s.store.Projects(r.Context(), scope(r))
if err != nil {
d.Err = err.Error()
}
d.Projects = list
s.render(w, "projects.html", d)
}
// projectAction creates or closes one, and returns the reason when it
// will not - the page redisplays with the form still filled in, which is
// the difference between a refusal and losing what you typed.
func (s *Server) projectAction(r *http.Request) error {
if err := r.ParseForm(); err != nil {
return err
}
who := userFromContext(r.Context())
if who == "" {
return errors.New("sign in before creating a project")
}
switch r.PostFormValue("do") {
case "create":
name := strings.TrimSpace(r.PostFormValue("name"))
if name == "" {
return errors.New("a project needs a name")
}
// Created for the person asking. An administrator filing one for
// somebody else is a thing to add when somebody needs it, not a
// field to leave lying around answering nothing.
p, err := s.store.CreateProject(r.Context(), who, who, name,
strings.TrimSpace(r.PostFormValue("description")), who)
if err != nil {
return err
}
log.Printf("project %q created by %s", p.Name, who)
return nil
case "close":
id, err := strconv.ParseInt(r.PostFormValue("project"), 10, 64)
if err != nil || id <= 0 {
return errors.New("which project?")
}
if err := s.store.CloseProject(r.Context(), id, scope(r)); err != nil {
return err
}
log.Printf("project %d closed by %s", id, who)
return nil
}
return errors.New("unknown action")
}
// pageUsage is the answer to "what did that cost", for clusters that are
// gone as much as for the ones still running.
func (s *Server) pageUsage(w http.ResponseWriter, r *http.Request) {
d := s.base("usage", r)
if s.store == nil {
d.Err = "No database is configured, so nothing can be recorded or shown here. Set -db or $HPCOD_DB_DSN."
s.render(w, "usage.html", d)
return
}
rows, err := s.store.Usage(r.Context(), scope(r), 200)
if err != nil {
d.Err = err.Error()
}
d.Usage = rows
s.render(w, "usage.html", d)
}