67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
|
|
//go:build llm_script
|
||
|
|
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type cloudflarePricingImportConfig struct {
|
||
|
|
URL string
|
||
|
|
Fixture string
|
||
|
|
DryRun bool
|
||
|
|
Timeout time.Duration
|
||
|
|
SnapshotOnly bool
|
||
|
|
SnapshotOut string
|
||
|
|
SignatureOut string
|
||
|
|
}
|
||
|
|
|
||
|
|
func runCloudflarePricingImport(cfg cloudflarePricingImportConfig, db *sql.DB, out io.Writer) error {
|
||
|
|
client := &http.Client{Timeout: cfg.Timeout}
|
||
|
|
raw, err := fetchRawPricingPage(cfg.URL, cfg.Fixture, client)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if cfg.SnapshotOnly || strings.TrimSpace(cfg.SnapshotOut) != "" || strings.TrimSpace(cfg.SignatureOut) != "" {
|
||
|
|
snapshotPath, signaturePath := resolveCloudflarePricingSnapshotPaths(cfg.SnapshotOut, cfg.SignatureOut, "", time.Now())
|
||
|
|
signature, err := writeCloudflarePricingSnapshotArtifacts(raw, cfg.URL, snapshotPath, signaturePath, time.Now())
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if cfg.SnapshotOnly {
|
||
|
|
_, err = fmt.Fprintf(out,
|
||
|
|
"source=cloudflare-pricing-snapshot snapshot_only=true byte_size=%d sha256=%s structure_sha256=%s snapshot_out=%s signature_out=%s\n",
|
||
|
|
signature.ByteSize, signature.SHA256, signature.StructureSHA256, snapshotPath, signaturePath,
|
||
|
|
)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
records, err := parseCloudflarePricingCatalog(raw)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
records = dedupeOfficialPricingRecords(records)
|
||
|
|
if cfg.DryRun {
|
||
|
|
_, err = fmt.Fprintf(out, "source=cloudflare-pricing-import models=%d operator=%s dry_run=true\n", len(records), records[0].OperatorName)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if db == nil {
|
||
|
|
return fmt.Errorf("db is required when dry-run=false")
|
||
|
|
}
|
||
|
|
if err := upsertOfficialPricingRecords(db, records, "cloudflare-pricing-import"); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
var tableRows int
|
||
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM region_pricing`).Scan(&tableRows); err != nil {
|
||
|
|
return fmt.Errorf("count region_pricing: %w", err)
|
||
|
|
}
|
||
|
|
_, err = fmt.Fprintf(out, "source=cloudflare-pricing-import models=%d operator=%s table_rows=%d dry_run=false\n", len(records), records[0].OperatorName, tableRows)
|
||
|
|
return err
|
||
|
|
}
|