Skip to content

Commit

Permalink
Chart size and content
Browse files Browse the repository at this point in the history
  • Loading branch information
springfall2008 authored Feb 6, 2025
1 parent dae100b commit 01bd0dc
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions apps/predbat/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

TIME_FORMAT_DAILY = "%Y-%m-%d"


class WebInterface:
def __init__(self, base) -> None:
self.abort = False
Expand All @@ -32,8 +31,10 @@ def __init__(self, base) -> None:
self.pv_forecast_hist = {}
self.cost_today_hist = {}
self.compare_hist = {}
self.cost_yesterday_hist = {}
self.cost_yesterday_car_hist = {}

def history_attribute(self, history, state_key="state", last_updated_key="last_updated", scale=1.0, attributes=False, print=False, daily=False):
def history_attribute(self, history, state_key="state", last_updated_key="last_updated", scale=1.0, attributes=False, print=False, daily=False, offset_days=0):
results = {}
last_updated_time = None
last_day_stamp = None
Expand Down Expand Up @@ -82,6 +83,8 @@ def history_attribute(self, history, state_key="state", last_updated_key="last_u

# Add the state to the result
if daily:
if offset_days:
day_stamp += timedelta(days=offset_days)
results[day_stamp.strftime(TIME_FORMAT_DAILY)] = state
else:
results[last_updated_time] = state
Expand All @@ -97,11 +100,13 @@ def history_update(self):
self.pv_forecast_hist = self.history_attribute(self.base.get_history_wrapper("sensor." + self.base.prefix + "_pv_forecast_h0", 7))
self.cost_today_hist = self.history_attribute(self.base.get_history_wrapper(self.base.prefix + ".ppkwh_today", 2))
self.cost_hour_hist = self.history_attribute(self.base.get_history_wrapper(self.base.prefix + ".ppkwh_hour", 2))
self.cost_yesterday_hist = self.history_attribute(self.base.get_history_wrapper(self.base.prefix + ".cost_yesterday", 28), daily=True, offset_days=-1)
self.cost_yesterday_car_hist = self.history_attribute(self.base.get_history_wrapper(self.base.prefix + ".cost_yesterday_car", 28), daily=True, offset_days=-1)

compare_list = self.base.get_arg("compare_list", [])
compare_list = self.base.get_arg('compare_list', [])
for item in compare_list:
id = item.get("id", None)
if id and self.base.comparison:
if id and self.base.comparison:
self.compare_hist[id] = {}
result = self.base.comparison.get_comparison(id)
if result:
Expand Down Expand Up @@ -319,13 +324,14 @@ def get_chart_series(self, name, results, chart_type, color):
text += " }\n"
return text

def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname="chart"):
def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname='chart', fullsize=True):
"""
Render a chart
"""
midnight_str = (self.base.midnight_utc + timedelta(days=1)).strftime(TIME_FORMAT)
text = ""
text += """
if fullsize:
text += """
<script>
window.onresize = function(){ location.reload(); };
var width = window.innerWidth;
Expand All @@ -342,6 +348,7 @@ def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname="ch
else {
width = height * 1.68;
}
var options = {
chart: {
type: 'line',
Expand All @@ -352,6 +359,33 @@ def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname="ch
start: 'minute', offset: '-12h'
},
"""
else:
text += """
<script>
window.onresize = function(){ location.reload(); };
var width = window.innerWidth;
var height = window.innerHeight;
width = width / 3 * 2;
height = height / 3 * 2;
if (height * 1.68 > width) {
height = width / 1.68;
}
else {
width = height * 1.68;
}
var options = {
chart: {
type: 'line',
width: width,
height: height
},
span: {
start: 'day'
},
"""

text += " series: [\n"
first = True
opacity = []
Expand Down Expand Up @@ -795,7 +829,7 @@ async def html_charts(self, request):
args = request.query
chart = args.get("chart", "Battery")
self.default_page = "./charts?chart={}".format(chart)
text = self.get_header("Predbat Charts", refresh=60 * 5)
text = self.get_header("Predbat Charts", refresh=60*5)
text += "<body>\n"
text += "<h2>{} Chart</h2>\n".format(chart)
text += '- <a href="./charts?chart=Battery">Battery</a> '
Expand All @@ -816,7 +850,7 @@ async def html_apps(self, request):
Render apps.yaml as an HTML page
"""
self.default_page = "./apps"
text = self.get_header("Predbat Apps.yaml", refresh=60 * 5)
text = self.get_header("Predbat Apps.yaml", refresh=60*5)
text += "<body>\n"
text += "<a href='./debug_apps'>apps.yaml</a><br>\n"
text += "<table>\n"
Expand Down Expand Up @@ -920,7 +954,9 @@ async def html_compare(self, request):
"""
Return the Predbat compare page as an HTML page
"""
text = self.get_header("Predbat Compare", refresh=60)
self.default_page = "./compare"

text = self.get_header("Predbat Compare", refresh=5*60)

text += "<body>\n"
text += '<form class="form-inline" action="./compare" method="post" enctype="multipart/form-data" id="compareform">\n'
Expand All @@ -937,7 +973,7 @@ async def html_compare(self, request):
text += "<table>\n"
text += "<tr><th>ID</th><th>Name</th><th>Date</th><th>Metric</th><th>Cost</th><th>Cost 10%</th><th>Export</th><th>Import</th><th>Final SOC</th><th>Iboost</th><th>Carbon</th><th>Result</th>\n"

compare_list = self.base.get_arg("compare_list", [])
compare_list = self.base.get_arg('compare_list', [])

for compare in compare_list:
name = compare.get("name", "")
Expand All @@ -960,13 +996,11 @@ async def html_compare(self, request):
best = result.get("best", False)
existing_tariff = result.get("existing_tariff", False)

selected = "<td bgcolor=#FFaaaa>Best<td>" if best else "<td>&nbsp;</td>"
selected = '<td bgcolor=#FFaaaa>Best<td>' if best else "<td>&nbsp;</td>"
if existing_tariff:
selected += "<td bgcolor=#aaFFaa>Existing<td>"
selected += '<td bgcolor=#aaFFaa>Existing<td>'

text += "<tr><td><a href='#heading-{}'>{}</a></td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td>{}\n".format(
id, id, name, date, metric, cost, cost10, export, imported, soc, final_iboost, final_carbon_g, selected
)
text += "<tr><td><a href='#heading-{}'>{}</a></td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td>{}\n".format(id, id, name, date, metric, cost, cost10, export, imported, soc, final_iboost, final_carbon_g, selected)

text += "</table>\n"

Expand All @@ -976,21 +1010,16 @@ async def html_compare(self, request):
for compare in compare_list:
name = compare.get("name", "")
id = compare.get("id", "")
series_data.append({"name": name, "data": self.compare_hist.get(id, {}).get("metric", {}), "opacity": "1.0", "stroke_width": "2", "stroke_curve": "smooth", "chart_type": "bar"})
series_data.append({"name" : name, "data" : self.compare_hist.get(id, {}).get("metric", {}), "chart_type": "bar"})
series_data.append({"name" : "Actual", "data" : self.cost_yesterday_hist, "chart_type": "bar"})
series_data.append({"name" : "Car actual", "data" : self.cost_yesterday_car_hist, "chart_type": "bar"})

now_str = self.base.now_utc.strftime(TIME_FORMAT)
text += self.render_chart(series_data, self.base.currency_symbols[1], "Tariff Comparison - metric", now_str)

# Charts
# text += '<div id="chart2"></div>'
# series_data = []
# for compare in compare_list:
# name = compare.get("name", "")
# id = compare.get("id", "")
# series_data.append({"name" : name, "data" : self.compare_hist.get(id, {}).get("cost", {}), "opacity" : "1.0", "stroke_width" : "2", "stroke_curve" : "smooth"})

# now_str = self.base.now_utc.strftime(TIME_FORMAT)
# text += self.render_chart(series_data, self.base.currency_symbols[1], "Tariff Comparison - cost", now_str, tagname='chart2')
if self.compare_hist:
text += self.render_chart(series_data, self.base.currency_symbols[1], "Tariff Comparison - metric", now_str, fullsize=False)
else:
text += "<br><h2>Loading chart (please wait)...</h2><br>"

# HTML Plans
for compare in compare_list:
Expand All @@ -1008,7 +1037,7 @@ async def html_compare(self, request):
if html:
text += html
else:
text += "<p>No data yet</p>"
text += "<br><p>No data yet....</p>"

text += "</body></html>\n"
return web.Response(content_type="text/html", text=text)
Expand Down

0 comments on commit 01bd0dc

Please sign in to comment.