app.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """General function module"""
  2. import random
  3. from datetime import datetime, timedelta
  4. # libraries and data
  5. import matplotlib.pyplot as plt
  6. import matplotlib.dates as mdates
  7. import pandas as pd
  8. from pandas.plotting import register_matplotlib_converters
  9. from telegram import ParseMode
  10. from app import LOGGER, SCHEDULER, TELEGRAM_BOT, RESOURCE_NAMES, RESOURCE_IDS, RESOURCE_MAX, \
  11. jobs, api, database
  12. register_matplotlib_converters()
  13. def check_resources(state_id, capital_id, resource_id, do_refill, alt):
  14. """Check resources and refill if necessary"""
  15. regions = api.download_resources(state_id, resource_id)
  16. LOGGER.info('state %6s: check resource %s', state_id, RESOURCE_IDS[resource_id])
  17. print_resources(regions, resource_id)
  18. refill_percentage = 25
  19. database.save_resources(state_id, regions, resource_id)
  20. if do_refill and need_refill(regions, refill_percentage):
  21. max_seconds = max_refill_seconds(regions, refill_percentage, 900)
  22. random_seconds = random.randint(0, max_seconds)
  23. random_time_delta = timedelta(seconds=random_seconds)
  24. scheduled_date = datetime.now() + random_time_delta
  25. job_id = 'refill_{}_{}'.format(capital_id, resource_id)
  26. LOGGER.info(
  27. 'state %s: refil resource %s at %s (%s minutes)', state_id, RESOURCE_IDS[resource_id],
  28. scheduled_date, round(random_time_delta.seconds / 60)
  29. )
  30. job = SCHEDULER.get_job(job_id)
  31. if not job:
  32. SCHEDULER.add_job(
  33. jobs.refill_resource,
  34. 'date',
  35. args=[state_id, capital_id, resource_id, alt],
  36. id=job_id,
  37. run_date=scheduled_date
  38. )
  39. def print_resources(regions, resource_id):
  40. """print resources"""
  41. if regions:
  42. print('region expl max D left c % t %')
  43. for region in regions.values():
  44. region['explored_percentage'] = 100 / region['maximum'] * region['explored']
  45. region['total_left'] = region['explored'] + region['limit_left']
  46. region['total_percentage'] = 100 / RESOURCE_MAX[resource_id] * region['total_left']
  47. print('{:25}: {:7.2f}{:4}{:4}{:5}{:7.2f}{:7.2f}'.format(
  48. region['region_name'],
  49. region['explored'],
  50. region['maximum'],
  51. region['deep_exploration'],
  52. region['limit_left'],
  53. region['explored_percentage'],
  54. region['total_percentage'],
  55. ))
  56. else:
  57. LOGGER.error('no region to print data')
  58. def need_refill(regions, limit):
  59. """Check if refill is needed"""
  60. for region in regions.values():
  61. percentage = 100 / region['maximum'] * region['explored']
  62. if percentage < limit and region['limit_left']:
  63. return True
  64. return False
  65. def max_refill_seconds(regions, limit, max_time):
  66. """Give random seconds for next refill"""
  67. lowest_percentage = limit
  68. for region in regions.values():
  69. percentage = 100 / region['maximum'] * region['explored']
  70. if percentage < lowest_percentage:
  71. lowest_percentage = percentage
  72. return int(max_time / limit * lowest_percentage)
  73. def send_telegram_update(state_id, group_id, resource_name):
  74. """Send resource update to telegram"""
  75. date = datetime.now()
  76. # date = datetime.today().replace(hour=18, minute=5) - timedelta(1)
  77. resource_id = RESOURCE_NAMES[resource_name]
  78. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  79. if message:
  80. print(message)
  81. TELEGRAM_BOT.sendMessage(
  82. chat_id=group_id,
  83. text='```\n{}```'.format(message),
  84. parse_mode=ParseMode.MARKDOWN
  85. )
  86. else:
  87. LOGGER.error('no data for Telegram message')
  88. def graph():
  89. """make graph"""
  90. date = datetime.now() # - timedelta(1)
  91. region_4001 = database.get_resources(4001, date, 0)
  92. region_4002 = database.get_resources(4002, date, 0)
  93. #region_4003 = database.get_resources(4003, date, 0)
  94. #region_4004 = database.get_resources(4004, date, 0)
  95. #region_4008 = database.get_resources(4008, date, 0)
  96. # resource_tmp = np.random.randn(2499)+range(2500, 1, -1)
  97. # Make a data frame
  98. data_frame = pd.DataFrame({
  99. # 'x': range(1, 2500),
  100. # '4001': resource_tmp,
  101. 'x': list(region_4001.keys()),
  102. 'Northern Netherlands': list(region_4001.values()),
  103. 'Eastern Netherlands': list(region_4002.values()),
  104. #'Western Netherlands': list(region_4003.values()),
  105. #'Southern Netherlands': list(region_4004.values()),
  106. #'Amsterdam': list(region_4008.values()),
  107. })
  108. major_fmt = mdates.DateFormatter('%m-%d %H:%M')
  109. fig, ax = plt.subplots()
  110. ax.xaxis.set_major_formatter(major_fmt)
  111. fig.autofmt_xdate()
  112. end_date_time = date.replace(hour=20, minute=0, second=0, microsecond=0)
  113. start_date_time = end_date_time - timedelta(hours=24)
  114. ax.set_xlim([start_date_time, end_date_time])
  115. ax.set_ylim([0, 2500])
  116. # style
  117. plt.style.use('seaborn-darkgrid')
  118. # create a color palette
  119. palette = plt.get_cmap('Set1')
  120. # multiple line plot
  121. num = 0
  122. for column in data_frame.drop('x', axis=1):
  123. num += 1
  124. plt.plot(
  125. data_frame['x'],
  126. data_frame[column],
  127. marker='',
  128. color=palette(num),
  129. linewidth=1,
  130. alpha=0.9,
  131. label=column
  132. )
  133. # Add legend
  134. plt.legend(loc=3, ncol=1)
  135. # plt.setp(plt.xticks()[1], rotation=30, ha='right')
  136. # Add titles
  137. plt.title(
  138. 'Resource limit left | {}'.format(date.strftime('%Y-%m-%d')),
  139. loc='left',
  140. fontsize=12,
  141. fontweight=1
  142. )
  143. plt.xlabel("Time")
  144. plt.ylabel("Resources")
  145. plt.savefig('foo.png')