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

Fixing issue #9 #48

Closed
wants to merge 4 commits into from
Closed
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 @@ -142,20 +142,20 @@ public Page<SubmissionDTO> list(@PageableDefault(value = DEFAULT_PAGE_SIZE, sort
direction = Sort.Direction.DESC) Pageable pageable) {

Pageable p;
if (!isCustomSort(pageable)) {
p = pageable;
} else {
p = buildPageRequest(pageable);
}
String sortField = isFinishedSort(pageable) ? "finished" : isSubmittedSort(pageable) ? "submitted" : (isStatusSort(pageable) ? "status" : "");
Page<Analysis> analyses;
if (isFinishedSort(pageable)) {
analyses = analysisRepository.findAllPagedOrderByFinished(p);
} else if (isSubmittedSort(pageable)) {
analyses = analysisRepository.findAllPagedOrderBySubmitted(p);
} else if (isStatusSort(pageable)) {
analyses = analysisRepository.findAllPagedOrderByState(p);
} else {
if ("".equals(sortField)) {
if (!isCustomSort(pageable)) {
p = pageable;
} else {
p = buildPageRequest(pageable);
}
analyses = analysisRepository.findAll(p);
}else{
analyses =analysisRepository
.findAllPagingSortingByCalculateFields(pageable.getSort().get().findFirst().get().getDirection().name(),
sortField,
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()));
}
return analyses.map(analysis -> analysisToSubmissionDTO.convert(analysis));
}
Expand Down Expand Up @@ -222,25 +222,26 @@ private boolean isSortOf(final Pageable pageable, Function<String, Boolean> pred
return false;
}

