Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Spaces Management - Meeds-io/MIPs#160 #245

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.meeds.analytics.model.filter.AnalyticsPeriod;
import io.meeds.analytics.model.filter.AnalyticsPeriodType;
import io.meeds.analytics.model.filter.AnalyticsTableFilter;
import io.meeds.analytics.model.filter.aggregation.AnalyticsAggregationType;

public interface AnalyticsService {

Expand Down Expand Up @@ -148,4 +149,49 @@ PercentageChartValue computePercentageChartData(AnalyticsFilter filter,
*/
void addUIWatcher(StatisticWatcher uiWatcher);

/**
* @param operations (Optional) {@link List} of collected operations to
* consider
* @param fieldName (Optional) Field Name to filter
* @param fieldValues (Optional) Field values to filter
* @param sortBy Sort field name
* @param sortDirection Sort direction: asc or desc
* @param limit limit of results to retrieve
* @return {@link List} of {@link StatisticData} representing the collected
* data
*/
List<StatisticData> getSamples(List<String> operations,
String fieldName,
List<String> fieldValues,
String sortBy,
String sortDirection,
int limit);

/**
* @param operations (Optional) {@link List} of collected operations to
* consider
* @param fieldName (Optional) Field Name to filter
* @param fieldValues (Optional) Field values to filter
* @param xAggregationField x axis field
* @param xAggregationType x axis aggregation type: MAX, MIN, SUM, COUNT,
* CARDINALITY (COUNT Distinct), TERMS, DATA or HISTOGRAM
* @param xAggregationSortDirection sort direction: asc or desc
* @param yAggregationField y axis field
* @param yAggregationType y axis aggregation type: MAX, MIN, SUM, COUNT,
* CARDINALITY (COUNT Distinct), TERMS, DATA or HISTOGRAM
* @param yAggregationSortDirection sort direction: asc or desc
* @param limit limit of x Axis results to retrieve
* @return ChartDataList
*/
ChartDataList getChart(List<String> operations, // NOSONAR
String fieldName,
List<String> fieldValues,
String xAggregationField,
AnalyticsAggregationType xAggregationType,
String xAggregationSortDirection,
String yAggregationField,
AnalyticsAggregationType yAggregationType,
String yAggregationSortDirection,
int limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public static void addSpaceStatistics(StatisticData statisticData, Space space)
return;
}
statisticData.setSpaceId(Long.parseLong(space.getId()));
statisticData.addParameter("spaceTemplateId", space.getTemplateId());
statisticData.addParameter("spaceVisibility", space.getVisibility());
statisticData.addParameter("spaceRegistration", space.getRegistration());
statisticData.addParameter("spaceCreatedTime", space.getCreatedTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public void likeComment(ActivityLifeCycleEvent event) {
}
}

@Override
public void pinActivity(ActivityLifeCycleEvent event) {
try {
StatisticData statisticData = addActivityStatisticEvent(event, "pinActivity");
statisticData.setUserId(Long.parseLong(event.getUserId()));
addStatisticData(statisticData);
} catch (Exception e) {
handleErrorProcessingOperation(event, e);
}
}

private void handleErrorProcessingOperation(ActivityLifeCycleEvent event, Exception exception) {
LOG.warn("Error adding Statistic data for activity {} with event {}", event.getActivityId(), event.getType(), exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -87,6 +88,7 @@
import io.meeds.analytics.model.filter.aggregation.AnalyticsAggregationType;
import io.meeds.analytics.model.filter.aggregation.AnalyticsPercentageLimit;
import io.meeds.analytics.model.filter.search.AnalyticsFieldFilter;
import io.meeds.analytics.utils.AnalyticsUtils;
import io.meeds.common.ContainerTransactional;

import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -455,6 +457,65 @@ public void addUIWatcher(StatisticWatcher uiWatcher) {
uiWatchers.add(uiWatcher);
}

@Override
public List<StatisticData> getSamples(List<String> operations,
String fieldName,
List<String> fieldValues,
String sortBy,
String sortDirection,
int limit) {
AnalyticsFilter searchFilter = new AnalyticsFilter();
ArrayList<AnalyticsFieldFilter> filters = new ArrayList<>();
searchFilter.setFilters(filters);
if (CollectionUtils.isNotEmpty(operations)) {
searchFilter.addInSetFilter(AnalyticsUtils.FIELD_OPERATION, operations.toArray(new String[operations.size()]));
}
if (StringUtils.isNotEmpty(fieldName) && CollectionUtils.isNotEmpty(fieldValues)) {
searchFilter.addInSetFilter(fieldName, fieldValues.toArray(new String[fieldValues.size()]));
}
searchFilter.addXAxisAggregation(new AnalyticsAggregation(AnalyticsAggregationType.TERMS,
sortBy,
sortDirection,
null,
limit));
searchFilter.setLimit(limit);
return retrieveData(searchFilter);
}

@Override
public ChartDataList getChart(List<String> operations,
String fieldName,
List<String> fieldValues,
String xAggregationField,
AnalyticsAggregationType xAggregationType,
String xAggregationSortDirection,
String yAggregationField,
AnalyticsAggregationType yAggregationType,
String yAggregationSortDirection,
int limit) {
AnalyticsFilter searchFilter;
searchFilter = new AnalyticsFilter();
searchFilter.setFilters(new ArrayList<>());
if (CollectionUtils.isNotEmpty(operations)) {
searchFilter.addInSetFilter(AnalyticsUtils.FIELD_OPERATION, operations.toArray(new String[operations.size()]));
}
if (StringUtils.isNotEmpty(fieldName) && CollectionUtils.isNotEmpty(fieldValues)) {
searchFilter.addInSetFilter(fieldName, fieldValues.toArray(new String[fieldValues.size()]));
}
searchFilter.addXAxisAggregation(new AnalyticsAggregation(xAggregationType,
xAggregationField,
xAggregationSortDirection,
null,
limit));
searchFilter.setYAxisAggregation(new AnalyticsAggregation(yAggregationType,
yAggregationField,
yAggregationSortDirection,
null,
1));
searchFilter.setHideLabel(true);
return computeChartData(searchFilter);
}

private List<StatisticFieldValue> buildFieldValuesResponse(String jsonResponse) throws JSONException {
JSONObject json = new JSONObject(jsonResponse);
JSONObject aggregations = json.has(AGGREGATIONS_RESPONSE_BODY) ? json.getJSONObject(AGGREGATIONS_RESPONSE_BODY) : null;
Expand Down Expand Up @@ -789,9 +850,13 @@ private void appendSortQuery(StringBuilder esQuery,
AnalyticsAggregationType aggregationType) {
if (aggregationType.isUseSort()) {
String sortField = null;
String sortDirection = aggregation.getSortDirection();
if ((i + 1) < aggregationsSize) {
AnalyticsAggregation nextAggregation = aggregations.get(i + 1);
sortField = getSortField(nextAggregation);
if (nextAggregation != null) {
sortDirection = nextAggregation.getSortDirection();
}
} else if (aggregationType == AnalyticsAggregationType.TERMS) {
sortField = "_count";
}
Expand All @@ -800,7 +865,7 @@ private void appendSortQuery(StringBuilder esQuery,
,
"order": {"$sortField": "$sortDirection"}
""".replace(SORT_FIELD_REQUEST_BODY_PARAM, sortField)
.replace(SORT_DIRECTION_REQUEST_BODY_PARAM, aggregation.getSortDirection()));
.replace(SORT_DIRECTION_REQUEST_BODY_PARAM, sortDirection));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 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.rest;

import java.util.Collections;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.meeds.analytics.api.service.AnalyticsService;
import io.meeds.analytics.model.chart.ChartAggregationResult;
import io.meeds.analytics.model.chart.ChartDataList;
import io.meeds.analytics.model.filter.aggregation.AnalyticsAggregationType;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("chart")
@Tag(name = "/analytics/rest/chart", description = "Retrieve Analytics Chart Data")
public class AnalyticsChartRest {

@Autowired
private AnalyticsService analyticsService;

@GetMapping
@Secured("analytics")
@Operation(summary = "Retrieve Analytics two dimensions Chat Data",
method = "GET",
description = "This will compute a two dimensions matrix of data useful for chart display")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
})
public List<ChartAggregationResult> getChart(
@Parameter(description = "Collected Statistic Operations to consider in query",
required = false)
@RequestParam(name = "operation",
required = false)
List<String> operations,
@Parameter(description = "Filtered field name",
required = false)
@RequestParam(name = "fieldName",
required = false)
String fieldName,
@Parameter(description = "Filtered field values",
required = false)
@RequestParam(name = "fieldValue",
required = false)
List<String> fieldValues,
@Parameter(description = "X Axis aggregation field",
required = true)
@RequestParam("xAggregationField")
String xAggregationField,
@Parameter(description = "X Axis aggregation type: MAX, MIN, SUM, COUNT, CARDINALITY (COUNT Distinct), TERMS, DATA or HISTOGRAM",
required = true)
@RequestParam("xAggregationType")
AnalyticsAggregationType xAggregationType,
@Parameter(description = "X Axis sort direction: asc or desc",
required = false)
@RequestParam(name = "xAggregationSortDirection",
required = false)
String xAggregationSortDirection,
@Parameter(description = "Y Axis aggregation field",
required = true)
@RequestParam("yAggregationField")
String yAggregationField,
@Parameter(description = "Y Axis aggregation type: MAX, MIN, SUM, COUNT, CARDINALITY (COUNT Distinct), TERMS, DATA or HISTOGRAM",
required = true)
@RequestParam("yAggregationType")
AnalyticsAggregationType yAggregationType,
@Parameter(description = "Y Axis sort direction: asc or desc",
required = true)
@RequestParam(name = "yAggregationSortDirection",
required = false)
String yAggregationSortDirection,
@Parameter(description = "Limit of x Axis results to retrieve",
required = true)
@RequestParam("limit")
int limit) {
ChartDataList chartData = analyticsService.getChart(operations,
fieldName,
fieldValues,
xAggregationField,
xAggregationType,
xAggregationSortDirection,
yAggregationField,
yAggregationType,
yAggregationSortDirection,
limit);
if (chartData != null && CollectionUtils.size(chartData.getCharts()) == 1) {
return chartData.getCharts().getFirst().getAggregationResults();
} else {
return Collections.emptyList();
}
}

}
Loading