Esempio n. 1
0
    def get(self, params, providers):
        log.info("params of getting metrics %s"%params)
        start = params['start']
        end = params['end']

        duration  =  end-start
        step = params.get('step', duration)
        step_count = int(duration/step) if duration else 1
        domain_points = 43+13*step_count
        step_points = domain_points//step_count 
        dt = duration/(step_points) 
        log.debug("step count: %s, step_points: %s, step: %s"%(
            step_count,
            step_points,
            step)
        )

        domain = [ start + step*i for i in range(step_count) ] + [end]
        log.debug('domain for metrics: %s'%domain)
        prov = providers[0]
        values = []
        for step_start, step_end in zip(domain[:-1],domain[1:]):
            step_values = [ get_datum(prov[t])*dt for t in np.linspace(step_start, step_end, step_points, endpoint=False) ]
            val =  sum(step_values).dict()
            val.update({"time":step_start})
            values.append(val)

        log.info('returning values %s'%values)
        return {'data':values,'name':'integral','domain':domain}
Esempio n. 2
0
def DMX(my_name, network_config, connectors=[]):
    log.info("DMX is listening named as:%s" % my_name)
    log.info("DMX connectors: %s" % connectors)
    source_addr = network_config.get_address(my_name)
    # But maybe use PUB-SUB for output ??
    ###> Prepare the zmq sockets
    ctx = zmq.Context()
    source = ctx.socket(zmq.PULL)
    log.debug("Binding DMX pull to %s" % source_addr)
    source.bind(source_addr)
    #-----
    #connectors  = ['console','telegram']
    conn_addr = [(conn, network_config.get_address(conn))
                 for conn in connectors]
    sockets = {}
    for name, addr in conn_addr:
        sock = ctx.socket(zmq.REQ)
        sock.connect(addr)
        if name == 'login':
            trig = 'auth'
        else:
            trig = ''
        sockets[name] = {'socket': sock, 'trig': trig}
    ###<
    while True:
        notif = source.recv_json()
        log.debug("DMX in: %s" % notif)
        for name, props in sockets.items():
            socket = props['socket']
            target = notif.get('action')
            if props['trig']:
                if not target: continue
                m = target.split('.')
                if not len(m) > 2: continue
                if m[2] != props['trig']:
                    continue
            log.debug("Sending notif to %s connector" % name)
            socket.send_json(notif)
        for name, props in sockets.items():
            socket = props['socket']
            target = notif.get('action')
            if props['trig']:
                if not target: continue
                m = target.split('.')
                if not len(m) > 2: continue
                if m[2] != props['trig']:
                    continue
            log.debug("getting answer %s connector" % name)
            answer = socket.recv_string()
            log.debug('dmx %s answ %s' % (name, answer))
Esempio n. 3
0
File: db.py Progetto: danlkv/GoalNet
 def register_user(self, pwd_hash, email):
     #TODO: what if user_id exists?
     # can an attacker overwrite credentials?
     existing_ = self.db_call({'request': 'get.user', 'email': email})
     if existing_:
         user_id = existing_['user_id']
         log.debug("User exists with id %s" % user_id)
         if pwd_hash != existing_['pwd_hash']:
             return user_id, None
         else:
             return user_id, gen_token(user_id)
     else:
         user_id = gen_user_id()
         token = gen_token(user_id)
         self.db_call({
             'request': 'new.user',
             'email': email,
             'user_id': user_id,
             'pwd_hash': pwd_hash,
             'token': token
         })
         return user_id, token
Esempio n. 4
0
    async def server_listen_loop(self):
        source = self.source
        timeout_ms = self.timeout_ms

        async with trio.open_nursery() as nursery:
            while True:
                # need to poll socket to prevent blocking
                # coroutine task queue when client is silent
                status = source.poll(timeout_ms)
                if status == 1:
                    # we've got message, receive it
                    msg = self._recv()
                else:
                    # let trio process some other handlers
                    await trio.sleep(0)
                    continue
                # use counter to trace responses
                conn_id = next(COUNTER)

                log.debug('Got message: %s id: %i' % (msg, conn_id))
                nursery.start_soon(self.connection_handler, msg, conn_id)
                # give control to some other tasks
                await trio.sleep(0)
Esempio n. 5
0
File: db.py Progetto: danlkv/GoalNet
    async def node_fun(self, message, drain):
        log.info('message: %s' % message)

        def response(notif):
            user_id = message.get('user_id')
            if user_id:
                notif.update({'user_id': user_id})
            return notif

        def handle_action(message, aciton, module, target):
            if module == 'test' or module == 'webext':
                if target == 'record':
                    res = self.statdata.record(message, action=action)
                    res.update({"action": module + ".record"})
                if target == 'metrics':
                    res = self.statdata.metric(message, action=action)
                    res.update({"action": module + ".metrics"})
                return res
            if module == 'user':
                if target == 'auth':
                    if action == 'add':
                        # get user identity
                        try:
                            pwd_hash = message['pwd_hash']
                            email = message['email']
                        except KeyError as e:
                            errmsg = 'no record found with name %s' % e
                            log.error(errmsg)
                            return {"error": errmsg}
                        log.info('new emaii %s' % (email))
                        # Issue an auth token
                        user_id, token = self.register_user(pwd_hash, email)
                        if not token:
                            return {"error": 'auth error'}
                        # generate a user id if no id provided
                        # return an action field to so the webserver knows to rememner
                        return {
                            "user_id": user_id,
                            "token": "%s" % token,
                            'action': message['action']
                        }
                if target == 'module':
                    if aciton == 'add':
                        user_id = message['user_id']
                        name = message['name']
                        self.add_module(user_id, name)
                        modls = self.get_modules(user_id)
                        return {"modules": modls, 'action': 'user.modules'}
                    if aciton == 'get':
                        user_id = message['user_id']
                        modls = self.get_modules(user_id)
                        return {"modules": modls, 'action': 'user.modules'}

            if action == 'get':
                return self.db.get_all()
            else:
                log.info("saving message to db %s" % message)
                self.db.put(message)
                return response({'status': 0, 'db_len': self.db.len()})

        ## Parse action
        action_path = get_action_path(message)
        if len(action_path) != 3:
            return response({'error': 'action path is invalid'})
        if action_path:
            action = action_path[0]
            module = action_path[1]
            target = action_path[2]
            resp = handle_action(message, action, module, target)
            log.debug('responding %s' % resp)
            return response(resp)

        return response({"error": 'No action field'})