feat(bulk-api-tester): add comparison drafts library and multi-draft combiner under single heading

This commit is contained in:
2026-10-28 14:36:36 +05:30
parent ad48d28310
commit ce75adb467
6 changed files with 1389 additions and 18 deletions
+552
View File
@@ -0,0 +1,552 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/go-chi/chi/v5"
)
var (
draftImageRegex = regexp.MustCompile(`/api/comparison-drafts/([a-zA-Z0-9_\-]+)/images/([a-zA-Z0-9_\-\.]+)`)
compImageRegex = regexp.MustCompile(`/api/comparison/([a-zA-Z0-9_\-]+)/images/([a-zA-Z0-9_\-\.]+)`)
)
type DraftItem struct {
Index int `json:"index"`
Status any `json:"status"`
Latency int64 `json:"latency"`
InputFilename string `json:"inputFilename"`
OutputFilename string `json:"outputFilename"`
ContentType string `json:"contentType"`
BeforeImage string `json:"beforeImage"`
AfterImage string `json:"afterImage"`
TextContent string `json:"textContent"`
HasBefore bool `json:"hasBefore"`
HasAfter bool `json:"hasAfter"`
SourceDraftID string `json:"sourceDraftId,omitempty"`
}
type ComparisonDraft struct {
ID string `json:"id"`
Title string `json:"title"`
Notes string `json:"notes"`
Endpoint string `json:"endpoint"`
SpaceName string `json:"spaceName"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Total int `json:"total"`
SuccessCount int `json:"successCount"`
ErrorCount int `json:"errorCount"`
AvgLatency int64 `json:"avgLatency"`
Items []DraftItem `json:"items"`
}
type SaveDraftRequest struct {
ID string `json:"id,omitempty"`
Title string `json:"title"`
Notes string `json:"notes"`
Endpoint string `json:"endpoint"`
SpaceName string `json:"spaceName"`
Items []ShareRequestItem `json:"items"`
}
type CombineDraftsRequest struct {
Heading string `json:"heading"`
Items []CombineItemInput `json:"items"`
}
type CombineItemInput struct {
Index int `json:"index"`
Status any `json:"status"`
Latency int64 `json:"latency"`
InputFilename string `json:"inputFilename"`
OutputFilename string `json:"outputFilename"`
ContentType string `json:"contentType"`
BeforeImage string `json:"beforeImage"`
AfterImage string `json:"afterImage"`
BeforeData string `json:"beforeData"`
AfterData string `json:"afterData"`
TextContent string `json:"textContent"`
SourceDraftID string `json:"sourceDraftId"`
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return err
}
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func resolveDiskPathFromURL(rawURL string) string {
if rawURL == "" {
return ""
}
if m := draftImageRegex.FindStringSubmatch(rawURL); len(m) == 3 {
p := filepath.Join("data", "bulk-api-tester", "drafts", m[1], "images", filepath.Base(m[2]))
if _, err := os.Stat(p); err == nil {
return p
}
}
if m := compImageRegex.FindStringSubmatch(rawURL); len(m) == 3 {
p := filepath.Join("data", "bulk-api-tester", "comparisons", m[1], "images", filepath.Base(m[2]))
if _, err := os.Stat(p); err == nil {
return p
}
}
return ""
}
func listComparisonDraftsHandler(w http.ResponseWriter, r *http.Request) {
draftsDir := filepath.Join("data", "bulk-api-tester", "drafts")
entries, err := os.ReadDir(draftsDir)
if err != nil {
writeJSON(w, http.StatusOK, []ComparisonDraft{})
return
}
var drafts []ComparisonDraft
for _, entry := range entries {
if !entry.IsDir() {
continue
}
id := entry.Name()
if !validIDRegex.MatchString(id) {
continue
}
metaPath := filepath.Join(draftsDir, id, "meta.json")
var d ComparisonDraft
if err := readJSON(metaPath, &d); err == nil && d.ID != "" {
drafts = append(drafts, d)
}
}
// Sort newest first
sort.Slice(drafts, func(i, j int) bool {
return drafts[i].CreatedAt > drafts[j].CreatedAt
})
w.Header().Set("Cache-Control", "no-cache, must-revalidate")
writeJSON(w, http.StatusOK, drafts)
}
func getComparisonDraftHandler(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" || !validIDRegex.MatchString(id) {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "Invalid draft ID"})
return
}
metaPath := filepath.Join("data", "bulk-api-tester", "drafts", id, "meta.json")
var draft ComparisonDraft
if err := readJSON(metaPath, &draft); err != nil {
writeJSON(w, http.StatusNotFound, map[string]any{"error": "Draft not found"})
return
}
w.Header().Set("Cache-Control", "no-cache, must-revalidate")
writeJSON(w, http.StatusOK, draft)
}
func saveComparisonDraftHandler(w http.ResponseWriter, r *http.Request) {
// Set 500MB limit on request body
r.Body = http.MaxBytesReader(w, r.Body, 500*1024*1024)
var req SaveDraftRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
slog.Error("failed to decode save draft request", "error", err)
writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid request body: " + err.Error()})
return
}
if len(req.Items) == 0 {
writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "No items provided for comparison draft"})
return
}
draftID := req.ID
if draftID == "" || !validIDRegex.MatchString(draftID) {
draftID = fmt.Sprintf("draft_%x_%s", time.Now().Unix(), randomHexString(4))
}
draftDir := filepath.Join("data", "bulk-api-tester", "drafts", draftID)
imagesDir := filepath.Join(draftDir, "images")
if err := os.MkdirAll(imagesDir, 0755); err != nil {
slog.Error("failed to create draft directory", "dir", draftDir, "error", err)
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to create storage directory"})
return
}
var items []DraftItem
var totalLatency int64
successCount := 0
errorCount := 0
for i, it := range req.Items {
itemIndex := it.Index
if itemIndex <= 0 {
itemIndex = i + 1
}
dItem := DraftItem{
Index: itemIndex,
Status: it.Status,
Latency: it.Latency,
InputFilename: it.InputFilename,
OutputFilename: it.OutputFilename,
ContentType: it.ContentType,
TextContent: it.TextContent,
SourceDraftID: draftID,
}
isSuccess := false
switch v := it.Status.(type) {
case float64:
if int(v) >= 200 && int(v) < 300 {
isSuccess = true
}
case int:
if v >= 200 && v < 300 {
isSuccess = true
}
case string:
if strings.HasPrefix(v, "2") {
isSuccess = true
}
}
if isSuccess {
successCount++
} else {
errorCount++
}
totalLatency += it.Latency
// Save Before Image
if it.BeforeData != "" {
beforeBytes, ext, err := decodeImageData(it.BeforeData)
if err == nil && len(beforeBytes) > 0 {
filename := fmt.Sprintf("before_%d.%s", itemIndex, ext)
filePath := filepath.Join(imagesDir, filename)
if err := os.WriteFile(filePath, beforeBytes, 0644); err == nil {
dItem.BeforeImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, filename)
dItem.HasBefore = true
}
}
}
// Save After Image
if it.AfterData != "" {
afterBytes, ext, err := decodeImageData(it.AfterData)
if err == nil && len(afterBytes) > 0 {
filename := fmt.Sprintf("after_%d.%s", itemIndex, ext)
filePath := filepath.Join(imagesDir, filename)
if err := os.WriteFile(filePath, afterBytes, 0644); err == nil {
dItem.AfterImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, filename)
dItem.HasAfter = true
}
}
}
items = append(items, dItem)
}
title := strings.TrimSpace(req.Title)
if title == "" {
title = fmt.Sprintf("Draft Run (%d results) - %s", len(items), time.Now().Format("Jan 02, 15:04"))
}
var avgLatency int64
if len(items) > 0 {
avgLatency = totalLatency / int64(len(items))
}
nowStr := time.Now().UTC().Format(time.RFC3339)
draftData := ComparisonDraft{
ID: draftID,
Title: title,
Notes: req.Notes,
Endpoint: req.Endpoint,
SpaceName: req.SpaceName,
CreatedAt: nowStr,
UpdatedAt: nowStr,
Total: len(items),
SuccessCount: successCount,
ErrorCount: errorCount,
AvgLatency: avgLatency,
Items: items,
}
metaPath := filepath.Join(draftDir, "meta.json")
if err := atomicWriteJSON(metaPath, draftData); err != nil {
slog.Error("failed to save draft metadata", "path", metaPath, "error", err)
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to save draft metadata"})
return
}
slog.Info("saved comparison draft", "draft_id", draftID, "items_count", len(items))
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"draftId": draftID,
"draft": draftData,
})
}
func deleteComparisonDraftHandler(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" || !validIDRegex.MatchString(id) {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "Invalid draft ID"})
return
}
draftDir := filepath.Join("data", "bulk-api-tester", "drafts", id)
if err := os.RemoveAll(draftDir); err != nil {
slog.Error("failed to delete draft", "dir", draftDir, "error", err)
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to delete draft"})
return
}
slog.Info("deleted comparison draft", "draft_id", id)
writeJSON(w, http.StatusOK, map[string]any{"success": true, "draftId": id})
}
func getDraftImageHandler(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
file := chi.URLParam(r, "file")
if id == "" || !validIDRegex.MatchString(id) || file == "" {
http.Error(w, "Invalid parameters", http.StatusBadRequest)
return
}
cleanFile := filepath.Base(file)
if cleanFile != file {
http.Error(w, "Invalid filename", http.StatusBadRequest)
return
}
filePath := filepath.Join("data", "bulk-api-tester", "drafts", id, "images", cleanFile)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
http.Error(w, "Draft image not found", http.StatusNotFound)
return
}
ext := strings.ToLower(filepath.Ext(cleanFile))
contentType := "application/octet-stream"
switch ext {
case ".png":
contentType = "image/png"
case ".jpg", ".jpeg":
contentType = "image/jpeg"
case ".webp":
contentType = "image/webp"
case ".gif":
contentType = "image/gif"
case ".svg":
contentType = "image/svg+xml"
case ".avif":
contentType = "image/avif"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
http.ServeFile(w, r, filePath)
}
func combineDraftsHandler(w http.ResponseWriter, r *http.Request) {
// Set 500MB limit on request body
r.Body = http.MaxBytesReader(w, r.Body, 500*1024*1024)
var req CombineDraftsRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
slog.Error("failed to decode combine drafts request", "error", err)
writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid request body: " + err.Error()})
return
}
if len(req.Items) == 0 {
writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "No items provided to combine"})
return
}
// Create new shareable comparison ID
shareID := fmt.Sprintf("cmp_%x_%s", time.Now().Unix(), randomHexString(4))
compDir := filepath.Join("data", "bulk-api-tester", "comparisons", shareID)
imagesDir := filepath.Join(compDir, "images")
if err := os.MkdirAll(imagesDir, 0755); err != nil {
slog.Error("failed to create combined comparison directory", "dir", compDir, "error", err)
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to create comparison directory"})
return
}
var items []ComparisonItem
var totalLatency int64
successCount := 0
errorCount := 0
for i, it := range req.Items {
newIndex := i + 1
cItem := ComparisonItem{
Index: newIndex,
Status: it.Status,
Latency: it.Latency,
InputFilename: it.InputFilename,
OutputFilename: it.OutputFilename,
ContentType: it.ContentType,
TextContent: it.TextContent,
}
isSuccess := false
switch v := it.Status.(type) {
case float64:
if int(v) >= 200 && int(v) < 300 {
isSuccess = true
}
case int:
if v >= 200 && v < 300 {
isSuccess = true
}
case string:
if strings.HasPrefix(v, "2") {
isSuccess = true
}
}
if isSuccess {
successCount++
} else {
errorCount++
}
totalLatency += it.Latency
// Handle Before Image
if it.BeforeData != "" {
beforeBytes, ext, err := decodeImageData(it.BeforeData)
if err == nil && len(beforeBytes) > 0 {
filename := fmt.Sprintf("before_%d.%s", newIndex, ext)
dst := filepath.Join(imagesDir, filename)
if err := os.WriteFile(dst, beforeBytes, 0644); err == nil {
cItem.BeforeImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename)
cItem.HasBefore = true
}
}
} else if it.BeforeImage != "" {
srcDiskPath := resolveDiskPathFromURL(it.BeforeImage)
if srcDiskPath != "" {
ext := strings.ToLower(filepath.Ext(srcDiskPath))
if ext == "" {
ext = ".png"
}
filename := fmt.Sprintf("before_%d%s", newIndex, ext)
dst := filepath.Join(imagesDir, filename)
if err := copyFile(srcDiskPath, dst); err == nil {
cItem.BeforeImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename)
cItem.HasBefore = true
} else {
slog.Warn("failed to copy before image", "src", srcDiskPath, "dst", dst, "error", err)
}
}
}
// Handle After Image
if it.AfterData != "" {
afterBytes, ext, err := decodeImageData(it.AfterData)
if err == nil && len(afterBytes) > 0 {
filename := fmt.Sprintf("after_%d.%s", newIndex, ext)
dst := filepath.Join(imagesDir, filename)
if err := os.WriteFile(dst, afterBytes, 0644); err == nil {
cItem.AfterImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename)
cItem.HasAfter = true
}
}
} else if it.AfterImage != "" {
srcDiskPath := resolveDiskPathFromURL(it.AfterImage)
if srcDiskPath != "" {
ext := strings.ToLower(filepath.Ext(srcDiskPath))
if ext == "" {
ext = ".png"
}
filename := fmt.Sprintf("after_%d%s", newIndex, ext)
dst := filepath.Join(imagesDir, filename)
if err := copyFile(srcDiskPath, dst); err == nil {
cItem.AfterImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename)
cItem.HasAfter = true
} else {
slog.Warn("failed to copy after image", "src", srcDiskPath, "dst", dst, "error", err)
}
}
}
items = append(items, cItem)
}
heading := strings.TrimSpace(req.Heading)
if heading == "" {
heading = fmt.Sprintf("Combined Comparison (%d results)", len(items))
}
var avgLatency int64
if len(items) > 0 {
avgLatency = totalLatency / int64(len(items))
}
shareData := ComparisonShare{
ID: shareID,
Title: heading,
Endpoint: "Combined Multi-Draft Run",
SpaceName: "Drafts Combiner",
CreatedAt: time.Now().UTC().Format(time.RFC3339),
Total: len(items),
SuccessCount: successCount,
ErrorCount: errorCount,
AvgLatency: avgLatency,
Items: items,
}
metaPath := filepath.Join(compDir, "meta.json")
if err := atomicWriteJSON(metaPath, shareData); err != nil {
slog.Error("failed to save combined comparison meta", "path", metaPath, "error", err)
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to save comparison meta"})
return
}
slog.Info("created combined comparison share", "share_id", shareID, "heading", heading, "items_count", len(items))
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"shareId": shareID,
"url": "/tool/bulk-api-tester/compare/" + shareID,
"directUrl": "/compare/" + shareID,
"heading": heading,
"total": len(items),
})
}