-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathanimation.py
84 lines (79 loc) · 3.71 KB
/
animation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#coding=utf-8
import sys
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(abspath(__file__))))
import time
import datetime
from datetime import datetime
import _pickle
import const as ct
import pandas as pd
from cindex import CIndex
from cmysql import CMySQL
from base.clog import getLogger
from common import create_redis_obj
from combination import Combination
from combination_info import CombinationInfo
logger = getLogger(__name__)
class CAnimation:
def __init__(self, dbinfo = ct.DB_INFO, redis_host = None):
self.redis = create_redis_obj() if redis_host is None else create_redis_obj(redis_host)
self.mysql_client = CMySQL(dbinfo)
self.table = ct.ANIMATION_INFO
self.trigger = ct.SYNC_ANIMATION_2_REDIS
if not self.create(): raise Exception("create animation table %s table failed" % self.table)
if not self.register(): raise Exception("create animation trigger %s failed" % self.trigger)
def register(self):
sql = "create trigger %s after insert on %s for each row set @set=gman_do_background('%s', json_object('date', NEW.date, 'time', NEW.time, 'price', NEW.price, 'volume', NEW.volume, 'amount', NEW.amount, 'name', NEW.name));" % (self.trigger, self.table, self.trigger)
return True if self.trigger in self.mysql_client.get_all_triggers() else self.mysql_client.register(sql, self.trigger)
def create(self):
sql = 'create table if not exists %s(time varchar(10) not null, date varchar(10) not null, price float, volume float, amount float, name varchar(30) not null, PRIMARY KEY (date, time, name))' % self.table
return True if self.table in self.mysql_client.get_all_tables() else self.mysql_client.create(sql, self.table)
def get_combination_dict(self):
df = CombinationInfo().get()
cdict = dict()
for _index, code in df['code'].items():
cdict[code] = df.loc[_index]['name']
return cdict
def collect(self):
cdata = dict()
cdata['name'] = list()
cdata['price'] = list()
cdata['volume'] = list()
cdata['amount'] = list()
cdict = self.get_combination_dict()
if 0 == len(cdict): return False
for code in ct.INDEX_DICT:
df_byte = self.redis.get(CIndex.get_redis_name(CIndex.get_dbname(code)))
if df_byte is None: continue
df = _pickle.loads(df_byte)
price = float(df.last_price.tolist()[0])
p_volume = float(df.volume.tolist()[0])
p_amount = float(df.turnover.tolist()[0])
cdata['name'].append(ct.INDEX_DICT[code])
cdata['price'].append(price)
cdata['volume'].append(p_volume)
cdata['amount'].append(p_amount)
for code in cdict:
key = cdict[code]
df_byte = self.redis.get(Combination.get_redis_name(code))
if df_byte is None: continue
df = _pickle.loads(df_byte)
price = float(df.price.tolist()[0])
p_volume = float(df.volume.tolist()[0])
p_amount = float(df.amount.tolist()[0])
cdata['name'].append(key)
cdata['price'].append(price)
cdata['volume'].append(p_volume)
cdata['amount'].append(p_amount)
df = pd.DataFrame.from_dict(cdata)
df['time'] = datetime.fromtimestamp(time.time()).strftime('%H:%M:%S')
df['date'] = datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d')
self.mysql_client.set(df, self.table)
return True
if __name__ == '__main__':
ani = CAnimation(ct.DB_INFO)
ani.collect()
#ani.redis.srem('all_tables', ani.table)
#ani.redis.srem('all_triggers', ani.trigger)
#ani.mysql_client.exec_sql("drop table animation;")