getting aggregations uising the correct dsm api method

This commit is contained in:
Antonis Lempesis 2022-05-24 14:01:08 +00:00
parent 94a8b1abf9
commit a61b259ca3
3 changed files with 44 additions and 78 deletions

View File

@ -43,6 +43,8 @@ public interface RepositoryService {
Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException; Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException;
List<AggregationDetails> getRepositoryAggregations(String id) throws JSONException;
List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException; List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException;
Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException; Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException;

View File

@ -105,12 +105,6 @@ public class RepositoryServiceImpl implements RepositoryService {
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
} }
private String getAuthenticatedUserEmail() {
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
return authenticationToken.getUserInfo().getEmail();
}
@PostConstruct @PostConstruct
private void init() { private void init() {
LOGGER.debug("Initialization method of repository api!"); LOGGER.debug("Initialization method of repository api!");
@ -130,8 +124,6 @@ public class RepositoryServiceImpl implements RepositoryService {
dataSourceClass.get("opendoar").add(key); dataSourceClass.get("opendoar").add(key);
} else if (key.contains("datarepository")) { } else if (key.contains("datarepository")) {
dataSourceClass.putIfAbsent("re3data", Collections.singletonList("datarepository::unknown")); dataSourceClass.putIfAbsent("re3data", Collections.singletonList("datarepository::unknown"));
} else if (key.contains("pubsrepository::journal")) {
dataSourceClass.putIfAbsent("journal", Collections.singletonList("pubsrepository::journal"));
} }
} }
@ -177,7 +169,7 @@ public class RepositoryServiceImpl implements RepositoryService {
@Override @Override
public List<Repository> getRepositories(List<String> ids, int page, int size) throws JSONException { public List<Repository> getRepositories(List<String> ids, int page, int size) throws JSONException {
List<Repository> repos = new ArrayList<>(); List<Repository> repos = new ArrayList<>();
LOGGER.debug("Retreiving repositories with ids : " + String.join(", ", ids)); LOGGER.debug("Retrieving repositories with ids : " + String.join(", ", ids));
UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size)));
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
@ -331,29 +323,17 @@ LOGGER.debug("json: " + jsonArray);
return r; return r;
} }
private Collection<Repository> getRepositoriesByMode(String mode, List<Repository> rs) {
List<Repository> reps = new ArrayList<>();
for (Repository r : rs) {
if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode))
reps.add(r);
}
return reps;
}
@Override @Override
public List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException { public List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException {
String userEmail = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(); String userEmail = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail();
LOGGER.debug("Retreiving repositories of authenticated user : " + userEmail); LOGGER.debug("Retrieving repositories of authenticated user : " + userEmail);
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles()); Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles());
return getRepositories(new ArrayList<>(repoIds)); return getRepositories(new ArrayList<>(repoIds));
} }
@Override @Override
public List<Repository> getRepositoriesOfUser(String userEmail, String page, String size) throws JSONException { public List<Repository> getRepositoriesOfUser(String userEmail, String page, String size) throws JSONException {
LOGGER.debug("Retreiving repositories of authenticated user : " + userEmail); LOGGER.debug("Retrieving repositories of authenticated user : " + userEmail);
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail)); Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail));
return getRepositories(new ArrayList<>(repoIds)); return getRepositories(new ArrayList<>(repoIds));
} }
@ -373,7 +353,7 @@ LOGGER.debug("json: " + jsonArray);
@Override @Override
public RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException { public RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException {
LOGGER.debug("Retreiving repositories with id : " + id); LOGGER.debug("Retrieving repositories with id : " + id);
RepositorySnippet repo = null; RepositorySnippet repo = null;
UriComponents uriComponents = searchSnipperDatasource("0", "100"); UriComponents uriComponents = searchSnipperDatasource("0", "100");
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
@ -392,7 +372,7 @@ LOGGER.debug("json: " + jsonArray);
@Override @Override
public Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException { public Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException {
LOGGER.debug("Retreiving repositories with id : " + id); LOGGER.debug("Retrieving repositories with id : " + id);
Repository repo = null; Repository repo = null;
UriComponents uriComponents = searchDatasource("0", "100"); UriComponents uriComponents = searchDatasource("0", "100");
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
@ -409,60 +389,41 @@ LOGGER.debug("json: " + jsonArray);
} }
@Override
public List<AggregationDetails> getRepositoryAggregations(String id) throws JSONException {
LOGGER.debug("Retrieving aggregations for repository with id : " + id);
UriComponents uriComponents = getAggregationHistory(id);
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
JSONArray aggregationInfo = new JSONObject(rs).getJSONArray("aggregationInfo");
List<AggregationDetails> aggregationHistory = new ArrayList<>(Converter.getAggregationHistoryFromJson(aggregationInfo));
return aggregationHistory;
// return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream()
// .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
// .collect(Collectors.toList());
}
@Override @Override
public List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException { public List<AggregationDetails> getRepositoryAggregations(String id, int from, int size) throws JSONException {
LOGGER.debug("Retreiving aggregations for repository with id : " + id); return getRepositoryAggregations(id).subList(from, from + size);
UriComponents uriComponents = searchDatasource(from + "", size + "");
RequestFilter requestFilter = new RequestFilter();
requestFilter.setId(id);
List<AggregationDetails> aggregationHistory = new ArrayList<>();
long start = System.currentTimeMillis();
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
long end = System.currentTimeMillis();
System.out.println("Aggregations request through rest template took " + (end - start) + "ms");
JSONObject repository = new JSONObject(rs);
if (repository.getJSONArray("datasourceInfo").length() == 0)
return aggregationHistory;
start = System.currentTimeMillis();
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
end = System.currentTimeMillis();
System.out.println("Getting aggregations history from json " + (end - start) + "ms");
return aggregationHistory.size() == 0 ? aggregationHistory : aggregationHistory.stream()
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
.limit(size)
.collect(Collectors.toList());
} }
@Override @Override
public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException { public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(String id) throws JSONException {
LOGGER.debug("Retreiving aggregations (by year) for repository with id : " + id); LOGGER.debug("Retrieving aggregations (by year) for repository with id : " + id);
UriComponents uriComponents = searchDatasource("0", "100");
RequestFilter requestFilter = new RequestFilter();
requestFilter.setId(id);
List<AggregationDetails> aggregationHistory = new ArrayList<>(); List<AggregationDetails> aggregationHistory = getRepositoryAggregations(id);
Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>(); Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>();
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
JSONObject repository = new JSONObject(rs);
if (repository.getJSONArray("datasourceInfo").length() == 0)
return aggregationByYear;
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0)));
return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory); return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory);
} }
private Map<String, List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) { private Map<String, List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) {
Map<String, List<AggregationDetails>> aggregationByYear;
aggregationHistory = aggregationHistory.stream() aggregationHistory = aggregationHistory.stream()
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) .sorted(Comparator.comparing(AggregationDetails::getDate).reversed())
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -477,7 +438,7 @@ LOGGER.debug("json: " + jsonArray);
String page, String page,
String size) throws JSONException { String size) throws JSONException {
LOGGER.debug("Retreiving repositories with official name : " + name); LOGGER.debug("Retrieving repositories with official name : " + name);
UriComponents uriComponents = searchDatasource("0", "100"); UriComponents uriComponents = searchDatasource("0", "100");
RequestFilter requestFilter = new RequestFilter(); RequestFilter requestFilter = new RequestFilter();
requestFilter.setOfficialname(name); requestFilter.setOfficialname(name);
@ -571,7 +532,7 @@ LOGGER.debug("json: " + jsonArray);
String json_repository = Converter.repositoryObjectToJson(repository); String json_repository = Converter.repositoryObjectToJson(repository);
LOGGER.debug("JSON to add(update) -> " + json_repository); LOGGER.debug("JSON to add(update) -> " + json_repository);
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders); HttpEntity<String> httpEntity = new HttpEntity<>(json_repository, httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
@ -1000,6 +961,13 @@ LOGGER.debug("json: " + jsonArray);
return null; return null;
} }
private UriComponents getAggregationHistory(String repoId) {
return UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/aggregationhistory/")
.path("/repoId")
.build().expand(repoId).encode();
}
private UriComponents searchDatasource(String page, String size) { private UriComponents searchDatasource(String page, String size) {
return UriComponentsBuilder return UriComponentsBuilder

View File

@ -402,16 +402,11 @@ public class Converter {
return list; return list;
} }
public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject datasourceInfo) throws JSONException { public static List<AggregationDetails> getAggregationHistoryFromJson(JSONArray aggregationInfo) throws JSONException {
JSONArray rs;
List<AggregationDetails> aggregationDetailsList = new ArrayList<>(); List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
if (datasourceInfo.get("aggregationHistory") != null && !datasourceInfo.get("aggregationHistory").toString().equals("null")) { for (int i = 0; i < aggregationInfo.length(); i++)
rs = new JSONArray(datasourceInfo.get("aggregationHistory").toString()); aggregationDetailsList.add(jsonToAggregationDetails(aggregationInfo.getJSONObject(i)));
for (int i = 0; i < rs.length(); i++)
aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i)));
}
return aggregationDetailsList; return aggregationDetailsList;
} }
@ -420,17 +415,18 @@ public class Converter {
AggregationDetails aggregationDetails = new AggregationDetails(); AggregationDetails aggregationDetails = new AggregationDetails();
aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
if (aggregationObject.has("collectionMode")) if (aggregationObject.has("collectionMode"))
aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString()); aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
if (aggregationObject.has("indexedVersion")) if (aggregationObject.has("indexedVersion"))
aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString())); aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString()));
aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString())); aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString())); aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
return aggregationDetails; return aggregationDetails;
} }
public static List<Timezone> toTimezones(List<String> timezones) { public static List<Timezone> toTimezones(List<String> timezones) {
List<Timezone> tmz = new ArrayList<>(); List<Timezone> tmz = new ArrayList<>();