Skip to content

Commit

Permalink
Issue 30411 refactor collector payload key names (#30784)
Browse files Browse the repository at this point in the history
Renaming a lot of properties and adding a few extras
  • Loading branch information
jdotcms authored Dec 6, 2024
1 parent da51ee9 commit dc55296
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
if (UtilMethods.isSet(whoIAM)) {

final CollectorContextMap innerCollectorContextMap = new WrapperCollectorContextMap(collectorContextMap,
Map.of("uri", cachedVanityUrl.forwardTo));
Map.of(CollectorContextMap.URI, cachedVanityUrl.forwardTo));
match.get(whoIAM).collect(innerCollectorContextMap, collectorPayloadBean);
}

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

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -66,8 +69,6 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put(PERSONA,
WebAPILocator.getPersonalizationWebAPI().getContainerPersonalization(request));

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(COME_FROM_VANITY_URL, false);
collectorPayloadBean.put(IS_EXPERIMENT_PAGE, false);
Expand All @@ -78,9 +79,23 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put(EVENT_SOURCE, EventSource.DOT_CMS.getName());
}

setUserInfo(request, collectorPayloadBean);

return collectorPayloadBean;
}

private void setUserInfo(final HttpServletRequest request, final CollectorPayloadBean collectorPayloadBean) {

final User user = WebAPILocator.getUserWebAPI().getUser(request);
if (Objects.nonNull(user)) {

final HashMap<String, String> userObject = new HashMap<>();
userObject.put(ID, user.getUserId().toString());
userObject.put(EMAIL, user.getEmailAddress());
collectorPayloadBean.put(USER_OBJECT, userObject);
}
}

