From c84b4dbf260b07be705b65a891d4acf4efe5ef1a Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Tue, 21 Nov 2023 06:55:05 +0100 Subject: [PATCH] feat: Collect statistics when Rewards sent - MEED-2890 - Meeds-io/meeds#1266 This change will introduce a new listener to collect statistics when rewards are sent for a past period. --- translations.properties | 1 + .../RewardSucceedAnalyticsListener.java | 87 +++++++++++++++++++ .../RewardSucceedNotificationListener.java | 30 ++----- .../portal/wallet-reward-configuration.xml | 5 ++ .../conf/test-wallet-reward-configuration.xml | 5 ++ .../locale/portlet/Analytics_en.properties | 20 +++++ 6 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedAnalyticsListener.java create mode 100644 wallet-webapps/src/main/resources/locale/portlet/Analytics_en.properties diff --git a/translations.properties b/translations.properties index da338d5e4..fad72a4bc 100644 --- a/translations.properties +++ b/translations.properties @@ -26,3 +26,4 @@ Portlets.properties=wallet-services/src/main/resources/locale/portlet/Portlets_e webui_en.properties=wallet-webapps/src/main/resources/locale/navigation/portal/global_en.properties RewardingGroupNavigation.properties=wallet-webapps/src/main/resources/locale/navigation/group/platform/rewarding_en.properties NotificationAdministration.properties=wallet-webapps/src/main/resources/locale/portlet/NotificationAdministration_en.properties +Analytics.properties=wallet-webapps/src/main/resources/locale/portlet/Analytics_en.properties diff --git a/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedAnalyticsListener.java b/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedAnalyticsListener.java new file mode 100644 index 000000000..e86fe5c8b --- /dev/null +++ b/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedAnalyticsListener.java @@ -0,0 +1,87 @@ +/* + * This file is part of the Meeds project (https://meeds.io/). + * Copyright (C) 2020 Meeds Association + * contact@meeds.io + * 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 org.exoplatform.wallet.reward.listener; + +import java.time.Instant; +import java.util.Date; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.apache.commons.lang.StringUtils; + +import org.exoplatform.analytics.model.StatisticData; +import org.exoplatform.analytics.utils.AnalyticsUtils; +import org.exoplatform.commons.api.persistence.ExoTransactional; +import org.exoplatform.services.listener.Asynchronous; +import org.exoplatform.services.listener.Event; +import org.exoplatform.services.listener.Listener; +import org.exoplatform.wallet.model.reward.RewardReport; +import org.exoplatform.wallet.model.reward.WalletPluginReward; +import org.exoplatform.wallet.model.reward.WalletReward; + +/** + * A listener that is triggered when rewards has been successfully sent. This + * will collect reward statistics. + */ +@Asynchronous +public class RewardSucceedAnalyticsListener extends Listener { + + @ExoTransactional + public void onEvent(Event event) throws Exception { + RewardReport rewardReport = event.getSource(); + StatisticData statisticData = new StatisticData(); + statisticData.setModule("wallet"); + statisticData.setSubModule("reward"); + statisticData.setOperation("sendPeriodRewards"); + statisticData.addParameter("rewardPeriodStartDate", + Date.from(Instant.ofEpochSecond(rewardReport.getPeriod().getStartDateInSeconds()))); + statisticData.addParameter("rewardPeriodEndDate", + Date.from(Instant.ofEpochSecond(rewardReport.getPeriod().getEndDateInSeconds()))); + statisticData.addParameter("rewardPeriodTimeZone", rewardReport.getPeriod().getTimeZone()); + statisticData.addParameter("rewardPeriodType", rewardReport.getPeriod().getRewardPeriodType().name().toLowerCase()); + statisticData.addParameter("rewardTransactionsCount", rewardReport.getSuccessTransactionCount()); + statisticData.addParameter("rewardTokensSent", rewardReport.getTokensSent()); + statisticData.addParameter("rewardTokensToSend", rewardReport.getTokensToSend()); + statisticData.addParameter("rewardRecipientWalletCount", rewardReport.getValidRewardCount()); + statisticData.addParameter("rewardParticipantWalletCount", rewardReport.getRewards().size()); + + Map rewardPluginPoints = rewardReport.getValidRewards() + .stream() + .map(WalletReward::getRewards) + .flatMap(Set::stream) + .collect(Collectors.toMap(WalletPluginReward::getPluginId, + WalletPluginReward::getPoints, + Double::sum)); + rewardPluginPoints.forEach((pId, + points) -> statisticData.addParameter("rewardPlugin" + StringUtils.capitalize(pId) + "Points", + points)); + Map rewardPluginAmounts = rewardReport.getValidRewards() + .stream() + .map(WalletReward::getRewards) + .flatMap(Set::stream) + .collect(Collectors.toMap(WalletPluginReward::getPluginId, + WalletPluginReward::getAmount, + Double::sum)); + rewardPluginAmounts.forEach((pId, + amount) -> statisticData.addParameter("rewardPlugin" + StringUtils.capitalize(pId) + "Amount", + amount)); + + AnalyticsUtils.addStatisticData(statisticData); + } + +} diff --git a/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedNotificationListener.java b/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedNotificationListener.java index 021974f03..c4792f67b 100644 --- a/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedNotificationListener.java +++ b/wallet-reward-services/src/main/java/org/exoplatform/wallet/reward/listener/RewardSucceedNotificationListener.java @@ -21,12 +21,11 @@ import org.exoplatform.commons.api.notification.NotificationContext; import org.exoplatform.commons.api.notification.model.PluginKey; +import org.exoplatform.commons.api.persistence.ExoTransactional; import org.exoplatform.commons.notification.impl.NotificationContextImpl; -import org.exoplatform.container.*; -import org.exoplatform.container.component.RequestLifeCycle; -import org.exoplatform.services.listener.*; -import org.exoplatform.services.log.ExoLogger; -import org.exoplatform.services.log.Log; +import org.exoplatform.services.listener.Asynchronous; +import org.exoplatform.services.listener.Event; +import org.exoplatform.services.listener.Listener; import org.exoplatform.wallet.model.reward.RewardReport; /** @@ -35,31 +34,14 @@ */ @Asynchronous public class RewardSucceedNotificationListener extends Listener { - private static final Log LOG = ExoLogger.getLogger(RewardSucceedNotificationListener.class); - - private ExoContainer container; - - public RewardSucceedNotificationListener(PortalContainer container) { - this.container = container; - } @Override + @ExoTransactional public void onEvent(Event event) throws Exception { RewardReport rewardReport = event.getSource(); - ExoContainerContext.setCurrentContainer(container); - RequestLifeCycle.begin(container); - try { - sendNotification(rewardReport); - } catch (Exception e) { - LOG.error("Error processing transaction notification {}", event.getData(), e); - } finally { - RequestLifeCycle.end(); - } - } - - private void sendNotification(RewardReport rewardReport) { NotificationContext ctx = NotificationContextImpl.cloneInstance(); ctx.append(REWARD_REPORT_NOTIFICATION_PARAM, rewardReport); ctx.getNotificationExecutor().with(ctx.makeCommand(PluginKey.key(REWARD_SUCCESS_NOTIFICATION_ID))).execute(ctx); } + } diff --git a/wallet-reward-services/src/main/resources/conf/portal/wallet-reward-configuration.xml b/wallet-reward-services/src/main/resources/conf/portal/wallet-reward-configuration.xml index fbc6d8669..8f2774dbd 100644 --- a/wallet-reward-services/src/main/resources/conf/portal/wallet-reward-configuration.xml +++ b/wallet-reward-services/src/main/resources/conf/portal/wallet-reward-configuration.xml @@ -130,6 +130,11 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. addListener org.exoplatform.wallet.reward.listener.RewardSucceedNotificationListener + + exo.wallet.reward.report.success + addListener + org.exoplatform.wallet.reward.listener.RewardSucceedAnalyticsListener + exo.wallet.transaction.replaced addListener diff --git a/wallet-reward-services/src/test/resources/conf/test-wallet-reward-configuration.xml b/wallet-reward-services/src/test/resources/conf/test-wallet-reward-configuration.xml index 03455641a..a510d4283 100644 --- a/wallet-reward-services/src/test/resources/conf/test-wallet-reward-configuration.xml +++ b/wallet-reward-services/src/test/resources/conf/test-wallet-reward-configuration.xml @@ -120,6 +120,11 @@ org.exoplatform.services.listener.ListenerService + + exo.wallet.reward.report.success + addListener + org.exoplatform.wallet.reward.listener.RewardSucceedAnalyticsListener + exo.wallet.reward.report.success addListener diff --git a/wallet-webapps/src/main/resources/locale/portlet/Analytics_en.properties b/wallet-webapps/src/main/resources/locale/portlet/Analytics_en.properties new file mode 100644 index 000000000..71b55caf6 --- /dev/null +++ b/wallet-webapps/src/main/resources/locale/portlet/Analytics_en.properties @@ -0,0 +1,20 @@ +analytics.field.label.rewardPeriodStartDate=Reward period start date +analytics.field.label.rewardPeriodEndDate=Reward period end date +analytics.field.label.rewardPeriodTimeZone=Reward period time zone +analytics.field.label.rewardPeriodType=Reward period type +analytics.field.label.rewardTransactionsCount=Reward transactions count +analytics.field.label.rewardTokensSent=Reward tokens sent +analytics.field.label.rewardTokensToSend=Reward tokens to sent +analytics.field.label.rewardRecipientWalletCount=Reward recipients count +analytics.field.label.rewardParticipantWalletCount=Reward participants count +analytics.field.label.rewardPluginGamificationPoints=Gamification reward: total points +analytics.field.label.rewardPluginGamificationAmount=Gamification reward: tokens sent + +analytics.wallet=Wallet +analytics.reward=Reward +analytics.sendPeriodRewards=Period rewards sent +analytics.week=Week +analytics.month=Month +analytics.quarter=Quarter +analytics.semester=Semester +analytics.year=Year