def rangecontraints_DELETE_one(self):
        """ Find and delete one rangeconstraint by id with DELETE method """
        app_id = self.request.swagger_data['appid']
        confkey_id = self.request.swagger_data['ckid']
        rc_id = self.request.swagger_data['rcid']
        rangeconstraint = RangeConstraint.get(rc_id)

        try:
            # Checks if ConfigurationKey exists with given Application
            if not ConfigurationKey.get(confkey_id).application_id == app_id:
                raise Exception('Application with id %s does not have' %
                                app_id +
                                ' ConfigurationKey with id %s' % confkey_id)
            # Checks if given RangeConstraint is owned by ConfigurationKey
            elif not rangeconstraint.configurationkey_id == confkey_id:
                raise Exception('ConfigurationKey with id %s does not' %
                                confkey_id +
                                ' have RangeConstraint with id %s' % rc_id)

            RangeConstraint.destroy(rangeconstraint)
        except Exception as e:
            print(e)
            print_log(datetime.datetime.now(), 'DELETE',
                      '/rangeconstraint/' + str(rc_id),
                      'Delete rangeconstraint', 'Failed')
            return self.createResponse(None, 400)

        print_log(datetime.datetime.now(), 'DELETE',
                  '/rangeconstranit/' + str(rc_id), 'Delete rangeconstraint',
                  'Succeeded')
        return {}
Esempio n. 2
0
    def exclusionconstraints_POST(self):
        app_id = self.request.swagger_data['appid']
        exconstraint = self.request.swagger_data['exclusionconstraint']

        new_exconstraint = ExclusionConstraint(
            first_configurationkey_id=exconstraint[
                'first_configurationkey_id'],
            first_operator_id=exconstraint['first_operator_id'],
            first_value_a=None if len(exconstraint['first_value']) == 0 else
            exconstraint['first_value'][0],
            first_value_b=None if len(exconstraint['first_value']) <= 1 else
            exconstraint['first_value'][1],
            second_configurationkey_id=exconstraint[
                'second_configurationkey_id'],
            second_operator_id=exconstraint['second_operator_id'],
            second_value_a=None if len(exconstraint['second_value']) == 0 else
            exconstraint['second_value'][0],
            second_value_b=None if len(exconstraint['second_value']) <= 1 else
            exconstraint['second_value'][1])

        if not self.is_valid_exclusionconstraint(new_exconstraint, app_id):
            print_log(
                datetime.datetime.now(), 'POST',
                '/application/%s/exclusionconstraints' % (app_id),
                'Create new exclusionconstraint for configurationkey',
                'Failed: ExclusionConstraint is not valid: %s' %
                (new_exconstraint.as_dict()))

            return self.createResponse({}, 400)

        ExclusionConstraint.save(new_exconstraint)

        return new_exconstraint.as_dict()
    def rangecontraints_POST(self):
        """ Create new rangeconstraint for specific configurationkey """
        req_rangec = self.request.swagger_data['rangeconstraint']
        configkey_id = self.request.swagger_data['ckid']
        app_id = self.request.swagger_data['appid']

        rconstraint = RangeConstraint(configurationkey_id=configkey_id,
                                      operator_id=req_rangec.operator_id,
                                      value=req_rangec.value)

        try:
            # Checks if Configuration with such connection to Application exists
            if not is_valid_rangeconstraint(app_id, configkey_id, rconstraint):
                raise Exception('Application with id %s does not have' %
                                app_id +
                                ' ConfigurationKey with id %s' % configkey_id)
            RangeConstraint.save(rconstraint)
        except Exception as e:
            print_log(
                datetime.datetime.now(), 'POST', '/applications/%s/' % app_id +
                'configurationkeys/%s/rangeconstraints' % configkey_id,
                'Create new rangeconstraint for configurationkey',
                'Failed: %s' % e)
            return self.createResponse({}, 400)

        print_log(
            datetime.datetime.now(), 'POST', '/applications/%s' % app_id +
            '/configurationkeys/%s/rangeconstraints' % configkey_id,
            'Create new rangeconstraint for configurationkey', 'Succeeded')
        return rconstraint.as_dict()
