testPlot.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import time
  2. import unittest
  3. import logging
  4. from refill import time_border
  5. logging.basicConfig(filename='/var/www/html/data/log/qreader.log', level=logging.DEBUG)
  6. log = logging.getLogger('reader')
  7. class MyTestCase(unittest.TestCase):
  8. # __redis_host = '192.168.3.104'
  9. __redis_host = '192.168.3.46'
  10. def test_something(self):
  11. self.assertEqual(True, False) # add assertion here
  12. def test_listener(self):
  13. try:
  14. from refill import queueListener
  15. queueListener.set_redis(self.__redis_host, '6379')
  16. queueListener.prepare_data()
  17. except Exception as ex:
  18. log.error(ex)
  19. pass
  20. def test_timestart(self):
  21. stamp = int(time.time())
  22. for i in range(300):
  23. x = stamp + i
  24. l = time_border(300, x, True)
  25. r = time_border(300, x, False)
  26. print('l=', l, 'r=', r)
  27. def testChannel(self):
  28. from refill import ChannelReader
  29. reader = ChannelReader()
  30. days = reader.days()
  31. print('days=', days)
  32. for day in days:
  33. x = reader.tuple_path(day, card_types={4, 5, 6})
  34. x1 = reader.tuple_path(day, {'chizeng', 'ainika'}, {4, 5, 6})
  35. x2 = reader.tuple_path(day, {'chizeng', 'ainika'}, {4, 5, 6}, 50)
  36. def test_chpainter(self):
  37. from refill import ChannelCumPainter
  38. start_time = int(time.time()) - 10 * 86400 - 3600
  39. end_time = int(time.time()) - 8 * 86400
  40. painter = ChannelCumPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6})
  41. painter.paint()
  42. def test_ch_speed_analyze_painter(self):
  43. from refill import ChannelSpeedAnalyzePainter
  44. start_time = 1664632800
  45. end_time = 1664640000
  46. painter = ChannelSpeedAnalyzePainter(start_time=start_time, end_time=end_time, chnames=set(['feimingyunew']), card_types={4}, spec=100)
  47. painter.paint()
  48. def test_chcov_ratio(self):
  49. from refill import ChannelCovPainter
  50. start_time = int(time.time()) - 10 * 86400 - 3600
  51. end_time = int(time.time()) - 10 * 86400
  52. painter = ChannelCovPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6}, filter_wave=3600)
  53. painter.paint()
  54. def test_chcov_succ(self):
  55. from refill import ChannelCovSuccPainter
  56. start_time = int(time.time()) - 20 * 86400 - 3600
  57. end_time = int(time.time()) - 20 * 86400
  58. painter = ChannelCovSuccPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6}, filter_wave=3600)
  59. painter.paint()
  60. def test_mch_ratio_painter(self):
  61. from refill import MerchantCumRatioPainter
  62. start_time = int(time.time()) - 21 * 86400 - 3600
  63. end_time = int(time.time()) - 20 * 86400
  64. painter = MerchantCumRatioPainter(start_time=start_time, end_time=end_time, mchids=set(), card_types={4, 5, 6})
  65. painter.paint()
  66. def test_mch_covratio_painter(self):
  67. from refill import MerchantCovRatioPainter
  68. start_time = int(time.time()) - 21 * 86400 - 3600
  69. end_time = int(time.time()) - 20 * 86400
  70. painter = MerchantCovRatioPainter(start_time=start_time, end_time=end_time, mchids=set(), card_types={4, 5, 6})
  71. painter.paint()
  72. def test_mch_amount_painter(self):
  73. from refill import MerchantAmountPainter
  74. start_time = int(time.time()) - 20 * 86400 - 3600
  75. end_time = int(time.time()) - 20 * 86400
  76. painter = MerchantAmountPainter(start_time=start_time, end_time=end_time, mchids=set(), card_types={4, 5, 6})
  77. painter.paint()
  78. def test_ChannelWriter(self):
  79. from refill import ChannelWriter, open_hdf5
  80. import time
  81. hfive = open_hdf5('/var/www/html/data/stdata/channel.hdf5', True)
  82. chwriter = ChannelWriter(hfive)
  83. itema = {'channel_name': 'zero', 'time': int(time.time()), 'spec': 50, 'card_type': 4, 'channel_amount': 49, 'period': 30}
  84. itemb = {'channel_name': 'xxxxxx', 'time': int(time.time()), 'spec': 50, 'card_type': 4, 'channel_amount': 49, 'period': 30,
  85. 'mch_amount': 49.625}
  86. chwriter.write('ch_succ', itema)
  87. chwriter.write('ch_succ', itemb)
  88. def test_netcheck(self):
  89. from refill import NetchkReader
  90. import time
  91. from refill import day_stamp
  92. day = day_stamp(int(time.time()) - 4 * 86400)
  93. days = [day, day - 86400]
  94. net = NetchkReader()
  95. channels = net.tuple_path(day)
  96. channels = net.many_tuple_path(days)
  97. def test_net_painter(self):
  98. from refill import NetcheckCovPainter
  99. start_time = int(time.time()) - 5 * 86400
  100. end_time = int(time.time()) - 4 * 86400
  101. painter = NetcheckCovPainter(start_time=start_time, end_time=end_time)
  102. painter.paint()
  103. def testDays(self):
  104. from refill import MerchantReader
  105. from refill import ChannelReader
  106. try:
  107. chreader = ChannelReader()
  108. chdays = chreader.days()
  109. xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d)) for d in chdays]
  110. print(xlables)
  111. reader = MerchantReader()
  112. days = reader.days()
  113. except Exception as ex:
  114. log.error(ex)
  115. pass
  116. def test_jsonLoads(self):
  117. import json
  118. try:
  119. str = 4
  120. x = json.loads(str)
  121. print(x)
  122. except Exception as ex:
  123. print(ex)
  124. def test_partial(self):
  125. from functools import partial
  126. add_five = partial()
  127. def test_key(self):
  128. def person(name, age, *, city, job):
  129. print(name, age, city, job)
  130. person('Jack', 24, city='Beijing', job='Engineer')
  131. person('Jack', 24, 'Beijing', job='Engineer')
  132. person('Jack', 24, 'Beijing', 'Engineer')
  133. def test_none(self):
  134. x = None
  135. len = len(x)
  136. print(len)
  137. def test_set(self):
  138. x = set([(4, 50), (4, 100)])
  139. y = set([(4, 50), (5, 100)])
  140. z = x | y
  141. print(z)
  142. def test_env(self):
  143. import os
  144. x = os.getenv("PYCHARM_DISPLAY_PORT", "-1")
  145. print(x)
  146. def test_matplot(self):
  147. import matplotlib
  148. x = matplotlib.__version__
  149. print(x)
  150. def test_time(self):
  151. x = int(time.time())
  152. print(x)
  153. def test_rpop(self):
  154. import redis
  155. import json
  156. pool = redis.ConnectionPool(host=self.__redis_host, port=6379, db=0)
  157. r = redis.Redis(connection_pool=pool)
  158. item = r.rpop('REFILL_MONITOR_QUEUE')
  159. if item is None:
  160. print('hello')
  161. else:
  162. try:
  163. val = json.loads(item)
  164. method = val['method']
  165. params = val['params']
  166. print(method, params)
  167. except Exception as ex:
  168. log.error(ex)
  169. def test_consumer(self):
  170. from refill import WriterConsumer
  171. class PrintHandler:
  172. def write(self, method, msg):
  173. log.debug(msg)
  174. time.sleep(0.01)
  175. handler = PrintHandler()
  176. consumer = WriterConsumer(handler, 'PrintHandler')
  177. consumer.start()
  178. for i in range(100000):
  179. consumer.put('test', f'index = {i}')
  180. consumer.quit()
  181. consumer.join()
  182. def test_merge(self):
  183. from collections import defaultdict
  184. import time as time
  185. def merge(l, r):
  186. for name, ls in l.items():
  187. if name in r:
  188. ls.extend(r[name])
  189. k = set(ls)
  190. r[name] = list(k)
  191. else:
  192. r[name] = ls
  193. return r
  194. all = defaultdict(list)
  195. a = {'yunchonggongfs': [(4, 100), (4, 30), (4, 50)]}
  196. b = {'yunchonggongfs': [(4, 100), (4, 200), (4, 50)]}
  197. all = merge(a, all)
  198. all = merge(b, all)
  199. x = 0;
  200. def test_split(self):
  201. def split_card(card_specs):
  202. result = dict()
  203. for card_type, spec in card_specs:
  204. if card_type not in result:
  205. result[card_type] = []
  206. result[card_type].append(spec)
  207. return result
  208. tups = [(4, 100), (4, 50), (5, 10), (4, 30), (5, 30), (6, 100), (5, 100)]
  209. x = split_card(tups)
  210. y = 1
  211. def testFindLowest(self):
  212. # ratios = {'feimingyu_high': 5 / 215, 'lechong': 4 / 2108, 'lechong_high': 4 / 2223, 'weixue': 13 / 17101, 'yunchonggong': 7 / 23276,
  213. # 'yunchonggongx': 1 / 232760}
  214. # ratios = {'feimingyu_high': 0.05,'feimingyu_high': 5 / 215, 'lechong': 4 / 2108, 'lechong_high': 4 / 2223, 'weixue': 0.1, 'yunchonggong': 1 / 23276,'yunchonggongx': 1 / 232760}
  215. ratios = {'feimingyu_high': 0.3, 'lechong': 4 / 2108, 'lechong_high': 4 / 2223, 'weixue': 13 / 17101, 'yunchonggong': 1 / 23276}
  216. # ratios = {'lechong': 4 / 2108, 'lechong_high': 4 / 2223, 'weixue': 13 / 17101, 'yunchonggong': 1 / 23276}
  217. # ratios = {'feimingyu_high': 5 / 215, 'lechong': 4 / 2108, 'lechong_high': 4 / 2223, 'weixue': 13 / 17101, 'yunchonggong': 1 / 23276}
  218. # ratios = {'feimingyu_high': 0.33, 'lechong': 4 / 2108, 'lechong_high': 4 / 2223, 'weixue': 13 / 17101, 'yunchonggong': 1 / 23276}
  219. import math, cmath
  220. def classify_ratio(ratios):
  221. cls = {}
  222. indexer = []
  223. for name, ratio in ratios.items():
  224. index = round(math.log(ratio))
  225. log.debug(f'index={index} ratio={ratio}')
  226. if index not in cls:
  227. cls[index] = []
  228. indexer.append(index)
  229. cls[index].append(name)
  230. max_index = max(indexer)
  231. min_index = max_index - 5
  232. max_index = -7 if max_index < -7 else max_index
  233. min_index = -7 if min_index < -7 else min_index
  234. high = []
  235. low = []
  236. for index, names in cls.items():
  237. if index >= min_index:
  238. high.extend(names)
  239. else:
  240. low.extend(names)
  241. return high, low
  242. high, low = classify_ratio(ratios)
  243. log.debug(f'high={high},low={low}')
  244. if __name__ == '__main__':
  245. unittest.main()