|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + analyticsservice "example.com/multi-component-system/example/analytics-service/analytics-service" |
| 8 | + "github.com/google/uuid" |
| 9 | + "go.bytecodealliance.org/cm" |
| 10 | +) |
| 11 | + |
| 12 | +// Simple in-memory analytics service using wit-bindgen-go generated bindings |
| 13 | +type AnalyticsService struct { |
| 14 | + eventCount int |
| 15 | + funnelCount int |
| 16 | +} |
| 17 | + |
| 18 | +func init() { |
| 19 | + service := &AnalyticsService{ |
| 20 | + eventCount: 0, |
| 21 | + funnelCount: 0, |
| 22 | + } |
| 23 | + |
| 24 | + analyticsservice.Exports.TrackEvent = func(eventData cm.List[uint8]) bool { |
| 25 | + // Parse event data as JSON |
| 26 | + var event map[string]interface{} |
| 27 | + if err := json.Unmarshal(eventData.Slice(), &event); err != nil { |
| 28 | + fmt.Printf("Error deserializing event: %v\n", err) |
| 29 | + return false |
| 30 | + } |
| 31 | + |
| 32 | + service.eventCount++ |
| 33 | + fmt.Printf("Tracked event #%d\n", service.eventCount) |
| 34 | + return true |
| 35 | + } |
| 36 | + |
| 37 | + analyticsservice.Exports.GetMetrics = func(timeWindow string) cm.List[uint8] { |
| 38 | + // Simple metrics response |
| 39 | + metrics := map[string]interface{}{ |
| 40 | + "time_window": timeWindow, |
| 41 | + "metrics": []map[string]interface{}{ |
| 42 | + { |
| 43 | + "metric_name": "total_events", |
| 44 | + "value": float64(service.eventCount), |
| 45 | + "count": service.eventCount, |
| 46 | + "aggregation_type": "count", |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + data, _ := json.Marshal(metrics) |
| 52 | + return cm.ToList(data) |
| 53 | + } |
| 54 | + |
| 55 | + analyticsservice.Exports.CreateFunnel = func(funnelData cm.List[uint8]) string { |
| 56 | + // Parse funnel definition |
| 57 | + var funnel map[string]interface{} |
| 58 | + if err := json.Unmarshal(funnelData.Slice(), &funnel); err != nil { |
| 59 | + fmt.Printf("Error deserializing funnel: %v\n", err) |
| 60 | + return "" |
| 61 | + } |
| 62 | + |
| 63 | + // Generate funnel ID |
| 64 | + funnelID := uuid.New().String() |
| 65 | + service.funnelCount++ |
| 66 | + |
| 67 | + fmt.Printf("Created funnel: %s\n", funnelID) |
| 68 | + return funnelID |
| 69 | + } |
| 70 | + |
| 71 | + analyticsservice.Exports.GetFunnelResults = func(funnelID string) cm.List[uint8] { |
| 72 | + // Simple funnel results |
| 73 | + results := map[string]interface{}{ |
| 74 | + "funnel_id": funnelID, |
| 75 | + "steps": []map[string]interface{}{ |
| 76 | + { |
| 77 | + "step_name": "step1", |
| 78 | + "event_count": service.eventCount, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + data, _ := json.Marshal(results) |
| 84 | + return cm.ToList(data) |
| 85 | + } |
| 86 | + |
| 87 | + analyticsservice.Exports.HealthCheck = func() bool { |
| 88 | + return true |
| 89 | + } |
| 90 | + |
| 91 | + analyticsservice.Exports.GetServiceStats = func() string { |
| 92 | + stats := map[string]interface{}{ |
| 93 | + "service_name": "Go Analytics Service", |
| 94 | + "version": "1.0.0", |
| 95 | + "total_events": service.eventCount, |
| 96 | + "total_funnels": service.funnelCount, |
| 97 | + } |
| 98 | + |
| 99 | + data, _ := json.Marshal(stats) |
| 100 | + return string(data) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func main() { |
| 105 | + fmt.Println("Analytics Service initialized") |
| 106 | +} |
0 commit comments