testPlot.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_chcov(self):
  43. from refill import ChannelCovPainter
  44. start_time = int(time.time()) - 10 * 86400 - 3600
  45. end_time = int(time.time()) - 10 * 86400
  46. painter = ChannelCovPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6},filter_wave=3600)
  47. painter.paint()
  48. def test_mch_ratio_painter(self):
  49. from refill import MerchantCumRatioPainter
  50. start_time = int(time.time()) - 21 * 86400 - 3600
  51. end_time = int(time.time()) - 20 * 86400
  52. painter = MerchantCumRatioPainter(start_time=start_time, end_time=end_time, mchids=set(), card_types={4, 5, 6})
  53. painter.paint()
  54. def test_mch_covratio_painter(self):
  55. from refill import MerchantCovRatioPainter
  56. start_time = int(time.time()) - 21 * 86400 - 3600
  57. end_time = int(time.time()) - 20 * 86400
  58. painter = MerchantCovRatioPainter(start_time=start_time, end_time=end_time, mchids=set(), card_types={4, 5, 6})
  59. painter.paint()
  60. def test_mch_amount_painter(self):
  61. from refill import MerchantAmountPainter
  62. start_time = int(time.time()) - 20 * 86400 - 3600
  63. end_time = int(time.time()) - 20 * 86400
  64. painter = MerchantAmountPainter(start_time=start_time, end_time=end_time, mchids=set(), card_types={4, 5, 6})
  65. painter.paint()
  66. def testDays(self):
  67. from refill import MerchantReader
  68. from refill import ChannelReader
  69. try:
  70. chreader = ChannelReader()
  71. chdays = chreader.days()
  72. xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d)) for d in chdays]
  73. print(xlables)
  74. reader = MerchantReader()
  75. days = reader.days()
  76. except Exception as ex:
  77. log.error(ex)
  78. pass
  79. def test_jsonLoads(self):
  80. import json
  81. try:
  82. str = 4
  83. x = json.loads(str)
  84. print(x)
  85. except Exception as ex:
  86. print(ex)
  87. def test_partial(self):
  88. from functools import partial
  89. add_five = partial()
  90. def test_key(self):
  91. def person(name, age, *, city, job):
  92. print(name, age, city, job)
  93. person('Jack', 24, city='Beijing', job='Engineer')
  94. person('Jack', 24, 'Beijing', job='Engineer')
  95. person('Jack', 24, 'Beijing', 'Engineer')
  96. def test_none(self):
  97. x = None
  98. len = len(x)
  99. print(len)
  100. def test_set(self):
  101. x = set([(4, 50), (4, 100)])
  102. y = set([(4, 50), (5, 100)])
  103. z = x | y
  104. print(z)
  105. def test_env(self):
  106. import os
  107. x = os.getenv("PYCHARM_DISPLAY_PORT", "-1")
  108. print(x)
  109. def test_matplot(self):
  110. import matplotlib
  111. x = matplotlib.__version__
  112. print(x)
  113. def test_time(self):
  114. x = int(time.time())
  115. print(x)
  116. def test_rpop(self):
  117. import redis
  118. import json
  119. pool = redis.ConnectionPool(host=self.__redis_host, port=6379, db=0)
  120. r = redis.Redis(connection_pool=pool)
  121. item = r.rpop('REFILL_MONITOR_QUEUE')
  122. if item is None:
  123. print('hello')
  124. else:
  125. try:
  126. val = json.loads(item)
  127. method = val['method']
  128. params = val['params']
  129. print(method, params)
  130. except Exception as ex:
  131. log.error(ex)
  132. def test_consumer(self):
  133. from refill import WriterConsumer
  134. class PrintHandler:
  135. def write(self, method, msg):
  136. log.debug(msg)
  137. time.sleep(0.01)
  138. handler = PrintHandler()
  139. consumer = WriterConsumer(handler,'PrintHandler')
  140. consumer.start()
  141. for i in range(100000):
  142. consumer.put('test', f'index = {i}')
  143. consumer.quit()
  144. consumer.join()
  145. def test_merge(self):
  146. from collections import defaultdict
  147. import time as time
  148. def merge(l,r):
  149. for name,ls in l.items():
  150. if name in r:
  151. ls.extend(r[name])
  152. k = set(ls)
  153. r[name] = list(k)
  154. else:
  155. r[name] = ls
  156. return r
  157. all = defaultdict(list)
  158. a = {'yunchonggongfs': [(4, 100), (4, 30), (4, 50)]}
  159. b = {'yunchonggongfs': [(4, 100), (4, 200), (4, 50)]}
  160. all = merge(a,all)
  161. all = merge(b,all)
  162. x = 0;
  163. def test_split(self):
  164. def split_card(card_specs):
  165. result = dict()
  166. for card_type,spec in card_specs:
  167. if card_type not in result:
  168. result[card_type] = []
  169. result[card_type].append(spec)
  170. return result
  171. tups = [(4, 100), (4, 50) ,(5,10),(4, 30),(5,30),(6,100),(5,100),]
  172. x = split_card(tups)
  173. y = 1
  174. if __name__ == '__main__':
  175. unittest.main()