historygenerator.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #
  2. # Copyright (c) 2013-2016 Nick Dajda <nick.dajda@gmail.com>
  3. #
  4. # Distributed under the terms of the GNU GENERAL PUBLIC LICENSE
  5. #
  6. """Extends the Cheetah generator search list to add html historic data tables in a nice colour scheme.
  7. Tested on Weewx release 3.8.2.
  8. Works with all databases.
  9. Observes the units of measure and display formats specified in skin.conf.
  10. WILL NOT WORK with Weewx prior to release 3.0.
  11. -- Use this version for 2.4 - 2.7: https://github.com/brewster76/fuzzy-archer/releases/tag/v2.0
  12. To use it, add this generator to search_list_extensions in skin.conf:
  13. [CheetahGenerator]
  14. search_list_extensions = user.historygenerator.MyXSearch
  15. 1) The $alltime tag:
  16. Allows tags such as $alltime.outTemp.max for the all-time max
  17. temperature, or $seven_day.rain.sum for the total rainfall in the last
  18. seven days.
  19. 2) Nice colourful tables summarising history data by month and year:
  20. Adding the section below to your skins.conf file will create these new tags:
  21. $min_temp_table
  22. $max_temp_table
  23. $avg_temp_table
  24. $rain_table
  25. ############################################################################################
  26. #
  27. # HTML month/year colour coded summary table generator
  28. #
  29. [HistoryReport]
  30. # minvalues, maxvalues and colours should contain the same number of elements.
  31. #
  32. # For example, the [min_temp] example below, if the minimum temperature measured in
  33. # a month is between -50 and -10 (degC) then the cell will be shaded in html colour code #0029E5.
  34. #
  35. # colours = background colour
  36. # fontColours = foreground colour [optional, defaults to black if omitted]
  37. # Default is temperature scale
  38. minvalues = -50, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35
  39. maxvalues = -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 60
  40. colours = "#0029E5", "#0186E7", "#02E3EA", "#04EC97", "#05EF3D2, "#2BF207", "#8AF408", "#E9F70A", "#F9A90B", "#FC4D0D", "#FF0F2D"
  41. fontColours = "#FFFFFF", "#FFFFFF", "#000000", "#000000", "#000000", "#000000", "#000000", "#000000", "#FFFFFF", "#FFFFFF", "#FFFFFF"
  42. monthnames = Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  43. # The Raspberry Pi typically takes 15+ seconds to calculate all the summaries with a few years of weather date.
  44. # refresh_interval is how often in minutes the tables are calculated.
  45. refresh_interval = 60
  46. [[min_temp]] # Create a new Cheetah tag which will have a _table suffix: $min_temp_table
  47. obs_type = outTemp # obs_type can be any weewx observation, e.g. outTemp, barometer, wind, ...
  48. aggregate_type = min # Any of these: 'sum', 'count', 'avg', 'max', 'min'
  49. [[max_temp]]
  50. obs_type = outTemp
  51. aggregate_type = max
  52. [[avg_temp]]
  53. obs_type = outTemp
  54. aggregate_type = avg
  55. [[rain]]
  56. obs_type = rain
  57. aggregate_type = sum
  58. data_binding = alternative_binding
  59. # Override default temperature colour scheme with rain specific scale
  60. minvalues = 0, 25, 50, 75, 100, 150
  61. maxvalues = 25, 50, 75, 100, 150, 1000
  62. colours = "#E0F8E0", "#A9F5A9", "#58FA58", "#2EFE2E", "#01DF01", "#01DF01"
  63. fontColours = "#000000", "#000000", "#000000", "#000000", "#000000", "#000000"
  64. """
  65. from datetime import datetime
  66. import time
  67. import syslog
  68. import os.path
  69. from weewx.cheetahgenerator import SearchList
  70. from weewx.tags import TimespanBinder
  71. import weeutil.weeutil
  72. class MyXSearch(SearchList):
  73. def __init__(self, generator):
  74. SearchList.__init__(self, generator)
  75. self.table_dict = generator.skin_dict['HistoryReport']
  76. # Calculate the tables once every refresh_interval mins
  77. self.refresh_interval = int(self.table_dict.get('refresh_interval', 5))
  78. self.cache_time = 0
  79. self.search_list_extension = {}
  80. # Make bootstrap specific labels in config file available to
  81. if 'BootstrapLabels' in generator.skin_dict:
  82. self.search_list_extension['BootstrapLabels'] = generator.skin_dict['BootstrapLabels']
  83. else:
  84. syslog.syslog(syslog.LOG_DEBUG, "%s: No bootstrap specific labels found" % os.path.basename(__file__))
  85. # Make observation labels available to templates
  86. if 'Labels' in generator.skin_dict:
  87. self.search_list_extension['Labels'] = generator.skin_dict['Labels']
  88. else:
  89. syslog.syslog(syslog.LOG_DEBUG, "%s: No observation labels found" % os.path.basename(__file__))
  90. def get_extension_list(self, valid_timespan, db_lookup):
  91. """For weewx V3.x extensions. Should return a list
  92. of objects whose attributes or keys define the extension.
  93. valid_timespan: An instance of weeutil.weeutil.TimeSpan. This will hold the
  94. start and stop times of the domain of valid times.
  95. db_lookup: A function with call signature db_lookup(data_binding), which
  96. returns a database manager and where data_binding is an optional binding
  97. name. If not given, then a default binding will be used.
  98. """
  99. # Time to recalculate?
  100. if (time.time() - (self.refresh_interval * 60)) > self.cache_time:
  101. self.cache_time = time.time()
  102. #
  103. # The html history tables
  104. #
  105. t1 = time.time()
  106. ngen = 0
  107. for table in self.table_dict.sections:
  108. noaa = True if table == 'NOAA' else False
  109. table_options = weeutil.weeutil.accumulateLeaves(self.table_dict[table])
  110. # Get the binding where the data is allocated
  111. binding = table_options.get('data_binding', 'wx_binding')
  112. #
  113. # The all time statistics
  114. #
  115. # If this generator has been called in the [SummaryByMonth] or [SummaryByYear]
  116. # section in skin.conf then valid_timespan won't contain enough history data for
  117. # the colourful summary tables. Use the data binding provided as table option.
  118. alltime_timespan = weeutil.weeutil.TimeSpan(db_lookup(data_binding=binding).first_timestamp, db_lookup(data_binding=binding).last_timestamp)
  119. # First, get a TimeSpanStats object for all time. This one is easy
  120. # because the object valid_timespan already holds all valid times to be
  121. # used in the report. se the data binding provided as table option.
  122. all_stats = TimespanBinder(alltime_timespan, db_lookup, data_binding=binding, formatter=self.generator.formatter,
  123. converter=self.generator.converter)
  124. # Now create a small dictionary with keys 'alltime' and 'seven_day':
  125. self.search_list_extension['alltime'] = all_stats
  126. # Show all time unless starting date specified
  127. startdate = table_options.get('startdate', None)
  128. if startdate is not None:
  129. table_timespan = weeutil.weeutil.TimeSpan(int(startdate), db_lookup(binding).last_timestamp)
  130. table_stats = TimespanBinder(table_timespan, db_lookup, data_binding=binding, formatter=self.generator.formatter,
  131. converter=self.generator.converter)
  132. else:
  133. table_stats = all_stats
  134. table_name = table + '_table'
  135. self.search_list_extension[table_name] = self._statsHTMLTable(table_options, table_stats, table_name, binding, NOAA=noaa)
  136. ngen += 1
  137. t2 = time.time()
  138. syslog.syslog(syslog.LOG_INFO, "%s: Generated %d tables in %.2f seconds" %
  139. (os.path.basename(__file__), ngen, t2 - t1))
  140. return [self.search_list_extension]
  141. def _parseTableOptions(self, table_options, table_name):
  142. """Create an orderly list containing lower and upper thresholds, cell background and foreground colors
  143. """
  144. # Check everything's the same length
  145. l = len(table_options['minvalues'])
  146. for i in [table_options['maxvalues'], table_options['colours']]:
  147. if len(i) != l:
  148. syslog.syslog(syslog.LOG_INFO, "%s: minvalues, maxvalues and colours must have the same number of elements in table: %s"
  149. % (os.path.basename(__file__), table_name))
  150. return None
  151. font_color_list = table_options['fontColours'] if 'fontColours' in table_options else ['#000000'] * l
  152. return zip(table_options['minvalues'], table_options['maxvalues'], table_options['colours'], font_color_list)
  153. def _statsHTMLTable(self, table_options, table_stats, table_name, binding, NOAA=False):
  154. """
  155. table_options: Dictionary containing skin.conf options for particluar table
  156. all_stats: Link to all_stats TimespanBinder
  157. """
  158. cellColours = self._parseTableOptions(table_options, table_name)
  159. summary_column = weeutil.weeutil.to_bool(table_options.get("summary_column", False))
  160. if None is cellColours:
  161. # Give up
  162. return None
  163. if NOAA is True:
  164. unit_formatted = ""
  165. else:
  166. obs_type = table_options['obs_type']
  167. aggregate_type = table_options['aggregate_type']
  168. converter = table_stats.converter
  169. # obs_type
  170. readingBinder = getattr(table_stats, obs_type)
  171. # Some aggregate come with an argument
  172. if aggregate_type in ['max_ge', 'max_le', 'min_le', 'sum_ge']:
  173. try:
  174. threshold_value = float(table_options['aggregate_threshold'][0])
  175. except KeyError:
  176. syslog.syslog(syslog.LOG_INFO, "%s: Problem with aggregate_threshold. Should be in the format: [value], [units]" %
  177. (os.path.basename(__file__)))
  178. return "Could not generate table %s" % table_name
  179. threshold_units = table_options['aggregate_threshold'][1]
  180. try:
  181. reading = getattr(readingBinder, aggregate_type)((threshold_value, threshold_units))
  182. except IndexError:
  183. syslog.syslog(syslog.LOG_INFO, "%s: Problem with aggregate_threshold units: %s" % (os.path.basename(__file__),
  184. str(threshold_units)))
  185. return "Could not generate table %s" % table_name
  186. else:
  187. try:
  188. reading = getattr(readingBinder, aggregate_type)
  189. except KeyError:
  190. syslog.syslog(syslog.LOG_INFO, "%s: aggregate_type %s not found" % (os.path.basename(__file__),
  191. aggregate_type))
  192. return "Could not generate table %s" % table_name
  193. try:
  194. unit_type = reading.converter.group_unit_dict[reading.value_t[2]]
  195. except KeyError:
  196. syslog.syslog(syslog.LOG_INFO, "%s: obs_type %s no unit found" % (os.path.basename(__file__),
  197. obs_type))
  198. unit_formatted = ''
  199. # 'units' option in skin.conf?
  200. if 'units' in table_options:
  201. unit_formatted = table_options['units']
  202. else:
  203. if (unit_type == 'count'):
  204. unit_formatted = "Days"
  205. else:
  206. if unit_type in reading.formatter.unit_label_dict:
  207. unit_formatted = reading.formatter.unit_label_dict[unit_type]
  208. # For aggregrate types which return number of occurrences (e.g. max_ge), set format to integer
  209. # Don't catch error here - we absolutely need the string format
  210. if unit_type == 'count':
  211. format_string = '%d'
  212. else:
  213. format_string = reading.formatter.unit_format_dict[unit_type]
  214. htmlText = '<table class="table">'
  215. htmlText += " <thead>"
  216. htmlText += " <tr>"
  217. htmlText += " <th>%s</th>" % unit_formatted
  218. for mon in table_options.get('monthnames', ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']):
  219. htmlText += " <th>%s</th>" % mon
  220. if summary_column:
  221. if 'summary_heading' in table_options:
  222. htmlText += " <th></th>"
  223. htmlText += " <th align=\"center\">%s</th>\n" % table_options['summary_heading']
  224. htmlText += " </tr>"
  225. htmlText += " </thead>"
  226. htmlText += " <tbody>"
  227. for year in table_stats.years():
  228. year_number = datetime.fromtimestamp(year.timespan[0]).year
  229. htmlLine = (' ' * 8) + "<tr>\n"
  230. if NOAA is True:
  231. htmlLine += (' ' * 12) + "%s\n" % \
  232. self._NoaaYear(datetime.fromtimestamp(year.timespan[0]), table_options)
  233. else:
  234. htmlLine += (' ' * 12) + "<td>%d</td>\n" % year_number
  235. for month in year.months():
  236. if NOAA is True:
  237. #for property, value in vars(month.dateTime.value_t[0]).iteritems():
  238. # print property, ": ", value
  239. if (month.timespan[1] < table_stats.timespan.start) or (month.timespan[0] > table_stats.timespan.stop):
  240. # print "No data for... %d, %d" % (year_number, datetime.fromtimestamp(month.timespan[0]).month)
  241. htmlLine += "<td>-</td>\n"
  242. else:
  243. htmlLine += self._NoaaCell(datetime.fromtimestamp(month.timespan[0]), table_options)
  244. else:
  245. # update the binding to access the right DB
  246. obsMonth = getattr(month, obs_type)
  247. obsMonth.data_binding = binding;
  248. if unit_type == 'count':
  249. try:
  250. value = getattr(obsMonth, aggregate_type)((threshold_value, threshold_units)).value_t
  251. except:
  252. value = [0, 'count']
  253. else:
  254. value = converter.convert(getattr(obsMonth, aggregate_type).value_t)
  255. htmlLine += (' ' * 12) + self._colorCell(value[0], format_string, cellColours)
  256. if summary_column:
  257. obsYear = getattr(year, obs_type)
  258. obsYear.data_binding = binding;
  259. if unit_type == 'count':
  260. try:
  261. value = getattr(obsYear, aggregate_type)((threshold_value, threshold_units)).value_t
  262. except:
  263. value = [0, 'count']
  264. else:
  265. value = converter.convert(getattr(obsYear, aggregate_type).value_t)
  266. htmlLine += (' ' * 12) + "<td></td>\n"
  267. htmlLine += (' ' * 12) + self._colorCell(value[0], format_string, cellColours, center=True)
  268. htmlLine += (' ' * 8) + "</tr>\n"
  269. htmlText += htmlLine
  270. htmlText += (' ' * 8) + "</tr>\n"
  271. htmlText += (' ' * 4) + "</tbody>\n"
  272. htmlText += "</table>\n"
  273. return htmlText
  274. def _colorCell(self, value, format_string, cellColours, center=False):
  275. """Returns a '<td style= background-color: XX; color: YY"> z.zz </td>' html table entry string.
  276. value: Numeric value for the observation
  277. format_string: How the numberic value should be represented in the table cell.
  278. cellColours: An array containing 4 lists. [minvalues], [maxvalues], [background color], [foreground color]
  279. """
  280. cellText = "<td"
  281. if center:
  282. cellText += " align=\"center\""
  283. if value is not None:
  284. for c in cellColours:
  285. if (value >= float(c[0])) and (value <= float(c[1])):
  286. cellText += " style=\"background-color:%s; color:%s\"" % (c[2], c[3])
  287. formatted_value = format_string % value
  288. cellText += "> %s </td>\n" % formatted_value
  289. else:
  290. cellText += ">-</td>\n"
  291. return cellText
  292. def _NoaaCell(self, dt, table_options):
  293. cellText = '<td> <a href="text.php?report=%s" class="btn btn-default btn-xs active" role="button"> %s </a> </td>' % \
  294. (dt.strftime(table_options['month_filename']), dt.strftime("%m-%y"))
  295. return cellText
  296. def _NoaaYear(self, dt, table_options):
  297. cellText = '<td> <a href="text.php?report=%s" class="btn btn-primary btn-xs active" role="button"> %s </a> </td>' % \
  298. (dt.strftime(table_options['year_filename']), dt.strftime("%Y"))
  299. return cellText