Commit 57d8a44f authored by Muhammad Umair Zafar's avatar Muhammad Umair Zafar
Browse files

Merge branch 'Task2_PoC' into 'STF678_Task1_2'

add http logs endpoint in the meep-sandbox-api

See merge request mec/AdvantEDGE!20
parents 982e2e16 4400c6c0
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -280,6 +280,39 @@ paths:
        "404":
          description: "Not Found :  used when a client provided a URI that cannot\
            \ be mapped to a valid resource URI."
  /apiConsoleLogs/{sandbox_name}:
    get:
      tags:
      - API Console
      summary: Get the list of http logs in the activated scenario.
      description: Get the list of http logs in the activated scenario.
      operationId: ApiConsoleLogsGET
      parameters:
      - name: sandbox_name
        in: path
        description: Sandbox identifier
        required: true
        style: simple
        explode: true
        schema:
          type: string
      responses:
        "200":
          description: "Upon success, a response message content containing an array of the list of the HTTP logs."
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  description: Object representing the HTTP logs.
        "400":
          description: "Bad Request : used to indicate that incorrect parameters were\
            \ passed to the request."
        "401":
          description: "Unauthorized : used when the client did not submit credentials."
        "404":
          description: "Not Found : No Activated Scenario found against the provided sandbox."
  /sandboxUeController/{sandbox_name}:
    get:
      tags:
+18 −0
Original line number Diff line number Diff line
/*
 * MEC Sandbox API
 *
 * The MEC Sandbox API described using OpenAPI
 *
 * API version: 0.0.2
 * Contact: cti_support@etsi.org
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */
 package server

 import (
	 "net/http"
 )

func ApiConsoleLogsGET(w http.ResponseWriter, r *http.Request) {
	apiConsoleLogsGET(w, r)
}
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
@@ -158,4 +158,11 @@ var routes = Routes{
		"/sandbox-api/v1/sandboxUeController/{sandbox_name}",
		SandboxUeControllerPATCH,
	},

	Route{
		"ApiConsoleLogsGET",
		strings.ToUpper("Get"),
		"/sandbox-api/v1/apiConsoleLogs/{sandbox_name}",
		ApiConsoleLogsGET,
	},
}
+57 −0
Original line number Diff line number Diff line
@@ -554,6 +554,63 @@ func login(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, string(jsonResponse))
}

func apiConsoleLogsGET(w http.ResponseWriter, r *http.Request) {
	log.Info(">>> GET HTTP Logs of the activated scenario: ", r)

	// Retrieve path parameters
	u, _ := url.Parse(r.URL.String())
	log.Info("url: ", u.RequestURI())

	path := u.Path
    log.Info("Full path: ", path)

    // Split the path to get the path parameter
    pathParts := strings.Split(path, "/")
    sandboxName := pathParts[len(pathParts)-1]
    
    log.Info("Path parameter SANDBOX at the end is: ", sandboxName)
	
	targetSubstring := sandboxName
	found := false
	
	dbNames, _ := sandboxApiConnectors.metricStore.GetDbsInInfluxDb()
	
	var dbName string
	for _, db := range dbNames {
		if name, ok := db["name"]; ok {
			var strName string
			if s, ok := name.(string); ok {
				strName = s
			} else {
				strName = fmt.Sprintf("%v", name)
			}
			if strings.Contains(strName, targetSubstring) {
				dbName = strName
				found = true
				break
			}
		}
	}
	log.Info("DB NAME IS: ", dbName)
	
    if found {
        log.Info(fmt.Sprintf("DB Name %s is present in the DB List", dbName))
		metricsResult, _ := sandboxApiConnectors.metricStore.GetHttpMetricWithDbName(dbName, moduleName, "notification", "", 10)
		log.Info("METRICS in the InfluxDB ARE: ", metricsResult)

		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, metricsResult)
		log.Info("METRICS in the InfluxDB ARE: ", metricsResult)
		return
    } else {
		msg := fmt.Sprintf("No active Scenario found against Sandbox: %s", sandboxName)
		log.Error(msg)
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprint(w, msg)
		return
    }
}

func getNamespace(w http.ResponseWriter, r *http.Request) {

	log.Info(">>> GET Namespace: ", r)
+68 −0
Original line number Diff line number Diff line
@@ -190,3 +190,71 @@ func (ms *MetricStore) GetHttpMetric(loggerName string, msgType string, duration
	}
	return
}

func (ms *MetricStore) GetHttpMetricWithDbName(dbName string, loggerName string, msgType string, duration string, count int) (metrics []HttpMetric, err error) {
	// Make sure we have set a store
	log.Info("Inside GET HTTP METRICS")
	if ms.name == "" {
		err = errors.New("Store name not specified")
		return
	}

	// Get Http metrics
	tags := map[string]string{}
	if loggerName != "" {
		tags[HttpLoggerName] = loggerName
	}
	if msgType != "" {
		tags[HttpLoggerMsgType] = msgType
	}
	fields := []string{HttpLoggerName, HttpLoggerMsgType, HttpLogId, HttpUrl, HttpLogEndpoint, HttpMethod, HttpSrc, HttpDst, HttpBody, HttpRespBody, HttpRespCode, HttpProcTime}
	var valuesArray []map[string]interface{}
	valuesArray, err = ms.GetInfluxMetricforDbName(dbName, HttpLogMetricName, tags, fields, duration, count)
	if err != nil {
		log.Error("Failed to retrieve metrics with error: ", err.Error())
		return
	}

	// Format http metrics
	metrics = make([]HttpMetric, len(valuesArray))
	for index, values := range valuesArray {
		metrics[index].Time = values[HttpLogTime]
		metrics[index].Id = JsonNumToInt32(values[HttpLogId].(json.Number))
		// Tags
		if val, ok := values[HttpLoggerName].(string); ok {
			metrics[index].LoggerName = val
		}
		if val, ok := values[HttpLoggerMsgType].(string); ok {
			metrics[index].MsgType = val
		}
		// Values
		if val, ok := values[HttpUrl].(string); ok {
			metrics[index].Url = val
		}
		if val, ok := values[HttpLogEndpoint].(string); ok {
			metrics[index].Endpoint = val
		}
		if val, ok := values[HttpMethod].(string); ok {
			metrics[index].Method = val
		}
		if val, ok := values[HttpSrc].(string); ok {
			metrics[index].Src = val
		}
		if val, ok := values[HttpDst].(string); ok {
			metrics[index].Dst = val
		}
		if val, ok := values[HttpBody].(string); ok {
			metrics[index].Body = val
		}
		if val, ok := values[HttpRespBody].(string); ok {
			metrics[index].RespBody = val
		}
		if val, ok := values[HttpRespCode].(string); ok {
			metrics[index].RespCode = val
		}
		if val, ok := values[HttpProcTime].(string); ok {
			metrics[index].ProcTime = val
		}
	}
	return
}
 No newline at end of file
Loading