Skip to content

Commit

Permalink
#30443 adding changes for the event source (#30516)
Browse files Browse the repository at this point in the history
Adding the event source + some minor refactoring
  • Loading branch information
jdotcms authored Nov 20, 2024
1 parent 745ee55 commit 852d1c2
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
// this will be a new event
final CachedVanityUrl cachedVanityUrl = (CachedVanityUrl) collectorContextMap.get(Constants.VANITY_URL_OBJECT);

final Host currentHost = (Host)collectorContextMap.get("currentHost");
final Long languageId = (Long)collectorContextMap.get("langId");
final Host currentHost = (Host)collectorContextMap.get(CollectorContextMap.CURRENT_HOST);
final Long languageId = (Long)collectorContextMap.get(CollectorContextMap.LANG_ID);

final Host site = Try.of(()->this.hostAPI.find(currentHost.getIdentifier(), APILocator.systemUser(),
false)).get();
Expand All @@ -67,7 +67,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
match.get(whoIAM).collect(innerCollectorContextMap, collectorPayloadBean);
}

collectorPayloadBean.put("comeFromVanityURL", true);
collectorPayloadBean.put(COME_FROM_VANITY_URL, true);
return collectorPayloadBean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.util.PageMode;
import com.dotmarketing.util.UtilMethods;
import com.liferay.util.StringPool;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
Expand All @@ -14,6 +15,10 @@
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
* Collects the basic profile information for a collector payload bean.
* @author jsanca
*/
public class BasicProfileCollector implements Collector {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
Expand All @@ -28,46 +33,51 @@ public boolean test(CollectorContextMap collectorContextMap) {
public CollectorPayloadBean collect(final CollectorContextMap collectorContextMap,
final CollectorPayloadBean collectorPayloadBean) {

// todo: add the user id
final String requestId = (String)collectorContextMap.get("requestId");
final Long time = (Long)collectorContextMap.get("time");
final String clusterId = (String)collectorContextMap.get("cluster");
final String serverId = (String)collectorContextMap.get("server");
final String sessionId = (String)collectorContextMap.get("session");
final Boolean sessionNew = (Boolean)collectorContextMap.get("sessionNew");
final String requestId = (String)collectorContextMap.get(CollectorContextMap.REQUEST_ID);
final Long time = (Long)collectorContextMap.get(CollectorContextMap.TIME);
final String clusterId = (String)collectorContextMap.get(CollectorContextMap.CLUSTER);
final String serverId = (String)collectorContextMap.get(CollectorContextMap.SERVER);
final String sessionId = (String)collectorContextMap.get(CollectorContextMap.SESSION);
final Boolean sessionNew = (Boolean)collectorContextMap.get(CollectorContextMap.SESSION_NEW);

final Long timestamp = FunctionUtils.getOrDefault(Objects.nonNull(time), () -> time, System::currentTimeMillis);
final Instant instant = Instant.ofEpochMilli(timestamp);
final ZonedDateTime zonedDateTimeUTC = instant.atZone(ZoneId.of("UTC"));

collectorPayloadBean.put("request_id", requestId);
collectorPayloadBean.put("utc_time", FORMATTER.format(zonedDateTimeUTC));
collectorPayloadBean.put("cluster",
collectorPayloadBean.put(REQUEST_ID, requestId);
collectorPayloadBean.put(UTC_TIME, FORMATTER.format(zonedDateTimeUTC));
collectorPayloadBean.put(CLUSTER,
FunctionUtils.getOrDefault(Objects.nonNull(clusterId), ()->clusterId, ClusterFactory::getClusterId));
collectorPayloadBean.put("server",
collectorPayloadBean.put(SERVER,
FunctionUtils.getOrDefault(Objects.nonNull(serverId), ()->serverId,()->APILocator.getServerAPI().readServerId()));
collectorPayloadBean.put("sessionId", sessionId);
collectorPayloadBean.put("sessionNew", sessionNew);
collectorPayloadBean.put(SESSION_ID, sessionId);
collectorPayloadBean.put(SESSION_NEW, sessionNew);

if (UtilMethods.isSet(collectorContextMap.get("referer"))) {
collectorPayloadBean.put("referer", collectorContextMap.get("referer").toString());
if (UtilMethods.isSet(collectorContextMap.get(CollectorContextMap.REFERER))) {
collectorPayloadBean.put(REFERER, collectorContextMap.get(CollectorContextMap.REFERER).toString());
}

if (UtilMethods.isSet(collectorContextMap.get("user-agent"))) {
collectorPayloadBean.put("userAgent", collectorContextMap.get("user-agent").toString());
if (UtilMethods.isSet(collectorContextMap.get(CollectorContextMap.USER_AGENT))) {
collectorPayloadBean.put(USER_AGENT, collectorContextMap.get(CollectorContextMap.USER_AGENT).toString());
}

final HttpServletRequest request = (HttpServletRequest)collectorContextMap.get("request");

collectorPayloadBean.put("persona",
collectorPayloadBean.put(PERSONA,
WebAPILocator.getPersonalizationWebAPI().getContainerPersonalization(request));

collectorPayloadBean.put("renderMode", PageMode.get(request).toString().replace("_MODE", ""));
collectorPayloadBean.put(RENDER_MODE, PageMode.get(request).toString().replace("_MODE", StringPool.BLANK));

// Include default value for other boolean fields in the Clickhouse table
collectorPayloadBean.put("comeFromVanityURL", false);
collectorPayloadBean.put("isexperimentpage", false);
collectorPayloadBean.put("istargetpage", false);
collectorPayloadBean.put(COME_FROM_VANITY_URL, false);
collectorPayloadBean.put(IS_EXPERIMENT_PAGE, false);
collectorPayloadBean.put(IS_TARGET_PAGE, false);

if (Objects.nonNull(collectorPayloadBean.get(EVENT_SOURCE))) {
// this is the default event source
collectorPayloadBean.put(EVENT_SOURCE, EventSource.DOT_CMS.getName());
}

return collectorPayloadBean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@
*/
public interface Collector {

String CLUSTER = "cluster";
String COME_FROM_VANITY_URL = "comeFromVanityURL";
String CONTENT_TYPE_ID = "content_type_id";
String CONTENT_TYPE_NAME = "content_type_name";
String CONTENT_TYPE_VAR_NAME = "content_type_var_name";
String DETAIL_PAGE_URL = "detail_page_url";
String EVENT_SOURCE = "event_source";
String EVENT_TYPE = "event_type";
String HOST = "host";
String ID = "id";
String IS_EXPERIMENT_PAGE = "isexperimentpage";
String IS_TARGET_PAGE = "istargetpage";
String LANGUAGE = "language";
String LANGUAGE_ID = "language_id";
String OBJECT = "object";
String PERSONA = "persona";
String REFERER = "referer";
String RENDER_MODE = "renderMode";
String REQUEST_ID = "request_id";
String RESPONSE = "response";
String RESPONSE_CODE = "response_code";
String SERVER = "server";
String SESSION_ID = "sessionId";
String SESSION_NEW = "sessionNew";
String SITE = "site";
String TITLE = "title";
String URL = "url";
String USER_AGENT = "userAgent";
String UTC_TIME = "utc_time";
String VANITY_QUERY_STRING = "vanity_query_string";

/**
* Test if the collector should run
* @param collectorContextMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

public interface CollectorContextMap {

String REQUEST_ID = "requestId";
String TIME = "time";
String CLUSTER = "cluster";
String SERVER = "server";
String SESSION = "session";
String SESSION_NEW = "sessionNew";
String REFERER = "referer";
String USER_AGENT = "user-agent";
String CURRENT_HOST = "currentHost";
String LANG_ID = "langId";
String LANG = "lang";
String URI = "uri";
String HOST = "host";
String EVENT_TYPE = "eventType";
String PAGE_MODE = "pageMode";

Object get(String key);
RequestMatcher getRequestMatcher(); // since we do not have the previous step phase we need to keep this as an object, but will be a RequestMatcher
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.dotcms.analytics.track.matchers.UserCustomDefinedRequestMatcher;
import com.dotmarketing.beans.Host;
import com.liferay.util.StringPool;

import java.util.HashMap;
import java.util.Objects;

/**
Expand All @@ -21,17 +21,18 @@ public boolean test(final CollectorContextMap collectorContextMap) {
@Override
public CollectorPayloadBean collect(final CollectorContextMap collectorContextMap,
final CollectorPayloadBean collectorPayloadBean) {
final String uri = (String)collectorContextMap.get("uri");
final String host = (String)collectorContextMap.get("host");
final Host site = (Host) collectorContextMap.get("currentHost");
final String language = (String)collectorContextMap.get("lang");
collectorPayloadBean.put("url", uri);
collectorPayloadBean.put("host", Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put("language", language);
collectorPayloadBean.put("site", null != site?site.getIdentifier():"unknown");
final String eventType = (String)collectorContextMap.get("eventType") == null?
EventType.CUSTOM_USER_EVENT.getType():(String)collectorContextMap.get("eventType");
collectorPayloadBean.put("event_type", eventType);

final String uri = (String)collectorContextMap.get(CollectorContextMap.URI);
final String host = (String)collectorContextMap.get(CollectorContextMap.HOST);
final Host site = (Host) collectorContextMap.get(CollectorContextMap.CURRENT_HOST);
final String language = (String)collectorContextMap.get(CollectorContextMap.LANG);
collectorPayloadBean.put(URL, uri);
collectorPayloadBean.put(HOST, Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put(LANGUAGE, language);
collectorPayloadBean.put(SITE, null != site?site.getIdentifier():StringPool.UNKNOWN);
final String eventType = collectorContextMap.get(CollectorContextMap.EVENT_TYPE) == null?
EventType.CUSTOM_USER_EVENT.getType():(String)collectorContextMap.get(CollectorContextMap.EVENT_TYPE);
collectorPayloadBean.put(EVENT_TYPE, eventType);

return collectorPayloadBean;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dotcms.analytics.track.collectors;

/**
* Enum for the Event Sources
* @author jsanca
*/
public enum EventSource {

DOT_CMS("DOT_CMS"),
REST_API("REST_API"),
WORKFLOW("WORKFLOW"),
RULE("RULE");

private final String name;

EventSource(final String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAssetAPI;
import com.liferay.util.StringPool;
Expand Down Expand Up @@ -45,28 +44,28 @@ public boolean test(CollectorContextMap collectorContextMap) {
public CollectorPayloadBean collect(final CollectorContextMap collectorContextMap,
final CollectorPayloadBean collectorPayloadBean) {

final String uri = (String)collectorContextMap.get("uri");
final String host = (String)collectorContextMap.get("host");
final Host site = (Host) collectorContextMap.get("currentHost");
final Long languageId = (Long)collectorContextMap.get("langId");
final String language = (String)collectorContextMap.get("lang");
final String uri = (String)collectorContextMap.get(CollectorContextMap.URI);
final String host = (String)collectorContextMap.get(CollectorContextMap.HOST);
final Host site = (Host) collectorContextMap.get(CollectorContextMap.CURRENT_HOST);
final Long languageId = (Long)collectorContextMap.get(CollectorContextMap.LANG_ID);
final String language = (String)collectorContextMap.get(CollectorContextMap.LANG);
final HashMap<String, String> fileObject = new HashMap<>();

if (Objects.nonNull(uri) && Objects.nonNull(site) && Objects.nonNull(languageId)) {

getFileAsset(uri, site, languageId).ifPresent(fileAsset -> {
fileObject.put("id", fileAsset.getIdentifier());
fileObject.put("title", fileAsset.getTitle());
fileObject.put("url", uri);
fileObject.put(ID, fileAsset.getIdentifier());
fileObject.put(TITLE, fileAsset.getTitle());
fileObject.put(URL, uri);
});
}

collectorPayloadBean.put("object", fileObject);
collectorPayloadBean.put("url", uri);
collectorPayloadBean.put("host", Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put("language", language);
collectorPayloadBean.put("site", null != site?site.getIdentifier():"unknown");
collectorPayloadBean.put("event_type", EventType.FILE_REQUEST.getType());
collectorPayloadBean.put(OBJECT, fileObject);
collectorPayloadBean.put(URL, uri);
collectorPayloadBean.put(HOST, Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put(LANGUAGE, language);
collectorPayloadBean.put(SITE, null != site?site.getIdentifier():StringPool.UNKNOWN);
collectorPayloadBean.put(EVENT_TYPE, EventType.FILE_REQUEST.getType());

return collectorPayloadBean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.dotmarketing.portlets.htmlpageasset.model.IHTMLPage;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.PageMode;
import com.liferay.util.StringPool;
import io.vavr.control.Try;

import java.util.HashMap;
Expand Down Expand Up @@ -50,11 +51,11 @@ public boolean test(CollectorContextMap collectorContextMap) {
public CollectorPayloadBean collect(final CollectorContextMap collectorContextMap,
final CollectorPayloadBean collectorPayloadBean) {

final String uri = (String) collectorContextMap.get("uri");
final Host site = (Host) collectorContextMap.get("currentHost");
final Long languageId = (Long) collectorContextMap.get("langId");
final PageMode pageMode = (PageMode) collectorContextMap.get("pageMode");
final String language = (String)collectorContextMap.get("lang");
final String uri = (String) collectorContextMap.get(CollectorContextMap.URI);
final Host site = (Host) collectorContextMap.get(CollectorContextMap.CURRENT_HOST);
final Long languageId = (Long) collectorContextMap.get(CollectorContextMap.LANG_ID);
final PageMode pageMode = (PageMode) collectorContextMap.get(CollectorContextMap.PAGE_MODE);
final String language = (String)collectorContextMap.get(CollectorContextMap.LANG);

final UrlMapContext urlMapContext = new UrlMapContext(
pageMode, languageId, uri, site, APILocator.systemUser());
Expand All @@ -74,29 +75,29 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
.getOrNull();

final HashMap<String, String> pageObject = new HashMap<>();
pageObject.put("id", detailPageContent.getIdentifier());
pageObject.put("title", detailPageContent.getTitle());
pageObject.put("url", uri);
pageObject.put("detail_page_url", Try.of(detailPageContent::getURI).getOrElse(""));
collectorPayloadBean.put("object", pageObject);
pageObject.put(ID, detailPageContent.getIdentifier());
pageObject.put(TITLE, detailPageContent.getTitle());
pageObject.put(URL, uri);
pageObject.put(DETAIL_PAGE_URL, Try.of(detailPageContent::getURI).getOrElse(StringPool.BLANK));
collectorPayloadBean.put(OBJECT, pageObject);
}

collectorPayloadBean.put("event_type", EventType.PAGE_REQUEST.getType());
collectorPayloadBean.put("url", uri);
collectorPayloadBean.put("language", language);
collectorPayloadBean.put(EVENT_TYPE, EventType.PAGE_REQUEST.getType());
collectorPayloadBean.put(URL, uri);
collectorPayloadBean.put(LANGUAGE, language);

if (Objects.nonNull(site)) {
collectorPayloadBean.put("host", site.getHostname());
collectorPayloadBean.put(HOST, site.getHostname());
}
return collectorPayloadBean;
}

private boolean isUrlMap(final CollectorContextMap collectorContextMap){

final String uri = (String)collectorContextMap.get("uri");
final Long languageId = (Long)collectorContextMap.get("langId");
final PageMode pageMode = (PageMode)collectorContextMap.get("pageMode");
final Host site = (Host) collectorContextMap.get("currentHost");
final String uri = (String)collectorContextMap.get(CollectorContextMap.URI);
final Long languageId = (Long)collectorContextMap.get(CollectorContextMap.LANG_ID);
final PageMode pageMode = (PageMode)collectorContextMap.get(CollectorContextMap.PAGE_MODE);
final Host site = (Host) collectorContextMap.get(CollectorContextMap.CURRENT_HOST);

final UrlMapContext urlMapContext = new UrlMapContext(
pageMode, languageId, uri, site, APILocator.systemUser());
Expand Down
Loading

0 comments on commit 852d1c2

Please sign in to comment.