Skip to content

Commit

Permalink
feat: Collect statistics when Rewards sent - MEED-2890 - Meeds-io/mee…
Browse files Browse the repository at this point in the history
…ds#1266 (#456)

This change will introduce a new listener to collect statistics when
rewards are sent for a past period.
  • Loading branch information
boubaker authored Nov 21, 2023
1 parent 8c0eb1b commit ca022c4
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 24 deletions.
1 change: 1 addition & 0 deletions translations.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2023 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 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<RewardReport, Object> {

@ExoTransactional
public void onEvent(Event<RewardReport, Object> 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<String, Double> 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<String, Double> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -35,31 +34,14 @@
*/
@Asynchronous
public class RewardSucceedNotificationListener extends Listener<RewardReport, Object> {
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<RewardReport, Object> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<set-method>addListener</set-method>
<type>org.exoplatform.wallet.reward.listener.RewardSucceedNotificationListener</type>
</component-plugin>
<component-plugin>
<name>exo.wallet.reward.report.success</name>
<set-method>addListener</set-method>
<type>org.exoplatform.wallet.reward.listener.RewardSucceedAnalyticsListener</type>
</component-plugin>
<component-plugin>
<name>exo.wallet.transaction.replaced</name>
<set-method>addListener</set-method>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@

<external-component-plugins>
<target-component>org.exoplatform.services.listener.ListenerService</target-component>
<component-plugin>
<name>exo.wallet.reward.report.success</name>
<set-method>addListener</set-method>
<type>org.exoplatform.wallet.reward.listener.RewardSucceedAnalyticsListener</type>
</component-plugin>
<component-plugin>
<name>exo.wallet.reward.report.success</name>
<set-method>addListener</set-method>
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ca022c4

Please sign in to comment.