Skip to content

Commit

Permalink
feat: Add Category Analytics Fields - MEED-8054 - Meeds-io/meeds#2727 (
Browse files Browse the repository at this point in the history
…#258)

This change will add some event listeners to handle category management
and objects link to categories, such as spaces. In addition, this change
will implement some UI extensions to well display the category names in
Analytics Table and Analytics Charts.
  • Loading branch information
boubaker authored Jan 10, 2025
1 parent a6b2fd7 commit bb92a66
Show file tree
Hide file tree
Showing 18 changed files with 590 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import org.exoplatform.commons.api.persistence.ExoTransactional;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.security.ConversationState;
Expand All @@ -68,6 +69,9 @@
import io.meeds.analytics.api.service.StatisticDataQueueService;
import io.meeds.analytics.model.StatisticData;
import io.meeds.analytics.model.StatisticData.StatisticStatus;
import io.meeds.social.category.model.Category;
import io.meeds.social.category.model.CategoryObject;
import io.meeds.social.category.service.CategoryService;

public class AnalyticsUtils {
private static final Log LOG = ExoLogger.getLogger(AnalyticsUtils.class);
Expand Down Expand Up @@ -237,7 +241,7 @@ public static final String compueLabel(String chartKey, String chartValue) {
if (identityManager == null) {
return defaultLabel;
} else {
Identity identity = identityManager.getIdentity(chartValue);
Identity identity = getIdentity(chartValue);
if (identity == null) {
return defaultLabel;
} else {
Expand Down Expand Up @@ -334,6 +338,12 @@ public static final void addStatisticData(StatisticData statisticData) {
if (statisticData.getStatus() == null) {
statisticData.setStatus(StatisticStatus.OK);
}
if (statisticData.getUserId() == 0
&& ConversationState.getCurrent() != null
&& ConversationState.getCurrent().getIdentity() != null) {
statisticData.setUserId(getIdentityId(ActivityStream.ORGANIZATION_PROVIDER_ID,
ConversationState.getCurrent().getIdentity().getUserId()));
}

try {
StatisticDataQueueService analyticsQueueService = CommonsUtils.getService(StatisticDataQueueService.class);
Expand All @@ -357,12 +367,13 @@ public static Space getSpaceById(String spaceId) {
return spaceService.getSpaceById(spaceId);
}

public static Identity getIdentity(String identityId) {
if (StringUtils.isBlank(identityId)) {
public static Identity getIdentity(String identityIdString) {
if (StringUtils.isBlank(identityIdString)) {
return null;
}
IdentityManager identityManager = CommonsUtils.getService(IdentityManager.class);
return identityManager.getIdentity(identityId);
long identityId = parseId(identityIdString);
return identityId > 0 ? identityManager.getIdentity(identityId) : null;
}

public static long getIdentityId(String identityId) {
Expand Down Expand Up @@ -423,6 +434,7 @@ public static void addSpaceStatistics(StatisticData statisticData, Space space)
}
statisticData.setSpaceId(Long.parseLong(space.getId()));
statisticData.addParameter("spaceTemplateId", space.getTemplateId());
statisticData.addParameter("spaceCategoryIds", space.getCategoryIds());
statisticData.addParameter("spaceVisibility", space.getVisibility());
statisticData.addParameter("spaceRegistration", space.getRegistration());
statisticData.addParameter("spaceCreatedTime", space.getCreatedTime());
Expand Down Expand Up @@ -454,6 +466,37 @@ public static void addActivityStatisticsData(StatisticData statisticData, ExoSoc
}
}

public static void addCategoryStatistics(StatisticData statisticData, long categoryId) {
CategoryService categoryService = ExoContainerContext.getService(CategoryService.class);
addCategoryStatistics(statisticData, categoryService.getCategory(categoryId));
}

public static void addCategoryStatistics(StatisticData statisticData, Category category) {
if (category == null) {
return;
}
statisticData.addParameter("categoryId", category.getId());
statisticData.addParameter("categoryIcon", category.getIcon());
statisticData.addParameter("categoryAccessPermissionIds", category.getAccessPermissionIds());
statisticData.addParameter("categoryLinkPermissionIds", category.getLinkPermissionIds());
statisticData.addParameter("categoryCreatorId", category.getCreatorId());
statisticData.addParameter("categoryOwnerId", category.getOwnerId());
statisticData.addParameter("categoryParentId", category.getParentId());
}

public static void addCategoryLinkStatistics(StatisticData statisticData, CategoryObject categoryObject) {
statisticData.addParameter("categoryObjectType", categoryObject.getType());
statisticData.addParameter("categoryObjectId", categoryObject.getId());
statisticData.addParameter("categoryObjectParentId", categoryObject.getParentId());
if (categoryObject.getSpaceId() > 0) {
SpaceService spaceService = CommonsUtils.getService(SpaceService.class);
Space space = spaceService.getSpaceById(categoryObject.getSpaceId());
if (space != null) {
addSpaceStatistics(statisticData, space);
}
}
}

private static int getSize(String[] array) {
return array == null ? 0 : (int) Arrays.stream(array).filter(Objects::nonNull).distinct().count();
}
Expand All @@ -472,4 +515,13 @@ private static String getActivityType(ExoSocialActivity activity) {
}
return type;
}

public static long parseId(String id) {
try {
return Long.parseLong(id);
} catch (NumberFormatException ex) {
return 0;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import java.util.Date;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.commons.lang3.StringUtils;

import org.exoplatform.commons.api.persistence.ExoTransactional;
import org.exoplatform.services.listener.Asynchronous;
Expand All @@ -36,7 +36,6 @@
import org.exoplatform.social.core.activity.model.ExoSocialActivityImpl;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.core.manager.ActivityManager;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.metadata.tag.model.TagName;
import org.exoplatform.social.metadata.tag.model.TagObject;

Expand All @@ -54,9 +53,6 @@ public class AnalyticsActivityTagsListener extends Listener<TagObject, Set<TagNa
@Autowired
private ActivityManager activityManager;

@Autowired
private IdentityManager identityManager;

@Autowired
private ListenerService listenerService;

Expand Down Expand Up @@ -103,6 +99,7 @@ private void addEventStatistic(Set<TagName> tagNames, String activityId) {

private String getActivityPoster(ExoSocialActivity activity) {
Validate.notNull(activity.getUserId(), "activity.getUserId() must not be null!");
return identityManager.getIdentity(activity.getUserId()).getRemoteId();
Identity identity = AnalyticsUtils.getIdentity(activity.getUserId());
return identity == null ? null : identity.getRemoteId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2025 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.analytics.listener.social;

import static io.meeds.social.category.service.CategoryService.EVENT_SOCIAL_CATEGORY_ITEM_LINKED;
import static io.meeds.social.category.service.CategoryService.EVENT_SOCIAL_CATEGORY_ITEM_UNLINKED;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.listener.ListenerService;

import io.meeds.analytics.model.StatisticData;
import io.meeds.analytics.utils.AnalyticsUtils;
import io.meeds.social.category.model.CategoryObject;

import jakarta.annotation.PostConstruct;

@Component
public class AnalyticsCategoryLinkListener extends Listener<CategoryObject, Long> {

@Autowired
private ListenerService listenerService;

@PostConstruct
public void init() {
listenerService.addListener(EVENT_SOCIAL_CATEGORY_ITEM_LINKED, this);
listenerService.addListener(EVENT_SOCIAL_CATEGORY_ITEM_UNLINKED, this);
}

@Override
public void onEvent(Event<CategoryObject, Long> event) throws Exception {
StatisticData statisticData = new StatisticData();
statisticData.setTimestamp(new Date().getTime());
statisticData.setModule("social");
statisticData.setSubModule("category");
switch (event.getEventName()) {
case EVENT_SOCIAL_CATEGORY_ITEM_LINKED:
statisticData.setOperation("categoryObjectLinked");
break;
case EVENT_SOCIAL_CATEGORY_ITEM_UNLINKED:
statisticData.setOperation("categoryObjectUnlinked");
break;
default:
break;
}
AnalyticsUtils.addCategoryStatistics(statisticData, event.getData());
AnalyticsUtils.addCategoryLinkStatistics(statisticData, event.getSource());
AnalyticsUtils.addStatisticData(statisticData);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2025 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.analytics.listener.social;

import static io.meeds.social.category.service.CategoryService.EVENT_SOCIAL_CATEGORY_CREATED;
import static io.meeds.social.category.service.CategoryService.EVENT_SOCIAL_CATEGORY_DELETED;
import static io.meeds.social.category.service.CategoryService.EVENT_SOCIAL_CATEGORY_UPDATED;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.listener.ListenerService;

import io.meeds.analytics.model.StatisticData;
import io.meeds.analytics.utils.AnalyticsUtils;
import io.meeds.social.category.model.Category;

import jakarta.annotation.PostConstruct;

@Component
public class AnalyticsCategoryListener extends Listener<Category, Long> {

@Autowired
private ListenerService listenerService;

@PostConstruct
public void init() {
listenerService.addListener(EVENT_SOCIAL_CATEGORY_CREATED, this);
listenerService.addListener(EVENT_SOCIAL_CATEGORY_UPDATED, this);
listenerService.addListener(EVENT_SOCIAL_CATEGORY_DELETED, this);
}

@Override
public void onEvent(Event<Category, Long> event) throws Exception {
StatisticData statisticData = new StatisticData();
statisticData.setTimestamp(new Date().getTime());
statisticData.setModule("social");
statisticData.setSubModule("category");
switch (event.getEventName()) {
case EVENT_SOCIAL_CATEGORY_CREATED:
statisticData.setOperation("categoryCreated");
break;
case EVENT_SOCIAL_CATEGORY_UPDATED:
statisticData.setOperation("categoryUpdated");
break;
case EVENT_SOCIAL_CATEGORY_DELETED:
statisticData.setOperation("categoryDeleted");
break;
default:
break;
}
AnalyticsUtils.addCategoryStatistics(statisticData, event.getSource());
AnalyticsUtils.addStatisticData(statisticData);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,20 @@ analytics.jsonSettings.drawer.title=Edit JSON Settings
analytics.jsonSettings.edit.button=Edit JSON
analytics.cancel=Cancel
analytics.spaceTemplateId=Space Template Identifier
analytics.field.label.category=Category
analytics.categoryCreated=Create category
analytics.categoryUpdated=Update category
analytics.categoryDeleted=Delete category
analytics.categoryObjectLinked=Link a category to an object
analytics.categoryObjectUnlinked=Delete a category from an object
analytics.field.label.categoryId=Category
analytics.field.label.categoryAccessPermissionIds=Category access permission identity ids
analytics.field.label.categoryCreatorId=Category creator identity id
analytics.field.label.categoryIcon=Category icon
analytics.field.label.categoryLinkPermissionIds=Category link permission identity ids
analytics.field.label.categoryObjectId=Category: Object Type
analytics.field.label.categoryObjectType=Category: Object Identifier
analytics.field.label.categoryOwnerId=Category Owner Identity Id
analytics.field.label.categoryParentId=Parent Category
analytics.field.label.spaceCategoryIds=Space Categories
analytics.deletedCategory=Deleted category
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@
<depends>
<module>extensionRegistry</module>
</depends>
<depends>
<module>categoryVueComponents</module>
</depends>
<depends>
<module>commonVueComponents</module>
</depends>
<depends>
<module>jquery</module>
<as>$</as>
Expand Down
Loading

0 comments on commit bb92a66

Please sign in to comment.