专栏首页Python、Flask、Djangoredis 读取数据,并且对里面的时间进行排序读取。[第N次优化]

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

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}]

Tags: python, Redis

Archives QR Code

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Python 实现一个简单的上下文管理器

    简单、
  • 淘宝的npm镜像源【也可以别的方法】

    简单、
  • 备份博客数据的小脚本

    简单、
  • 关于App的启动顺序

    Dwyane
  • CSS3边框与圆角

    Leophen
  • 二等奖 DeepFake 假脸挑战赛 项目记录

    DeepFake技术是可以生成换脸的视频。这些内容生成和修改的技术可能会影响公共话语的质量或者侵犯他人的权力,尤其是考虑到这种伪造的视频可能被而已用来误导、操纵...

    机器学习炼丹术
  • PHP编程模拟病毒传播过程,告诉你为什么不能随意出门溜达?

    由于近期新冠状肺炎病毒的传播,我们看到上图中的病毒传播过程介绍。同时,为了让这个过程更加的直观,我们用计算机编程模拟了一个简单的模型来演示,通过视觉效果给大家做...

    云物缭绕
  • web.xml is missing and is set to true

    这时候需要右击项目——>Java EE Tools——>Generate Deployment Descriptor Stub.然后系统会在src/main/w...

    似水的流年
  • web.xml is missing and <failOnMissingWebXml> is set to true

    这时候需要右击项目——>Java EE Tools——>Generate Deployment Descriptor Stub.然后系统会在src/main/w...

    似水的流年
  • web.xml is missing and <failOnMissingWebXml> is set to true

    这时候需要右击项目——>Java EE Tools——>Generate Deployment Descriptor Stub.然后系统会在src/main...

    似水的流年

扫码关注云+社区

领取腾讯云代金券