From 7a9be1b0e21e3911d9f599e2634067d26ed3b02e Mon Sep 17 00:00:00 2001 From: evilbunny2008 <34211365+evilbunny2008@users.noreply.github.com> Date: Thu, 7 Jun 2018 23:52:02 +1000 Subject: [PATCH 1/2] Add last_year and last_year_todate to xstats.py Add last_year and last_year_todate to xstats.py to do comparisons to of rainfall up to this time last year, and for generating statistics for the last calendar year --- examples/xstats/bin/user/xstats.py | 31 +++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/examples/xstats/bin/user/xstats.py b/examples/xstats/bin/user/xstats.py index f663f091..662f8a17 100644 --- a/examples/xstats/bin/user/xstats.py +++ b/examples/xstats/bin/user/xstats.py @@ -12,6 +12,11 @@ This search list extension offers extra tags: 'seven_day': Statistics for the last seven days. 'thirty_day': Statistics for the last thirty days. + + 'last_year': Statistics for last calendar year. + + 'last_year_todate': Statistics of last calendar year until this time last year. + This especially useful for comprisons of rain fall up to this time last year. You can then use tags such as $alltime.outTemp.max for the all-time max temperature, or $seven_day.rain.sum for the total rainfall in the last seven @@ -69,10 +74,34 @@ class ExtendedStatistics(SearchList): context='thirty_day', formatter=self.generator.formatter, converter=self.generator.converter) + + # Now use a similar process to get statistics for last year. + year = datetime.date.today().year + start_ts = time.mktime((year - 1, 1, 1, 0, 0, 0, 0, 0, 0)) + stop_ts = time.mktime((year, 1, 1, 0, 0, 0, 0, 0, 0)) + last_year_stats = TimespanBinder(TimeSpan(start_ts, stop_ts), + db_lookup, + context='last_year', + formatter=self.generator.formatter, + converter=self.generator.converter) + + # Now use a similar process to get statistics for last year to date. + year = datetime.date.today().year + month = datetime.date.today().month + day = datetime.date.today().day + start_ts = time.mktime((year - 1, 1, 1, 0, 0, 0, 0, 0, 0)) + stop_ts = time.mktime((year - 1, month, day, 0, 0, 0, 0, 0, 0)) + last_year_todate_stats = TimespanBinder(TimeSpan(start_ts, stop_ts), + db_lookup, + context='last_year_todate', + formatter=self.generator.formatter, + converter=self.generator.converter return [{'alltime': all_stats, 'seven_day': seven_day_stats, - 'thirty_day': thirty_day_stats}] + 'thirty_day': thirty_day_stats, + 'last_year': last_year_stats, + 'last_year_todate': last_year_todate_stats}] # For backwards compatibility: ExtStats = ExtendedStatistics From f56df7b1f90be70491c1443496a76d35ddb6ece1 Mon Sep 17 00:00:00 2001 From: evilbunny2008 <34211365+evilbunny2008@users.noreply.github.com> Date: Fri, 7 Sep 2018 22:44:03 +1000 Subject: [PATCH 2/2] Update readme.txt --- examples/xstats/readme.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/xstats/readme.txt b/examples/xstats/readme.txt index 3f6e25b4..a2096633 100644 --- a/examples/xstats/readme.txt +++ b/examples/xstats/readme.txt @@ -16,7 +16,15 @@ This search list extension offers extra tags: thirty days ago. For example, "what is the maximum wind speed in the last thirty days?" $thirty_day.wind.max - + + 'last_year': Statistics for last calendar year, this is useful for + things like total rainfall for last year. + $last_year.rain.sum + + 'last_year_todate': Statistics of last calendar year until this time + last year. This is useful for comprisons of rain + fall up to this time last year. + $last_year_todate.rain.sum Installation instructions