From 31fefa643fecb8ee4fa740634318dafb3ac805ae Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sat, 2 May 2026 17:54:05 +0300 Subject: [PATCH] feat: Implement carrier metrics persistence with dedicated metrics table and timestamp updates - Replace mock logging with actual database persistence in UpdateCarrierMetrics - Add carrier record timestamp update with updated_at field - Add carrier_metrics table insertion with JSON-serialized metrics data - Add metricsRecord struct with CarrierID, MetricsData, and RecordedAt fields - Add zero-division protection for success rate calculation - Update logging to use structured fields with WithField --- .../internal/integration/carrier_config.go | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/apps/carrier-connector/internal/integration/carrier_config.go b/apps/carrier-connector/internal/integration/carrier_config.go index e414205..65fbd57 100644 --- a/apps/carrier-connector/internal/integration/carrier_config.go +++ b/apps/carrier-connector/internal/integration/carrier_config.go @@ -108,11 +108,45 @@ func (r *GormCarrierRepository) SaveCarrier(ctx context.Context, carrier *smdp.C // UpdateCarrierMetrics updates carrier metrics func (r *GormCarrierRepository) UpdateCarrierMetrics(ctx context.Context, id string, metrics *smdp.CarrierMetrics) error { - // In a real implementation, you might have a separate metrics table - // For now, we'll log the metrics update - r.logger.Info("Carrier metrics updated", "carrier_id", id, - "total_requests", metrics.TotalRequests, - "success_rate", float64(metrics.SuccessfulRequests)/float64(metrics.TotalRequests)*100) + // Persist metrics alongside the carrier record + updates := map[string]interface{}{ + "updated_at": time.Now(), + } + + if err := r.db.WithContext(ctx).Table("carriers").Where("id = ?", id).Updates(updates).Error; err != nil { + r.logger.WithError(err).Error("Failed to update carrier record timestamp") + return fmt.Errorf("failed to update carrier metrics: %w", err) + } + + // Store detailed metrics in a dedicated metrics table + metricsJSON, err := json.Marshal(metrics) + if err != nil { + return fmt.Errorf("failed to marshal carrier metrics: %w", err) + } + + metricsRecord := struct { + CarrierID string `gorm:"column:carrier_id;index"` + MetricsData string `gorm:"column:metrics_data;type:text"` + RecordedAt time.Time `gorm:"column:recorded_at"` + }{ + CarrierID: id, + MetricsData: string(metricsJSON), + RecordedAt: time.Now(), + } + + if err := r.db.WithContext(ctx).Table("carrier_metrics").Create(&metricsRecord).Error; err != nil { + r.logger.WithError(err).Error("Failed to persist carrier metrics") + return fmt.Errorf("failed to store carrier metrics: %w", err) + } + + successRate := float64(0) + if metrics.TotalRequests > 0 { + successRate = float64(metrics.SuccessfulRequests) / float64(metrics.TotalRequests) * 100 + } + r.logger.WithField("carrier_id", id). + WithField("total_requests", metrics.TotalRequests). + WithField("success_rate", successRate). + Info("Carrier metrics updated") return nil }