Refactor Plan & Todo – SolarWine Baseline
This file tracks the refactor steps for Gemini utilities, phenology, time features, IMS coverage, and performance/caching improvements.
High Priority
Unify Gemini client & JSON handling
-
genai_utils: verifyget_google_api_key,get_genai_client,extract_json_object(already created). -
day_ahead_advisor:- Use
extract_json_objectvia local_extract_jsonwrapper. - Confirm
api_keyandclientnow callgenai_utils.
- Use
-
llm_data_engineer:- Replace
api_keyproperty withget_google_api_key(self._api_key). - Replace
clientproperty withget_genai_client(self._api_key). - Swap local
_extract_jsonto useextract_json_object.
- Replace
-
routing_agent:- Replace
api_keyproperty withget_google_api_key(self._api_key). - Replace
clientproperty withget_genai_client(self._api_key).
- Replace
-
vineyard_chatbot:- Replace
api_keyproperty withget_google_api_key(self._api_key). - Replace
clientproperty withget_genai_client(self._api_key). - Make
has_api_keycallget_google_api_keyin a try/except instead of duplicating logic.
- Replace
-
Centralise phenology logic
-
phenology.py: confirmestimate_stage_for_dateandstage_id_and_description_for_datematch the biology plan and skills. -
app.py(Vineyard Advisor vine snapshot):- Replace inline month→stage mapping with
stage_id_and_description_for_date(today).
- Replace inline month→stage mapping with
- Any future modules that reason about "current stage" should import from
phenologyinstead of re-implementing month logic.
-
IMS cache coverage & observability
-
IMSClient.fetch_and_cache:- Add lightweight logging or counters for skipped chunks (date ranges that failed).
-
app.py– Forecasting tab:- Add a small IMS coverage panel that shows:
- First timestamp in cache
- Last timestamp in cache
- Number of days of coverage
- Age (minutes/hours) of the latest record
- Add a small IMS coverage panel that shows:
-
app.py– Vineyard Advisor tab:- Reuse the cached vine snapshot to also display IMS coverage (not just last point) where useful.
-
Medium Priority
Shared time-feature utilities
-
time_features.py: confirmadd_cyclical_time_featuresAPI (timestamp column or index). -
preprocessor.create_time_features:- Replace manual sin/cos code with
add_cyclical_time_features(df, timestamp_col="timestamp_utc"). - Keep raw
monthandday_of_yearfeatures as now.
- Replace manual sin/cos code with
-
chronos_forecaster.load_data:- Replace inline
hour_sin/cosanddoy_sin/coscomputations withadd_cyclical_time_features(resampled, index_is_timestamp=True).
- Replace inline
-
llm_data_engineer.engineer_features:- Replace its own cyclical feature block with a call to
add_cyclical_time_featureson the resolved timestamp.
- Replace its own cyclical feature block with a call to
-
Cache heavy shading simulations
-
tracker_optimizer.simulate_tilt_angles:- Add a simple in-module cache (e.g. dict keyed by
(len(df), tuple(sorted(angles)))or(date_min, date_max, angles)for the sample dataset). - Ensure caching does not break if
dfchanges (avoid caching across different data sources).
- Add a simple in-module cache (e.g. dict keyed by
-
vineyard_chatbot._tool_compare_angles:- Confirm it benefits from the new
simulate_tilt_anglescache (repeated queries reuse results).
- Confirm it benefits from the new
- Optionally expose precomputed angle summaries in the Shading tab for instant advisor-like responses.
-
Use phenology helper in future logic
- When adding or refactoring:
- Day-ahead planner
- Energy budget planner
- Operational modes / real-time control
- Always derive phenological stage via
phenology.estimate_stage_for_dateorstage_id_and_description_for_dateinstead of hand-written month rules.
- When adding or refactoring:
Low Priority (later)
- Vectorise
FarquharModel.compute_allif Stage 1 datasets grow significantly or are needed in near real-time. - Design and implement
thingsboard_clientaccording to thethingsboard-clientskill, then gradually wire real-time vine telemetry (soil moisture, irrigation, local temps) into Vineyard Advisor and future control logic. - Add focused tests for:
-
FarquharModel(known inputs, NaN handling). -
Preprocessor(merge, split, edge cases). -
SolarGeometry/ShadowModel(tracker tilt, mask shape). -
ChronosForecaster.load_data(grid coverage, growing-season filter, time features).
-