def testScalarTally(self): with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as manager: # Pick a random day, say 15 March: start_ts = int(time.mktime((2010, 3, 15, 0, 0, 0, 0, 0, -1))) stop_ts = int(time.mktime((2010, 3, 16, 0, 0, 0, 0, 0, -1))) # Sanity check that this is truly the start of day: self.assertEqual(start_ts, weeutil.weeutil.startOfDay(start_ts)) # Get a day's stats from the daily summaries: allStats = manager._get_day_summary(start_ts) # Now calculate the same summaries from the raw data in the archive. # Here are some random observation types: for stats_type in ['barometer', 'outTemp', 'rain']: # Now test all the aggregates: for aggregate in ['min', 'max', 'sum', 'count', 'avg']: # Compare to the main archive: res = manager.getSql( "SELECT %s(%s) FROM archive WHERE dateTime>? AND dateTime <=?;" % (aggregate, stats_type), (start_ts, stop_ts)) # The results from the daily summaries for this aggregation allStats_res = getattr(allStats[stats_type], aggregate) self.assertAlmostEqual(allStats_res, res[0], msg="Value check. Failing type %s, aggregate: %s" % (stats_type, aggregate)) # Check the times of min and max as well: if aggregate in ['min', 'max']: res2 = manager.getSql( "SELECT dateTime FROM archive WHERE %s = ? AND dateTime>? AND dateTime <=?" % (stats_type,), (res[0], start_ts, stop_ts)) stats_time = getattr(allStats[stats_type], aggregate + 'time') self.assertEqual(stats_time, res2[0], "Time check. Failing type %s, aggregate: %s" % (stats_type, aggregate))
def time_query(config_dict): """A low-level approach. Use a SQL query""" manager_dict = weewx.manager.get_manager_dict_from_config(config_dict, 'wx_binding') # Just use a regular ol' manager to avoid using the daily summary optimization: manager_dict['manager'] = 'weewx.manager.Manager' manager = weewx.manager.open_manager(manager_dict,initialize=False) print "start time=", timestamp_to_string(start_ts) print "stop time=", timestamp_to_string(stop_ts) print "Approximately %.1f months of data" % ((stop_ts - start_ts)/(30*24*3600)) t0 = time.time() vec = [] for span in weeutil.weeutil.genDaySpans(start_ts, stop_ts): query = 'SELECT dateTime, outTemp FROM archive WHERE dateTime > %d AND dateTime <= %d '\ 'AND outTemp = (SELECT MAX(outTemp) FROM archive WHERE dateTime > %d AND dateTime <= %d)' % (span + span) tup = manager.getSql(query) vec.append(tup) t1 = time.time() print "Elapsed query time=", t1-t0 print vec
def testWindTally(self): with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as manager: # Pick a random day, say 15 March: start_ts = int(time.mktime((2010, 3, 15, 0, 0, 0, 0, 0, -1))) stop_ts = int(time.mktime((2010, 3, 16, 0, 0, 0, 0, 0, -1))) # Sanity check that this is truly the start of day: self.assertEqual(start_ts, weeutil.weeutil.startOfDay(start_ts)) allStats = manager._get_day_summary(start_ts) # Test all the aggregates: for aggregate in ['min', 'max', 'sum', 'count', 'avg']: if aggregate == 'max': res = manager.getSql( "SELECT MAX(windGust) FROM archive WHERE dateTime>? AND dateTime <=?;", (start_ts, stop_ts)) else: res = manager.getSql( "SELECT %s(windSpeed) FROM archive WHERE dateTime>? AND dateTime <=?;" % (aggregate, ), (start_ts, stop_ts)) # From StatsDb: allStats_res = getattr(allStats['wind'], aggregate) self.assertAlmostEqual(allStats_res, res[0]) # Check the times of min and max as well: if aggregate == 'min': resmin = manager.getSql( "SELECT dateTime FROM archive WHERE windSpeed = ? AND dateTime>? AND dateTime <=?", (res[0], start_ts, stop_ts)) self.assertEqual(allStats['wind'].mintime, resmin[0]) elif aggregate == 'max': resmax = manager.getSql( "SELECT dateTime FROM archive WHERE windGust = ? AND dateTime>? AND dateTime <=?", (res[0], start_ts, stop_ts)) self.assertEqual(allStats['wind'].maxtime, resmax[0]) # Check RMS: (squaresum, count) = manager.getSql( "SELECT SUM(windSpeed*windSpeed), COUNT(windSpeed) from archive where dateTime>? AND dateTime<=?;", (start_ts, stop_ts)) rms = math.sqrt(squaresum / count) if count else None self.assertAlmostEqual(allStats['wind'].rms, rms)
def testTags(self): """Test common tags.""" global skin_dict db_binder = weewx.manager.DBBinder(self.config_dict) db_lookup = db_binder.bind_default() with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as manager: spans = {'day': weeutil.weeutil.TimeSpan(time.mktime((2010, 3, 15, 0, 0, 0, 0, 0, -1)), time.mktime((2010, 3, 16, 0, 0, 0, 0, 0, -1))), 'week': weeutil.weeutil.TimeSpan(time.mktime((2010, 3, 14, 0, 0, 0, 0, 0, -1)), time.mktime((2010, 3, 21, 0, 0, 0, 0, 0, -1))), 'month': weeutil.weeutil.TimeSpan(time.mktime((2010, 3, 1, 0, 0, 0, 0, 0, -1)), time.mktime((2010, 4, 1, 0, 0, 0, 0, 0, -1))), 'year': weeutil.weeutil.TimeSpan(time.mktime((2010, 1, 1, 0, 0, 0, 0, 0, -1)), time.mktime((2011, 1, 1, 0, 0, 0, 0, 0, -1)))} # This may not necessarily execute in the order specified above: for span in spans: start_ts = spans[span].start stop_ts = spans[span].stop tagStats = weewx.tags.TimeBinder(db_lookup, stop_ts, formatter=default_formatter, rain_year_start=1, skin_dict=skin_dict) # Cycle over the statistical types: for stats_type in ('barometer', 'outTemp', 'rain'): # Now test all the aggregates: for aggregate in ('min', 'max', 'sum', 'count', 'avg'): # Compare to the main archive: res = manager.getSql( "SELECT %s(%s) FROM archive WHERE dateTime>? AND dateTime <=?;" % (aggregate, stats_type), (start_ts, stop_ts)) archive_result = res[0] value_helper = getattr(getattr(getattr(tagStats, span)(), stats_type), aggregate) self.assertAlmostEqual(float(str(value_helper.formatted)), archive_result, places=1) # Check the times of min and max as well: if aggregate in ('min', 'max'): res2 = manager.getSql( "SELECT dateTime FROM archive WHERE %s = ? AND dateTime>? AND dateTime <=?" % ( stats_type,), (archive_result, start_ts, stop_ts)) stats_value_helper = getattr(getattr(getattr(tagStats, span)(), stats_type), aggregate + 'time') self.assertEqual(stats_value_helper.raw, res2[0]) # Do the tests for a report time of midnight, 1-Apr-2010 tagStats = weewx.tags.TimeBinder(db_lookup, spans['month'].stop, formatter=default_formatter, rain_year_start=1, skin_dict=skin_dict) self.assertEqual(str(tagStats.day().barometer.avg), "29.333 inHg") self.assertEqual(str(tagStats.day().barometer.min), "29.000 inHg") self.assertEqual(str(tagStats.day().barometer.max), "29.935 inHg") self.assertEqual(str(tagStats.day().barometer.mintime), "01:00:00") self.assertEqual(str(tagStats.day().barometer.maxtime), "00:00:00") self.assertEqual(str(tagStats.week().barometer.avg), "30.097 inHg") self.assertEqual(str(tagStats.week().barometer.min), "29.000 inHg") self.assertEqual(str(tagStats.week().barometer.max), "31.000 inHg") self.assertEqual(str(tagStats.week().barometer.mintime), "01:00:00 (Wednesday)") self.assertEqual(str(tagStats.week().barometer.maxtime), "01:00:00 (Monday)") self.assertEqual(str(tagStats.month().barometer.avg), "29.979 inHg") self.assertEqual(str(tagStats.month().barometer.min), "29.000 inHg") self.assertEqual(str(tagStats.month().barometer.max), "31.000 inHg") self.assertEqual(str(tagStats.month().barometer.mintime), "03/03/10 00:00:00") self.assertEqual(str(tagStats.month().barometer.maxtime), "03/05/10 00:00:00") self.assertEqual(str(tagStats.year().barometer.avg), "29.996 inHg") self.assertEqual(str(tagStats.year().barometer.min), "29.000 inHg") self.assertEqual(str(tagStats.year().barometer.max), "31.000 inHg") self.assertEqual(str(tagStats.year().barometer.mintime), "01/02/10 00:00:00") self.assertEqual(str(tagStats.year().barometer.maxtime), "01/04/10 00:00:00") self.assertEqual(str(tagStats.day().outTemp.avg), "38.4°F") self.assertEqual(str(tagStats.day().outTemp.min), "18.5°F") self.assertEqual(str(tagStats.day().outTemp.max), "58.9°F") self.assertEqual(str(tagStats.day().outTemp.mintime), "04:00:00") self.assertEqual(str(tagStats.day().outTemp.maxtime), "16:00:00") self.assertEqual(str(tagStats.week().outTemp.avg), "38.7°F") self.assertEqual(str(tagStats.week().outTemp.min), "16.5°F") self.assertEqual(str(tagStats.week().outTemp.max), "60.9°F") self.assertEqual(str(tagStats.week().outTemp.mintime), "04:00:00 (Sunday)") self.assertEqual(str(tagStats.week().outTemp.maxtime), "16:00:00 (Saturday)") self.assertEqual(str(tagStats.month().outTemp.avg), "28.8°F") self.assertEqual(str(tagStats.month().outTemp.min), "-1.0°F") self.assertEqual(str(tagStats.month().outTemp.max), "58.9°F") self.assertEqual(str(tagStats.month().outTemp.mintime), "03/01/10 03:00:00") self.assertEqual(str(tagStats.month().outTemp.maxtime), "03/31/10 16:00:00") self.assertEqual(str(tagStats.year().outTemp.avg), "48.3°F") self.assertEqual(str(tagStats.year().outTemp.min), "-20.0°F") self.assertEqual(str(tagStats.year().outTemp.max), "100.0°F") self.assertEqual(str(tagStats.year().outTemp.mintime), "01/01/10 03:00:00") self.assertEqual(str(tagStats.year().outTemp.maxtime), "07/02/10 16:00:00") # Check the special aggregate types "exists" and "has_data": self.assertTrue(tagStats.year().barometer.exists) self.assertTrue(tagStats.year().barometer.has_data) self.assertFalse(tagStats.year().bar.exists) self.assertFalse(tagStats.year().bar.has_data) self.assertTrue(tagStats.year().inHumidity.exists) self.assertFalse(tagStats.year().inHumidity.has_data)