Skip to content

Commit

Permalink
Merge pull request #250 from Daveiano/215-per-month-tables
Browse files Browse the repository at this point in the history
Per month tables stats
  • Loading branch information
Daveiano committed Feb 5, 2024
2 parents a5bfc3e + a3eb55b commit b862716
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 23 deletions.
59 changes: 57 additions & 2 deletions bin/user/weewx_wdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from weewx.wxformulas import beaufort
from weeutil.weeutil import TimeSpan, rounder, to_bool, to_int, startOfDay, startOfArchiveDay
from weeutil.config import search_up, accumulateLeaves

from weewx.tags import TimespanBinder

try:
import weeutil.logger
Expand Down Expand Up @@ -354,7 +354,20 @@ def __init__(self, generator):
"wx_binding"
)

# print(self.generator.config_dict["StdWXCalculate"]["Calculations"])
def format_raw_value(self, value, obs):
"""
Returns a ValueHelper for a raw value and an obs type.
Args:
value (float): The value
obs (string): The observation
"""
target_unit_t = self.generator.converter.getTargetUnit(
obs_type=obs)
value_vt = (value, target_unit_t[0], target_unit_t[1])

return ValueHelper(value_t=value_vt, formatter=self.generator.formatter)


def get_software_obs(self):
"""
Expand Down Expand Up @@ -1148,6 +1161,48 @@ def get_dwd_warnings(self):


class WdcArchiveUtil(SearchList):
def __init__(self, generator):
SearchList.__init__(self, generator)

def get_extension_list(self, timespan, db_lookup):
"""Returns a search list extension with two additions.
Parameters:
timespan: An instance of weeutil.weeutil.TimeSpan. This will
hold the start and stop times of the domain of
valid times.
db_lookup: This is a function that, given a data binding
as its only parameter, will return a database manager
object.
"""

self.db_lookup = db_lookup

search_list_extension = {
"get_stat_table_month_obs": self.get_stat_table_month_obs,
"get_day_archive_enabled": self.get_day_archive_enabled,
"get_archive_days_array": self.get_archive_days_array,
"filter_months": self.filter_months,
"fake_get_report_years": self.fake_get_report_years,
}

return [search_list_extension]

def get_stat_table_month_obs(self, start_ts, end_ts):
"""
Returns a TimeSpanBinder a period.
Args:
start_ts (int): The start timestamp
end_ts (int): The end timestamp
"""
month_timespan = TimeSpan(start_ts, end_ts)
month_timespan_binder = TimespanBinder(month_timespan, self.db_lookup,
formatter=self.generator.formatter,
converter=self.generator.converter)
return month_timespan_binder

def get_day_archive_enabled(self):
"""
Get day archive enabled.
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,4 @@ See https://github.com/Daveiano/weewx-wdc/compare/v3.3.0...580071ca175a03fe4924b
- Updated sorting of min/max graphs in the `temp_min_max_avg` graph GH-247
- Added new property for charts: `legendPosition` - Change legend position GH-228
- Added option to set default theme (light/dark/auto) GH-241
- added per-month statistic tables GH-215
1 change: 1 addition & 0 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(self):
"skins/weewx-wdc/includes/stat-tile.inc",
"skins/weewx-wdc/includes/stat-tile-xaggs.inc",
"skins/weewx-wdc/includes/stat-tile-modals.inc",
"skins/weewx-wdc/includes/stat-table.inc",
"skins/weewx-wdc/includes/conditions-table.inc",
"skins/weewx-wdc/includes/climatological-days.inc",
"skins/weewx-wdc/includes/ui-shell.inc",
Expand Down
49 changes: 34 additions & 15 deletions skins/weewx-wdc/includes/climatological-days.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!--prettier-ignore-->
#errorCatcher Echo
#encoding UTF-8
#from datetime import datetime
Expand Down Expand Up @@ -28,7 +27,7 @@
type="module"
src="https://1.www.s81c.com/common/carbon/web-components/version/v1.21.0/tabs.min.js"
></script>
<!--prettier-ignore-->

<bx-tabs trigger-content="Select an item" value="table" type="container">
<bx-tab id="tab-table" target="panel-table" value="table">$gettext('Table')</bx-tab>
#if $getattr($get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday), 'rain').has_data
Expand All @@ -52,6 +51,20 @@
>$gettext('Climatogram')</bx-tab
>
#end if

#if 'stat_tables' in $DisplayOptions
#for $stat_table in $DisplayOptions.get('stat_tables')
#if $getattr($get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday), $DisplayOptions['stat_tables'][$stat_table]['observation']).has_data
<bx-tab
id="tab-$stat_table"
target="panel-$stat_table"
value="$stat_table"
>
$DisplayOptions['stat_tables'][$stat_table]['label']
</bx-tab>
#end if
#end for
#end if
</bx-tabs>

