This commit is contained in:
greeneclipse committed 2026-10-20 17:03:09 +02:00
1 parent 486502852e
commit 0e48ea427b
23 files changed
+386 -78

No files matched your search

@@ -1,7 +1,6 @@
package httpapi
import (
"encoding/csv"
"net/http"
"strconv"
"strings"
@@ -37,7 +36,10 @@ func (s *Server) handleAdminExportAICosts(w http.ResponseWriter, r *http.Request
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="descrybe-ai-usage-costs.csv"`)
w.Header().Set("Cache-Control", "private, no-store")
out := csv.NewWriter(w)
out, err := newUTF8CSVWriter(w)
if err != nil {
return
}
_ = out.Write([]string{"from", "to", "company_id", "company", "calls", "failed_calls", "input_tokens", "cached_input_tokens", "output_tokens", "total_tokens", "provider_cost_usd", "notice"})
v := func(row map[string]any, key string) string { return strconv.FormatInt(int64(row[key].(int64)), 10) }
for _, row := range result {
@@ -1,7 +1,6 @@
package httpapi
import (
"encoding/csv"
"net/http"
"strconv"
"strings"
@@ -25,7 +24,10 @@ func (s *Server) handleAdminExportCompanyInvoices(w http.ResponseWriter, r *http
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="descrybe-stripe-invoices.csv"`)
w.Header().Set("Cache-Control", "private, no-store")
out := csv.NewWriter(w)
out, err := newUTF8CSVWriter(w)
if err != nil {
return
}
_ = out.Write([]string{"company_id", "invoice_id", "number", "status", "currency", "created_unix", "amount_due_minor", "amount_paid_minor", "amount_remaining_minor", "hosted_invoice_url", "invoice_pdf"})
for _, row := range rows {
_ = out.Write([]string{id.String(), row.ID, row.Number, row.Status, row.Currency,
@@ -1,11 +1,13 @@
package httpapi
import (
"errors"
"math"
"net/http"
"strconv"
"strings"
"github.com/descrybe/descrybe-v2/apps/api/internal/platformsettings"
"github.com/google/uuid"
)
@@ -18,11 +20,11 @@ var editableCostFeatures = map[string]bool{
func (s *Server) handleAdminGetProcessingCosts(w http.ResponseWriter, r *http.Request) {
rows, err := s.Pool.Query(r.Context(), `
SELECT 'default', '', feature_name, cost_per_unit FROM processing_costs WHERE is_active = true AND feature_name IN ('title_ai','description_ai','seo_meta_ai','product_ai')
SELECT 'default', '', feature_name, cost_per_unit::text FROM processing_costs WHERE is_active = true AND feature_name IN ('title_ai','description_ai','seo_meta_ai','product_ai')
UNION ALL
SELECT 'plan', plan_id::text, feature_name, cost_per_unit FROM plan_processing_costs WHERE feature_name IN ('title_ai','description_ai','seo_meta_ai','product_ai')
SELECT 'plan', plan_id::text, feature_name, cost_per_unit::text FROM plan_processing_costs WHERE feature_name IN ('title_ai','description_ai','seo_meta_ai','product_ai')
UNION ALL
SELECT 'company', company_id::text, feature_name, cost_per_unit FROM company_processing_costs WHERE feature_name IN ('title_ai','description_ai','seo_meta_ai','product_ai')
SELECT 'company', company_id::text, feature_name, cost_per_unit::text FROM company_processing_costs WHERE feature_name IN ('title_ai','description_ai','seo_meta_ai','product_ai')
ORDER BY 1, 2, 3`)
if err != nil {
LogAndError(w, http.StatusInternalServerError, "could not load processing costs", err)
@@ -31,12 +33,19 @@ func (s *Server) handleAdminGetProcessingCosts(w http.ResponseWriter, r *http.Re
defer rows.Close()
out := make([]map[string]any, 0, 16)
for rows.Next() {
var scope, id, feature string
var cost float64
if err := rows.Scan(&scope, &id, &feature, &cost); err != nil {
var scope, id, feature, costText string
if err := rows.Scan(&scope, &id, &feature, &costText); err != nil {
LogAndError(w, http.StatusInternalServerError, "could not scan processing costs", err)
return
}
cost, parseErr := strconv.ParseFloat(costText, 64)
if parseErr != nil || math.IsNaN(cost) || math.IsInf(cost, 0) {
if parseErr == nil {
parseErr = errors.New("non-finite cost value")
}
LogAndError(w, http.StatusInternalServerError, "invalid stored processing cost", parseErr)
return
}
out = append(out, map[string]any{"scope": scope, "id": id, "feature": feature, "cost_per_unit": cost})
}
if err := rows.Err(); err != nil {
@@ -130,7 +139,7 @@ func (s *Server) handleCompanyContentCosts(w http.ResponseWriter, r *http.Reques
// Monetary credit valuation is an admin reference rate. Stripe invoices, pack
// checkout prices and historic wallet ledger values are not changed by it.
const creditValueEURSetting = "billing.reference_eur_per_credit"
const creditValueEURSetting = platformsettings.KeyBillingReferenceEURPerCredit
func (s *Server) handleAdminCreditValueEUR(w http.ResponseWriter, r *http.Request) {
if s.PlatformSettings == nil {
@@ -1,7 +1,6 @@
package httpapi
import (
"encoding/csv"
"net/http"
"strconv"
@@ -23,7 +22,10 @@ func (s *Server) handleBillingUsageExport(w http.ResponseWriter, r *http.Request
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="descrybe-usage.csv"`)
w.Header().Set("Cache-Control", "private, no-store")
out := csv.NewWriter(w)
out, err := newUTF8CSVWriter(w)
if err != nil {
return
}
_ = out.Write([]string{"company_id", "range", "metric", "value", "notes"})
rows := []struct {
label string
+15
View File
@@ -0,0 +1,15 @@
package httpapi
import (
"encoding/csv"
"io"
)
// newUTF8CSVWriter writes the UTF-8 signature so Excel can recognize characters
// such as č, š and ž when opening exported CSVs directly.
func newUTF8CSVWriter(w io.Writer) (*csv.Writer, error) {
if _, err := io.WriteString(w, "\ufeff"); err != nil {
return nil, err
}
return csv.NewWriter(w), nil
}
@@ -0,0 +1,29 @@
package httpapi
import (
"bytes"
"encoding/csv"
"testing"
)
func TestUTF8CSVWriterPreservesSlovenianText(t *testing.T) {
var b bytes.Buffer
w, err := newUTF8CSVWriter(&b)
if err != nil {
t.Fatal(err)
}
if err := w.Write([]string{"Opis", "Žarnica, črpalka, šoba"}); err != nil {
t.Fatal(err)
}
w.Flush()
if err := w.Error(); err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(b.Bytes(), []byte("\ufeff")) {
t.Fatalf("no UTF-8 BOM: %v", b.Bytes())
}
rows, err := csv.NewReader(bytes.NewReader(bytes.TrimPrefix(b.Bytes(), []byte("\ufeff")))).ReadAll()
if err != nil || len(rows) != 1 || rows[0][1] != "Žarnica, črpalka, šoba" {
t.Fatalf("round trip failed: %v: %v", rows, err)
}
}