예제 #1
0
def launch_app(client_id):
    # TODO refactor this. No DRY!!!
    user = request.session.user
    app = (App.query.filter_by(user_id=user.email,
                               client_id=client_id).first())
    if app is None:
        return NOT_FOUND
    if request.method == 'GET':
        # prompt user to select a patient to launch
        # TODO make this more readable
        patients = [{
            'id':
            pt.resource_id,
            'desc':
            json.loads(pt.data).get('text',
                                    {}).get('div', 'No description available')
        } for pt in Resource.query.filter_by(owner_id=user.email,
                                             resource_type='Patient').all()]
        if len(patients) > 0:
            return render_template('launch_context.html',
                                   cont_url=request.url,
                                   resources=json.dumps({'Patient': patients}))
        else:
            selected_pt = None
    else:
        selected_pt = request.form['Patient']

    ctx = Context()
    if selected_pt is not None:
        ctx.context = json.dumps({'Patient': selected_pt})
    db.session.add(ctx)
    db.session.commit()
    # launch!
    launch_args = {'launch': ctx.id, 'iss': get_api_base()}
    return redirect('%s?%s' % (app.launch_uri, urlencode(launch_args)))
예제 #2
0
def launch_app(client_id):
    # TODO refactor this. No DRY!!!
    user = request.session.user
    app = App.query.filter_by(user_id=user.email, client_id=client_id).first()
    if app is None:
        return NOT_FOUND
    if request.method == "GET":
        # prompt user to select a patient to launch
        # TODO make this more readable
        patients = [
            {"id": pt.resource_id, "desc": json.loads(pt.data).get("text", {}).get("div", "No description available")}
            for pt in Resource.query.filter_by(owner_id=user.email, resource_type="Patient").all()
        ]
        if len(patients) > 0:
            return render_template(
                "launch_context.html", cont_url=request.url, resources=json.dumps({"Patient": patients})
            )
        else:
            selected_pt = None
    else:
        selected_pt = request.form["Patient"]

    ctx = Context()
    if selected_pt is not None:
        ctx.context = json.dumps({"Patient": selected_pt})
    db.session.add(ctx)
    db.session.commit()
    # launch!
    launch_args = {"launch": ctx.id, "iss": get_api_base()}
    return redirect("%s?%s" % (app.launch_uri, urlencode(launch_args)))
예제 #3
0
    def __init__(self, data):
        """
        Initializes the base template.

        @param data - Dictionary representing template.

        """
        self.data = data
        self.ctx = Context()
예제 #4
0
파일: decorators.py 프로젝트: motord/banter
 def decorated(remark, retort, **kwargs):
     channel=remark['channel']
     user=remark['fromUser']
     q=Context.gql("WHERE channel = :1 AND user = :2", channel.key, user)
     c=q.get()
     if not c:
         c=Context(channel=channel.key, user=user)
         c.put()
     kwargs['context']=c
     return func(remark, retort, **kwargs)
예제 #5
0
 def post(self):
     name = self.request.get('name')
     if not name:
         errorMessage = 'name can not be empty'
         self.render('newContext.html', errorMessage=errorMessage)
     elif ' ' in name:
         errorMessage = 'No space allowed.'
         self.render('newContext.html', errorMessage=errorMessage)
     else:
         context = Context(name=name, user=self.user)
         context.put()
         self.redirect('/context/%s' % str(context.key().id()))
예제 #6
0
def get_context(file_path: str) -> Context:
    with open(file_path, 'r') as file:
        context_dict = json.load(file)

        context = Context(**context_dict)

        for i in range(len(context.containers)):
            container = context.containers[i] = Container(**context.containers[i])
            container.ports = Port(**container.ports)

        for i in range(len(context.networks)):
            context.networks[i] = Network(**context.networks[i])

        return context