Esempio n. 4
0
    def data_for_app_GET(self):
        """ List all configurationkeys and rangeconstraints of specific application.
            Returns application with configurationkeys, rangeconstraints and exclusionconstraints
        """
        app_id = self.request.swagger_data['id']
        app = Application.get(app_id)
        if app is None:
            print_log(datetime.datetime.now(), 'GET',
                      '/applications/' + str(id) + '/rangeconstraints',
                      'Get all things of one application', None)
            return self.createResponse(None, 400)
        if app.apikey is None:
            app = self.set_app_apikey(app, app_id)
        configurationkeys = app.configurationkeys
        ranges = list(
            concat(list(map(lambda _: _.rangeconstraints, configurationkeys))))
        exclusions = self.get_app_exclusionconstraints(app_id)

        app_data = app.as_dict()
        app_data = assoc(app_data, 'configurationkeys',
                         list(map(lambda _: _.as_dict(), configurationkeys)))
        app_data = assoc(app_data, 'rangeconstraints',
                         list(map(lambda _: _.as_dict(), ranges)))
        app_data = assoc(app_data, 'exclusionconstraints',
                         list(map(lambda _: _.as_dict(), exclusions)))

        return app_data
    def rangeconstraints_for_configuratinkey_DELETE(self):
        """
            Delete all rangeconstraints of one specific configurationkey
            To include all the database connection checks which rangecontraints_DELETE_one contains, it is used in this
            function.
        """
        configkey_id = self.request.swagger_data['ckid']
        app_id = self.request.swagger_data['appid']
        configurationkey = ConfigurationKey.get(configkey_id)
        errors = 0

        try:
            for rc in configurationkey.rangeconstraints:
                # Set rangeconstraint's id, so rangecontraints_DELETE_one can use it
                self.request.swagger_data['rcid'] = rc.id
                if not self.rangecontraints_DELETE_one() == {}:
                    errors += 1
        except Exception as e:
            print(e)
            errors += 1
        finally:
            if (errors > 0):
                print_log(
                    datetime.datetime.now(), 'DELETE',
                    '/application/%s' % app_id + '/configurationkeys/' +
                    str(configkey_id) + '/rangeconstraints',
                    'Delete rangeconstraints of configurationkey', 'Failed')
                return self.createResponse(None, 400)

        return {}
Esempio n. 6
0
    def configurations_POST(self):
        """
        Create new Configuration to ExperimentGroup. Requires Application's id, Experiment's id, ExperimentGroup's id
        and Configuration to be created. This will fail if Configuration, or given ids are not valid.
        :return: If successfully created, returns created Configuration. If it fails, returns response with HTTP code
        400
        """
        app_id = self.request.swagger_data['appid']
        exp_id = self.request.swagger_data['expid']
        expgroup_id = self.request.swagger_data['expgroupid']
        req_config = self.request.swagger_data['configuration']

        req_config.experimentgroup_id = expgroup_id

        if self.is_valid_configuration(app_id, exp_id, expgroup_id,
                                       req_config):
            Configuration.save(req_config)
            print_log(datetime.datetime.now(), 'POST',
                      '/applications/%s/experiments/%s/experimentgroups/%s',
                      'Create Configuration to ExperimentGroup', 'Success')
            return req_config.as_dict()

        print_log(datetime.datetime.now(), 'POST',
                  '/applications/%s/experiments/%s/experimentgroups/%s',
                  'Create Configuration to ExperimentGroup',
                  'Failed: Invalid Configuration')
        return self.createResponse(None, 400)
Esempio n. 7
0
    def client_GET(self):
        result = get_client_by_id_and_app(self.request.swagger_data)

        if not result:
            print_log(datetime.datetime.now(), 'GET','applications/%s/clients/%s' \
                % (self.request.swagger_data['appid'], self.request.swagger_data['clientid']),
                'Get client', 'Failed')
            return self.createResponse(None, 400)
        return result.as_dict()
 def configurationkeys_GET(self):
     """ List all configurationkeys with GET method """
     app_id = self.request.swagger_data['id']
     if Application.get(app_id) is None:
         print_log(datetime.datetime.now(), 'GET', '/applications/%s/configurationkeys'\
             % app_id, 'Get configurationkeys', 'Failed')
         return self.createResponse(None, 400)
     app_conf_keys = ConfigurationKey.query().join(Application).filter(Application.id == app_id)
     return list(map(lambda _: _.as_dict(), app_conf_keys))