@Override
public boolean isEventCreator(){
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,51 @@
*/
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 ID = "identifier";
String TITLE = "title";
String INODE = "inode"; // NOT USED YET BUT MAY BE DESIRED
String BASE_TYPE = "baseType";
String PATH = "path"; // NOT USED YET BUT MAY BE DESIRED
String LIVE = "live"; // NOT USED YET BUT MAY BE DESIRED
String WORKING = "working"; // NOT USED YET BUT MAY BE DESIRED
String CONTENT_TYPE_VAR_NAME = "contentType";
String CONTENT_TYPE_NAME = "contentTypeName";
String CONTENT_TYPE_ID = "contentTypeId";
String SITE_ID = "conHost";
String SITE_NAME = "conHostName";
String LANGUAGE_ID = "languageId";
String LANGUAGE = "language";

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 CLUSTER = "cluster";
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 RENDER_MODE = "renderMode";
String RESPONSE = "response";
String RESPONSE_CODE = "response_code";


String COME_FROM_VANITY_URL = "comeFromVanityURL";
String DETAIL_PAGE_URL = "detail_page_url";
String IS_EXPERIMENT_PAGE = "isexperimentpage";
String IS_TARGET_PAGE = "istargetpage";
String VANITY_QUERY_STRING = "vanity_query_string";
String VANITY_URL_KEY = "vanity_url";
String FORWARD_TO = "forwardTo";

String EMAIL = "email";
String USER_OBJECT = "user";
/**
* Test if the collector should run
* @param collectorContextMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public interface CollectorContextMap {

String REQUEST_ID = "requestId";
String REQUEST = "request";
String TIME = "time";
String CLUSTER = "cluster";
String SERVER = "server";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
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(SITE_NAME, Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put(LANGUAGE, language);
collectorPayloadBean.put(SITE, null != site?site.getIdentifier():StringPool.UNKNOWN);
collectorPayloadBean.put(SITE_ID, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAssetAPI;
import com.liferay.util.StringPool;
import io.vavr.control.Try;

import java.util.HashMap;
import java.util.Objects;
Expand Down Expand Up @@ -57,14 +58,20 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
fileObject.put(ID, fileAsset.getIdentifier());
fileObject.put(TITLE, fileAsset.getTitle());
fileObject.put(URL, uri);
fileObject.put(CONTENT_TYPE_ID, fileAsset.getContentType().id());
fileObject.put(CONTENT_TYPE_NAME, fileAsset.getContentType().name());
fileObject.put(CONTENT_TYPE_VAR_NAME, fileAsset.getContentType().variable());
fileObject.put(BASE_TYPE, fileAsset.getContentType().baseType().name());
fileObject.put(LIVE, String.valueOf(Try.of(()->fileAsset.isLive()).getOrElse(false)));
fileObject.put(WORKING, String.valueOf(Try.of(()->fileAsset.isWorking()).getOrElse(false)));
});
}

collectorPayloadBean.put(OBJECT, fileObject);
collectorPayloadBean.put(URL, uri);
collectorPayloadBean.put(HOST, Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put(SITE_NAME, Objects.nonNull(site)?site.getHostname():host);
collectorPayloadBean.put(LANGUAGE, language);
collectorPayloadBean.put(SITE, null != site?site.getIdentifier():StringPool.UNKNOWN);
collectorPayloadBean.put(SITE_ID, 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 @@ -78,6 +78,12 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
pageObject.put(ID, detailPageContent.getIdentifier());
pageObject.put(TITLE, detailPageContent.getTitle());
pageObject.put(URL, uri);
pageObject.put(CONTENT_TYPE_ID, urlMapContentlet.getContentType().id());
pageObject.put(CONTENT_TYPE_NAME, urlMapContentlet.getContentType().name());
pageObject.put(CONTENT_TYPE_VAR_NAME, urlMapContentlet.getContentType().variable());
pageObject.put(BASE_TYPE, urlMapContentlet.getContentType().baseType().name());
pageObject.put(LIVE, String.valueOf(Try.of(()->urlMapContentlet.isLive()).getOrElse(false)));
pageObject.put(WORKING, String.valueOf(Try.of(()->urlMapContentlet.isWorking()).getOrElse(false)));
pageObject.put(DETAIL_PAGE_URL, Try.of(detailPageContent::getURI).getOrElse(StringPool.BLANK));
collectorPayloadBean.put(OBJECT, pageObject);
}
Expand All @@ -87,7 +93,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put(LANGUAGE, language);

if (Objects.nonNull(site)) {
collectorPayloadBean.put(HOST, site.getHostname());
collectorPayloadBean.put(SITE_NAME, site.getHostname());
}
return collectorPayloadBean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,23 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
pageObject.put(CONTENT_TYPE_ID, urlMapContentType.id());
pageObject.put(CONTENT_TYPE_NAME, urlMapContentType.name());
pageObject.put(CONTENT_TYPE_VAR_NAME, urlMapContentType.variable());
pageObject.put(BASE_TYPE, urlMapContentType.baseType().name());
pageObject.put(LIVE, String.valueOf(Try.of(()->urlMapContentlet.isLive()).getOrElse(false)));
pageObject.put(WORKING, String.valueOf(Try.of(()->urlMapContentlet.isWorking()).getOrElse(false)));
collectorPayloadBean.put(EVENT_TYPE, EventType.URL_MAP.getType());
}
} else {
final IHTMLPage page = Try.of(() ->
this.pageAPI.getPageByPath(uri, site, languageId, true)).get();
pageObject.put(ID, page.getIdentifier());
pageObject.put(TITLE, page.getTitle());
final Contentlet pageContentlet = (Contentlet) page;
pageObject.put(CONTENT_TYPE_ID, pageContentlet.getContentType().id());
pageObject.put(CONTENT_TYPE_NAME, pageContentlet.getContentType().name());
pageObject.put(CONTENT_TYPE_VAR_NAME, pageContentlet.getContentType().variable());
pageObject.put(BASE_TYPE, pageContentlet.getContentType().baseType().name());
pageObject.put(LIVE, String.valueOf(Try.of(()->page.isLive()).getOrElse(false)));
pageObject.put(WORKING, String.valueOf(Try.of(()->page.isWorking()).getOrElse(false)));
collectorPayloadBean.put(EVENT_TYPE, EventType.PAGE_REQUEST.getType());
}
pageObject.put(URL, uri);
Expand All @@ -92,7 +102,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put(LANGUAGE, language);

if (Objects.nonNull(site)) {
collectorPayloadBean.put(HOST, site.getHostname());
collectorPayloadBean.put(SITE_NAME, site.getHostname());
}

return collectorPayloadBean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
public class SyncVanitiesCollector implements Collector {

public static final String VANITY_URL_KEY = "vanity_url";
public static final String FORWARD_TO = "forward_to";

public SyncVanitiesCollector() {
}
Expand All @@ -32,9 +30,9 @@ public boolean test(CollectorContextMap collectorContextMap) {
@Override
public CollectorPayloadBean collect(final CollectorContextMap collectorContextMap,
final CollectorPayloadBean collectorPayloadBean) {
if (null != collectorContextMap.get("request")) {
if (null != collectorContextMap.get(CollectorContextMap.REQUEST)) {

final HttpServletRequest request = (HttpServletRequest)collectorContextMap.get("request");
final HttpServletRequest request = (HttpServletRequest)collectorContextMap.get(CollectorContextMap.REQUEST);
final String vanityUrl = (String)request.getAttribute(Constants.CMS_FILTER_URI_OVERRIDE);
final String vanityQueryString = (String)request.getAttribute(Constants.CMS_FILTER_QUERY_STRING_OVERRIDE);
if (request instanceof VanityUrlRequestWrapper) {
Expand Down Expand Up @@ -66,7 +64,7 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa
collectorPayloadBean.put(URL, uri);
collectorPayloadBean.put(LANGUAGE, language);
collectorPayloadBean.put(LANGUAGE_ID, languageId);
collectorPayloadBean.put(SITE, site.getIdentifier());
collectorPayloadBean.put(SITE_ID, site.getIdentifier());
collectorPayloadBean.put(EVENT_TYPE, EventType.VANITY_REQUEST.getType());

return collectorPayloadBean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public RequestMatcher getRequestMatcher() {
final CollectorPayloadBean collectorPayloadBean = new ConcurrentCollectorPayloadBean();
customerEventCollector.collect(collectorContextMap, collectorPayloadBean);

Assert.assertEquals("/test-path", collectorPayloadBean.get("url"));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get("host"));
Assert.assertEquals("en", collectorPayloadBean.get("language"));
Assert.assertEquals("1", collectorPayloadBean.get("site"));
Assert.assertEquals(EventType.CUSTOM_USER_EVENT.getType(), collectorPayloadBean.get("event_type"));
Assert.assertEquals("/test-path", collectorPayloadBean.get(Collector.URL));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get(Collector.SITE_NAME));
Assert.assertEquals("en", collectorPayloadBean.get(Collector.LANGUAGE));
Assert.assertEquals("1", collectorPayloadBean.get(Collector.SITE_ID));
Assert.assertEquals(EventType.CUSTOM_USER_EVENT.getType(), collectorPayloadBean.get(Collector.EVENT_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.dotcms.UnitTestBase;
import com.dotcms.analytics.track.matchers.RequestMatcher;
import com.dotcms.contenttype.model.type.BaseContentType;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotmarketing.beans.Host;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import org.junit.Assert;
Expand Down Expand Up @@ -30,9 +32,24 @@ public void test_collect_easy_path() throws IOException {
final FilesCollector filesCollector = new FilesCollector() {
@Override
protected Optional<Contentlet> getFileAsset(String uri, Host host, Long languageId) {

final Contentlet contentlet = Mockito.mock(Contentlet.class);
Mockito.when(contentlet.getIdentifier()).thenReturn("1");
Mockito.when(contentlet.getTitle()).thenReturn("Test");
try {
Mockito.when(contentlet.isLive()).thenReturn(true);
Mockito.when(contentlet.isWorking()).thenReturn(true);
} catch (Exception e) {

}
final ContentType contentType = Mockito.mock(ContentType.class);
Mockito.when(contentlet.getContentType()).thenReturn(contentType);
Mockito.when(contentType.id()).thenReturn("1");
Mockito.when(contentType.name()).thenReturn("file");
Mockito.when(contentType.variable()).thenReturn("file");
final BaseContentType baseType = Mockito.mock(BaseContentType.class);
Mockito.when(contentType.baseType()).thenReturn(baseType);
Mockito.when(baseType.name()).thenReturn("file");
return Optional.ofNullable(contentlet);
}
};
Expand Down Expand Up @@ -70,13 +87,12 @@ public RequestMatcher getRequestMatcher() {

final Map<String, String> fileObject = (Map<String, String>) collectorPayloadBean.get("object");
Assert.assertNotNull(fileObject);
Assert.assertEquals("1", fileObject.get("id"));
Assert.assertEquals("Test", fileObject.get("title"));
Assert.assertEquals("/test-path", fileObject.get("url"));
Assert.assertEquals("/test-path", collectorPayloadBean.get("url"));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get("host"));
Assert.assertEquals("en", collectorPayloadBean.get("language"));
Assert.assertEquals("1", collectorPayloadBean.get("site"));
Assert.assertEquals(EventType.FILE_REQUEST.getType(), collectorPayloadBean.get("event_type"));
Assert.assertEquals("1", fileObject.get(Collector.ID));
Assert.assertEquals("Test", fileObject.get(Collector.TITLE));
Assert.assertEquals("/test-path", fileObject.get(Collector.URL));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get(Collector.SITE_NAME));
Assert.assertEquals("en", collectorPayloadBean.get(Collector.LANGUAGE));
Assert.assertEquals("1", collectorPayloadBean.get(Collector.SITE_ID));
Assert.assertEquals(EventType.FILE_REQUEST.getType(), collectorPayloadBean.get(Collector.EVENT_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import com.dotcms.analytics.track.matchers.RequestMatcher;
import com.dotmarketing.beans.Host;
import com.dotmarketing.cms.urlmap.URLMapAPIImpl;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.htmlpageasset.business.HTMLPageAssetAPI;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

/**
* Test for the {@link PagesCollector} class
Expand All @@ -29,9 +26,9 @@ public class PagesCollectorTest {
public void test_collect_easy_path() throws IOException {

final PagesCollector pagesCollector = new PagesCollector(Mockito.mock(HTMLPageAssetAPI.class), Mockito.mock(URLMapAPIImpl.class));
final Host host = new Host();
host.setIdentifier("1");
host.setHostname("www.dotcms.com");
final Host host = Mockito.mock(Host.class);
Mockito.when(host.getIdentifier()).thenReturn("1");
Mockito.when(host.getHostname()).thenReturn("www.dotcms.com");
final CollectorContextMap collectorContextMap = new CollectorContextMap() {
@Override
public Object get(final String key) {
Expand Down Expand Up @@ -62,9 +59,9 @@ public RequestMatcher getRequestMatcher() {
final CollectorPayloadBean collectorPayloadBean = new ConcurrentCollectorPayloadBean();
pagesCollector.collect(collectorContextMap, collectorPayloadBean);

Assert.assertEquals("/test-path", collectorPayloadBean.get("url"));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get("host"));
Assert.assertEquals("en", collectorPayloadBean.get("language"));
Assert.assertEquals(EventType.PAGE_REQUEST.getType(), collectorPayloadBean.get("event_type"));
Assert.assertEquals("/test-path", collectorPayloadBean.get(Collector.URL));
Assert.assertEquals("www.dotcms.com", collectorPayloadBean.get(Collector.SITE_NAME));
Assert.assertEquals("en", collectorPayloadBean.get(Collector.LANGUAGE));
Assert.assertEquals(EventType.PAGE_REQUEST.getType(), collectorPayloadBean.get(Collector.EVENT_TYPE));
}
}
Loading

0 comments on commit dc55296

Please sign in to comment.