예제 #7
0
    def show(self):
        print(self.__header)

        input_func = get_option_input()

        def get_input():
            selected_option = input_func('Enter option: ')
            if selected_option not in self.__next_menus.keys():
                raise UserInputOptionException
            return selected_option

        while True:
            username = input('Enter username: '******'Enter password: '******'status'] == 'success':
                menu_context = Context(context['user'], context['profile'])
                next_menu = MainMenu(self.__user_controller,
                                     self.__profile_controller,
                                     self.__post_controller)
                next_menu.show()
            elif context['status'] == 'failed':
                print('Login error!')
                print(self.__options)

                selected_option = self.input_secure_wrap(get_input)

                try:
                    self.__next_menus[selected_option](
                        self.__user_controller, self.__profile_controller,
                        self.__post_controller)
                except ExitFromMenuException:
                    return
예제 #8
0
 def __init__(self, user_controller, profile_controller, post_controller):
     self.__user_controller = user_controller
     self.__profile_controller = profile_controller
     self.__post_controller = post_controller
     self.__context = Context()
     self.__next_menus = {
         '1': lambda *_: raise_exception(ExitFromMenuException)
     }
예제 #9
0
파일: ui.py 프로젝트: FishLi1/FHIR-Genomics
def launch_app(client_id):
    # TODO refactor this. No DRY!!!
    user = request.session.user
    app = (App.query
            .filter_by(user_id=user.email, client_id=client_id)
            .first())
    if app is None:
        return NOT_FOUND
    if request.method == 'GET':
        # prompt user to select a patient to launch
        # TODO make this more readable
        patients = [{'id': pt.resource_id, 'desc': json.loads(pt.data).get('text', {}).get('div', 'No description available')}
                for pt in Resource
                .query
                .filter_by(
                    owner_id=user.email,
                    resource_type='Patient')
                .all()]
        if len(patients) > 0:
            return render_template(
                    'launch_context.html',
                    cont_url=request.url,
                    resources=json.dumps({'Patient': patients}))
        else:
            selected_pt = None
    else:
        selected_pt = request.form['Patient']

    ctx = Context()
    if selected_pt is not None:
        ctx.context = json.dumps({'Patient': selected_pt})
    db.session.add(ctx)
    db.session.commit()
    # launch!
    launch_args = {
        'launch': ctx.id,
        'iss': get_api_base()
    }
    return redirect('%s?%s'% (
        app.launch_uri,
        urlencode(launch_args)))
예제 #10
0
 def __init__(self, user_controller, profile_controller, post_controller):
     self.__user_controller = user_controller
     self.__profile_controller = profile_controller
     self.__post_controller = post_controller
     self.__context = Context()
     self.__next_menus = {
         '1': self.edit_first_name,
         '2': self.edit_second_name,
         '3': self.edit_last_name,
         '4': self.edit_age,
         '5': lambda *_: raise_exception(ExitFromMenuException)
     }
예제 #11
0
    def consume(self, entity):
        """
        Templates a dashboard if the entity is usuable.

        @param entity - Entity instance

        """
        # Make copy. This template can produce multiple documents.
        doc = copy.deepcopy(self.data)

        self.ctx = Context()

        # Iterate over rows
        for r in doc['dashboard'].get('rows', []):
            self.row(r, entity)

        if not self.ctx.current_entity:
            return

        # Apply context to ids
        id_ = doc['dashboard'].get('id')
        if id_:
            id_ = self.substitute(id_, self.ctx).replace(' ', '-').lower()
            doc['dashboard']['id'] = id_
            doc['_id'] = id_

        # Add entity label to tags
        tags = doc.get('tags', [])
        tags.append(self.ctx.entity_label())
        doc['tags'] = list(set(tags))

        # Apply context to title
        title = doc['dashboard'].get('title')
        if title:
            title = self.substitute(title, self.ctx)
            doc['dashboard']['title'] = title
            doc['title'] = title

        self.docs.append(doc)
