8
8
import org .springframework .beans .factory .annotation .Autowired ;
9
9
import org .springframework .stereotype .Component ;
10
10
import pl .cyfronet .indigo .engine .extension .bean .IndigoDocument ;
11
+ import pl .cyfronet .indigo .repository .DocumentWeightRepository ;
11
12
import pl .cyfronet .indigo .rest .bean .IndigoWrapper ;
12
13
import pl .cyfronet .indigo .rest .bean .preferences .Preference ;
13
14
import pl .cyfronet .indigo .rest .bean .preferences .Preferences ;
@@ -28,6 +29,9 @@ public class IndigoConverter {
28
29
@ Autowired
29
30
private MetricFacade metricFacade ;
30
31
32
+ @ Autowired
33
+ private DocumentWeightRepository documentWeightRepository ;
34
+
31
35
public IndigoWrapper convertSlasListForRestApi (List <Document > slas , String login ) {
32
36
IndigoWrapper result = IndigoWrapper .builder ().
33
37
preferences (Arrays .asList (preparePreferences (slas , login ))).
@@ -43,25 +47,47 @@ private Preferences preparePreferences(List<Document> slas, String login) {
43
47
List <Priority > storagePriorities = new ArrayList <>();
44
48
for (Document sla : slas ) {
45
49
if (sla .getState ("serviceType" ).equals ("computing" )) {
46
- addPriorities (computePriorities , sla , ((IndigoDocument )sla ).getSite (), "weightComputing" );
50
+ addPriorities (computePriorities , sla , ((IndigoDocument ) sla ).getSite (), "weightComputing" );
47
51
} else {
48
- addPriorities (storagePriorities , sla , ((IndigoDocument )sla ).getSite (), "weightStorage" );
52
+ addPriorities (storagePriorities , sla , ((IndigoDocument ) sla ).getSite (), "weightStorage" );
49
53
}
50
54
}
51
- result .setPreferences (preparePreference (computePriorities , storagePriorities ));
55
+ result .setPreferences (preparePreference (normalizePriorities ( computePriorities ), normalizePriorities ( storagePriorities ) ));
52
56
return result ;
53
57
}
54
58
59
+ private List <Priority > normalizePriorities (List <Priority > priorities ) {
60
+ //k -> group number 1...n
61
+ //n -> number of groups
62
+ // 1 / (n -1) * k
63
+ Double one = new Double (1 );
64
+ Double groupNumber = getGroupNumber (priorities );
65
+ priorities .forEach (priority -> {
66
+ priority .setWeight (one / (groupNumber - one ) * priority .getWeight ());
67
+ });
68
+ return priorities ;
69
+ }
70
+
71
+ private Double getGroupNumber (List <Priority > priorities ) {
72
+ Double result = new Double (0 );
73
+ for (Priority priority : priorities ) {
74
+ if (priority .getWeight ()>result ) result = priority .getWeight ();
75
+ }
76
+ return ++result ;
77
+ }
78
+
55
79
private void addPriorities (List <Priority > priorities , Document sla , String serviceId , String weight ) {
56
80
Priority priority = Priority .builder ().
57
81
sla_id (sla .getId ()).
58
82
service_id (serviceId ).
59
- //TODO - RESTORE IT
60
- // weight(Double.parseDouble(sla.getMetrics().get(weight).toString())).build();
61
- weight (0.5 ).build ();
83
+ weight (getWeight (sla .getId ())).build ();
62
84
priorities .add (priority );
63
85
}
64
86
87
+ private Double getWeight (String id ) {
88
+ return new Double (documentWeightRepository .findByDocument (id ).getWeight ());
89
+ }
90
+
65
91
private List <Preference > preparePreference (List <Priority > computePriorities , List <Priority > storagePriorities ) {
66
92
Preference computing = Preference .builder ().service_type ("compute" ).priority (computePriorities ).build ();
67
93
Preference storage = Preference .builder ().service_type ("storage" ).priority (storagePriorities ).build ();
@@ -72,7 +98,7 @@ private List<Preference> preparePreference(List<Priority> computePriorities, Lis
72
98
public List <Sla > prepareSlaList (List <Document > slas , String login ) {
73
99
List <Sla > result = new ArrayList <>();
74
100
for (Document _doc : slas ) {
75
- IndigoDocument doc = (IndigoDocument )_doc ;
101
+ IndigoDocument doc = (IndigoDocument ) _doc ;
76
102
Document provider = null ;
77
103
Sla .SlaBuilder slaBuilder = Sla .builder ().id (doc .getId ()).provider (doc .getProviderId ());
78
104
@@ -111,12 +137,12 @@ private List<Target> prepareTargets(Document sla) {
111
137
112
138
Map <String , Target > restrictions = new HashMap <>();
113
139
114
- for (Metric metric : documentMetrics ) {
140
+ for (Metric metric : documentMetrics ) {
115
141
116
- if (sla .getMetrics ().containsKey (metric .getId ()) && metric .getId ().contains ("-" )) {
142
+ if (sla .getMetrics ().containsKey (metric .getId ()) && metric .getId ().contains ("-" )) {
117
143
String key = metric .getId ().split ("-" )[0 ];
118
144
String constraint = metric .getId ().split ("-" )[1 ];
119
- if (!restrictions .containsKey (key )) {
145
+ if (!restrictions .containsKey (key )) {
120
146
restrictions .put (key , Target .builder ().type (key ).unit (metric .getUnit ()).restrictions (new HashMap <>()).build ());
121
147
}
122
148
0 commit comments