def get_conf_key_by_appid_and_ckid(app_id, confkey_id):
    try:
        return ConfigurationKey.query()\
            .filter(ConfigurationKey.id == confkey_id)\
            .join(Application)\
            .filter(Application.id == app_id)\
            .one()
    except Exception as e:
        print_log(e)
        return None
Esempio n. 10
0
 def client_DELETE(self):
     result = get_client_by_id_and_app(self.request.swagger_data)
     if not result:
         print_log(datetime.datetime.now(), 'DELETE', '/clients/' + str(id),
                   'Delete client', 'Failed')
         return self.createResponse(None, 400)
     Client.destroy(result)
     print_log(datetime.datetime.now(), 'DELETE', '/clients/' + str(id),
               'Delete client', 'Succeeded')
     return {}
Esempio n. 11
0
 def configurationkeys_GET_one(self):
     """ Find and return one configurationkey by id with GET method """
     app_id = self.request.swagger_data['appid']
     confkey_id = self.request.swagger_data['ckid']
     confkey = get_conf_key_by_appid_and_ckid(app_id, confkey_id)
     if confkey is None:
         print_log(datetime.datetime.now(), 'GET',\
             '/applications/%s/configurationkeys/' % app_id
             + str(confkey_id), 'Get one configurationkey', 'Failed')
         return self.createResponse(None, 400)
     return confkey.as_dict()
Esempio n. 12
0
    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()
Esempio n. 13
0
 def exclusionconstraints_GET_one(self):
     """ Find and return one ExclusionConstraint by id with GET method """
     app_id = self.request.swagger_data['appid']
     exconst_id = self.request.swagger_data['ecid']
     exconstraint = self.get_exclusionconstraint(app_id, exconst_id)
     if exconstraint is None:
         print_log(
             datetime.datetime.now(), 'GET',
             '/applications/%s/exclusionconstraints/' +
             str(exconst_id) % app_id, 'Get one exclusionconstraint',
             'Failed')
         return self.createResponse(None, 400)
     return exconstraint.as_dict()
Esempio n. 14
0
    def configurationkeys_PUT_one(self):
        """ Updates only the name of configurationkey"""
        app_id = self.request.swagger_data['appid']
        confkey_id = self.request.swagger_data['ckid']
        configkey_req = self.request.swagger_data['configurationkey']
        if self.is_valid_configurationkey(configkey_req):
            ConfigurationKey.update(configkey_req.id, "name", configkey_req.name)
            updated = ConfigurationKey.get(configkey_req.id)
            return updated.as_dict()

        print_log(datetime.datetime.now(), 'PUT', 'applications/%s/configurationkeys/%s' % (app_id, confkey_id),
                  'Update ConfigurationKey', 'Failed: Invalid Configurationkey')
        return self.createResponse('Bad Request: invalid ConfigurationKey', 400)
Esempio n. 15
0
 def configurationkeys_DELETE_one(self):
     """ Find and delete one configurationkey by id with delete method """
     confkey_id = self.request.swagger_data['ckid']
     app_id = self.request.swagger_data['appid']
     confkey = get_conf_key_by_appid_and_ckid(app_id, confkey_id)
     if not confkey:
         print_log(datetime.datetime.now(), 'DELETE', '/applications/%s/' % app_id +
             '/configurationkeys/%s/' % confkey_id,
              'Delete configurationkey', 'Failed')
         return self.createResponse(None, 400)
     ConfigurationKey.destroy(confkey)
     print_log(datetime.datetime.now(), 'DELETE', '/applications/%s' % app_id +
         '/configurationkeys/%s' % confkey_id, 'Delete configurationkey', 'Succeeded')
     return {}
Esempio n. 16
0
    def applications_GET_one(self):
        """ Find and return one application by id with GET method """
        app_id = self.request.swagger_data['id']
        app = Application.get(app_id)
        if app is None:
            print_log(datetime.datetime.now(), 'GET',
                      '/applications/' + str(app_id), 'Get one application',
                      None)
            return self.createResponse(None, 400)

        if app.apikey is None:
            app = self.set_app_apikey(app, app_id)

        return app.as_dict()
