Example #1
0
 def _query_product_name(self, task_vars):
     url = "{0}{1}{2}{3}".format('http://', settings.HOST_GET_BINDING,
                                 '/v1/products/', task_vars['product_key'])
     headers = {'Authorization': settings.INNER_API_TOKEN}
     result = {}
     try:
         response = requests.get(url, headers=headers)
         data = json.loads(response.content)
         result['common.product_name'] = data['name']
     except Exception, e:
         logger.exception(e)
Example #2
0
    def _query_data(self, task_vars):
        result = {}

        try:
            cache_la = get_redis_la()
            data = cache_la.get('dev_latest:{}'.format(task_vars['did']))
            if data:
                data = json.loads(data)
                result.update({
                    '.'.join(['data', k]): v
                    for k, v in data['attr'].items()
                })
        except Exception, e:
            logger.exception(e)
Example #3
0
def _query_data(task_vars):
    result = {}

    try:
        cache_la = get_redis_la()
        data = cache_la.get('dev_latest:{}'.format(task_vars['did']))
        if data:
            data = json.loads(data)
            for k, v in data['attr'].items():
                if task_vars.get('data.{}'.format(k)) is None:
                    # print 'k:', k, 'v:', v
                    result.update({'.'.join(['data', k]): v})
            # result.update({'.'.join(['data', k]): v for k, v in data['attr'].items()})
    except Exception, e:
        logger.exception(e)
Example #4
0
    def _query_device_data(self, task_vars, did):
        if did in task_vars:
            return task_vars[did]

        result = {}

        try:
            cache_la = get_redis_la()
            data = cache_la.get('dev_latest:{}'.format(did))
            if data:
                data = json.loads(data)
                result = data['attr']
                task_vars[did] = result
        except Exception, e:
            logger.exception(e)
Example #5
0
 def get_json(self, content, params, refresh, name, rule_id, now_ts):
     cache_key = 're_core_{0}_{1}'.format(rule_id, name)
     cache = redis.Redis(connection_pool=redis_pool)
     result = {}
     do_refresh = True
     try:
         if cache.get(cache_key + '_lock'):
             do_refresh = False
         else:
             is_success = cache.setnx(cache_key + '_lock',
                                      json.dumps(now_ts))
             if is_success:
                 cache.setex(cache_key + '_lock', json.dumps(now_ts),
                             refresh)
                 do_refresh = True
             else:
                 do_refresh = False
     except Exception, e:
         logger.exception(e)
Example #6
0
class JsonCore(BaseInnerCore):
    '''
    execute script task
    '''

    core_name = 'json'
    params = ['source', 'params', 'refresh', 'content', 'name']
    index = settings.INDEX['json']

    def _process(self, task_list, task_vars, custom_vars, para_task,
                 extern_params, rule_id):
        result = False
        while task_list:
            task = task_list.pop(0)
            if self.core_name != task[0]:
                task_list[0:0] = [task]
                result = True
                break

            extra_task = []
            query_list = []
            params = {}
            tmp_dict = {x: task[self.index[x]] for x in self.params}
            for symbol in tmp_dict['params']:
                if task_vars.has_key(symbol):
                    params[symbol] = task_vars[symbol]
                elif custom_vars.has_key(symbol):
                    extra_task.append(custom_vars[symbol])
                elif symbol.split('.')[0] in custom_vars:
                    alias = symbol.split('.')[0]
                    if alias in task_vars:
                        params[symbol] = get_value_from_json(
                            symbol, task_vars[alias])
                    else:
                        extra_task.append(custom_vars[alias])
                else:
                    query_list.append(symbol)

            if extra_task or query_list:
                task_list[0:0] = ([['que', 'q',
                                    list(set(query_list)), False]]
                                  if query_list else []) + extra_task + [task]
                result = True
                break

            task_vars[tmp_dict['name']] = self.get_json(
                tmp_dict['content'], params, int(tmp_dict['refresh']),
                tmp_dict['name'], rule_id, task_vars['sys.timestamp_ms'])
            result = True

        return result

    def get_json(self, content, params, refresh, name, rule_id, now_ts):
        cache_key = 're_core_{0}_{1}'.format(rule_id, name)
        cache = redis.Redis(connection_pool=redis_pool)
        result = {}
        do_refresh = True
        try:
            if cache.get(cache_key + '_lock'):
                do_refresh = False
            else:
                is_success = cache.setnx(cache_key + '_lock',
                                         json.dumps(now_ts))
                if is_success:
                    cache.setex(cache_key + '_lock', json.dumps(now_ts),
                                refresh)
                    do_refresh = True
                else:
                    do_refresh = False
        except Exception, e:
            logger.exception(e)

        if do_refresh:
            _content = json.dumps(content)
            for key, val in params.items():
                _content = _content.replace('"${' + key + '}"',
                                            json.dumps(val))
            content = json.loads(_content)
            url = content['url']
            headers = content.get('headers', {})
            data = content.get('data', {})
            method = content.get('method', 'get')
            if 'get' == method:
                query_string = '&'.join(
                    map(lambda x: '{0}={1}'.format(*x),
                        data.items())) if data else ''
                url = (url + '?' + query_string) if query_string else url
                response = requests.get(url, headers=headers)
            elif 'post' == method:
                response = requests.post(url,
                                         data=json.dumps(data),
                                         headers=headers)
            else:
                raise Exception('invalid method: {}'.format(method))

            result = json.loads(response.content)

            try:
                cache.set(cache_key, response.content)
            except Exception, e:
                logger.exception(e)
Example #7
0
                                         headers=headers)
            else:
                raise Exception('invalid method: {}'.format(method))

            result = json.loads(response.content)

            try:
                cache.set(cache_key, response.content)
            except Exception, e:
                logger.exception(e)
        else:
            try:
                result = cache.get(cache_key)
                result = json.loads(result)
            except Exception, e:
                logger.exception(e)

        return result


class QueryCore(BaseInnerCore):
    '''
    execute que task
    '''

    core_name = 'que'
    index = settings.INDEX['que']
    params = ['type', 'target', 'pass']

    def _process(self, task_list, task_vars, custom_vars, para_task,
                 extern_params, rule_id):