예제 #12
0
def create_context():
    '''
    "inject" launch id into an OAuth2 auth request
    '''
    if request.method == 'GET':
        requested_scope = json.loads(
            request.args['auth_req'])['scope'].split(' ')
        # find types of resources requested by application to launch with
        resource_types = [
            scope.rsplit('/', 1)[1] for scope in requested_scope
            if scope.startswith('launch/')
        ]
        resources = (Resource.query.filter(
            Resource.owner_id == request.session.user_id,
            Resource.resource_type.in_(resource_types)).all())
        resources_by_types = {}
        for res in resources:
            content = json.loads(res.data)
            description = content['text'].get('div',
                                              'No description available')
            resources_by_types.setdefault(res.resource_type, []).append({
                'id':
                res.resource_id,
                'desc':
                description
            })
        if len(resources_by_types) > 0:
            # prompt user to select resources
            return render_template('launch_context.html',
                                   cont_url=request.url,
                                   resources=json.dumps(resources_by_types))
        else:
            selected = {}
    else:
        selected = request.form
    # create launch context with selected resources
    ctx = Context(context=json.dumps(selected))
    db.session.add(ctx)
    db.session.commit()
    # inject newly created context id
    auth_req_args = json.loads(request.args['auth_req'])
    scope = auth_req_args['scope'].split(' ')
    scope.append('launch:%s' % ctx.id)
    auth_req_args['scope'] = ' '.join(scope)
    return redirect('%s?%s' %
                    (url_for('auth.authorize'), urlencode(auth_req_args)))