protected void initProps() {

propertiesMap.put("author.fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("analysis", p -> p.add("title"));
propertiesMap.put("study", p -> p.add("studyTitle"));
propertiesMap.put("status", p -> p.add("journal.state"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line removed?

}
protected void initProps() {

private class EngineStatusResponse {
public EngineStatusResponse(final ExecutionEngineStatus status) {
this.status = status;
}
public ExecutionEngineStatus status;
propertiesMap.put("author.fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("analysis", p -> p.add("title"));
propertiesMap.put("study", p -> p.add("studyTitle"));
}

private class EngineStatusResponse {

public EngineStatusResponse(final ExecutionEngineStatus status) {
this.status = status;
}

public ExecutionEngineStatus status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package com.odysseusinc.arachne.datanode.repository;

import com.odysseusinc.arachne.datanode.model.analysis.Analysis;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -26,79 +24,40 @@
import java.util.List;
import java.util.Optional;

public interface AnalysisRepository extends JpaRepository<Analysis, Long> {

Optional<Analysis> findById(Long id);

@Query(nativeQuery = true, value =
"SELECT analyses.* FROM analyses WHERE analyses.id = :id AND analyses.callback_password = :password")
Optional<Analysis> findOneExecuting(@Param("id") Long id, @Param("password") String password);

@Query(nativeQuery = true, value =
"SELECT analyses.* "
+ "FROM analyses "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = analyses.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id "
+ " WHERE journal.state = :state AND analyses.central_id IS NOT NULL")
List<Analysis> findAllByState(@Param("state") String state);

@Query(nativeQuery = true, value =
"SELECT analyses.* "
+ "FROM analyses "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = analyses.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id "
+ " WHERE journal.state NOT IN (:states)")
List<Analysis> findAllByNotStateIn(@Param("states") List<String> states);

@Query(nativeQuery = true, value =
"SELECT analyses.* "
+ " FROM analyses "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = analyses.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id "
+ " WHERE journal.state = :state AND journal.date < :time")
List<Analysis> findAllExecutingMoreThan(@Param("state") String state, @Param("time") Date time);

@Query(nativeQuery = true, value =
"select a.* from analyses a "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = a.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id"
+ " \n--#pageable\n",
countQuery = "select count(a.*) from analyses a "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = a.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id")
Page<Analysis> findAllPagedOrderByState(Pageable pageable);

@Query(nativeQuery = true, value =
"select a.* from analyses a "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = a.id "
+ " JOIN (SELECT analysis_id, min(date) AS submitted FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS SUB ON journal.date = SUB.submitted AND journal.analysis_id=SUB.analysis_id"
+ " \n--#pageable\n",
countQuery = "select count(a.*) from analyses a "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = a.id "
+ " JOIN (SELECT analysis_id, min(date) AS submitted FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS SUB ON journal.date = SUB.submitted AND journal.analysis_id=SUB.analysis_id")
Page<Analysis> findAllPagedOrderBySubmitted(Pageable pageable);

@Query(nativeQuery = true, value =
"select a.*, "
+ "case journal.state "
+ " when 'EXECUTING' then null "
+ " when 'CREATED' then null"
+ " else journal.date "
+ "end as finished FROM analyses a "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = a.id "
+ " JOIN (SELECT analysis_id, max(date) AS finished FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS SUB ON journal.date = SUB.finished AND journal.analysis_id=SUB.analysis_id"
+ " \n--#pageable\n",
countQuery = "select count(a.*) from analyses a "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = a.id "
+ " JOIN (SELECT analysis_id, max(date) AS finished FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS SUB ON journal.date = SUB.finished AND journal.analysis_id=SUB.analysis_id")
Page<Analysis> findAllPagedOrderByFinished(Pageable pageable);
}
public interface AnalysisRepository extends JpaRepository<Analysis, Long>,
AnalysisRepositoryExtended {

Optional<Analysis> findById(Long id);

@Query(nativeQuery = true, value =
"SELECT analyses.* FROM analyses WHERE analyses.id = :id AND analyses.callback_password = :password")
Optional<Analysis> findOneExecuting(@Param("id") Long id, @Param("password") String password);

@Query(nativeQuery = true, value =
"SELECT analyses.* "
+ "FROM analyses "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = analyses.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id "
+ " WHERE journal.state = :state AND analyses.central_id IS NOT NULL")
List<Analysis> findAllByState(@Param("state") String state);

@Query(nativeQuery = true, value =
"SELECT analyses.* "
+ "FROM analyses "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = analyses.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id "
+ " WHERE journal.state NOT IN (:states)")
List<Analysis> findAllByNotStateIn(@Param("states") List<String> states);

@Query(nativeQuery = true, value =
"SELECT analyses.* "
+ " FROM analyses "
+ " JOIN analysis_state_journal AS journal ON journal.analysis_id = analyses.id "
+ " JOIN (SELECT analysis_id, max(date) AS latest FROM analysis_state_journal "
+ " GROUP BY analysis_id) AS FOO ON journal.date = FOO.latest AND journal.analysis_id=FOO.analysis_id "
+ " WHERE journal.state = :state AND journal.date < :time")
List<Analysis> findAllExecutingMoreThan(@Param("state") String state, @Param("time") Date time);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.odysseusinc.arachne.datanode.repository;

import com.odysseusinc.arachne.datanode.model.analysis.Analysis;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface AnalysisRepositoryExtended {
Page<Analysis> findAllPagingSortingByCalculateFields(String direction, String sortField, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.odysseusinc.arachne.datanode.repository.implementation;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import com.odysseusinc.arachne.datanode.model.analysis.Analysis;
import com.odysseusinc.arachne.datanode.repository.AnalysisRepositoryExtended;

@Repository
public class AnalysisRepositoryImpl implements AnalysisRepositoryExtended {
@PersistenceContext
private EntityManager entityManager;
@Override
public Page<Analysis> findAllPagingSortingByCalculateFields(String direction,
String sortField, Pageable pageable) {
String queryString = "SELECT \n" +
" a.* \n" +
"FROM \n" +
" analyses a \n" +
" JOIN analysis_state_journal AS J ON J.analysis_id = a.id \n" +
" JOIN (\n" +
" SELECT \n" +
" J.analysis_id, \n" +
" MAX(J.date) AS finished, \n" +
" MIN(J.date) AS submitted \n" +
" FROM \n" +
" analysis_state_journal J \n" +
" GROUP BY \n" +
" J.analysis_id\n" +
" ) JS ON J.analysis_id = JS.analysis_id \n" +
" AND J.date = JS.finished \n" +
"ORDER BY \n";

if(sortField.equals("finished")){
queryString = queryString + "CASE WHEN J.state IN('EXECUTING', 'CREATED') THEN null ELSE JS.finished END ";
}else if(sortField.equals("status")){
queryString = queryString + "CASE WHEN J.state = 'ABORT_FAILURE' THEN 'Failed to Abort' WHEN J.state = 'EXECUTION_FAILURE' THEN 'Failed' ELSE J.state END ";
}else if(sortField.equals("submitted")){
queryString = queryString + "JS.submitted ";
}
queryString = queryString + direction + " NULLS LAST";

Query query = entityManager.createNativeQuery(queryString, Analysis.class);
query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
query.setMaxResults(pageable.getPageSize());
List<Analysis> content = query.getResultList();

Query countQuery = entityManager.createNativeQuery("SELECT COUNT(*) FROM analyses");
Long countResult = ((Number)countQuery.getSingleResult()).longValue();

return new PageImpl<>(content, PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()), countResult);
}
}