Esempio n. 17
0
 def applications_DELETE_one(self):
     """ Find and delete one application by id with delete method """
     app_id = self.request.swagger_data['id']
     app = Application.get(app_id)
     if not app:
         print_log(datetime.datetime.now(), 'DELETE',
                   '/applications/' + str(app_id), 'Delete application',
                   'Failed')
         return self.createResponse(None, 400)
     Application.destroy(app)
     print_log(datetime.datetime.now(), 'DELETE',
               '/applications/' + str(app_id), 'Delete application',
               'Succeeded')
     return {}
Esempio n. 18
0
    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
Esempio n. 19
0
    def applications_POST(self):
        """ Create new application with POST method """
        req_app = self.request.swagger_data['application']
        app = Application(
            name=req_app.name,
            apikey=self.get_unused_apikey(),
            experiment_distribution=req_app.experiment_distribution)
        if self.is_valid_application(req_app):
            Application.save(app)
            print_log(req_app.name, 'POST', '/applications',
                      'Create new application', app)
            return app.as_dict()

        print_log(req_app.name, 'POST', '/applications',
                  'Create new application', 'Failed: Invalid Application')
        return self.createResponse('Bad Request: invalid Application', 400)
Esempio n. 20
0
    def configurations_GET(self):
        client = get_client_by_id_and_app(self.request.swagger_data)
        client_id = self.request.swagger_data['clientid']
        app_id = self.request.swagger_data['appid']
        if client is None:
            print_log(
                datetime.datetime.now(), 'GET',
                '/applications/%s/clients/%s/configurations failed' %
                (app_id, client_id), 'List configurations for specific client',
                'Failed')
            return self.createResponse(None, 400)

        current_groups = client.experimentgroups
        configs = list(map(lambda _: _.configurations, current_groups))
        result = list(map(lambda _: _.as_dict(), list(concat(configs))))
        return result
Esempio n. 21
0
def get_client_configurations(client, application):
    expgroup = assign_to_experimentgroup(client, application)

    try:
        configs = list(map(lambda _: _.as_dict(), expgroup.configurations))
    except AttributeError as e:
        # In this case, experimentgroup is None, and has already been logged
        return None
    if len(configs) == 0:
        print_log(
            datetime.datetime.now(), 'POST', '/configurations',
            'Get client configurations',
            'Failed: No Configurations on ExperimentGroup with id %s' %
            expgroup.id)
        return None

    return configs
Esempio n. 22
0
    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))
Esempio n. 23
0
    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 {}
Esempio n. 24
0
    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))
Esempio n. 25
0
    def exclusionconstraints_DELETE_one(self):
        """ Find and delete one exclusionconstraint by id with DELETE method """
        app_id = self.request.swagger_data['appid']
        exconst_id = self.request.swagger_data['ecid']
        exconstraint = self.get_exclusionconstraint(app_id, exconst_id)

        logmessage_address = '/applications/%s/exclusionconstraints/%s'\
            % (app_id, exconst_id)

        if exconstraint is None:
            print_log(datetime.datetime.now(), 'DELETE', logmessage_address,
                      'Delete exclusionconstraint', 'Failed')
            return self.createResponse(None, 400)

        ExclusionConstraint.destroy(exconstraint)
        print_log(datetime.datetime.now(), 'DELETE', logmessage_address,
                  'Delete exclusionconstraint', 'Succeeded')
        return {}
Esempio n. 26
0
    def clients_GET(self):
        """
            Explanation: maps as_dict() -function to every client-object (this is returned by client.all())
            Creates a list and returns it. In future we might would like general json-serialization to make this even
            more simpler.
        """
        app_id = self.request.swagger_data['appid']

        if not Application.get(app_id):
            print_log('/applications/%s/clients failed' % app_id)
            return self.createResponse(None, 400)

        clients = Client.query()\
        .join(Client.experimentgroups)\
        .join(Experiment)\
        .join(Application)\
        .filter(Application.id == app_id)

        return list(map(lambda _: _.as_dict(), clients))