예제 #13
0
def main(datadir,courses,users):

    COURSES = False
    USERS = False

    if not courses and not users:
        COURSES = True
        USERS = True
    else:
        if courses:
            COURSES = True
        if users:
            USERS = True

    preflight(datadir,COURSES,USERS)

    try:
        dictConfig(Config.logging)
        logger = logging.getLogger('main')
    except KeyError:
        print("Invalid configuration in Config.py: logging missing")
        sys.exit()

    logger.debug("datadir: " + datadir + ", COURSES: " + str(COURSES) + ", USERS: " + str(USERS))

    try:
        if Config.collab['verify_certs'] == 'True':
            VERIFY_CERTS = True
        else:
            VERIFY_CERTS = False
    except KeyError:
        errMsg = "Invalid configuration in Config.py: collab.verify_certs missing."
        logger.critical(errMsg)
        sys.exit()
    
    logger.info("Starting bulk creation process")

    try:
        authorized_session = AuthController.AuthController(Config.collab['collab_base_url'],Config.collab['collab_key'],Config.collab['collab_secret'],VERIFY_CERTS)
        authorized_session.setToken()
    except ValueError:
        errMsg = "Invalid configuration in Config.py: collab settings missing or incomplete"
        logger.critical(errMsg)
        sys.exit()

    ctxDict = {}
    usrDict = {}
    sesDict = {}

    try:
        ctxCtrl = ContextController.ContextController(Config.collab['collab_base_url'], authorized_session, VERIFY_CERTS)
        usrCtrl = UserController.UserController(Config.collab['collab_base_url'], authorized_session, VERIFY_CERTS)
        sesCtrl = SessionController.SessionController(Config.collab['collab_base_url'], authorized_session, VERIFY_CERTS)
    except KeyError:
        errMsg = "Invalid configuration in Config.py: collab settings missing or incomplete"
        logger.critical(errMsg)
        sys.exit()

    try:
        session_config = Config.session_settings
    except KeyError:
        errMsg = "Invalid configuration in Config.py: session settings missing"
        logger.critical(errMsg)
        sys.exit()

    try:
        teachercsv = open('output/teacherenrollments.csv', 'w', newline='')
        teacherwriter = csv.writer(teachercsv, delimiter=',', quotechar='"')
        teacherwriter.writerow([ 'TEACHERID', 'TEACHEREMAIL', 'COURSENAME', 'URL'])

        studentcsv = open('output/studentenrollments.csv', 'w', newline='')
        studentwriter = csv.writer(studentcsv, delimiter=',', quotechar='"')
        studentwriter.writerow([ 'COURSEID', 'TEACHERID', 'TEACHERNAME', 'STUDENTID', 'STUDENTNAME', 'STUDENTEMAIL', 'COURSENAME', 'URL'])

    except Exception as e:
        errMsg = "Error opening output file: " + str(e)
        logger.critical(errMsg)
        sys.exit()

    if COURSES:
        with open(datadir + '/course.csv', newline='') as csvfile:
                courses = csv.reader(csvfile, delimiter=',', quotechar='"')

                next(courses)

                for course in courses:

                    crsId = course[0]
                    crsName = course[1]
                    insId = course[2]
                    insName = course[3]
                    insEmail = course[4]

                    if insEmail is None or insEmail == "":
                        logger.error("Instructor <" + insName + "> email address is blank")
                        continue
                    elif validate_email(insEmail) == False:
                        logger.error("Instructor <" + insName + "> does not have a valid email address: <" + insEmail + ">")
                        continue

                    logger.debug(crsId + ',' + crsName + ',' + insId + ',' + insName + ',' + insEmail)
                    
                    try:
                        ctxId = ctxCtrl.getContext(crsName)

                        if ctxId['contextId'] is None:
                            ctx = Context.Context(crsName,crsName,crsName,crsId)
                            ctxres = ctxCtrl.createContext(ctx.getContextJson())

                            for k in ctxres:
                                result = k
                                break

                            if result == '200':
                                ctxId = { 'contextId' :  ctxres[result]}
                            else:
                                logger.error("Error creating context for course " + crsName + ", " + result + ": " + ctxres[result])
                                continue

                        logger.debug(ctxId['contextId'])

                        ctxDict[crsId] = {
                            "contextId" : ctxId['contextId'],
                            "teacherId" : insId
                        }
                    except Exception as e:
                        logger.error("Error creating Context: " + str(e))
                        continue

                    try:
                        ses = Session.Session(crsName, crsName, str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), None, session_config)
                        sesres = sesCtrl.createSession(ses.getSessionJson())

                        logger.debug(ses.getSessionJson())

                        for k in sesres:
                            result = k
                            break

                        if result == '200':
                            ses.setId(sesres[result])
                            logger.info("Session: " + ses.getId() + ", CREATED, for course " + crsName)
                        else:
                            logger.error("Error creating session for course " + crsName + ", " + result + ": " + sesres[result])
                            continue
                        
                        sesDict[crsId] = ses
                    except Exception as e:
                        logger.error("Error creating Session: " + str(e))
                        continue

                    try:
                        if ctxCtrl.assignSessionToContext(ctxId['contextId'],ses.getId()) == False:
                            logger.error("Error assigning session to context for course " + crsName + ", session: " + ses.getId() + ", context: " + ctxId['contextId'])
                            continue
                    except Exception as e:
                        logger.error("Error assigning session to Context: " + str(e))
                        continue

                    try:
                        teacher = User.User(insName,insId,insEmail)

                        teacherres = usrCtrl.createUser(teacher.getUserJson())

                        for k in teacherres:
                            result = k
                            break

                        if result == '200':
                            teacher.setId(teacherres[result])
                        else:
                            logger.error("Error creating user " + insName + " for course " + crsName + ", " + result + ": " + teacherres[result])
                            continue

                        logger.debug(teacher.getUserJson())
                        usrDict[insId] = teacher
                    except Exception as e:
                        logger.error("Error creating user " + insName + " for course " + crsName + ": " + str(e))
                        continue

                    try:
                        urlres = sesCtrl.enrollUser(ses.getId(),teacher.getId(),'moderator')

                        for k in urlres:
                            result = k
                            break

                        if result == '200':
                            teacherUrl = urlres[result]
                        else:
                            logger.error("Error creating enrollment " + insName + " for course " + crsName + ", " + result + ": " + urlres[result])
                            continue
                    except Exception as e:
                        logger.error("Error creating enrollment " + insName + " for course " + crsName + ": " + str(e))
                        continue
                                    
                    try:
                        teacherwriter.writerow([ teacher.getExtId(), teacher.getEmail(), ses.getName(), teacherUrl])

                        logger.info("Session link: " + teacherUrl + " for User: "******" CREATED for course " + crsName)
                    except KeyError as ke:
                        logger.error("Error writing teacher enrollment information: " + str(ke))
                        continue

    if USERS:
        with open(datadir + '/student.csv', newline='') as csvfile:
                users = csv.reader(csvfile, delimiter=',', quotechar='|')

                next(users)

                for user in users:
                    logger.debug('Students: ' + str(user))

                    studentId = user[0]
                    studentName = user[1]
                    studentEmail = user[2]

                    if studentEmail is None or studentEmail == "":
                        logger.error("Student <" + studentName + "'s> is blank")
                        continue
                    elif validate_email(studentEmail) == False:
                        logger.error("Student <" + studentName + "> does not have a valid email address: <" + studentEmail + ">")
                        continue

                    try:
                        student = User.User(studentName,studentId,studentEmail)

                        studentres = usrCtrl.createUser(student.getUserJson())

                        for k in studentres:
                            result = k
                            break

                        if result == '200':
                            student.setId(studentres[result])
                            logger.info("Student: " + student.getId() + ", CREATED, for user " + student.getDisplayName())
                        else:
                            logger.error("Error creating user " + studentName + ", " + result + ": " + studentres[result])
                            continue

                        logger.debug(student.getUserJson())
                        usrDict[studentId] = student
                    except Exception as e:
                        logger.error("Error creating student " + studentName + ": " + str(e))
                        continue

        with open(datadir + '/enrollment.csv', newline='') as csvfile:
                enrollments = csv.reader(csvfile, delimiter=',', quotechar='|')

                next(enrollments)

                for enrollment in enrollments:
                    logger.debug('Enrollments: ' + str(enrollment))

                    courseId = enrollment[0]
                    studentId = enrollment[1]

                    try:
                        session = sesDict[courseId]
                    except KeyError:
                        logger.error("courseId " + courseId + " not found")
                        continue

                    try:
                        user = usrDict[studentId]
                    except KeyError:
                        logger.error("studentId " + studentId + " not found")
                        continue

                    try:
                        urlres = sesCtrl.enrollUser(session.getId(),user.getId(),'participant')

                        for k in urlres:
                            result = k
                            break

                        if result == '200':
                            studentUrl = urlres[result]
                        else:
                            logger.error("Error creating enrollment " + user.getDisplayName() + " for course " + session.getName() + ", " + result + ": " + urlres[result])
                            continue
                    except Exception as e:
                        logger.error("Error creating enrollment " + user.getDisplayName() + " for course " + session.getName() + ": " + str(e))
                        continue
              
                    try:
                        studentwriter.writerow([ courseId, ctxDict[courseId]['teacherId'], usrDict[ctxDict[courseId]['teacherId']].getDisplayName(), user.getExtId(), user.getDisplayName(), user.getEmail(), session.getName(), studentUrl])

                        logger.info("Session link: " + studentUrl + " for User: "******" CREATED for course " + session.getName())
                    except KeyError as ke:
                        logger.error("Error writing student enrollment information: " + str(ke))
                        continue

    logger.info("Bulk creation processing complete")
