Skip to main content

Use Cases

As built

Derived from network/model/model.yaml. This context's service is built; the model carries its as-built contracts, and identifiers (Err... names, routes, CloudEvents types) are the names the service uses.

One section per command of aggregates[NodeScorecard].commands. The use cases live in the proposed package internal/application/usecases. The step-by-step runtime view is on Sequence Diagrams.

Neither command has a REST route: RecordObservation is driven by Kafka and Recompute by a timer. The HTTP status and problem type in each errors table are therefore the mapping a future administrative route must use, and what the error means for the consumer or the timer today. The two REST routes are reads; they are listed at the end.

Triggers at a glance​

Use caseTriggerInbound adapterEvents emitted
RecordObservationa consumed fact about a nodeinternal/adapters/inbound/kafka consumers (group node-performance)none
Recomputetimer, batched per minuteticker in the composition root cmd/node-performanceNodeScorecardUpdated, NodeThresholdBreached

How save and publish commit​

Every use case that changes a NodeScorecard saves it, writes its events to outbox_events, and (for consumers) writes the processed_events inbox row, all in one Postgres transaction. A relay drains the outbox to retailnetwork.node-performance.events. The aggregate row carries a version; a save with a stale version fails and the whole transaction is retried.

RecordObservation​

Counts one observed fact on the node's two scorecards.

  • Actor / trigger: a consumed CloudEvent of one of these types: com.retailnetwork.partner.vendor-connectivity.purchaseorder.PurchaseOrderIssued, com.retailnetwork.partner.vendor-connectivity.purchaseorder.PurchaseOrderAccepted, com.retailnetwork.partner.vendor-connectivity.purchaseorder.PurchaseOrderRejected, com.retailnetwork.partner.vendor-connectivity.purchaseorder.PurchaseOrderExpired, com.retailnetwork.partner.vendor-connectivity.purchaseorder.VendorShipmentConfirmed, com.retailnetwork.logistics.shipping-and-tracking.journey.PackageHandedOver, com.retailnetwork.commerce.delivery-promise.promise.PromiseKept, com.retailnetwork.commerce.delivery-promise.promise.PromiseBroken, com.retailnetwork.commerce.offer-availability.inventoryfeed.InventoryFeedApplied.

  • Inbound adapter: internal/adapters/inbound/kafka, one consumer per topic (retailnetwork.vendor-connectivity.events, retailnetwork.shipping-and-tracking.events, retailnetwork.delivery-promise.events, retailnetwork.offer-availability.events, retailnetwork.node-topology.events), dispatching on the full type. The four types with no use in the model (VendorOnboarded, SubmissionProcessed, NodeRegistered, NodeOperationalProfileDeclared) are written to the inbox and acknowledged without calling this use case.

  • Preconditions: the message is a valid CloudEvent; the data carries a nodeId.

  • Main flow:

    1. Decode the envelope and map data to an Observation: the source CloudEvents id, the type, the nodeId, the envelope time, and the source fields a metric reads (for example poNumber, requiredShipBy, ackBy, acceptedAt, handedOverAt, appliedAt).
    2. Begin a transaction.
    3. Insert the CloudEvents id into processed_events. If it is already there, commit, acknowledge the offset and stop.
    4. Load the scorecards nodeId:7d and nodeId:30d. A missing one is created PROVISIONAL with no observations.
    5. Call NodeScorecard.RecordObservation(obs) on each.
    6. Save both with their version check, mark them due for Recompute.
    7. Commit, then acknowledge the Kafka offset.
  • Errors:

    ErrorHTTP statusProblem typeConsumer behaviour
    ErrDuplicateObservationnone (consumer-side; the REST contract has no such problem type, ADR 0002 A18)noneTreated as success: commit the inbox row, acknowledge.
    Stale version on save (infrastructure)none (consumer-side)noneRoll back and retry the message.
    Undecodable message or missing nodeId, or an unknown purchase order (ErrValidation, ErrPurchaseOrderUnknown)none (consumer-side)noneRetried or sent to the dead-letter path; the REST validation-failed is only for the HTTP routes.
  • Events emitted: none.

  • Idempotency: the inbox on the CloudEvents id; behind it, ErrDuplicateObservation on the same source event id inside the aggregate.

Recompute​

Recomputes one scorecard's metrics over its window and grades it.

  • Actor / trigger: the per-minute timer ("recompute batched per minute").

  • Inbound adapter: a ticker started in cmd/node-performance (the fleet's composition-root pattern), calling the use case once per pass.

  • Preconditions: the scorecard exists and is due: it received an observation since the last pass, or an observation is about to leave its window.

  • Main flow:

    1. List due scorecards.
    2. For each, begin a transaction and load it.
    3. Call NodeScorecard.Recompute(now, minObservations, grading) (minObservations is 50, ADR 0002 A5, gap G33):
      1. drop observations older than the window;
      2. compute Metrics (each rate in [0,1], ErrRateOutOfRange otherwise) and observations;
      3. if observations < minObservations, set state PROVISIONAL and grade PROVISIONAL; otherwise set state GRADED and the grade by the Grading policy;
      4. compare each metric with the previous one under ThresholdAlert and record a NodeThresholdBreached for each downward crossing.
    4. Save with the version check; write NodeScorecardUpdated and any NodeThresholdBreached to outbox_events in the same transaction.
    5. Commit.
  • Errors:

    ErrorHTTP statusProblem typeTimer behaviour
    ErrRateOutOfRangenone (timer-side; not an HTTP answer)noneA defect: log, skip this scorecard, keep the pass going.
    ErrInsufficientObservationsnonenoneNot reached by Recompute, which chooses PROVISIONAL for a thin window; it guards any other path that would set a grade.
    Stale version on save (infrastructure)nonenoneRoll back; the scorecard stays due and the next pass retries.
  • Events emitted: NodeScorecardUpdated after every saved recompute; NodeThresholdBreached once per metric that crossed its threshold downward.

  • Idempotency: the result is a function of the observations in the window and now. A pass that runs twice on the same data saves the same metrics and republishes the same values with a new CloudEvents id; consumers keep the latest per nodeId and window.

Queries (not use cases)​

RouteCallerReadsErrors
GET /scorecards/{nodeId}vendor-connectivity (GetScorecard)both scorecards of the node404 not-found (ErrNotFound) until the first observation of the node
GET /scorecards?grade=not named in the modelscorecards with that current grade; optional window, limit (1 to 500, default 100) and after (keyset on scorecardId), with nextCursor when more rows exist (ADR 0002 A18)422 validation-failed (ErrValidation) for a grade or window outside the accepted values, or an over-long cursor

Both are in apis/node-performance/openapi.yaml. The only problem types of this context are not-found and validation-failed (ADR 0002 A18). Neither changes state, so neither takes an Idempotency-Key.