def experiments_POST(self): """ Create new experiment """ app_id = self.request.swagger_data['appid'] req_exp = self.request.swagger_data['experiment'] exp = Experiment(name=req_exp.name, startDatetime=req_exp.startDatetime, endDatetime=req_exp.endDatetime, application_id=app_id) Experiment.save(exp) print_log(req_exp.name, 'POST', '/experiments', 'Create new experiment', exp) return exp.as_dict()
def get_dataitems_for_experiment(self, id): """ get dataitems list from a specific experiment """ experiment = Experiment.get(id) dataitems = [] for expgroup in experiment.experimentgroups: dataitems.extend(self.get_dataitems_for_experimentgroup(expgroup.id)) return dataitems
def get_total_dataitems_for_experiment(self, experiment_id): """ get total dataitems from the specific experiment """ count = 0 experiment = Experiment.get(experiment_id) for expgroup in experiment.experimentgroups: count += self.get_total_dataitems_for_expgroup(expgroup.id) return count
def experiments_GET(self): """ List all Application's Experiments including Experiments' status """ app_id = self.request.swagger_data['appid'] experiments = Experiment.query().join(Application)\ .filter(Application.id == app_id).all() return list(map(lambda _: assoc(_.as_dict(), 'status',\ _.get_status()), experiments))
def is_client_in_running_experiments(client): experiments = Experiment.query()\ .join(ExperimentGroup)\ .join(ExperimentGroup.clients).filter(Client.id == client.id).all() running_experiments = list( filter(lambda _: _.get_status() == 'running', experiments)) return len(running_experiments) > 0
def get_clients_for_experiment(self, id): """ get clients from the specific experiment """ experiment = Experiment.get(id) if experiment is None: return None clients = [] for experimentgroup in experiment.experimentgroups: clients.extend(experimentgroup.clients) return clients
def experiment_DELETE(self): """ Delete one experiment """ app_id = self.request.swagger_data['appid'] exp_id = self.request.swagger_data['expid'] log_address = '/applications/%s/experiments/%s' % (app_id, exp_id) exp = Experiment.query().join(Application)\ .filter(Application.id == app_id, Experiment.id == exp_id)\ .one_or_none() if exp is None: print_log(datetime.datetime.now(), 'DELETE', log_address,\ 'Delete experiment', 'Failed') return self.createResponse(None, 400) Experiment.destroy(exp) print_log(datetime.datetime.now(), 'DELETE', log_address,\ 'Delete experiment', 'Succeeded') return {}
def get_total_dataitems_for_client_in_experiment(self, client_id, exp_id): """ get total dataitems for specific client in specific experiment """ experiment = Experiment.get(exp_id) start_datetime = experiment.startDatetime end_datetime = experiment.endDatetime count = DataItem.query().filter( and_(DataItem.client_id == client_id, start_datetime <= DataItem.startDatetime, DataItem.endDatetime <= end_datetime)).count() return count
def assign_client_to_experiment(self, client_id, experiment_id): """ randomly assign client to different experiment """ experimentgroups = Experiment.get(experiment_id).experimentgroups if len(experimentgroups) == 1: experimentgroup = experimentgroups[0] else: experimentgroup = experimentgroups[random.randint(0, len(experimentgroups) - 1)] self.dbsession.query(Client)\ .filter_by(id=client_id).first()\ .experimentgroups.append(experimentgroup)
def create_experiment(self, data): """create experiment""" name = data['name'] experimentgroups = data['experimentgroups'] start_datetime = datetime.datetime.strptime(data['startDatetime'], "%Y-%m-%d %H:%M:%S") end_datetime = datetime.datetime.strptime(data['endDatetime'], "%Y-%m-%d %H:%M:%S") experiment = Experiment( name=name, startDatetime=start_datetime, endDatetime=end_datetime, experimentgroups=experimentgroups) self.dbsession.add(experiment) return self.dbsession.query(Experiment).filter( Experiment.id == self.dbsession.query(func.max(Experiment.id))).first()
def experiments_for_client_GET(self): client = get_client_by_id_and_app(self.request.swagger_data) app_id = self.request.swagger_data['appid'] if not client: return self.createResponse(None, 400) experiments = Experiment.query()\ .filter(Experiment.application_id == app_id)\ .join(ExperimentGroup)\ .join(ExperimentGroup.clients)\ .filter(Client.id == client.id) print(experiments) result = map(lambda _: _.as_dict(), experiments) return list(result)
def experiments_GET_one(self): """ Find and return one Application's Experiment by id with GET method """ app_id = self.request.swagger_data['appid'] exp_id = self.request.swagger_data['expid'] exp = Experiment.query().join(Application)\ .filter(Application.id == app_id, Experiment.id == exp_id).one_or_none() if exp is None: print_log(datetime.datetime.now(), 'GET',\ '/applications/%s/experiments/%s' % (app_id, exp_id),\ 'Get one experiment', 'Failed') return self.createResponse(None, 400) exp_dict = exp.as_dict() exp_dict['status'] = exp.get_status() return exp_dict
def experimentgroup_GET(self): app_id = self.request.swagger_data['appid'] exp_id = self.request.swagger_data['expid'] experiment = Experiment.query().join(Application)\ .filter(Application.id == app_id, Experiment.id == exp_id)\ .one_or_none() if experiment == None: print_log(datetime.datetime.now(), 'GET', \ '/applications/%s/experiments/%s/experimentgroups' % ((app_id, exp_id)), 'List all ExperimentGroups for specific experiment', 'Failed') return self.createResponse(None, 400) experimentgroups = ExperimentGroup.query().filter( ExperimentGroup.experiment_id == experiment.id) return list(map(lambda _: _.as_dict(), experimentgroups))
def clients_for_experiment_GET(self): """ List all clients for specific experiment """ app_id = self.request.swagger_data['appid'] exp_id = self.request.swagger_data['expid'] exp = Experiment.query().join(Application)\ .filter(Experiment.id == exp_id, Application.id == app_id)\ .one_or_none() if exp == None: print_log(datetime.datetime.now(), 'GET', '/experiments/' + str(id) + '/clients', 'List all clients for specific experiment', 'Failed') return self.createResponse(None, 400) clients = Client.query()\ .join(Client.experimentgroups, Experiment, Application)\ .filter(Experiment.id == exp_id, Application.id == app_id).all() return list(map(lambda _: _.as_dict(), clients))
def get_status_for_experiment(self, id): """ get status of the experiment by comparing start datetime and end datetime """ # open = 'open' running = 'running' finished = 'finished' waiting = 'waiting' experiment = Experiment.get(id) date_time_now = datetime.datetime.now() start_datetime = experiment.startDatetime end_datetime = experiment.endDatetime if start_datetime >= end_datetime: # validate this earlier return None if start_datetime <= date_time_now and date_time_now <= end_datetime: return running elif date_time_now > end_datetime: return finished elif date_time_now < start_datetime: return waiting return None
def get_dataitems_for_client_in_experiment(self, client_id, exp_id): """ get dataitems from specific client in specific experiment """ experiment = Experiment.get(exp_id) start_datetime = experiment.startDatetime end_datetime = experiment.endDatetime return self.get_dataitems_for_client_on_period(client_id, start_datetime, end_datetime)