@mxpicture/tesla-superchargers-mate 0.1.44 → 0.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/sql/setup.sql +59 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mxpicture/tesla-superchargers-mate",
3
- "version": "0.1.44",
3
+ "version": "0.1.45",
4
4
  "description": "TeslaMate integration",
5
5
  "type": "module",
6
6
  "author": "MXPicture",
@@ -49,7 +49,7 @@
49
49
  "access": "public"
50
50
  },
51
51
  "dependencies": {
52
- "@mxpicture/tesla-superchargers-generated": "^0.1.44",
52
+ "@mxpicture/tesla-superchargers-generated": "^0.1.45",
53
53
  "postgres": "^3.4.9",
54
54
  "short-uuid": "^6.0.3",
55
55
  "zod": "^4.3.6"
package/sql/setup.sql CHANGED
@@ -151,18 +151,34 @@ END $$;
151
151
  ALTER TABLE pic_pricebooks
152
152
  ADD COLUMN IF NOT EXISTS fetched_at TIMESTAMPTZ;
153
153
 
154
- -- backfill: the current group was confirmed by the charger's last fetch (both
155
- -- carry that fetch's uuid); a superseded group was only ever seen at its own
156
- -- valid_since.
154
+ -- backfill: the current group was confirmed by the charger's last fetch; a
155
+ -- superseded group was only ever seen at its own valid_since. The current
156
+ -- group is the one with the max valid_since — resolving it via
157
+ -- c.last_fetch_uuid = p.last_fetch_uuid (as this first did) silently missed
158
+ -- every charger whose uuid had drifted off its pricebooks, and those fell
159
+ -- through to the fallback below: fetched_at = valid_since, which
160
+ -- pic_price_bound_v then reads as "last confirmed the day the price changed"
161
+ -- and truncates the site's history to that single day.
162
+ -- GREATEST over COALESCE keeps fetched_at >= valid_since even if the charger
163
+ -- row somehow predates its own current group (a zero-day interval would drop
164
+ -- the site from the history views entirely), and covers a NULL c.fetched_at
165
+ -- the same way COALESCE did.
157
166
  UPDATE pic_pricebooks p
158
167
  SET
159
- fetched_at = COALESCE(c.fetched_at, p.valid_since)
168
+ fetched_at = GREATEST(c.fetched_at, p.valid_since)
160
169
  FROM
161
170
  pic_chargers c
162
171
  WHERE
163
172
  c.id = p.pic_chargers_id
164
- AND c.last_fetch_uuid = p.last_fetch_uuid
165
- AND p.fetched_at IS NULL;
173
+ AND p.fetched_at IS NULL
174
+ AND p.valid_since = (
175
+ SELECT
176
+ max(cur.valid_since)
177
+ FROM
178
+ pic_pricebooks cur
179
+ WHERE
180
+ cur.pic_chargers_id = p.pic_chargers_id
181
+ );
166
182
 
167
183
  UPDATE pic_pricebooks
168
184
  SET
@@ -174,6 +190,35 @@ ALTER TABLE pic_pricebooks
174
190
  ALTER COLUMN fetched_at
175
191
  SET NOT NULL;
176
192
 
193
+ -- repair for databases the uuid-joined backfill above already ran on: it left
194
+ -- every drifted charger's current group at fetched_at = valid_since, and the
195
+ -- guard it used (fetched_at IS NULL) no longer holds, so re-running setup
196
+ -- cannot heal them. Raise those to the charger's last contact — the value the
197
+ -- corrected backfill would have written. Idempotent: after this runs
198
+ -- fetched_at > valid_since, so the row no longer matches; a no-op on a fresh
199
+ -- database (no rows) and on one that was migrated correctly (the current group
200
+ -- already carries the charger's fetched_at). A group whose only contact really
201
+ -- was its own creation is raised to the charger's last fetch too, which is at
202
+ -- worst the same claim the backfill makes; the next successful re-fetch
203
+ -- overwrites it with a real confirmation either way.
204
+ UPDATE pic_pricebooks p
205
+ SET
206
+ fetched_at = c.fetched_at
207
+ FROM
208
+ pic_chargers c
209
+ WHERE
210
+ c.id = p.pic_chargers_id
211
+ AND p.fetched_at = p.valid_since
212
+ AND c.fetched_at > p.fetched_at
213
+ AND p.valid_since = (
214
+ SELECT
215
+ max(cur.valid_since)
216
+ FROM
217
+ pic_pricebooks cur
218
+ WHERE
219
+ cur.pic_chargers_id = p.pic_chargers_id
220
+ );
221
+
177
222
  CREATE INDEX IF NOT EXISTS pic_pricebooks_loc_idx ON pic_pricebooks (pic_chargers_id);
178
223
 
179
224
  -- group lookups: current group / group at a point in time. Resolved by
@@ -660,6 +705,10 @@ SELECT
660
705
  h.start_uuid AS last_fetch_uuid,
661
706
  h.is_tou,
662
707
  h.time_zone,
708
+ -- how far this snapshot is actually known: its successor's valid_since, or
709
+ -- the last fetch that confirmed it. Carried through so a consumer can stop a
710
+ -- series where the knowledge stops instead of extending the last value on.
711
+ h."end" AS valid_until,
663
712
  EXTRACT(
664
713
  DOW
665
714
  FROM
@@ -681,6 +730,10 @@ FROM
681
730
  CREATE VIEW pic_price_history_v AS
682
731
  SELECT
683
732
  ((k.date + b.start_time) AT TIME ZONE k.time_zone) AS TIMESTAMP,
733
+ -- the instant this row's snapshot stops being known — see
734
+ -- pic_price_history_key_v. A panel drawing a step series must stop there;
735
+ -- carrying the last value past it claims a price nobody checked.
736
+ k.valid_until,
684
737
  k.pic_chargers_id,
685
738
  c.location_url_slug,
686
739
  k.last_fetch_uuid,