Skip to content

Commit

Permalink
Implement #69
Browse files Browse the repository at this point in the history
  • Loading branch information
ylvion committed Apr 10, 2022
1 parent 2e862b5 commit a7a4bc6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -268,28 +269,64 @@ public void cleanCaches() {
}

/**
* Request all activities of the given LRS and save them in a cache
* Request all activities of the given LRS and save them in a cache.
* Has to request the activities' IDs and Types with separate queries because the query's result will be empty, if an activity type is not given for a single activity in the dataset
*
* @param connection {@link LrsConnection} to use
* @return {@link List} of IDs of LRS' activities
* @return {@link Map} with activity types as keys and IDs of the corresponding activities in a {@link List}
*/
@Cacheable(
cacheNames = "lrsActivities",
key = "#connection.connectionId"
)
public List<String> getActivitiesOfLrs(LrsConnection connection) {
DaveVis getActivities = this.prepareGetActivitiesOfLRS();
List<String> activities = this.daveConnectorLifecycleManager.getConnector(connection)
public Map<String, List<String>> getActivitiesOfLrs(LrsConnection connection) {
List<String> allActivities = getActivities(connection);
Map<String, List<String>> activitiesByType = getActivitiesByType(connection);
List<String> activitiesWithKnownType = activitiesByType.values().stream().flatMap(List::stream).toList();
List<String> activitiesWithUnknownType = allActivities.stream()
.filter((activity) -> !(activitiesWithKnownType.contains(activity)))
.toList();
if (!activitiesWithUnknownType.isEmpty()) {
activitiesByType.put("unknown", activitiesWithUnknownType);
}
return activitiesByType;
}

private List<String> getActivities(LrsConnection connection) {
DaveVis getActivities = this.prepareGetActivitiesOfLRS(false);
List<String> activityList = this.daveConnectorLifecycleManager.getConnector(connection)
.getAnalysisResult(this.fileManagementService.prepareQuery(getActivities, "all").getAbsolutePath(),
this.fileManagementService.prepareVisualisation(getActivities).getAbsolutePath());
return activities.stream()
return activityList.stream()
.map((s) -> s.replace("\n", ""))
.map((s) -> s.substring(1, s.length() - 1))
.map((s) -> s.split(" +")[1])
.map((s) -> s.replace("\"", ""))
.map((s) -> s.split(" +")[1])
.toList();
}

private Map<String, List<String>> getActivitiesByType(LrsConnection connection) {
DaveVis getActivitiesType = this.prepareGetActivitiesOfLRS(true);
List<String> activitiesWithType = this.daveConnectorLifecycleManager.getConnector(connection)
.getAnalysisResult(this.fileManagementService.prepareQuery(getActivitiesType, "all").getAbsolutePath(),
this.fileManagementService.prepareVisualisation(getActivitiesType).getAbsolutePath());
return activitiesWithType.stream()
.map((s) -> s.replace("\n", ""))
.map((s) -> s.substring(1, s.length() - 1))
.map((s) -> s.replace("\"", ""))
.map((s) -> s.split(" +"))
.map((sarr) -> Pair.of(sarr[1], sarr[2]))
.collect(
Collectors.groupingBy(
Pair::getSecond,
Collectors.collectingAndThen(
Collectors.toList(),
(list) -> list.stream().map(Pair::getFirst).toList()
)
)
);
}

/**
* Add an Analysis to an existing Dashboard description
*
Expand Down Expand Up @@ -398,11 +435,12 @@ public String getNameOfAnalysis(UUID analysisId) {
/**
* Helper function to create Analysis for requesting the Activities of an LRS.
* Must not be deleted or modified and is therefore not seeded like the predefined analyses
*
* @param withType indicates if Activities' type should also be requested
*/
public DaveVis prepareGetActivitiesOfLRS() {
return new DaveVis("Activities of LRS",
new DaveQuery("Activities of LRS", """
[:find (count ?s) ?c :where [?s :statement/object ?o][?o :activity/id ?c]]"""),
public DaveVis prepareGetActivitiesOfLRS(Boolean withType) {
DaveVis analysis = new DaveVis("Activities of LRS",
new DaveQuery("Activities of LRS", ""),
new DaveGraphDescription("Top 10",
"""
{
Expand Down Expand Up @@ -484,6 +522,14 @@ public DaveVis prepareGetActivitiesOfLRS() {
}
]
}"""), true);
if (withType) {
analysis.setQuery(new DaveQuery("Activities of LRS", """
[:find (count ?s) ?c ?t :where [?s :statement/object ?o][?o :activity/id ?c][?o :activity.definition/type ?t]]"""));
} else {
analysis.setQuery(new DaveQuery("Activities of LRS", """
[:find (count ?s) ?c :where [?s :statement/object ?o][?o :activity/id ?c]]"""));
}
return analysis;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.tudresden.inf.verdatas.xapitools.dave.dashboards.controllers;

import com.google.common.base.Supplier;
import de.tudresden.inf.verdatas.xapitools.dave.dashboards.DaveDashboardService;
import de.tudresden.inf.verdatas.xapitools.dave.persistence.DaveDashboard;
import de.tudresden.inf.verdatas.xapitools.dave.persistence.DaveVis;
import de.tudresden.inf.verdatas.xapitools.lrs.LrsConnection;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.core.annotation.Order;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -15,10 +17,7 @@
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -68,18 +67,37 @@ public ModelAndView showSelectAnalysis(@RequestParam(name = "flow") UUID dashboa
if (!cache.orElse(true)) this.daveDashboardService.cleanCaches();
DaveDashboard dashboard = this.daveDashboardService.getDashboard(dashboardId);
LrsConnection lrsConnection = dashboard.getLrsConnection();
List<String> activities = this.daveDashboardService.getActivitiesOfLrs(lrsConnection)
Map<String, List<String>> activitiesByType = this.daveDashboardService.getActivitiesOfLrs(lrsConnection)
.entrySet()
.stream()
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());

.map((entry) -> Map.entry(
Arrays.stream(entry.getKey().split("/"))
.reduce((acc, s) -> s)
.orElse(entry.getKey()),
entry.getValue()
.stream()
.sorted()
.collect(Collectors.toList())
))
.collect(
Collectors.groupingBy(
Map.Entry::getKey,
Collectors.collectingAndThen(Collectors.toList(),
(entries) -> entries.stream().map(Map.Entry::getValue).flatMap(List::stream).toList()
)
)
);

Map<String, String> activityToType = new HashMap<>();
activitiesByType.forEach((key, value) -> value.forEach((activity) -> activityToType.put(activity, key)));
ModelAndView mav = new ModelAndView("bootstrap/dave/dashboard/analysis");
mav.addObject("flow", dashboardId.toString());
mav.addObject("possibleActivities", activities);
mav.addObject("possibleActivities", activitiesByType);
mav.addObject("possibleAnalysis", this.daveDashboardService.getAllAnalysis(true)
.sorted(Comparator.comparing(DaveVis::getName))
.toList());
mav.addObject("dashboardVisualisations", this.daveDashboardService.getVisualisationsOfDashboard(dashboard));
mav.addObject("activityTypes", activityToType);
mav.addObject("mode", DaveDashboardMavController.Mode.CREATING);
return mav;
}
Expand Down Expand Up @@ -121,7 +139,6 @@ public RedirectView addVisualisationToDashboard(@RequestParam(name = "flow") UUI
UUID analysisIdentifier = analysis.getId();
this.daveDashboardService.addVisualisationToDashboard(dashboard, activityId, analysisIdentifier);


return new RedirectView(DaveDashboardMavController.Mode.CREATING.equals(mode) ? "../visualisations" : "../../edit/visualisations");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
<div class="mb-3 mt-3 input-group" id="componentSelectBase">
<select class="form-select" id="componentSelectActivity" name="activity" required>
<option selected value="all">Whole LRS</option>
<option th:each="activity: ${possibleActivities}"
th:text="${activity}"
th:value="${activity}"></option>
<optgroup th:each="entry: ${possibleActivities.entrySet()}" th:label="${entry.getKey()}">
<option th:each="activity: ${entry.getValue()}"
th:text="${activity}"
th:value="${activity}"></option>
</select>
<select class="form-select" name="analysis" required>
<option selected value="">Select analysis</option>
Expand Down Expand Up @@ -46,6 +47,7 @@ <h3 class="mb-3">Selected analyses</h3>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col">#</th>
<th scope="col">activityType</th>
<th scope="col">activity</th>
<th scope="col">analysis</th>
<th scope="col"></th>
Expand Down Expand Up @@ -74,6 +76,7 @@ <h3 class="mb-3">Selected analyses</h3>
</form>
</td>
<th scope="row" th:text="${stat.index + 1}"></th>
<td th:text="${activityTypes.get(visualisation.getFirst())}"></td>
<td th:text="${visualisation.getFirst()}"></td>
<td th:text="${visualisation.getSecond().getName()}"></td>
<td>
Expand Down

0 comments on commit a7a4bc6

Please sign in to comment.