<div class="bx-ce-demo-devenv--tab-panels">
Expand Down Expand Up @@ -81,7 +94,6 @@
<bx-structured-list-row data-test="$clim_day">
<bx-structured-list-cell>$gettext($clim_day)</bx-structured-list-cell>
<bx-structured-list-cell>
<!--prettier-ignore-->
#set $time_span = $get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday)
$get_climatological_day($clim_day, $time_span.start.raw, $time_span.end.raw)
</bx-structured-list-cell>
Expand All @@ -90,21 +102,18 @@
<bx-tooltip>
<bx-tooltip-body>
<p>
<!--prettier-ignore-->
$get_climatological_day_description($clim_day)
</p>
</bx-tooltip-body>
</bx-tooltip>
</div>
<span>
<!--prettier-ignore-->
$get_climatological_day_description($clim_day)
</span>
</bx-structured-list-cell>
</bx-structured-list-row>
#end for

<!--prettier-ignore-->
#if $context == 'alltime'
<bx-structured-list-row data-test="total-number">
<bx-structured-list-cell
Expand All @@ -113,7 +122,6 @@
></bx-structured-list-cell
>
<bx-structured-list-cell>
<!--prettier-ignore-->
#set $start_dt = datetime.fromtimestamp($alltime.start.raw)
#set $end_dt = datetime.fromtimestamp($latest.dateTime.raw)
#set $delta = $end_dt - $start_dt
Expand All @@ -134,12 +142,10 @@
</bx-structured-list-body>
</bx-structured-list>

<!--prettier-ignore-->
#if $show_climatological_days_per_month
#include "includes/year-stats-table.inc"
#end if
</div>
<!--prettier-ignore-->
#if $getattr($get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday), 'rain').has_data
<div
id="panel-diagram-rain"
Expand All @@ -156,7 +162,6 @@
>
</bx-structured-list-header-row>
</bx-structured-list-head>
<!--prettier-ignore-->
<bx-structured-list-body>
#if $context == 'alltime' and $to_bool($DisplayOptions.get('show_last_rain', True)) and $last_rain is not None
<bx-structured-list-row>
Expand Down Expand Up @@ -213,7 +218,6 @@
#end if

<script>
/* prettier-ignore */
#set $obs_context = $get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday)
var calendarDataRain = $get_calendar_data('rain', 'sum', $obs_context.start.raw, $obs_context.end.raw);
</script>
Expand All @@ -227,7 +231,6 @@
></div>
</div>
#end if
<!--prettier-ignore-->
#if $getattr($get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday), 'outTemp').has_data
<div
id="panel-diagram-temp-avg"
Expand All @@ -236,7 +239,6 @@
hidden
>
<script>
/* prettier-ignore */
#set $obs_context = $get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday)
var calendarDataTempAvg = $get_calendar_data('outTemp', 'avg', $obs_context.start.raw, $obs_context.end.raw);
</script>
Expand All @@ -250,9 +252,8 @@
></div>
</div>
#end if
<!-- prettier-ignore -->

#if $show_climatogram_year or $show_climatogram_alltime
<!-- prettier-ignore -->
<div
id="panel-climatogram"
role="tabpanel"
Expand All @@ -276,4 +277,22 @@
#end if
</div>
#end if

#if 'stat_tables' in $DisplayOptions
#for $stat_table in $DisplayOptions.get('stat_tables')
#if $getattr($get_time_span_from_context($context, $day, $week, $month, $year, $alltime, $yesterday), $DisplayOptions['stat_tables'][$stat_table]['observation']).has_data
<div
id="panel-$stat_table"
role="tabpanel"
aria-labelledby="tab-$stat_table"
hidden
>
#set global $stat_table_obs = $DisplayOptions['stat_tables'][$stat_table]['observation']
#set global $stat_table_aggregate_types = $to_list($DisplayOptions['stat_tables'][$stat_table]['aggregate_types'])

#include "includes/stat-table.inc"
</div>
#end if
#end for
#end if
</div>
153 changes: 153 additions & 0 deletions skins/weewx-wdc/includes/stat-table.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#errorCatcher Echo
#encoding UTF-8

#import calendar
#import datetime
#from weeutil.weeutil import startOfDay, startOfArchiveDay

<script
type="module"
src="https://1.www.s81c.com/common/carbon/web-components/version/v1.21.0/structured-list.min.js"
></script>

#if $context == 'alltime'
#set $archive_months = $SummaryByMonth
#if len($SummaryByYear) == 0
#set $SummaryByYear = $fake_get_report_years($alltime.start.format("%Y"), $latest.dateTime.format("%Y"))
#end if
#elif $context == 'year'
#set $SummaryByYear = [str($year_name)]
#set $archive_months = $filter_months($SummaryByMonth, $year_name)
#end if

#for $agg in $stat_table_aggregate_types
<h3 class="bx--type-productive-heading-02">
$gettext($agg.capitalize()) $obs.label[$stat_table_obs]
</h3>

