Skip to content

Commit

Permalink
Calbacks for saving to validated, resetting
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbland committed Feb 1, 2024
1 parent 82edd74 commit 72c4dad
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 31 deletions.
215 changes: 184 additions & 31 deletions validated/dash_apps/finished_apps/daily_validation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from datetime import datetime
from decimal import Decimal

import dash
import plotly.graph_objects as go
from dash import dcc, html
from dash import Input, Output, State, dcc, html
from dash_ag_grid import AgGrid
from django_plotly_dash import DjangoDash

from station.models import Station
from validated.functions import daily_validation, detail_list
from validated.tables import create_daily_table, create_detail_table
from validated.functions import (
daily_validation,
detail_list,
get_conditions,
reset_detail_validated,
save_detail_to_validated,
save_to_validated,
)
from validated.tables import create_columns_daily, create_columns_detail
from variable.models import Variable

DEFAULT_FONT = "Open Sans, Raleway, Dosis, Ubuntu, sans-serif"
Expand Down Expand Up @@ -43,38 +52,81 @@
)

# Tables
table_daily = create_daily_table(data)
table_detail = create_detail_table(data_detail)
table_daily = AgGrid(
id="table_daily",
rowData=data["data"],
columnDefs=create_columns_daily(value_columns=data["value_columns"]),
columnSize="sizeToFit",
defaultColDef={
"resizable": True,
"sortable": True,
"checkboxSelection": {
"function": "params.column == params.columnApi.getAllDisplayedColumns()[0]"
},
"headerCheckboxSelection": {
"function": "params.column == params.columnApi.getAllDisplayedColumns()[0]"
},
"headerCheckboxSelectionFilteredOnly": True,
},
dashGridOptions={"rowSelection": "multiple", "suppressRowClickSelection": True},
selectAll=True,
)

table_detail = AgGrid(
id="table_detail",
rowData=data_detail["series"],
columnDefs=create_columns_detail(value_columns=data_detail["value_columns"]),
columnSize="sizeToFit",
defaultColDef={
"resizable": True,
"sortable": True,
"checkboxSelection": {
"function": "params.column == params.columnApi.getAllDisplayedColumns()[0]"
},
"headerCheckboxSelection": {
"function": "params.column == params.columnApi.getAllDisplayedColumns()[0]"
},
"headerCheckboxSelectionFilteredOnly": True,
},
dashGridOptions={"rowSelection": "multiple", "suppressRowClickSelection": True},
selectAll=True,
)


# Plot
plot = go.Figure()
plot.add_trace(
go.Scatter(
x=data["series"]["measurement"]["time"],
y=data["series"]["measurement"]["average"],
mode="lines",
name="Measurement",
line=dict(color="black"),
def create_plot(data):
plot = go.Figure()
plot.add_trace(
go.Scatter(
x=data["series"]["measurement"]["time"],
y=data["series"]["measurement"]["average"],
mode="lines",
name="Measurement",
line=dict(color="black"),
)
)
)
plot.add_trace(
go.Scatter(
x=data["series"]["selected"]["time"],
y=data["series"]["selected"]["average"],
mode="lines",
name="Selected",
line=dict(color="#636EFA"),
plot.add_trace(
go.Scatter(
x=data["series"]["selected"]["time"],
y=data["series"]["selected"]["average"],
mode="lines",
name="Selected",
line=dict(color="#636EFA"),
)
)
)
plot.add_trace(
go.Scatter(
x=data["series"]["validated"]["time"],
y=data["series"]["validated"]["average"],
mode="lines",
name="Validated",
line=dict(color="#00CC96"),
plot.add_trace(
go.Scatter(
x=data["series"]["validated"]["time"],
y=data["series"]["validated"]["average"],
mode="lines",
name="Validated",
line=dict(color="#00CC96"),
)
)
)
return plot


line_graph = create_plot(data=data)

# Layout
app.layout = html.Div(
Expand All @@ -84,15 +136,116 @@
style={"font-family": DEFAULT_FONT},
),
table_daily,
html.Button("Save to Validated", id="daily-save-button"),
html.Div(
id="daily-status-message",
children=[""],
style={"font-family": DEFAULT_FONT},
),
html.H1(
children="Detail of Selected Day",
style={"font-family": DEFAULT_FONT},
),
table_detail,
html.Button("Save to Validated", id="detail-save-button"),
html.Button("Reset Validated", id="detail-reset-button"),
html.Div(
id="detail-status-message",
children=[""],
style={"font-family": DEFAULT_FONT},
),
html.H1(
children="Plot",
style={"font-family": DEFAULT_FONT},
),
dcc.Graph(figure=plot),
dcc.Graph(id="plot", figure=line_graph),
]
)


@app.callback(
[
Output("daily-status-message", "children"),
Output("detail-status-message", "children"),
Output("plot", "figure"),
],
[
Input("daily-save-button", "n_clicks"),
Input("detail-save-button", "n_clicks"),
Input("detail-reset-button", "n_clicks"),
],
[
State("table_daily", "selectedRows"),
State("table_detail", "selectedRows"),
State("table_detail", "rowData"),
],
prevent_initial_call=True,
)
def combined_callback(
daily_save_clicks,
detail_save_clicks,
detail_reset_clicks,
daily_selected_rows,
detail_selected_rows,
detail_row_data,
):
global data_detail

ctx = dash.callback_context
if not ctx.triggered:
return dash.no_update
else:
button_id = ctx.triggered[0]["prop_id"].split(".")[0]

# Daily save
if button_id == "daily-save-button" and daily_selected_rows is not None:
conditions = get_conditions(daily_selected_rows)
save_to_validated(
variable=variable,
station=station,
to_delete=conditions,
start_date=start_time,
end_date=end_time,
minimum=minimum,
maximum=maximum,
)
daily_status = f"{len(daily_selected_rows)} entries saved to Validated"
detail_status = dash.no_update

# Detail save
elif button_id == "detail-save-button" and detail_selected_rows is not None:
save_detail_to_validated(
data_list=detail_selected_rows,
variable=variable,
station=station,
)

daily_status = dash.no_update
detail_status = f"{len(detail_selected_rows)} entries saved to Validated"

# Detail reset
elif button_id == "detail-reset-button":
reset_detail_validated(
data_list=detail_row_data,
variable=variable,
station=station,
)

daily_status = dash.no_update
detail_status = "Validation reset"

else:
daily_status = dash.no_update
detail_status = dash.no_update

# Reload data and redraw plot
data = daily_validation(
station=station,
variable=variable,
start_time=start_time,
end_time=end_time,
minimum=minimum,
maximum=maximum,
)
figure = create_plot(data)
return daily_status, detail_status, figure
22 changes: 22 additions & 0 deletions validated/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,28 @@ def save_detail_to_validated(
return len(insert_result) == len(model_instances)


def reset_detail_validated(data_list, variable: Variable, station: Station):
"""TODO.
Args:
start_time: TODO.
end_time: TODO.
variable: The variable to update.
station: The station this records relate to.
Returns:
True if the selected data is inserted successfully in the database.
"""
start_time = data_list[0]["time"]
end_time = data_list[-1]["time"]

validated = apps.get_model(app_label="validated", model_name=variable.variable_code)
validated.timescale.filter(
time__range=[start_time, end_time],
station_id=station.station_id,
).delete()


def data_report(
temporality: str,
station: Station,
Expand Down

0 comments on commit 72c4dad

Please sign in to comment.