From de98c2a2ff0c7bf392f79757b54f4382762df040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Jehli=C4=8Dka?= Date: Mon, 22 Mar 2021 23:58:06 +0100 Subject: [PATCH] EFGS: Metrics Preparing total/yesterday's amount of uploaded, downloaded keys and publisher for download from metrics API. --- internal/firebase/structs/structs.go | 22 +++++---- .../metricsapi/prepare-new-version.go | 45 +++++++++++++++---- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/internal/firebase/structs/structs.go b/internal/firebase/structs/structs.go index 4325423..808a026 100644 --- a/internal/firebase/structs/structs.go +++ b/internal/firebase/structs/structs.go @@ -66,12 +66,18 @@ type StringValue struct { //MetricsData Data of metrics. type MetricsData struct { - Modified int64 `json:"modified"` - Date string `json:"date"` - ActivationsYesterday int32 `json:"activations_yesterday"` - ActivationsTotal int32 `json:"activations_total"` - KeyPublishersYesterday int32 `json:"key_publishers_yesterday"` - KeyPublishersTotal int32 `json:"key_publishers_total"` - NotificationsYesterday int32 `json:"notifications_yesterday"` - NotificationsTotal int32 `json:"notifications_total"` + Modified int64 `json:"modified"` + Date string `json:"date"` + ActivationsYesterday int32 `json:"activations_yesterday"` + ActivationsTotal int32 `json:"activations_total"` + KeyPublishersYesterday int32 `json:"key_publishers_yesterday"` + KeyPublishersTotal int32 `json:"key_publishers_total"` + NotificationsYesterday int32 `json:"notifications_yesterday"` + NotificationsTotal int32 `json:"notifications_total"` + EfgsKeysUploadedTotal int32 `json:"efgs_keys_uploaded_total"` + EfgsKeysUploadedYesterday int32 `json:"efgs_keys_uploaded_yesterday"` + EfgsKeysDownloadedTotal int32 `json:"efgs_keys_downloaded_total"` + EfgsKeysDownloadedYesterday int32 `json:"efgs_keys_downloaded_yesterday"` + EfgsPublishersTotal int32 `json:"efgs_publishers_total"` + EfgsPublishersYesterday int32 `json:"efgs_publishers_yesterday"` } diff --git a/internal/functions/metricsapi/prepare-new-version.go b/internal/functions/metricsapi/prepare-new-version.go index d328df5..52351da 100644 --- a/internal/functions/metricsapi/prepare-new-version.go +++ b/internal/functions/metricsapi/prepare-new-version.go @@ -86,17 +86,28 @@ func prepareNewVersion(ctx context.Context, config *config) error { return fmt.Errorf("Error while fetching data: %v", err) } + yestEfgs, err := getEfgsCounter(ctx, config, yesterday.Format("20060102")) + if err != nil { + return fmt.Errorf("Error while fetching data: %v", err) + } + var today = config.now.Format("20060102") data := structs.MetricsData{ - Modified: config.now.Unix(), - Date: today, - ActivationsYesterday: yestActivations, - ActivationsTotal: yestData.ActivationsTotal + yestActivations, - KeyPublishersYesterday: yestPublishers, - KeyPublishersTotal: yestData.KeyPublishersTotal + yestPublishers, - NotificationsYesterday: yestNotifications, - NotificationsTotal: yestData.NotificationsTotal + yestNotifications, + Modified: config.now.Unix(), + Date: today, + ActivationsYesterday: yestActivations, + ActivationsTotal: yestData.ActivationsTotal + yestActivations, + KeyPublishersYesterday: yestPublishers, + KeyPublishersTotal: yestData.KeyPublishersTotal + yestPublishers, + NotificationsYesterday: yestNotifications, + NotificationsTotal: yestData.NotificationsTotal + yestNotifications, + EfgsKeysDownloadedYesterday: int32(yestEfgs.KeysDownloaded), + EfgsKeysDownloadedTotal: yestData.EfgsKeysDownloadedTotal + int32(yestEfgs.KeysDownloaded), + EfgsKeysUploadedYesterday: int32(yestEfgs.KeysUploaded), + EfgsKeysUploadedTotal: yestData.EfgsKeysUploadedTotal + int32(yestEfgs.KeysUploaded), + EfgsPublishersYesterday: int32(yestEfgs.Publishers), + EfgsPublishersTotal: yestData.EfgsPublishersTotal + int32(yestEfgs.Publishers), } logger.Debugf("Collected data: %+v", data) @@ -169,3 +180,21 @@ func getPublishersCount(ctx context.Context, config *config, key string) (int32, return int32(data.PublishersCount), nil } + +func getEfgsCounter(ctx context.Context, config *config, key string) (*structs.EfgsCounter, error) { + logger := logging.FromContext(ctx) + + logger.Debugf("Getting EFGS publishers with key %v", key) + + var data structs.EfgsCounter + + if err := config.realtimedbClient.NewRef(constants.DbEfgsCountersPrefix+key).Get(ctx, &data); err != nil { + if status.Code(err) != codes.NotFound { + return nil, fmt.Errorf("Error while querying Firestore: %v", err) + } + + logger.Debug("EFGS counter for '%v' was not found, using default value", key) + } + + return &data, nil +}