Ask a provider for a classification once, even when it has none

Merged
JA
2mo ago
+161-4

Summary

A capable securities provider that answered a classification request with nothing (no sector, no industry, an unmapped kind) was asked again on every sync. Nothing recorded that it had already been asked. With EODHD each ask spends one of the 20 daily requests prices share. This PR records a successful ask and stops asking after it. It also fixes the reset path, which kept industry and so never let the provider answer again.

Fixes jaysbeekay/sure#348. Decides drop 3's U2 question (jaysbeekay/sure#122, option A); this goes upstream inside U2 (part of we-promise/sure#3494).

What changed

  • Migration: a nullable securities.classification_fetched_at datetime, with no default and no backfill.
  • Security::Provided#import_provider_details:
    • wants_classification also requires classification_fetched_at.nil?.
    • On a successful response it stamps classification_fetched_at with update_column, whether or not anything came back, the same way store_constituents stamps constituents_fetched_at.
    • A failed response isn't stamped, so it's retried.
    • Capability still gates the ask, not the stamp.
  • HoldingsController#reset_classification also clears industry and classification_fetched_at, so a reset asks the provider again.

Deliberately not changed:

  • Time-based re-asking. A reset, or clear_cache: true, re-asks.
  • Which providers declare classification.
  • Per-provider budget accounting.

Migration notes

One nullable column, no default, no backfill, reversible (add_column in change). Existing securities start un-stamped, so each is asked once more and then settles.

Verification

Tests were written first. On main, four fail:

  • Security::ClassificationIngestionTest:
    • "a capable provider that answers with nothing is asked once": no record was written.
    • "a failed answer is not recorded, so the next sync asks again": there was no record after the successful retry.
    • "an answer with a sector is written and recorded".
  • HoldingsClassificationTest: "a reset clears the industry and the record of an earlier answer, so the provider is asked again" failed on "the reset kept the industry".

These guards pass before and after:

  • a provider that cannot classify leaves no record;
  • a locked security leaves no record;
  • clear_cache asks a recorded security again.

Breaking the fix on purpose fails a test each time:

BreakageCaught by
Nil-stamp term dropped from the gateasked once
Stamp written on a failed responsefailed answer is retried
Stamp written only when data came backasked once, failed answer is retried
Reset keeps the stampreset clears the record
Reset keeps industryreset clears the industry

Commands:

  • bin/rails test on ee111cd73: 12,335 runs, 53,066 assertions, 0 failures, 0 errors, 35 skips.
  • bin/rubocop on the changed files: no offenses.
  • bin/brakeman --no-pager: 0 warnings.

Blast radius

  • import_provider_details has three callers:
    • MarketDataImporter and Account::MarketDataImporter, which pass include_classification: true;
    • HoldingsController#sync_prices, which passes neither keyword, so classification isn't wanted and it's unaffected.
  • update_column skips callbacks, so apply_classification_defaults doesn't run for the stamp. Classification attributes still go through update(attrs) as before.
  • Siblings:
    • reset_classification is the only writer that clears classification.
    • The AI classification proposal writes its own source and lock, and is unchanged.
    • Display readers of classification_source, sector and industry don't read the stamp.