Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ changes what your listeners see, it says so.
technology, the current breadcrumb is marked, and a refused sign-in puts the cursor on the
password.

### Fixed — the statistics, the rest of the way

- **The day boundaries were an hour out on clock-change days in Sydney, Auckland, Lord
Howe and Santiago.** Local midnight was found by probing the zone's offset once, at a
moment ten or eleven hours later than the real one — on the far side of a 02:00 change.
A second probe lands it. London and New York were never affected, and every day of a
year in eight zones is now checked by a test.
- **Deleting an episode no longer deletes its history.** The request log cascaded away
with the episode row, so the show's and the instance's totals shrank. The rows stay,
and the log says "An episode since deleted" where the title was. Removing a show still
removes its log, as it should.
- **"vs the previous 30 days" compares like with like.** The current period runs to
tomorrow and so held twenty-nine days and a morning, measured against thirty whole
days: every card read "down" until the evening. The previous period is now measured
to the same point, and the card says so.
- **Filtering the statistics page to a show filters the whole page.** The link from a
show's page landed on instance-wide cards, chart and tables above a show-only log. Now
every figure follows the show, with a note and a link back to every show.
- **"Failed requests" and "Failures only" agree.** The card counted only audio that
failed; the log showed every failure, a cover that would not serve included. Both now
count every kind. Bytes and "episodes fetched" are still audio only.
- **The CSV holds every row.** It stopped quietly at fifty thousand while the page
promised all of them; it is now streamed, batch by batch, with no ceiling.

### Changed — the advert pass no longer gets in the way of listeners

- **Reading an episode no longer stalls everything else.** Decoding an hour-long MP3 to
Expand Down
39 changes: 39 additions & 0 deletions src/db/migrations/013-media-access-keeps-history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- Request history outlives the episode it was about.
--
-- `media_access.episode_id` cascaded on delete, so removing an episode — from the
-- page, or by deleting its file — also removed every request ever made for it, and
-- the show's and the instance's totals shrank to match. A download that happened
-- happened; the log is a record of it, not a property of the episode row. The link
-- to `episodes` is dropped (the id is kept, so the log can still say which episode
-- it was and the page can say it is gone), and the link to `shows` keeps cascading:
-- removing a show is the one case where the owner means to forget it.
--
-- SQLite cannot alter a foreign key in place, so the table is rebuilt. Additive in
-- effect — every row and index comes back as it was — and an older image reading
-- this table sees exactly the columns it expects.
CREATE TABLE media_access_kept (
id INTEGER PRIMARY KEY AUTOINCREMENT,
episode_id TEXT,
show_id TEXT REFERENCES shows(id) ON DELETE CASCADE,
requested_at TEXT NOT NULL,
kind TEXT NOT NULL,
status_code INTEGER NOT NULL,
bytes_sent INTEGER,
total_bytes INTEGER,
range_header TEXT,
client TEXT,
error TEXT
);

INSERT INTO media_access_kept
(id, episode_id, show_id, requested_at, kind, status_code, bytes_sent, total_bytes, range_header, client, error)
SELECT id, episode_id, show_id, requested_at, kind, status_code, bytes_sent, total_bytes, range_header, client, error
FROM media_access;

DROP TABLE media_access;
ALTER TABLE media_access_kept RENAME TO media_access;