예제 #14
0
class SingleEntityTemplate(BaseTemplate):
    """
    Dashboard template for a single entity dashboard.
    Will produce one dashboard doc per entity that contains
    the required metrics.

    """
    def __init__(self, data):
        """
        Inits the SingleEntityTemplate.

        @param data - Dictionary definining the template

        """
        super(SingleEntityTemplate, self).__init__(data)
        self.docs = []

    def es_ops(self):
        """
        Produces the operations needed to store the dashboards created by this
        template in elastic search.

        @returns - List of dictionaries

        """
        ops = []
        for d in self.docs:
            d.update({
                "_op_type": "index",
                "_index": "grafana-dash",
                "_type": "dashboard",
                "dashboard": json.dumps(d['dashboard'])
            })
            ops.append(d)
        return ops

    def consume(self, entity):
        """
        Templates a dashboard if the entity is usuable.

        @param entity - Entity instance

        """
        # Make copy. This template can produce multiple documents.
        doc = copy.deepcopy(self.data)

        self.ctx = Context()

        # Iterate over rows
        for r in doc['dashboard'].get('rows', []):
            self.row(r, entity)

        if not self.ctx.current_entity:
            return

        # Apply context to ids
        id_ = doc['dashboard'].get('id')
        if id_:
            id_ = self.substitute(id_, self.ctx).replace(' ', '-').lower()
            doc['dashboard']['id'] = id_
            doc['_id'] = id_

        # Add entity label to tags
        tags = doc.get('tags', [])
        tags.append(self.ctx.entity_label())
        doc['tags'] = list(set(tags))

        # Apply context to title
        title = doc['dashboard'].get('title')
        if title:
            title = self.substitute(title, self.ctx)
            doc['dashboard']['title'] = title
            doc['title'] = title

        self.docs.append(doc)
