aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/metrics/statistics-provider.go
diff options
context:
space:
mode:
authorgururajarao79 <gb00566633@techmahindra.com>2024-11-22 14:28:41 +0100
committergururajarao79 <gb00566633@techmahindra.com>2024-11-27 13:26:18 +0100
commit542b9d143da57bcb3894f730c9ca2200aa7b89cb (patch)
treeb72c52f5bc3aad77c82600b8541d6db2a55ef651 /pkg/metrics/statistics-provider.go
parent55772eebb9ec2bb12cd16e5c2531dceb0860ad5b (diff)
phase1 opa pdp changes
For details on scope and implementation, please check. https://lf-onap.atlassian.net/wiki/spaces/DW/pages/51150925/OPA+PDP Code Coverage Total: 70.8% Issue-ID: POLICY-5156 Change-Id: Ied07ee1596e9f447183fb715baaa68c704a9fe99 Signed-off-by: gururajarao79 <gb00566633@techmahindra.com>
Diffstat (limited to 'pkg/metrics/statistics-provider.go')
-rw-r--r--pkg/metrics/statistics-provider.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/pkg/metrics/statistics-provider.go b/pkg/metrics/statistics-provider.go
new file mode 100644
index 0000000..67cee79
--- /dev/null
+++ b/pkg/metrics/statistics-provider.go
@@ -0,0 +1,65 @@
+// Handles an HTTP request to fetch the current system statistics.
+// It aggregates various decision counts (e.g., indeterminate, permit, deny)
+// and error counts into a structured response and sends it back to the client in JSON format.
+package metrics
+
+import (
+ "encoding/json"
+ "net/http"
+ "policy-opa-pdp/pkg/log"
+ "policy-opa-pdp/pkg/model/oapicodegen"
+ "policy-opa-pdp/pkg/utils"
+
+ "github.com/google/uuid"
+ openapi_types "github.com/oapi-codegen/runtime/types"
+)
+
+func FetchCurrentStatistics(res http.ResponseWriter, req *http.Request) {
+
+ requestId := req.Header.Get("X-ONAP-RequestID")
+ var parsedUUID *uuid.UUID
+ var statisticsParams *oapicodegen.StatisticsParams
+
+ if requestId != "" && utils.IsValidUUID(requestId) {
+ tempUUID, err := uuid.Parse(requestId)
+ if err != nil {
+ log.Warnf("Error Parsing the requestID: %v", err)
+ } else {
+ parsedUUID = &tempUUID
+ statisticsParams = &oapicodegen.StatisticsParams{
+ XONAPRequestID: (*openapi_types.UUID)(parsedUUID),
+ }
+ res.Header().Set("X-ONAP-RequestID", statisticsParams.XONAPRequestID.String())
+ }
+ } else {
+ log.Warnf("Invalid or Missing Request ID")
+ requestId = "000000000000"
+ res.Header().Set("X-ONAP-RequestID", requestId)
+ }
+
+ var statReport oapicodegen.StatisticsReport
+
+ statReport.IndeterminantDecisionsCount = IndeterminantDecisionsCountRef()
+ statReport.PermitDecisionsCount = PermitDecisionsCountRef()
+ statReport.DenyDecisionsCount = DenyDecisionsCountRef()
+ statReport.TotalErrorCount = TotalErrorCountRef()
+
+ // not implemented hardcoding the values to zero
+ // will be implemeneted in phase-2
+ zerovalue := int64(0)
+ onevalue := int64(1)
+ statReport.TotalPoliciesCount = &zerovalue
+ statReport.TotalPolicyTypesCount = &onevalue
+ statReport.DeployFailureCount = &zerovalue
+ statReport.DeploySuccessCount = &zerovalue
+ statReport.UndeployFailureCount = &zerovalue
+ statReport.UndeploySuccessCount = &zerovalue
+
+ value := int32(200)
+ statReport.Code = &value
+
+ res.Header().Set("Content-Type", "application/json")
+ res.WriteHeader(http.StatusOK)
+ json.NewEncoder(res).Encode(statReport)
+
+}