#set $per_month = {}
#set $per_year = {}
#set $per_all = []

<bx-structured-list class="year-obs-stat-table" data-testid="$agg-$stat_table_obs">
<bx-structured-list-head>
<bx-structured-list-header-row>
#if $context == "alltime"
<bx-structured-list-header-cell>
$gettext('Year')
</bx-structured-list-header-cell>
#end if

#for $month in range(1, 13)
<bx-structured-list-header-cell>
$gettext(calendar.month_abbr[$month])
</bx-structured-list-header-cell>
#end for

<bx-structured-list-header-cell>
$gettext($agg.capitalize())
</bx-structured-list-header-cell>
</bx-structured-list-header-row>
</bx-structured-list-head>
<bx-structured-list-body>
#for $year in $SummaryByYear
#if not $year in $per_year
#set $per_year[$year] = []
#end if
<bx-structured-list-row>
#if $context == "alltime"
<bx-structured-list-cell>$year</bx-structured-list-cell>
#end if

#set $months_for_year = $filter_months($archive_months, $year)

#for $month in range(1, 13)
#if not $month in $per_month
#set $per_month[$month] = []
#end if

#set $month_from_range = $year + '-' + str($month).zfill(2)
#set $time_span_start_ts = $startOfDay($datetime.datetime.strptime($month_from_range, "%Y-%m").replace(day=1).timestamp())
#set $time_span_end_ts = $datetime.datetime.strptime($month_from_range, "%Y-%m").replace(day=$calendar.monthrange(int($year), int($month))[1], hour=23, minute=59).timestamp()

#if $month_from_range in $months_for_year
#set $agg_for_value = $agg

#if $stat_table_obs in $DisplayOptions.get('stat_tiles_show_sum', ['rain', 'ET', 'hail', 'snow', 'lightning_strike_count', 'windrun'])
#set $agg_for_value = 'sum'
#end if

#set $stat_month = $get_stat_table_month_obs($time_span_start_ts, $time_span_end_ts)
#set $raw_value = $getattr($getattr($stat_month, $stat_table_obs), $agg_for_value).raw

#silent $per_month[$month].append($raw_value)
#silent $per_year[$year].append($raw_value)
#silent $per_all.append($raw_value)

<bx-structured-list-cell data-testid="month-$month_from_range-$agg-$stat_table_obs">
$getattr($getattr($stat_month, $stat_table_obs), $agg_for_value)
</bx-structured-list-cell>
#else
<bx-structured-list-cell>-</bx-structured-list-cell>
#end if
#end for

<bx-structured-list-cell>
#if $year in $per_year
<strong data-testid="year-$year-$agg-$stat_table_obs">
#if $agg == 'max'
$format_raw_value($max($per_year[$year]), $stat_table_obs)
#elif $agg == 'min'
$format_raw_value($min($per_year[$year]), $stat_table_obs)
#elif $agg == 'sum'
$format_raw_value($sum($per_year[$year]), $stat_table_obs)
#elif $agg == 'avg'
$format_raw_value($sum($per_year[$year])/$len($per_year[$year]), $stat_table_obs)
#end if
</strong>
#else
<strong>-</strong>
#end if
</bx-structured-list-cell>
</bx-structured-list-row>
#end for
<bx-structured-list-row>
<bx-structured-list-cell>
$gettext($agg.capitalize())
</bx-structured-list-cell>

#for $month in range(1, 13)
#if $per_month[$month]
<bx-structured-list-cell data-testid="agg-month-$month-$agg-$stat_table_obs">
#if $agg == 'max'
$format_raw_value($max($per_month[$month]), $stat_table_obs)
#elif $agg == 'min'
$format_raw_value($min($per_month[$month]), $stat_table_obs)
#elif $agg == 'sum'
$format_raw_value($sum($per_month[$month]), $stat_table_obs)
#elif $agg == 'avg'
$format_raw_value($sum($per_month[$month])/$len($per_month[$month]), $stat_table_obs)
#end if
</bx-structured-list-cell>
#else
<bx-structured-list-cell>-</bx-structured-list-cell>
#end if
#end for

<bx-structured-list-cell>
<strong data-testid="total-$agg-$stat_table_obs">
#if $agg == 'max'
$format_raw_value($max($per_all), $stat_table_obs)
#elif $agg == 'min'
$format_raw_value($min($per_all), $stat_table_obs)
#elif $agg == 'sum'
$format_raw_value($sum($per_all), $stat_table_obs)
#elif $agg == 'avg'
$format_raw_value($sum($per_all)/$len($per_all), $stat_table_obs)
#end if
</strong>
</bx-structured-list-cell>
</bx-structured-list-row>
</bx-structured-list-body>
</bx-structured-list>
#end for
Loading

0 comments on commit b862716

Please sign in to comment.