dipan004 commited on
Commit
61c52dc
·
verified ·
1 Parent(s): fb23e13

Create services_transform.go

Browse files
backend/services/services_transform.go ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import (
4
+ "fmt"
5
+ "math/rand"
6
+ "net/url"
7
+ "strings"
8
+ "time"
9
+
10
+ "github.com/creepurl/backend/database"
11
+ "github.com/creepurl/backend/models"
12
+ )
13
+
14
+ type TransformService struct {
15
+ shortener *ShortenerService
16
+ rng *rand.Rand
17
+ }
18
+
19
+ func NewTransformService(db *database.DB, baseURL string) *TransformService {
20
+ return &TransformService{
21
+ shortener: NewShortenerService(db, baseURL),
22
+ rng: rand.New(rand.NewSource(time.Now().UnixNano())),
23
+ }
24
+ }
25
+
26
+ // Transform uses baseURL from cfg (set at startup via env var), not from request
27
+ func (s *TransformService) Transform(rawURL string, destructionLevel int) (*models.TransformResponse, error) {
28
+ if err := validateURL(rawURL); err != nil {
29
+ return nil, err
30
+ }
31
+
32
+ level := clampLevel(destructionLevel)
33
+ count := variantCount(level)
34
+
35
+ links, err := s.shortener.Create(rawURL, level, count)
36
+ if err != nil {
37
+ return nil, fmt.Errorf("generation failed: %w", err)
38
+ }
39
+
40
+ return &models.TransformResponse{
41
+ Links: links,
42
+ Metrics: s.generateMetrics(level),
43
+ }, nil
44
+ }
45
+
46
+ func (s *TransformService) generateMetrics(level int) models.TrustMetrics {
47
+ trustPct := max(0, 100-level*18-s.rng.Intn(12))
48
+
49
+ packetStates := []string{"Lost", "Fragmented", "Corrupted", "Void", "Null", "Decaying"}
50
+ machineStates := []string{"Corrupted", "Unstable", "Collapsed", "Void", "Failing", "Critical"}
51
+ readabilityStates := []string{"Declining", "Critical", "Terminal", "Lost", "Gone", "Deleted"}
52
+
53
+ return models.TrustMetrics{
54
+ TrustStability: fmt.Sprintf("%d%%", trustPct),
55
+ PacketIntegrity: packetStates[min(level-1, len(packetStates)-1)],
56
+ MachineConfidence: machineStates[min(level-1, len(machineStates)-1)],
57
+ HumanReadability: readabilityStates[min(level-1, len(readabilityStates)-1)],
58
+ }
59
+ }
60
+
61
+ func validateURL(rawURL string) error {
62
+ if strings.TrimSpace(rawURL) == "" {
63
+ return fmt.Errorf("url is required")
64
+ }
65
+ if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
66
+ rawURL = "https://" + rawURL
67
+ }
68
+ parsed, err := url.Parse(rawURL)
69
+ if err != nil || parsed.Host == "" {
70
+ return fmt.Errorf("invalid URL format")
71
+ }
72
+ return nil
73
+ }
74
+
75
+ func clampLevel(level int) int {
76
+ if level < 1 { return 1 }
77
+ if level > 5 { return 5 }
78
+ return level
79
+ }
80
+
81
+ func variantCount(level int) int {
82
+ switch level {
83
+ case 1: return 3
84
+ case 2: return 4
85
+ case 3: return 5
86
+ case 4: return 6
87
+ case 5: return 7
88
+ }
89
+ return 4
90
+ }
91
+
92
+ func max(a, b int) int {
93
+ if a > b { return a }
94
+ return b
95
+ }
96
+
97
+ func min(a, b int) int {
98
+ if a < b { return a }
99
+ return b
100
+ }