CREATE INDEX idx_media_access_episode ON media_access(episode_id, requested_at DESC);
CREATE INDEX idx_media_access_show ON media_access(show_id, requested_at DESC);
CREATE INDEX idx_media_access_time ON media_access(requested_at DESC);
CREATE INDEX idx_media_access_failures ON media_access(status_code, requested_at DESC);
31 changes: 26 additions & 5 deletions src/lib/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,15 @@ export function toLocalInputValue(value, { timeZone } = {}) {

/**
* Reads a `datetime-local` value back, interpreting it in the given zone.
* Works by probing the offset that zone had at roughly that instant, which
* handles DST correctly for every case except the one ambiguous hour when
* clocks go back — where either interpretation is defensible.
*
* The zone's offset is probed at the wall-clock instant read as UTC, and then once
* more at the instant that first guess produced. One probe was enough for London
* and New York, where the clocks change in the small hours and the first guess
* lands on the right side of the change; it was an hour out at midnight on the
* transition days in Sydney, Auckland and Santiago, where the guess — ten or eleven
* hours later than the real instant — fell on the far side of a 02:00 change. The
* second probe catches that. The one ambiguous hour when clocks go back has two
* defensible answers; the earlier offset wins.
*/
export function fromLocalInputValue(input, { timeZone } = {}) {
if (typeof input !== 'string' || !input.trim()) return null;
Expand All @@ -127,8 +133,23 @@ export function fromLocalInputValue(input, { timeZone } = {}) {
const [, y, mo, d, h, mi, s = '00'] = match;
const asUtc = Date.UTC(+y, +mo - 1, +d, +h, +mi, +s);
if (!timeZone) return new Date(asUtc).toISOString();
const offsetMs = zoneOffsetMs(new Date(asUtc), timeZone);
return new Date(asUtc - offsetMs).toISOString();
const firstOffset = zoneOffsetMs(new Date(asUtc), timeZone);
const firstGuess = asUtc - firstOffset;
const secondOffset = zoneOffsetMs(new Date(firstGuess), timeZone);
if (secondOffset === firstOffset) return new Date(firstGuess).toISOString();
const secondGuess = asUtc - secondOffset;
// Two different answers: the wall-clock time asked for sits beside a clock change.
// Each guess is judged by what it reads back as in the zone. One that reads back
// exactly wins (the earlier instant on the one night a time occurs twice). In the
// hour the clocks skip no instant reads back exactly, so the first instant on the
// far side of the gap wins — it is the start of the day that was asked for, which
// is what a midnight is used for; the other guess belongs to the day before.
const readback = (instant) => instant + zoneOffsetMs(new Date(instant), timeZone);
const candidates = [firstGuess, secondGuess].sort((a, b) => a - b);
const exact = candidates.filter((instant) => readback(instant) === asUtc);
if (exact.length) return new Date(exact[0]).toISOString();
const onOrAfter = candidates.filter((instant) => readback(instant) >= asUtc);
return new Date(onOrAfter.length ? onOrAfter[0] : candidates[candidates.length - 1]).toISOString();
}

function zoneOffsetMs(date, timeZone) {
Expand Down
24 changes: 18 additions & 6 deletions src/lib/time-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,40 @@ export const DEFAULT_RANGE = '30d';
* edge at `from`, so no event falls in both and none falls in the gap between them.
* Deriving it by subtracting a duration from `from` instead would drift by an hour
* across a transition and quietly move events between the two periods being compared.
*
* `prevTo` is where the comparison stops: the same distance into the previous period
* as `now` is into this one. The current period ends tomorrow, so at nine in the
* morning it holds `days - 1` whole days and nine hours; measured against `days`
* whole days it read "down" every morning and caught up every evening. The same
* elapsed time in both windows is the comparison the card claims to make.
*/
export function resolveRange(key, { timeZone, now = new Date() } = {}) {
const resolvedKey = Object.prototype.hasOwnProperty.call(RANGES, key) ? key : DEFAULT_RANGE;
const { days, label, lede } = RANGES[resolvedKey];

if (days === null) {
return { key: resolvedKey, label, lede, days: null, from: null, to: null, prevFrom: null };
return { key: resolvedKey, label, lede, days: null, from: null, to: null, prevFrom: null, prevTo: null };
}

const instant = now instanceof Date ? now : new Date(now);
const today = localDay(Number.isNaN(instant.getTime()) ? new Date() : instant, timeZone);
const moment = Number.isNaN(instant.getTime()) ? new Date() : instant;
const today = localDay(moment, timeZone);

// `days - 1` back, not `days`: "the last 7 days" is seven calendar days counting
// today, which is what the label promises the reader.
const from = localMidnight(shiftDay(today, -(days - 1)), timeZone);
const prevFrom = localMidnight(shiftDay(today, -(2 * days - 1)), timeZone);
const elapsedMs = moment.getTime() - new Date(from).getTime();

return {
key: resolvedKey,
label,
lede,
days,
// `days - 1` back, not `days`: "the last 7 days" is seven calendar days counting
// today, which is what the label promises the reader.
from: localMidnight(shiftDay(today, -(days - 1)), timeZone),
from,
to: localMidnight(shiftDay(today, 1), timeZone),
prevFrom: localMidnight(shiftDay(today, -(2 * days - 1)), timeZone),
prevFrom,
prevTo: new Date(new Date(prevFrom).getTime() + Math.max(0, elapsedMs)).toISOString(),
};
}

Expand Down
Loading