Use Cases
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 case | Trigger | Inbound adapter | Events emitted |
|---|---|---|---|
RecordObservation | a consumed fact about a node | internal/adapters/inbound/kafka consumers (group node-performance) | none |
Recompute | timer, batched per minute | ticker in the composition root cmd/node-performance | NodeScorecardUpdated, 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 fulltype. 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
datacarries anodeId. -
Main flow:
- Decode the envelope and map
datato anObservation: the source CloudEventsid, thetype, thenodeId, the envelopetime, and the source fields a metric reads (for examplepoNumber,requiredShipBy,ackBy,acceptedAt,handedOverAt,appliedAt). - Begin a transaction.
- Insert the CloudEvents
idintoprocessed_events. If it is already there, commit, acknowledge the offset and stop. - Load the scorecards
nodeId:7dandnodeId:30d. A missing one is createdPROVISIONALwith no observations. - Call
NodeScorecard.RecordObservation(obs)on each. - Save both with their version check, mark them due for
Recompute. - Commit, then acknowledge the Kafka offset.
- Decode the envelope and map
-
Errors:
Error HTTP status Problem typeConsumer behaviour ErrDuplicateObservationnone (consumer-side; the REST contract has no such problem type, ADR 0002 A18) none Treated as success: commit the inbox row, acknowledge. Stale version on save (infrastructure) none (consumer-side) none Roll back and retry the message. Undecodable message or missing nodeId, or an unknown purchase order (ErrValidation,ErrPurchaseOrderUnknown)none (consumer-side) none Retried or sent to the dead-letter path; the REST validation-failedis only for the HTTP routes. -
Events emitted: none.
-
Idempotency: the inbox on the CloudEvents
id; behind it,ErrDuplicateObservationon 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:
- List due scorecards.
- For each, begin a transaction and load it.
- Call
NodeScorecard.Recompute(now, minObservations, grading)(minObservationsis 50, ADR 0002 A5, gap G33):- drop observations older than the window;
- compute
Metrics(each rate in[0,1],ErrRateOutOfRangeotherwise) andobservations; - if
observations < minObservations, set statePROVISIONALand gradePROVISIONAL; otherwise set stateGRADEDand the grade by theGradingpolicy; - compare each metric with the previous one under
ThresholdAlertand record aNodeThresholdBreachedfor each downward crossing.
- Save with the version check; write
NodeScorecardUpdatedand anyNodeThresholdBreachedtooutbox_eventsin the same transaction. - Commit.
-
Errors:
Error HTTP status Problem typeTimer behaviour ErrRateOutOfRangenone (timer-side; not an HTTP answer) none A defect: log, skip this scorecard, keep the pass going. ErrInsufficientObservationsnone none Not reached by Recompute, which choosesPROVISIONALfor a thin window; it guards any other path that would set a grade.Stale version on save (infrastructure) none none Roll back; the scorecard stays due and the next pass retries. -
Events emitted:
NodeScorecardUpdatedafter every saved recompute;NodeThresholdBreachedonce 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 CloudEventsid; consumers keep the latest pernodeIdandwindow.
Queries (not use cases)
| Route | Caller | Reads | Errors |
|---|---|---|---|
GET /scorecards/{nodeId} | vendor-connectivity (GetScorecard) | both scorecards of the node | 404 not-found (ErrNotFound) until the first observation of the node |
GET /scorecards?grade= | not named in the model | scorecards 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.