#-*-coding:utf-8-*-
#20160418 通过分析日志文件,对比redis的数据,更新redis的数据
import os
import redis
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
r=None
def byRedis(groupId,msgId):
global r
if r==None:
r = redis.Redis(host='192.168.1.227',port=6382,db=0)
print "-------------------------------------->redis"
if not r.ping() :
print "cannot ping redis"
return
key= 'groupmsgcounter_{0}'.format(groupId)
oldmsgId=r.get( key )
if oldmsgId == None:
oldmsgId=0
#print "redis error:",key,oldmsgId,msgId
oldmsgId=long(oldmsgId)
#print "redis:",key,oldmsgId,msgId
if oldmsgId < msgId:
print "less:",key,oldmsgId,msgId
return
newmsgId=max(msgId,oldmsgId )
if newmsgId == 0 :
return
print newmsgId
#r.set(key,newmsgId)
'''
cli-groupmsgid:124 svr-groupmsgid:124
groupid:72057594037938371
'''
def parse1(lineData):
flag1='cli-groupmsgid:'
flag2='svr-groupmsgid:'
flag3='lpContext:'
flag4='groupid:'
flag5='grouprealid:'
s=lineData
n1=s.find(flag1)
n2=s.find(flag2)
n3=s.find(flag3)
n4=s.find(flag4)
n5=s.find(flag5)
# print lineData
a=s[n1+len(flag1):n2]
b=s[n2++len(flag2):n3]
c=s[n4+len(flag4):n5]
a=long(a)
b=long(b)
c=long (c)
return (c,max(a,b))
#print n1,n2,n3
def paseFile(fileName):
result=dict()
with open(fileName) as f:
lines=f.readlines()
for line in lines:
lineData=line.strip()
(groupId,msgId) = parse1(lineData)
msgId2=msgId
if groupId in result:
msgId2=max(result[groupId],msgId)
result[groupId]=msgId2
# break
return result
def testTwoDict( result1,result2):
for (d,x) in result1.items():
msgId=x
if d in result2:
if msgId < result2[d] :
msgId=result2[d]
#print "find:",d,x,msgId
byRedis(d,msgId)
def main():
result1=paseFile("test1.txt")
result2=paseFile("test2.txt")
result3=dict()
testTwoDict( result1,result2)
testTwoDict( result2,result1)
main()