예제 #15
0
class BaseTemplate(object):
    """
    Base Template class. Actual templates should extend this class.

    """
    def __init__(self, data):
        """
        Initializes the base template.

        @param data - Dictionary representing template.

        """
        self.data = data
        self.ctx = Context()

    def substitute(self, _str, ctx):
        """
        Treats _str as a template and does a safe substitute passing
        ctx as the mapping.

        @param _str - String template
        @param ctx - Dictionary or dictionary like object
            containing the mapping.
        @returns - String

        """
        if isinstance(_str, str) or isinstance(_str, unicode):
            _str = Template(_str).safe_substitute(ctx)
        return _str

    def add_metric(self, panel, metric, ctx=None, target=None):
        """
        Adds a metric to the panel.

        @param panel - Dictionary panel representation
        @param metric - Dictionary metric representation
        @param ctx - Dictionary or dictionary like object
            containing the mapping. If none, self.ctx will be used
        @param target - Optional target. If none, the metric target
            will be used

        """
        panel_type = panel.get('type')

        if target is None:
            target = metric['target']
        if ctx is None:
            ctx = self.ctx

        obj = {'target': self.substitute(target, ctx)}

        # Add to graph
        if panel_type == 'graph':
            panel['targets'].append(obj)

        # Else add to warnings
        elif panel_type == 'warning':
            obj['op'] = metric.get('op', '')
            obj['threshold'] = metric.get('threshold')
            obj['id'] = panel.get('counter', 0)
            panel['counter'] = panel.get('counter', 0) + 1
            panel['warnings'].append(obj)

    def finalize(self, doc):
        """
        Performs a few post templating takes.
        Removes the following:
            - type field of root level document
            - removes the metric fields from graph level documents.
        Performs the final templating of complex multi entity metrics.
        @returns - Dictionary

        """
        # Remove type
        if 'type' in doc:
            del doc['type']

        # Remove required metrics
        if 'required-metrics' in doc:
            del doc['required-metrics']

        # Remove metric specification
        for r in doc['dashboard'].get('rows', []):
            for p in r.get('panels', []):
                if 'metrics' in p:
                    for m in p['metrics']:
                        self.post_template_metric(p, m)
                    del p['metrics']
        return doc

    def row(self, row, entity):
        """
        Handles templating of the row. Mainly iterates over all panels
        in the row.

        @param row - Dictionary with row representation.
        @param entity - Entity model instance

        """
        # Iterate over panels
        for p in row.get('panels', []):
            self.panel(p, entity)

    def panel_text(self, panel, entity):
        """
        Handles templating of a text type panel.
        Does nothing for now but may be extended by subclasses.

        @param panel - Dictionary containing panel representation.
        @param entity - Entity model instance

        """
        pass

    def panel_graph(self, panel, entity):
        """
        Handles templating of a graph type panel.
        Ensures the graph has a target field that is a list.
        Iterates over the metrics specification and adds targets
        for the metrics that the entity has.

        @param panel - Dictionary panel representation.
        @param entity - Entity model instance.

        """
        # Init targets to list if not set
        if panel.get('metrics', []) and 'targets' not in panel:
            panel['targets'] = []

        # Iterate over metrics
        for m in panel.get('metrics', []):
            self.metric(panel, m, entity)

    def panel_warning(self, panel, entity):
        """
        Handles templating of a warning type panel.

        @param panel - Dictionary panel representation
        @param entity - Entity model instance

        """
        # Init warnings to empty list if not set
        if panel.get('metrics', []) and 'warnings' not in panel:
            panel['warnings'] = []

        # Iterate over metrics
        for m in panel.get('metrics', []):
            self.metric(panel, m, entity)

    def panel(self, panel, entity):
        """
        Handles templating of a panel.
        Mainly determines the type of the panel and delegates.

        @param panel - Dictionary representation of the panel
        @param entity - Entity model instance

        """
        _type = panel.get('type')
        if _type == 'text':
            self.panel_text(panel, entity)
        elif _type == 'graph':
            self.panel_graph(panel, entity)
        elif _type == 'warning':
            self.panel_warning(panel, entity)

    def metric(self, panel, metric, entity):
        """
        Branches on the type field of a metric dictionary.

        @param panel - Dictionary representation of the panel.
        @param metric - Dictionary representation of the metric.
        @param entity - Entity model instance.

        """
        if metric['type'] == "simple":
            self.simple_metric(panel, metric, entity)
        elif metric['type'] == 'complex' and metric.get('multi-entity'):
            self.complex_metric_multi(panel, metric, entity)
        elif metric['type'] == 'complex':
            self.complex_metric(panel, metric, entity)

    def complex_metric(self, panel, metric, entity):
        """
        Complex metrics involve multiple metrics.
        An example is one metric as a percentage of another metrics.
        The idea is to temporarily store each individual target by a name
        that can then be used when creating the final complex target.

        @param panel - Dictionary representation of the panel
        @param metric - Dictionary representation of the metric
        @param entity - Entity model instance.

        """
        # Template the named targets
        named_targets = {}
        for name, m in metric['named-metrics'].iteritems():
            query = Query(m['query'])
            result = query.query(entity)
            if not result:
                return
            for entity, check, metric_obj in result:
                self.ctx.update(entity=entity, check=check, metric=metric_obj)
                if name not in named_targets:
                    named_targets[name] = []
                named_targets[name] \
                    .append(self.substitute(m['target'], self.ctx))

        for name, targets in named_targets.iteritems():
            named_targets[name] = ','.join(targets)

        # Apply named targets to the final target
        target = self.substitute(metric['target'], named_targets)

        # Apply the context to the final target
        self.add_metric(panel, metric, target=target)

    def complex_metric_multi(self, panel, metric, entity):
        """
        Complex multi entity metrics involve similar metrics on multiple
        entities.
        An example would be taking the average response time of an api on
        all entities that serve the api.

        The targets of each entity are stored in a targets field for each
        named metric. These can then be referenced later to create the final
        complex target in a final pass of the document.

        @param panel - Dictionary representation of the panel
        @param metric - Dictionary representation of the metric.
        @param entity - Entity model instance.

        """
        # Iterate over named targets. Only add to aggregation if
        # all named targets are present
        named_targets = {}
        for name, m in metric['named-metrics'].iteritems():
            query = Query(m['query'])
            result = query.query(entity)
            if not result:
                return
            for entity, check, metric_obj in result:
                self.ctx.update(entity=entity, check=check, metric=metric_obj)
                if name not in named_targets:
                    named_targets[name] = []
                named_targets[name] \
                    .append(self.substitute(m['target'], self.ctx))

        # We have a target for each query - save them
        for name, targets in named_targets.iteritems():
            if not metric['named-metrics'][name].get('targets'):
                metric['named-metrics'][name]['targets'] = []
            metric['named-metrics'][name]['targets'] += targets

    def simple_metric(self, panel, metric, entity):
        """
        Generates a simple target for a simple metrics.

        @param panel - Dictionary representation of a panel
        @param metric - Dictionary representation of a metric
        @param entity - Entity model instance

        """
        query = Query(metric['query'])
        result = query.query(entity)
        if not result:
            return
        for entity, check, metric_obj in result:
            self.ctx.update(entity=entity, check=check, metric=metric_obj)
            self.add_metric(panel, metric)

    def post_template_metric(self, panel, metric):
        """
        Templates final results of multi entity metrics.
        Such as in averages.

        @param panel - Dictionary representation of a panel
        @param metric - Dictionary representation of a metric

        """
        if metric['type'] == 'complex' and metric.get('multi-entity'):
            named_ctx = {}
            for name, m in metric['named-metrics'].iteritems():
                # Check for empty aggregated targets. Return if even one
                # is empty
                if not m.get('targets'):
                    return
                named_ctx[name] = ','.join(m['targets'])
            self.add_metric(panel, metric, ctx=named_ctx)
