Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions internal/middleware/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import (

type myResponseWriter struct {
http.ResponseWriter
buf *bytes.Buffer
buf *bytes.Buffer
status int
}

func (mrw *myResponseWriter) WriteHeader(code int) {
mrw.status = code
mrw.ResponseWriter.WriteHeader(code)
}

func (mrw *myResponseWriter) Write(p []byte) (int, error) {
Expand All @@ -21,6 +27,7 @@ func (mrw *myResponseWriter) Write(p []byte) (int, error) {
func NewCacheMw(c *utils.Cache) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Cache only GET requests
if r.Method != "GET" {
next.ServeHTTP(w, r)
return
Expand All @@ -35,23 +42,30 @@ func NewCacheMw(c *utils.Cache) Middleware {
return
}

// if not cached pass it to the next handlerFunc
// if not cached, pass it to the next handlerFunc
mrw := &myResponseWriter{
ResponseWriter: w,
buf: &bytes.Buffer{},
status: http.StatusOK, // Default to 200
}

next.ServeHTTP(mrw, r)

cacheBuffer := bytes.Buffer{}
multiWriter := io.MultiWriter(w, &cacheBuffer)
// Cache only 200 OK responses
if mrw.status == http.StatusOK {
cacheBuffer := bytes.Buffer{}
multiWriter := io.MultiWriter(w, &cacheBuffer)

if _, err := io.Copy(multiWriter, mrw.buf); err != nil {
log.Printf("Failed to send out response from CacheMiddleware")
}
if _, err := io.Copy(multiWriter, mrw.buf); err != nil {
log.Printf("Failed to send out response from CacheMiddleware")
}

// cache result
c.Set(cacheKey, cacheBuffer.String())
// Save to cache
c.Set(cacheKey, cacheBuffer.String())
} else {
// Write response directly if not 200 OK
_, _ = io.Copy(w, mrw.buf)
}
})
}
}
2 changes: 1 addition & 1 deletion static/css/output.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/templates/training-datasets/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
hx-delete="/training-datasets/{{.ID}}"
hx-confirm="Are you sure you want to remove {{ .Name }}?">Remove</button>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-2">
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-2">
{{ range .AODFiles }}
<div class="file flex justify-between items-center gap-4 p-2 rounded-md bg-sky-200 dark:bg-sky-600" data-path="{{ .Path }}">
<div class="flex justify-start gap-2 items-center">
Expand Down
18 changes: 9 additions & 9 deletions web/templates/training-datasets/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
class="relative lg:w-1/2 w-full border-dotted rounded-md shadow-md dark:shadow-none overflow-hidden bg-sky-50 dark:bg-sky-800">
<div class="flex flex-col max-h-96 min-h-96">
<form id="find-aods-form"
class="flex flex-wrap content-stretch bg-white dark:bg-sky-700 justify-end gap-4 items-center border-b-2 border-dotted p-3"
class="flex content-stretch bg-white dark:bg-sky-700 justify-end gap-4 items-center border-b-2 border-dotted p-3"
hx-get="find-aods" hx-target="#file-list" hx-swap="innerHTML" hx-indicator="#file-list-overlay">
<label for="path">Path in Grid:</label>
<input class="bg-white border-sky-900 text-gray-800 rounded-lg flex-grow text-sm py-1 px-2 max-w-1/2"
<label for="path">Path:</label>
<input class="bg-white border-sky-900 text-gray-800 rounded-lg flex-grow text-sm py-1 px-2 max-w-1/2 flex-shrink"
placeholder="ex. /alice/sim/2024/LHC24f3/0/523397" type="text" id="path" name="path" required>
<button class="bg-sky-900 hover:bg-sky-800 text-gray-50 rounded-lg font-bold py-1 px-2"
type="submit">Find AO2Ds</button>
Expand All @@ -45,15 +45,15 @@ <h1 class="text-3xl">Create training Dataset</h1>
hx-post="/training-datasets"
hx-vals="js:{...getSelectedFiles()}"
hx-ext="json-enc"
class="grid grid-cols-2 auto-rows-auto gap-3">
<div class="text-lg text-red-600 col-span-2 text-center w-full" id="errors"></div>
<label class="justify-self-end text-lg self-center" for="train-dataset-name">Training dataset name:</label>
class="grid grid-cols-1 lg:grid-cols-2 items-start auto-rows-auto gap-3 w-full p-2">
<div class="text-lg text-red-600 col-span-1 lg:col-span-2 text-center w-full" id="errors"></div>
<label class="justify-self-start lg:justify-self-end text-lg self-center" for="train-dataset-name">Training dataset name:</label>
<input type="text" class="bg-white border-sky-900 text-gray-800 rounded-lg text-lg py-1 px-2"
name="name" id="train-dataset-name" required />
<h2 class="justify-self-end text-lg self-center">Selected files:</h2>
<ul class="grid grid-cols-1 lg:grid-cols-2 gap-2" id="selected-files"></ul>
<h2 class="justify-self-start lg:justify-self-end text-lg self-center">Selected files:</h2>
<ul class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-1 xl:grid-cols-2 gap-2" id="selected-files"></ul>
<button
class="col-span-2 justify-self-end bg-sky-900 hover:bg-sky-800 text-gray-50 rounded-lg font-bold py-1 px-2 text-lg"
class="col-span-1 lg:col-span-2 justify-self-end bg-sky-900 hover:bg-sky-800 text-gray-50 rounded-lg font-bold py-1 px-2 text-lg"
type="submit">Submit</button>
</form>
</div>
Expand Down
14 changes: 7 additions & 7 deletions web/templates/training-datasets/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{{ template "header" . }}
<div class="flex flex-col gap-4 my-4 items-center">
<h2 class="text-2xl">{{ .TrainingDataset.Name }}</h2>
<div class="grid grid-cols-2 justify-stretch items-center gap-4 text-lg">
<h2 class="text-right">Created by:</h2>
<div class="grid grid-cols-1 lg:grid-cols-2 justify-stretch items-start gap-4 text-lg">
<h2 class="lg:text-right">Created by:</h2>
<h3>{{ .TrainingDataset.User.FirstName }} {{ .TrainingDataset.User.FamilyName }} ({{ .TrainingDataset.User.Username }}) </h3>
<h2 class="text-right">Files:</h2>
<div class="flex flex-col gap-3 text-sm">
<h2 class="lg:text-right">Files:</h2>
<div class="grid grid-cols-1 xl:grid-cols-2 gap-3 text-sm">
{{ range .TrainingDataset.AODFiles }}
<div class="file flex items-center gap-4 p-2 rounded-md bg-sky-200 dark:bg-sky-600"
<div class="file flex justify-between items-center gap-4 p-2 rounded-md bg-sky-200 dark:bg-sky-600"
data-path="{{ .Path }}">
<div class="flex justify-start gap-2 items-center">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
Expand All @@ -26,9 +26,9 @@ <h2 class="text-right">Files:</h2>
</div>
{{ end }}
</div>
<h2 class="text-right">Created at:</h2>
<h2 class="lg:text-right">Created at:</h2>
<h3>{{ .TrainingDataset.CreatedAt.Format "02 Jan 06 15:04 MST" }}</h3>
<h2 class="text-right">Last update at:</h2>
<h2 class="lg:text-right">Last update at:</h2>
<h3>{{ .TrainingDataset.UpdatedAt.Format "02 Jan 06 15:04 MST" }}</h3>
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions web/templates/training-datasets/tree-browser.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ define "training-datasets_tree-browser" }}
<div class="flex flex-col max-h-96 flex-grow">
<div
class="flex sticky top-0 bg-white dark:bg-sky-700 gap-3 justify-end md:justify-between flex-wrap items-center border-b-2 border-dotted p-3">
class="flex sticky top-0 bg-white dark:bg-sky-700 gap-3 justify-between items-center border-b-2 border-dotted p-3">
{{ if ne .Path "/" }}
<button
class="p-2 rounded-md bg-sky-200 dark:bg-sky-600 hover:bg-sky-100 dark:hover:bg-sky-700 hover:cursor-pointer"
Expand All @@ -13,7 +13,7 @@
</svg>
</button>
{{ end }}
<p>{{ .Path }}</p>
<p class="truncate flex-shrink">{{ .Path }}</p>
{{ if ne .Path "/" }}
<button class="bg-sky-900 hover:bg-sky-800 text-gray-50 rounded-lg font-bold py-1 px-2" hx-get="find-aods"
hx-vals='{"path":"{{ .Path }}"}' hx-target="#file-list" hx-trigger="click" hx-indicator="#file-list-overlay">Find
Expand All @@ -24,18 +24,18 @@
{{ if not .Subdirs }}
<p class="p-5">No more subdirs, maybe you want to find AODs in this directory. If so use right-upper corner button.</p>
{{ else }}
<ul class="flex flex-col gap-2 px-5 py-2">
<ul class="flex flex-col gap-2 px-2 lg:px-5 py-2">
{{ range .Subdirs }}
<li>
<button
class="flex w-full justify-start gap-2 items-center p-2 rounded-md bg-sky-200 dark:bg-sky-600 hover:bg-sky-100 dark:hover:bg-sky-700 hover:cursor-pointer"
hx-get="explore-directory" hx-vals='{"path":"{{ .Path }}"}' hx-target="#file-tree" hx-trigger="click consume">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="size-4 lg:size-6">
stroke="currentColor" class="size-4 lg:size-6 flex-shrink-0">
<path stroke-linecap="round" stroke-linejoin="round"
d="M2.25 12.75V12A2.25 2.25 0 0 1 4.5 9.75h15A2.25 2.25 0 0 1 21.75 12v.75m-8.69-6.44-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z" />
</svg>
{{ .Name }}
<p class="truncate">{{ .Name }}</p>
</button>
</li>
{{ end }}
Expand Down
10 changes: 5 additions & 5 deletions web/templates/training-tasks/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ <h2 class="text-xl text-right">Training dataset - {{ .TrainingTask.TrainingDatas
{{ end }}
</div>
</div>
<div class="grid grid-cols-2 justify-stretch items-start gap-4 text-lg">
<h2 class="text-right">Created by:</h2>
<div class="grid grid-cols-1 lg:grid-cols-2 justify-stretch items-start gap-2 text-md lg:text-lg w-full p-2">
<h2 class="lg:text-right text-lg">Created by:</h2>
<h3>{{ .TrainingTask.User.FirstName }} {{ .TrainingTask.User.FamilyName }} ({{ .TrainingTask.User.Username }})
</h3>
<h2 class="text-right">Configuration:</h2>
<h2 class="lg:text-right text-lg">Configuration:</h2>
<div>
{{ range $key, $value := .TrainingTask.Configuration }}
<h3>{{ $key }}: {{ $value }}</h3>
{{ end }}
</div>
<h2 class="text-right">Created at:</h2>
<h2 class="lg:text-right text-lg">Created at:</h2>
<h3>{{ .TrainingTask.CreatedAt.Format "02 Jan 06 15:04 MST" }}</h3>
<h2 class="text-right">Last update at:</h2>
<h2 class="lg:text-right text-lg">Last update at:</h2>
<h3>{{ .TrainingTask.UpdatedAt.Format "02 Jan 06 15:04 MST" }}</h3>
</div>
</div>
Expand Down
Loading