X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fcontroller%2FWebAnalyticsExtAppController.java;h=a8eb1d63b93960db58a4e77c06c0176716f2971e;hb=cc6ee26dd12723032eb026e79f71a0fdd35dde03;hp=f1192f92b6e32090569b8e6fecd01c0fa2ce2f0e;hpb=a2c93d258e4c564880601470434a7c0e168785c8;p=portal.git diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WebAnalyticsExtAppController.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WebAnalyticsExtAppController.java index f1192f92..a8eb1d63 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WebAnalyticsExtAppController.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WebAnalyticsExtAppController.java @@ -4,6 +4,8 @@ * =================================================================== * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * =================================================================== + * Modifications Copyright (c) 2020 IBM + * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); @@ -55,7 +57,7 @@ import org.onap.portalapp.portal.logging.aop.EPAuditLog; import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice; import org.onap.portalapp.portal.logging.logic.EPLogUtil; import org.onap.portalapp.portal.service.AppsCacheService; -import org.onap.portalapp.portal.service.ConsulHealthService; +import org.onap.portalapp.portal.service.WidgetMService; import org.onap.portalapp.portal.transport.Analytics; import org.onap.portalapp.portal.utils.EPCommonSystemProperties; import org.onap.portalapp.portal.utils.EcompPortalUtils; @@ -77,6 +79,8 @@ import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.SuccessCallback; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @@ -89,22 +93,23 @@ import org.springframework.web.client.AsyncRestTemplate; @EPAuditLog @NoArgsConstructor public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseController { - private ConsulHealthService consulHealthService; + public static final String FEED_ML = "feed.ml"; + private WidgetMService widgetMService; private AppsCacheService appCacheService; private static final String MACHINE_LEARNING_SERVICE_CTX = "/ml_api"; private static final String REGISTER_ACTION = MACHINE_LEARNING_SERVICE_CTX + "/" + "registerAction"; - private static final String CONSUL_ML_SERVICE_ID = "machine-learning"; private static final String APP_KEY = "uebkey"; + private static final String ERROR_MSG = " Error retrieving Application to capture app name for analytics; Proceeding with empty app name"; private final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WebAnalyticsExtAppController.class); private final AsyncRestTemplate restTemplate = new AsyncRestTemplate(); private final SuccessCallback> successCallback = arg -> logger.info(EELFLoggerDelegate.debugLogger, arg.getBody()); private final FailureCallback failureCallback = arg -> logger.error(EELFLoggerDelegate.errorLogger, "storeAuxAnalytics failed", arg); @Autowired - public WebAnalyticsExtAppController(AppsCacheService appCacheService, ConsulHealthService consulHealthService) { + public WebAnalyticsExtAppController(AppsCacheService appCacheService, WidgetMService consulHealthService) { this.appCacheService = appCacheService; - this.consulHealthService = consulHealthService; + this.widgetMService = consulHealthService; } /** @@ -116,7 +121,7 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl * @return String */ @ApiOperation(value = "Gets javascript with functions that support gathering and reporting web analytics.", response = String.class) - @RequestMapping(value = { "/analytics" }, method = RequestMethod.GET, produces = "application/javascript") + @GetMapping(value = { "/analytics" }, produces = "application/javascript") public String getAnalyticsScript(HttpServletRequest request) { String responseText = ""; EPApp app = null; @@ -124,13 +129,13 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl try { app = getApp(request); } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, - " Error retrieving Application to capture app name for analytics; Proceeding with empty app name"); + logger.error(EELFLoggerDelegate.errorLogger, + ERROR_MSG, e); } if (app != null) { String restEndPoint = app.getAppRestEndpoint(); - if(restEndPoint.contains("/api")) { - version = restEndPoint.substring(restEndPoint.indexOf("/api")+4); + if(restEndPoint.indexOf("/api")!=-1) { + version = restEndPoint.substring(restEndPoint.indexOf("api")); } } String endPoint = "/storeAnalytics"; @@ -161,7 +166,7 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl * Analytics * @return PortalAPIResponse */ - @RequestMapping(value = { "/storeAnalytics" }, method = RequestMethod.POST, produces = "application/json") + @PostMapping(value = { "/storeAnalytics" }, produces = "application/json") @ResponseBody @ApiOperation(value = "Accepts data from partner applications with web analytics data.", response = PortalAPIResponse.class) public PortalAPIResponse storeAnalyticsScript(HttpServletRequest request, @RequestBody Analytics analyticsMap) { @@ -172,14 +177,18 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl appName = getAppName(request, appName); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, - " Error retrieving Application to capture app name for analytics; Proceeding with empty app name"); + ERROR_MSG, e); } try { - storeAuxAnalytics(analyticsMap, appName); + if(SystemProperties.containsProperty(FEED_ML) && + SystemProperties.getProperty(FEED_ML).equals("true")) { + storeAuxAnalytics(analyticsMap, appName); + } + } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, - " Error retrieving Application to capture app name for analytics; Proceeding with empty app name"); + ERROR_MSG, e); } MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC()); @@ -224,15 +233,16 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl return appRecord; } - private void storeAuxAnalytics(Analytics analyticsMap, String appName) { + protected void storeAuxAnalytics(Analytics analyticsMap, String appName) { logger.info(EELFLoggerDelegate.debugLogger, " Registering an action for recommendation: AppName/Function/UserId " + appName + "/" + analyticsMap.getFunction() + "/" + analyticsMap.getUserid()); + Map requestMapping = new HashMap<>(); requestMapping.put("id", analyticsMap.getUserid()); requestMapping.put("action", appName + "|" + analyticsMap.getFunction()); - + HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); @@ -241,12 +251,11 @@ public class WebAnalyticsExtAppController extends EPRestrictedRESTfulBaseControl // send it! ListenableFuture> out = restTemplate.exchange( - EcompPortalUtils.widgetMsProtocol() + "://" - + consulHealthService.getServiceLocation(CONSUL_ML_SERVICE_ID, - SystemProperties.getProperty("microservices.m-learn.local.port")) + EcompPortalUtils.widgetMLProtocol() + "://" + + widgetMService.getMLServiceLocation() + REGISTER_ACTION, HttpMethod.POST, entity, String.class); out.addCallback(successCallback, failureCallback); } -} +} \ No newline at end of file