Skip to content

Commit

Permalink
#56 fix priorities calculation for REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
kmarszalek committed Nov 22, 2019
1 parent 1e35f3a commit 57f1e10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
* Created by mszostak on 12.04.17.
*/
public interface DocumentWeightRepository extends CrudRepository<DocumentWeight, DocumentWeightPk> {
DocumentWeight findByDocument(String document);
}
46 changes: 36 additions & 10 deletions src/main/java/pl/cyfronet/indigo/rest/util/IndigoConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pl.cyfronet.indigo.engine.extension.bean.IndigoDocument;
import pl.cyfronet.indigo.repository.DocumentWeightRepository;
import pl.cyfronet.indigo.rest.bean.IndigoWrapper;
import pl.cyfronet.indigo.rest.bean.preferences.Preference;
import pl.cyfronet.indigo.rest.bean.preferences.Preferences;
Expand All @@ -28,6 +29,9 @@ public class IndigoConverter {
@Autowired
private MetricFacade metricFacade;

@Autowired
private DocumentWeightRepository documentWeightRepository;

public IndigoWrapper convertSlasListForRestApi(List<Document> slas, String login) {
IndigoWrapper result = IndigoWrapper.builder().
preferences(Arrays.asList(preparePreferences(slas, login))).
Expand All @@ -43,25 +47,47 @@ private Preferences preparePreferences(List<Document> slas, String login) {
List<Priority> storagePriorities = new ArrayList<>();
for (Document sla : slas) {
if (sla.getState("serviceType").equals("computing")) {
addPriorities(computePriorities, sla, ((IndigoDocument)sla).getSite(), "weightComputing");
addPriorities(computePriorities, sla, ((IndigoDocument) sla).getSite(), "weightComputing");
} else {
addPriorities(storagePriorities, sla, ((IndigoDocument)sla).getSite(), "weightStorage");
addPriorities(storagePriorities, sla, ((IndigoDocument) sla).getSite(), "weightStorage");
}
}
result.setPreferences(preparePreference(computePriorities, storagePriorities));
result.setPreferences(preparePreference(normalizePriorities(computePriorities), normalizePriorities(storagePriorities)));
return result;
}

private List<Priority> normalizePriorities(List<Priority> priorities) {
//k -> group number 1...n
//n -> number of groups
// 1 / (n -1) * k
Double one = new Double(1);
Double groupNumber = getGroupNumber(priorities);
priorities.forEach(priority -> {
priority.setWeight(one / (groupNumber - one) * priority.getWeight());
});
return priorities;
}

private Double getGroupNumber(List<Priority> priorities) {
Double result = new Double(0);
for (Priority priority: priorities) {
if(priority.getWeight()>result) result = priority.getWeight();
}
return ++result;
}

private void addPriorities(List<Priority> priorities, Document sla, String serviceId, String weight) {
Priority priority = Priority.builder().
sla_id(sla.getId()).
service_id(serviceId).
//TODO - RESTORE IT
// weight(Double.parseDouble(sla.getMetrics().get(weight).toString())).build();
weight(0.5).build();
weight(getWeight(sla.getId())).build();
priorities.add(priority);
}

private Double getWeight(String id) {
return new Double(documentWeightRepository.findByDocument(id).getWeight());
}

private List<Preference> preparePreference(List<Priority> computePriorities, List<Priority> storagePriorities) {
Preference computing = Preference.builder().service_type("compute").priority(computePriorities).build();
Preference storage = Preference.builder().service_type("storage").priority(storagePriorities).build();
Expand All @@ -72,7 +98,7 @@ private List<Preference> preparePreference(List<Priority> computePriorities, Lis
public List<Sla> prepareSlaList(List<Document> slas, String login) {
List<Sla> result = new ArrayList<>();
for (Document _doc : slas) {
IndigoDocument doc = (IndigoDocument)_doc;
IndigoDocument doc = (IndigoDocument) _doc;
Document provider = null;
Sla.SlaBuilder slaBuilder = Sla.builder().id(doc.getId()).provider(doc.getProviderId());

Expand Down Expand Up @@ -111,12 +137,12 @@ private List<Target> prepareTargets(Document sla) {

Map<String, Target> restrictions = new HashMap<>();

for(Metric metric : documentMetrics) {
for (Metric metric : documentMetrics) {

if(sla.getMetrics().containsKey(metric.getId()) && metric.getId().contains("-")) {
if (sla.getMetrics().containsKey(metric.getId()) && metric.getId().contains("-")) {
String key = metric.getId().split("-")[0];
String constraint = metric.getId().split("-")[1];
if(!restrictions.containsKey(key)) {
if (!restrictions.containsKey(key)) {
restrictions.put(key, Target.builder().type(key).unit(metric.getUnit()).restrictions(new HashMap<>()).build());
}

Expand Down

0 comments on commit 57f1e10

Please sign in to comment.