The Seven Year Itch.

MENU

redis 读取数据,并且对里面的时间进行排序读取。[第N次优化]

October 26, 2017 • Read: 520 • 数据库,Python,MAC,环境,Redis

def redis(start_time=0,end_time=0,fields=None):
    import redis
    import json

    client = redis.StrictRedis(host='192.168.31.49')
    addres =  fields.split(",")

    data = []

    for dd in addres:
        for key in client.keys(str("*")+str(dd)+str("*")):
            d = json.loads(key)
            at = int(d['timeStamp'])
            if int(start_time) < at <= int(end_time):
                tmp = client.get(key)
                if tmp is not None:
                    val = json.loads(tmp)
                    d['value'] = str(val['val'])
                    data.append(d)
        data.sort(key=lambda d: d["timeStamp"])

    result = dict()
    if addres is not None:
        try:
            for field in addres:
                da = {}
                da['val'] = []
                da['timeStamp'] = []
                da['device'] = ''
                da['description'] = None
                result[int(field)] = da
        except Exception,e:
            print e
    for d in data:
        # if d['address'] not in result:
        #     print d['address'],result
        #     continue
        try:

            addr = int(d['address'])
            value = d['value']
            timeStamp = int(d['timeStamp'])
            device = int(d['device'])
            description = response_redis_code(addr)
            result[addr]['val'].append(str(value))
            result[addr]['timeStamp'].append(str(timeStamp))
            result[addr]['device'] = device
            result[addr]['description'] = description
        except Exception,e:
            print e

    # 拼接数据,或者可以在 result 里直接填写 device 和 address 字段
    def join(item):
        item[1]['address'] = item[0]
        item[1]['device'] = device
        return item[1]


    # map 函数,或者直接使用列表推导式
    # return [join(item) for item in result.items()]
    return list(map(join, result.items()))

def redis(start_time=0,end_time=0,fields=None):
    import redis
    import json

    client = redis.StrictRedis(host='192.168.31.49')
    addres =  fields.split(",")

    data = []
    for dd in addres:
        for key in client.keys("*"+dd+"*"):
            d = json.loads(key)
            at = int(d['timeStamp'])
            if int(start_time) < at <= int(end_time):
                continue
            val = json.loads(client.get(key))
            d['value'] = val['val']
            data.append(d)
        data.sort(key=lambda d: d["timeStamp"])

    result = dict()
    if addres is not None:
        for field in addres:
            da = {}
            da['val'] = []
            da['timeStamp'] = []
            da['device'] = 0
            da['description'] = None
            result[int(field)] = da

    for d in data:
        if d['address'] not in result:
            print d['address'],result
            continue
        value = int(d['value'])
        timeStamp = int(d['timeStamp'])
        device = int(d['device'])
        description = response_redis_code(d['address'])
        result[d['address']]['val'].append(value)
        result[d['address']]['timeStamp'].append(timeStamp)
        result[d['address']]['device'] = device
        result[d['address']]['description'] = description


    # 拼接数据,或者可以在 result 里直接填写 device 和 address 字段
    def join(item):
        item[1]['address'] = item[0]
        item[1]['device'] = device
        return item[1]

    # map 函数,或者直接使用列表推导式
    # return [join(item) for item in result.items()]
    return list(map(join, result.items()))

import redis
import json

def redis(start_time=0,end_time=0):
    device = '1'
    client = redis.StrictRedis(host='192.168.31.49')

data = []
for key in client.keys():
    d = json.loads(key)
    at = int(d['timeStamp'])
    if at < start_time or at > end_time:
        continue
    val = json.loads(client.get(key))
    d['value'] = val['val']
    data.append(d)

data.sort(key=lambda d: d["timeStamp"])

result = {
   16407: {
       'description': u'UA相电压',
       'val': [],
       'timeStamp': [],
   },
   16410: {
       'description': u'UAB相电压',
       'val': [],
       'timeStamp': [],
   },
   16432: {
       'description': u'FR频率',
       'val': [],
       'timeStamp': [],
   }
}

for d in data:
    if d['address'] not in result or d['device'] != device:
        continue
    result[d['address']]['val'].append(d['val'])
    result[d['address']]['timeStamp'].append(d['timeStamp'])

# 拼接数据,或者可以在 result 里直接填写 device 和 address 字段
def join(item):
    item[1]['address'] = item[0]
    item[1]['device'] = device
    return item[1]

# map 函数,或者直接使用列表推导式
# return [join(item) for item in result.items()]
return list(map(join, result.items()))

下面的弃用

def redis(start_time=0,end_time=0):
    import redis
    import json
    pool = redis.ConnectionPool(host='192.168.31.49', port=6379, db=0)
    r = redis.Redis(connection_pool=pool)

    pipe = r.pipeline()
    pipe_size = 100000

    len = 0
    key_list = []
    arr_ts_16407 = []
    arr_val_16407 = []
    address_16407 = None
    address_16410 = None
    address_16432 = None
    arr_ts_16410 = []
    arr_val_16410 = []
    arr_ts_16432 = []
    arr_val_16432 = []
    description = []
    data = []
    for key in r.keys():
        tmp = json.loads(key)
        val = json.loads(r.get(key))
        tmp['value'] = val['val']
        data.append(tmp)

    for d in data:
        key_list.append(d)
    key_list.sort(key=lambda d: d["timeStamp"])
    for key in key_list:
        dec = key["address"]
        ts = key["timeStamp"]
        val = key["value"]
        if dec in [16407, 16410, 16432] and ts > start_time and ts <= end_time:
            if dec == 16407:
                arr_ts_16407.append(ts)
                arr_val_16407.append(val)
                address_16407 = 16407
            if dec == 16410:
                arr_ts_16410.append(ts)
                arr_val_16410.append(val)
                address_16410 = 16410
            if dec == 16432:
                arr_ts_16432.append(ts)
                arr_val_16432.append(val)
                address_16432 = 16432
    return [{"address": 16407, "timeStamp": arr_ts_16407,"val": arr_val_16407,'description':u"UA相电压","device":1},{"address": 16410, "timeStamp": arr_ts_16410,"val": arr_val_16410,'description':u"UAB相电压","device":1},{"address": 16432, "timeStamp": arr_ts_16432,"val": arr_val_16432,"description":u"FR频率","device":1}]

redis-读取数据

Tags: python, Redis
Archives QR Code
QR Code for this page
Tipping QR Code