-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#29866 adding first draft for the viewtool
- Loading branch information
Showing
6 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
16 changes: 15 additions & 1 deletion
16
dotCMS/src/main/java/com/dotcms/analytics/content/ContentAnalyticsAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
package com.dotcms.analytics.content; | ||
|
||
import com.dotcms.analytics.query.AnalyticsQuery; | ||
import com.dotcms.cube.CubeJSQuery; | ||
import com.liferay.portal.model.User; | ||
|
||
/** | ||
* | ||
* This interface provides the methods to run reports on content analytics. | ||
* | ||
* @author Jose Castro | ||
* @since Sep 13th, 2024 | ||
*/ | ||
public interface ContentAnalyticsAPI { | ||
|
||
/** | ||
* Run a report based on an analytics query | ||
* @param query | ||
* @param user | ||
* @return ReportResponse | ||
*/ | ||
ReportResponse runReport(final AnalyticsQuery query, final User user); | ||
|
||
/** | ||
* Runs a raw report based on a cubeJS query | ||
* @param cubeJSQuery | ||
* @param user | ||
* @return ReportResponse | ||
*/ | ||
ReportResponse runRawReport(CubeJSQuery cubeJSQuery, User user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
dotCMS/src/main/java/com/dotcms/analytics/viewtool/AnalyticsTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package com.dotcms.analytics.viewtool; | ||
|
||
import com.dotcms.analytics.content.ContentAnalyticsAPI; | ||
import com.dotcms.analytics.content.ReportResponse; | ||
import com.dotcms.analytics.query.AnalyticsQuery; | ||
import com.dotcms.analytics.query.AnalyticsQueryParser; | ||
import com.dotcms.cdi.CDIUtils; | ||
import com.dotcms.cube.CubeJSQuery; | ||
import com.dotcms.rest.api.v1.DotObjectMapperProvider; | ||
import com.dotmarketing.business.web.WebAPILocator; | ||
import com.dotmarketing.exception.DotRuntimeException; | ||
import com.dotmarketing.util.Logger; | ||
import com.liferay.portal.model.User; | ||
import org.apache.velocity.tools.view.context.ViewContext; | ||
import org.apache.velocity.tools.view.tools.ViewTool; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
import java.util.Map; | ||
|
||
/** | ||
* This class is a ViewTool that can be used to access the analytics data. | ||
* @author jsanca | ||
*/ | ||
public class AnalyticsTool implements ViewTool { | ||
|
||
private final ContentAnalyticsAPI contentAnalyticsAPI; | ||
private final AnalyticsQueryParser analyticsQueryParser; | ||
|
||
private User user = null; | ||
|
||
public AnalyticsTool() { | ||
this(CDIUtils.getBean(ContentAnalyticsAPI.class).get(), | ||
CDIUtils.getBean(AnalyticsQueryParser.class).get()); | ||
} | ||
|
||
|
||
public AnalyticsTool(final ContentAnalyticsAPI contentAnalyticsAPI, | ||
final AnalyticsQueryParser analyticsQueryParser) { | ||
|
||
this.contentAnalyticsAPI = contentAnalyticsAPI; | ||
this.analyticsQueryParser = analyticsQueryParser; | ||
} | ||
|
||
@Override | ||
public void init(final Object initData) { | ||
|
||
if (initData instanceof ViewContext) { | ||
|
||
final HttpServletRequest request = ((ViewContext) initData).getRequest(); | ||
final HttpSession session = request.getSession(false); | ||
|
||
if (session != null) { | ||
try { | ||
user = WebAPILocator.getUserWebAPI().getLoggedInUser(request); | ||
} catch (DotRuntimeException e) { | ||
Logger.error(this.getClass(), e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Runs an analytics report based on the string json query. | ||
* example: | ||
* <code> | ||
* #set($query = "{ | ||
* "dimensions": ["Events.referer", "Events.experiment", "Events.variant", "Events.utcTime", "Events.url", "Events.lookBackWindow", "Events.eventType"], | ||
* "measures": ["Events.count", "Events.uniqueCount"], | ||
* "filters": "Events.variant = ['B'] or Events.experiments = ['B']", | ||
* "limit":100, | ||
* "offset":1, | ||
* "timeDimensions":"Events.day day", | ||
* "orders":"Events.day ASC" | ||
* }") | ||
* | ||
* $dotanalytics.runReportFromJson($query) | ||
* </code> | ||
* @param query | ||
* @return | ||
*/ | ||
public ReportResponse runReportFromJson(final String query) { | ||
|
||
Logger.debug(this, () -> "Running report from json: " + query); | ||
return contentAnalyticsAPI.runReport(this.analyticsQueryParser.parseJsonToQuery(query), user); | ||
} | ||
|
||
/** | ||
* Runs an analytics report based on Map query. | ||
* example: | ||
* <code> | ||
* #set ($myQuery = {}) | ||
* $myMap.put('dimensions', ["Events.referer", "Events.experiment", "Events.variant", "Events.utcTime", "Events.url", "Events.lookBackWindow", "Events.eventType"]) | ||
* $myMap.put('measures', ["Events.count", "Events.uniqueCount"]) | ||
* $myMap.put('filters', "Events.variant = ['B'] or Events.experiments = ['B']") | ||
* $myMap.put('limit', 100) | ||
* $myMap.put('offset', 1) | ||
* $myMap.put('timeDimensions', "Events.day day") | ||
* $myMap.put('orders', "Events.day ASC") | ||
* | ||
* $dotanalytics.runReportFromMap($myQuery) | ||
* </code> | ||
* @param query | ||
* @return | ||
*/ | ||
public ReportResponse runReportFromMap(final Map<String, Object> query) { | ||
|
||
Logger.debug(this, () -> "Running report from map: " + query); | ||
final AnalyticsQuery analyticsQuery = DotObjectMapperProvider.getInstance() | ||
.getDefaultObjectMapper().convertValue(query, AnalyticsQuery.class); | ||
return contentAnalyticsAPI.runReport(analyticsQuery, user); | ||
} | ||
|
||
/** | ||
* Runs an analytics report based cube js raw json string query | ||
* | ||
* example: | ||
* <code> | ||
* #set($query = "{ | ||
* | ||
* }") | ||
* | ||
* $dotanalytics.runRawReportFromJson($query) | ||
* </code> | ||
* @param query | ||
* @return | ||
*/ | ||
public ReportResponse runRawReportFromJson(final Map<String, Object> query) { | ||
|
||
Logger.debug(this, () -> "Running report from raw query: " + query); | ||
final CubeJSQuery cubeJSQuery = DotObjectMapperProvider.getInstance() | ||
.getDefaultObjectMapper().convertValue(query, CubeJSQuery.class); | ||
return contentAnalyticsAPI.runRawReport(cubeJSQuery, user); | ||
} | ||
|
||
/** | ||
* Runs an analytics report based on Map query. | ||
* example: | ||
* <code> | ||
* #set ($myQuery = {}) | ||
* | ||
* | ||
* $dotanalytics.runRawReportFromMap($myQuery) | ||
* </code> | ||
* @param query | ||
* @return | ||
*/ | ||
public ReportResponse runRawReportFromMap(final Map<String, Object> query) { | ||
|
||
Logger.debug(this, () -> "Running report from raw query map: " + query); | ||
final CubeJSQuery cubeJSQuery = DotObjectMapperProvider.getInstance() | ||
.getDefaultObjectMapper().convertValue(query, CubeJSQuery.class); | ||
return contentAnalyticsAPI.runRawReport(cubeJSQuery, user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters