21
21
from sustainml_swig import NodeStatus
22
22
import sustainml_swig
23
23
import threading
24
+ import json
24
25
25
26
class OrchestratorNodeHandle (cpp_OrchestratorNodeHandle ):
26
27
@@ -243,6 +244,50 @@ def get_carbontracker(self, task_id):
243
244
'carbon_intensity' : carbon_intensity }
244
245
return json_output
245
246
247
+ def get_orchestrator (self , task_id ):
248
+
249
+ # retrieve node data
250
+ node_data = sustainml_swig .get_orchestrator (self .node_ , task_id )
251
+ if node_data is None :
252
+ return {'Error' : f"Failed to get { utils .string_node (utils .node_id .ORCHESTRATOR .value )} data for task { utils .string_task (task_id )} " }
253
+
254
+ # Parse data into json
255
+ task_json = {'problem_id' : task_id .problem_id (), 'iteration_id' : task_id .iteration_id ()}
256
+ modality = node_data .modality ()
257
+ problem_short_description = node_data .problem_short_description ()
258
+ problem_definition = node_data .problem_definition ()
259
+ inputs = node_data .inputs ()
260
+ outputs = node_data .outputs ()
261
+ minimum_samples = node_data .minimum_samples ()
262
+ maximum_samples = node_data .maximum_samples ()
263
+ optimize_carbon_footprint_manual = node_data .optimize_carbon_footprint_manual ()
264
+ previous_iteration = node_data .previous_iteration ()
265
+ optimize_carbon_footprint_auto = node_data .optimize_carbon_footprint_auto ()
266
+ desired_carbon_footprint = node_data .desired_carbon_footprint ()
267
+ geo_location_continent = node_data .geo_location_continent ()
268
+ geo_location_region = node_data .geo_location_region ()
269
+ extra_data_vector = node_data .extra_data ()
270
+ extra_data_list = [s for s in extra_data_vector ]
271
+ extra_data_bytes = bytes (extra_data_list )
272
+ extra_data_str = extra_data_bytes .decode ('utf-8' )
273
+ extra_data = json .loads (extra_data_str )
274
+ json_output = {'task_id' : task_json ,
275
+ 'modality' : modality ,
276
+ 'problem_short_description' : problem_short_description ,
277
+ 'problem_definition' : problem_definition ,
278
+ 'inputs' : " " .join (inputs ),
279
+ 'outputs' : " " .join (outputs ),
280
+ 'minimum_samples' : minimum_samples ,
281
+ 'maximum_samples' : maximum_samples ,
282
+ 'optimize_carbon_footprint_manual' : optimize_carbon_footprint_manual ,
283
+ 'previous_iteration' : previous_iteration ,
284
+ 'optimize_carbon_footprint_auto' : optimize_carbon_footprint_auto ,
285
+ 'desired_carbon_footprint' : desired_carbon_footprint ,
286
+ 'geo_location_continent' : geo_location_continent ,
287
+ 'geo_location_region' : geo_location_region ,
288
+ 'extra_data' : extra_data }
289
+ return json_output
290
+
246
291
def get_results (self , node_id , task_id ):
247
292
if task_id is None :
248
293
task_id = self .get_last_task_id ()
@@ -259,13 +304,34 @@ def get_results(self, node_id, task_id):
259
304
return self .get_hw_provider (task_id )
260
305
elif node_id == utils .node_id .CARBONTRACKER .value :
261
306
return self .get_carbontracker (task_id )
307
+ elif node_id == utils .node_id .ORCHESTRATOR .value :
308
+ return self .get_orchestrator (task_id )
262
309
else :
263
310
message = utils .string_node (node_id ) + " node does not have any results to show."
264
311
return {'message' : message , 'task_id' : utils .task_json (task_id )}
265
312
266
313
def send_user_input (self , json_data ):
267
- pair = self .node_ .prepare_new_task ()
314
+ if json_data .get ('previous_iteration' ) == 0 :
315
+ pair = self .node_ .prepare_new_task ()
316
+ else :
317
+ previous_task = sustainml_swig .TaskId ()
318
+ previous_task .iteration_id (json_data .get ('previous_iteration' ))
319
+ extra = json_data .get ('extra_data' , {})
320
+ previous_task .problem_id (extra .get ('previous_problem_id' ))
321
+ # Verify the last doesn't exist yet
322
+ existing_task = self .get_last_task_id ()
323
+ if existing_task is not None :
324
+ if (previous_task .problem_id () == existing_task .problem_id () and
325
+ previous_task .iteration_id () + 1 == existing_task .iteration_id ()):
326
+ print ("Task already taken. Using :" , utils .string_task (existing_task ))
327
+ return None
328
+ pair = self .node_ .prepare_new_iteration (previous_task )
268
329
task_id = pair [0 ]
330
+
331
+ print ("Task:" , utils .string_task (task_id ))
332
+ print ("Problem ID:" , task_id .problem_id ()) # Debugging
333
+ print ("Iteration ID:" , task_id .iteration_id ())
334
+
269
335
user_input = pair [1 ]
270
336
self .handler_ .register_task (task_id )
271
337
0 commit comments