예제 #16
0
 def __init__(self, user_controller, profile_controller, post_controller):
     self.__user_controller = user_controller
     self.__profile_controller = profile_controller
     self.__post_controller = post_controller
     self.__context = Context()
예제 #17
0
def storeContext(chat_id, from_user, context, msg=None, action=None):
    c = Context(chat_id, from_user, context, msg=msg, action=action)
    db.session.add(c)
    db.session.commit()
예제 #18
0
파일: data.py 프로젝트: Iydon/homework
def drop_data(file: str):
    '''Drop data from `file`.

    Arguments:
    ==========
        file: String

    Returns:
    ========
        None
    '''
    os.remove(file)


# Cache data
if os.path.exists(data_file):
    context, cities, airline, t = read_data(data_file)
else:
    context = Context()
    t = Time()
    cities = [City(city_capacity()) for i in range(city_number)]

    for city in cities:
        city.init_airport(airport_capacity, aircraft_capacity, aircraft_speed,
                          aircraft_capacity_ratio, people_number_ratio)

    airline = Airline(cities, time_limit, distance_to_weights)

    write_data(data_file, context, cities, airline, t)
예제 #19
0
 def __init__(self, parent, metadata):
     self.context = Context(metadata)
     self.indent = None
예제 #20
0
class ContextParser(Parser):
    def __init__(self, parent, metadata):
        self.context = Context(metadata)
        self.indent = None
    
    def parse_metadata(self, metadata, stack):
        if self.indent is None:
            self.indent = metadata.indent

        if metadata.indent < self.indent:
            self.close(metadata, stack)
            return

        match = regex_jump.match(metadata.line)
        if match:
            label = match["label"]
            jump = Jump(label, metadata)
            self.context.add_child(jump)
            return
        
        match = regex_call.match(metadata.line)
        if match:
            label = match["label"]
            call = Call(label, metadata)
            self.context.add_child(call)
            return
        
        match = regex_script.match(metadata.line)
        if match:
            script_code = match["script"]
            script = Script(script_code, metadata)
            self.context.add_child(script)
            return
        
        match = regex_condition.match(metadata.line)
        if match:
            parser = ConditionParser(self, metadata)
            stack.push(parser)
            stack.parse_metadata(metadata)
            return
        
        match = regex_menu.match(metadata.line)
        if match:
            parser = MenuParser(self, metadata)
            stack.push(parser)
            return
        
        match = regex_python_block.match(metadata.line)
        if match:
            parser = PythonScriptParser(self, metadata)
            stack.push(parser)
            return
        
        self.context.add_child(Text(metadata.line, metadata))

    def close(self, metadata, stack):
        stack.pop()
        stack.parse_metadata(metadata)
    
    def on_child_pop(self, child):
        self.context.add_child(child.model)

    @property
    def model(self):
        return self.context
예제 #21
0
def get_context(path: str) -> Context:
    with open(path, 'r') as file:
        context = Context(**json.load(file))
        context.sessions = [Session(**session) for session in context.sessions]
        return context