Skip to content

Commit

Permalink
Added boundary option to $span calls. [GH-14]
Browse files Browse the repository at this point in the history
  • Loading branch information
Daveiano committed Jun 15, 2022
1 parent e0e2cbf commit c59ed95
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 81 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.alwaysShowStatus": true,
"eslint.packageManager": "yarn"
"eslint.packageManager": "yarn",
"[python]": {
"editor.defaultFormatter": "ms-python.python"
}
}
108 changes: 65 additions & 43 deletions bin/user/diagram_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ def get_diagram_type(self, observation):
Returns:
str: A diagram type string
"""
if (observation in GeneralUtil.temp_obs or
'temp' in observation.lower()):
return 'temp'
if observation in GeneralUtil.temp_obs or "temp" in observation.lower():
return "temp"

if 'humidity' in observation.lower():
return 'humidity'
if "humidity" in observation.lower():
return "humidity"

if observation == 'windSpeed' or observation == 'windGust':
return 'wind'
if observation == "windSpeed" or observation == "windGust":
return "wind"

if observation == 'barometer' or observation == 'pressure':
return 'pressure'
if observation == "barometer" or observation == "pressure":
return "pressure"

return observation

Expand All @@ -44,10 +43,10 @@ def get_diagram(self, observation):
Returns:
str: A diagram string
"""
if observation == 'rain' or observation == 'ET':
return 'bar'
if observation == "rain" or observation == "ET":
return "bar"

return 'line'
return "line"

def get_aggregate_type(self, observation):
"""
Expand All @@ -60,15 +59,17 @@ def get_aggregate_type(self, observation):
Returns:
string: aggregate_type
"""
if observation == 'ET' or observation == 'rain':
return 'sum'
if observation == "ET" or observation == "rain":
return "sum"

if (observation == 'UV' or
observation == 'windGust' or
observation == 'rainRate'):
return 'max'
if (
observation == "UV"
or observation == "windGust"
or observation == "rainRate"
):
return "max"

return 'avg'
return "avg"

def get_aggregate_interval(self, observation, precision, *args, **kwargs):
"""
Expand All @@ -82,58 +83,79 @@ def get_aggregate_interval(self, observation, precision, *args, **kwargs):
Returns:
int: aggregate_interval
"""
alltime_start = kwargs.get('alltime_start', None)
alltime_end = kwargs.get('alltime_end', None)
alltime_start = kwargs.get("alltime_start", None)
alltime_end = kwargs.get("alltime_end", None)

if precision == 'day':
if observation == 'ET' or observation == 'rain':
if precision == "day":
if observation == "ET" or observation == "rain":
return 7200 # 2 hours

return 1800 # 30 minutes

if precision == 'week':
if observation == 'ET' or observation == 'rain':
if precision == "week":
if observation == "ET" or observation == "rain":
return 3600 * 24 # 1 day

return 900 * 8 # 2 hours

if precision == 'month':
if observation == 'ET' or observation == 'rain':
if precision == "month":
if observation == "ET" or observation == "rain":
return 3600 * 48 # 2 days

return 900 * 24 # 6 hours

if precision == 'year':
if observation == 'ET' or observation == 'rain':
if precision == "year":
if observation == "ET" or observation == "rain":
return 3600 * 432 # 8 days

return 3600 * 48 # 2 days

if precision == 'alltime':
if (alltime_start is not None and
alltime_end is not None):
if precision == "alltime":
if alltime_start is not None and alltime_end is not None:

d1 = datetime.strptime(alltime_start, '%d.%m.%Y')
d2 = datetime.strptime(alltime_end, '%d.%m.%Y')
d1 = datetime.strptime(alltime_start, "%d.%m.%Y")
d2 = datetime.strptime(alltime_end, "%d.%m.%Y")
delta = d2 - d1

if delta.days == 0:
# Edge case: code from year.
if observation == 'ET' or observation == 'rain':
if observation == "ET" or observation == "rain":
return 3600 * 432 # 8 days

return 3600 * 48 # 2 days