Esempio n. 27
0
    def applications_PUT(self):
        req_app = self.request.swagger_data['application']
        req_app_id = self.request.swagger_data['id']
        updated = Application.get(req_app_id)

        if req_app_id != req_app.id or updated is None or not self.is_valid_application(
                req_app):
            print_log(
                datetime.datetime.now(), 'PUT', '/applications/',
                'Update Application',
                'Failed: no such Application with id %s or ids didn\'t match' %
                req_app_id)
            return self.createResponse(None, 400)

        Application.update(updated.id, "name", req_app.name)
        Application.update(updated.id, "experiment_distribution",
                           req_app.experiment_distribution)
        updated = Application.get(updated.id)

        return updated.as_dict()
Esempio n. 28
0
def assign_to_experimentgroup(client, application):
    experiment = assign_to_experiment(client, application)

    try:
        expgroup = random.choice(list(experiment.experimentgroups))
    except AttributeError as e:
        # In this case, experiment is None, and has already been logged
        return None
    except IndexError as e:
        print_log(
            datetime.datetime.now(), 'POST', '/configurations',
            'Get client configurations',
            'Failed: No ExperimentGoups on Experiment with id %s' %
            experiment.id)
        return None

    if expgroup not in client.experimentgroups:
        client.experimentgroups.append(expgroup)
        Client.flush()

    return expgroup
Esempio n. 29
0
    def configurationkeys_POST(self):
        """
            Create new configurationkey to application.
            request.swagger_data['id'] takes the id and Application.get(app_id) returns the application by id.
        """
        app_id = self.request.swagger_data['id']
        application = Application.get(app_id)
        if application is None:
            print_log(datetime.datetime.now(), 'POST', '/applications/' + str(app_id) + '/configurationkeys',
                      'Create new configurationkey for application', 'Failed: No Application with id %s' % app_id)
            return self.createResponse({}, 400)
        new_confkey = self.request.swagger_data['configurationkey']
        name = new_confkey.name
        type = new_confkey.type.lower()
        configurationkey = ConfigurationKey(
            application=application,
            name=name,
            type=type
        )

        if self.is_valid_configurationkey(configurationkey):
            ConfigurationKey.save(configurationkey)
            print_log(datetime.datetime.now(), 'POST', '/applications/' + str(app_id) + '/configurationkeys',
                      'Create new configurationkey', 'Succeeded')
            return configurationkey.as_dict()

        print_log(datetime.datetime.now(), 'POST', '/applications/' + str(app_id) + '/configurationkeys',
                  'Create new configurationkey for application', 'Failed: Invalid ConfigurationKey')
        return self.createResponse({}, 400)
Esempio n. 30
0
    def experimentgroup_GET_one(self):
        """
            Show specific experiment group metadata
            Metadata includes ExperimentGroup's configurations, Clients and DataItems
        """
        app_id = self.request.swagger_data['appid']
        expid = self.request.swagger_data['expid']
        expgroupid = self.request.swagger_data['expgroupid']

        #expgroup = ExperimentGroup.get(expgroupid)
        expgroup = ExperimentGroup.query().join(Experiment, Application)\
            .filter(ExperimentGroup.id == expgroupid, Experiment.id == expid,\
                Application.id == app_id)\
            .one_or_none()

        if expgroup is None or expgroup.experiment.id != expid:
            print_log(
                datetime.datetime.now(), 'GET', '/experiments/' + str(expid) +
                '/experimentgroups/' + str(expgroupid),
                'Show specific experimentgroup metadata', None)
            return self.createResponse(None, 400)

        configurations = list(
            map(lambda _: _.as_dict(), expgroup.configurations))
        clients = list(map(lambda _: _.as_dict(), expgroup.clients))
        dataitems = list(map(lambda _: _.dataitems, expgroup.clients))
        dataitems_concat = list(concat(dataitems))
        dataitems_with_client = []
        for ditem in dataitems_concat:
            dataitem = ditem.as_dict()
            di_and_client = assoc(dataitem, 'client', ditem.client.as_dict())
            dataitems_with_client.append(di_and_client)
        experimentgroup = expgroup.as_dict()
        resultwithconf = assoc(experimentgroup, 'configurations',
                               configurations)
        resultwithclient = assoc(resultwithconf, 'clients', clients)
        result = assoc(resultwithclient, 'dataitems', dataitems_with_client)

        return result