if observation == 'ET' or observation == 'rain':
if observation == "ET" or observation == "rain":
return 3600 * (delta.days / 20) * 24 # Max of 20 bars

return 3600 * (delta.days / 100) * 24 # Max of 100 points
else:
if observation == 'ET' or observation == 'rain':
if observation == "ET" or observation == "rain":
return 3600 * 432 # 8 days

return 3600 * 96 # 4 days

def get_diagram_boundary(self, precision):
"""
boundary for observations series for diagrams.
Args:
precision (string): Day, week, month, year, alltime
Returns:
string: None | 'midnight'
"""
if precision == "day":
return None

if precision == "week":
return None

if precision == "month":
return None

if precision == "year" or precision == "alltime":
return "midnight"

def get_rounding(self, observation):
"""
Rounding settings for observations.
Expand All @@ -144,10 +166,10 @@ def get_rounding(self, observation):
Returns:
int: A rounding
"""
if observation == 'UV' or observation == 'cloudbase':
if observation == "UV" or observation == "cloudbase":
return 0

if observation == 'ET' or observation == 'rain':
if observation == "ET" or observation == "rain":
return 2

return 1
Expand All @@ -166,13 +188,13 @@ def get_hour_delta(self, precision):

hour_delta = 24

if precision == 'week':
if precision == "week":
hour_delta = 24 * 7

if precision == 'month':
if precision == "month":
hour_delta = 24 * 30 # monthrange(now.year, now.month)[1]

if precision == 'year':
if precision == "year":
days = 366 if isleap(now.year) else 365
hour_delta = 24 * days

Expand All @@ -190,7 +212,7 @@ def get_week_delta(self, precision):
"""
week_delta = 0

if precision == 'alltime':
if precision == "alltime":
week_delta = 1000 # TODO: This will stop to work after 19 years.

return week_delta
90 changes: 60 additions & 30 deletions bin/user/table_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,40 @@ def get_table_aggregate_interval(self, observation, precision):
Returns:
int: aggregate_interval
"""
if precision == 'day':
if precision == "day":
return 900 * 8 # 2 hours

if precision == 'week':
if precision == "week":
return 900 * 24 # 6 hours

if precision == 'month':
if precision == "month":
return 900 * 48 # 12 hours

if precision == 'year' or precision == 'alltime':
if precision == "year" or precision == "alltime":
return 3600 * 24 # 1 day

def get_table_boundary(self, precision):
"""
boundary for observations series for tables.
Args:
precision (string): Day, week, month, year, alltime
Returns:
string: None | 'midnight'
"""
if precision == "day":
return None

if precision == "week":
return None

if precision == "month":
return None

if precision == "year" or precision == "alltime":
return "midnight"

def get_table_headers(self, obs, period):
"""
Returns tableheaders for use in carbon data table.
Expand All @@ -51,11 +73,13 @@ def get_table_headers(self, obs, period):
"""
carbon_headers = []

carbon_headers.append({
"title": "Time",
"id": "time",
"sortCycle": "tri-states-from-ascending",
})
carbon_headers.append(
{
"title": "Time",
"id": "time",
"sortCycle": "tri-states-from-ascending",
}
)

for header in obs:
if getattr(period, header).has_data:
Expand Down Expand Up @@ -86,39 +110,45 @@ def get_table_rows(self, obs, period, precision):
# TODO: Get values directly from DB?
for observation in obs:
if getattr(period, observation).has_data:
series = getattr(period, observation).series(
aggregate_type=self.diagram_util.get_aggregate_type(observation),
aggregate_interval=self.get_table_aggregate_interval(
observation,
precision
),
time_series='start',
time_unit='unix_epoch'
).round(self.diagram_util.get_rounding(observation))
series = (
getattr(period, observation)
.series(
aggregate_type=self.diagram_util.get_aggregate_type(
observation
),
aggregate_interval=self.get_table_aggregate_interval(
observation, precision
),
time_series="start",
time_unit="unix_epoch",
)
.round(self.diagram_util.get_rounding(observation))
)

for start, data in zip(series.start, series.data):
cs_time = datetime.fromtimestamp(start.raw)
# The current series item by time.
cs_item = list(filter(
lambda x: (x['time'] == cs_time.isoformat()),
carbon_values
))
cs_item = list(
filter(
lambda x: (x["time"] == cs_time.isoformat()), carbon_values
)
)

if len(cs_item) == 0:
carbon_values.append({
"time": cs_time.isoformat(),
observation: data.raw,
'id': start.raw
})
carbon_values.append(
{
"time": cs_time.isoformat(),
observation: data.raw,
"id": start.raw,
}
)
else:
cs_item = cs_item[0]
cs_item_index = carbon_values.index(cs_item)
cs_item[observation] = data.raw if data.raw is not None else "-"
carbon_values[cs_item_index] = cs_item

# Sort per time
carbon_values.sort(
key=lambda item: datetime.fromisoformat(item['time'])
)
carbon_values.sort(key=lambda item: datetime.fromisoformat(item["time"]))

return carbon_values
2 changes: 1 addition & 1 deletion skins/weewx-wdc/includes/combined-diagram-tile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#else
<script>
/* prettier-ignore */
var $series_name = $getattr($span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision)), $combined_obs).series(aggregate_type=$get_aggregate_type($combined_obs), aggregate_interval=$get_aggregate_interval($combined_obs, $precision, alltime_start=$alltime.start.format('%d.%m.%Y'), alltime_end=$alltime.end.format('%d.%m.%Y')), time_series='both', time_unit='unix_epoch').round($get_rounding($combined_obs)).json;
var $series_name = $getattr($span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision), $boundary=$get_diagram_boundary($precision)), $combined_obs).series(aggregate_type=$get_aggregate_type($combined_obs), aggregate_interval=$get_aggregate_interval($combined_obs, $precision, alltime_start=$alltime.start.format('%d.%m.%Y'), alltime_end=$alltime.end.format('%d.%m.%Y')), time_series='both', time_unit='unix_epoch').round($get_rounding($combined_obs)).json;
</script>
<!--prettier-ignore-->
#end if
Expand Down
5 changes: 3 additions & 2 deletions skins/weewx-wdc/includes/data-table-tile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<div class="bx--col">
<script>
/*prettier-ignore*/
var tableHeaders = $get_table_headers($table_tile_observations, $span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision)));
var tableHeaders = $get_table_headers($table_tile_observations, $span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision), $boundary=$get_table_boundary($precision)));
/*prettier-ignore*/
var tableRows = $get_table_rows($table_tile_observations, $span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision)), $precision);
var tableRows = $get_table_rows($table_tile_observations, $span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision), $boundary=$get_table_boundary($precision)), $precision);
var tableTitle = '$gettext("All data")';
var precision = "$precision";
</script>
<div class="table"></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion skins/weewx-wdc/includes/diagram-tile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#else
<script>
/* prettier-ignore */
var $series_name = $getattr($span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision)), $partial_obs).series(aggregate_type=$get_aggregate_type($partial_obs), aggregate_interval=$get_aggregate_interval($partial_obs, $precision, alltime_start=$alltime.start.format('%d.%m.%Y'), alltime_end=$alltime.end.format('%d.%m.%Y')), time_series='both', time_unit='unix_epoch').round($get_rounding($partial_obs)).json;
var $series_name = $getattr($span($hour_delta=$get_hour_delta($precision), $week_delta=$get_week_delta($precision), $boundary=$get_diagram_boundary($precision)), $partial_obs).series(aggregate_type=$get_aggregate_type($partial_obs), aggregate_interval=$get_aggregate_interval($partial_obs, $precision, alltime_start=$alltime.start.format('%d.%m.%Y'), alltime_end=$alltime.end.format('%d.%m.%Y')), time_series='both', time_unit='unix_epoch').round($get_rounding($partial_obs)).json;
</script>
#end if

Expand Down
Loading

0 comments on commit c59ed95

Please sign in to comment.