Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(data-warehouse): add five Fleetio tables for procurement and lookups (#107693)

Tom Owersd1589bd1fe4f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
index a1da9d6b7b4c..af1643af4424 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
@@ -3228,10 +3228,10 @@ Diffed against: <https://developer.fleetio.com/sitemap.xml>
- [x] `service_entry_line_items (v2)` — line-item detail behind each synced service entry - labor, parts and cost breakdown (high)
- [x] `work_order_statuses` — lookup resolving the status ID on every synced work_orders row (high)
- [x] `vehicle_statuses` — lookup resolving the status ID on every synced vehicles row (high)
-- [ ] `vehicle_types` — lookup resolving vehicle type IDs, the main breakdown dimension for fleet analysis (high)
-- [ ] `service_tasks` — lookup resolving the task IDs referenced by service entries, work orders and service reminders (high)
-- [ ] `vendors` — lookup resolving vendor IDs on service entries, fuel entries and purchase orders (high)
-- [ ] `purchase_orders (and purchase_order_line_items)` — parts procurement transactions and their line items; parts are synced but never their purchases (medium)
+- [x] `vehicle_types` — lookup resolving vehicle type IDs, the main breakdown dimension for fleet analysis (high)
+- [x] `service_tasks` — lookup resolving the task IDs referenced by service entries, work orders and service reminders (high)
+- [x] `vendors` — lookup resolving vendor IDs on service entries, fuel entries and purchase orders (high)
+- [x] `purchase_orders (and purchase_order_line_items)` — parts procurement transactions and their line items; parts are synced but never their purchases (medium)
- [ ] `faults` — diagnostic trouble code events streamed off vehicles - a primary reliability signal (medium)
- [ ] `location_entries` — vehicle location history, enabling utilization and route analysis (medium)
- [ ] `service_reminders` — upcoming and overdue maintenance state, the operational counterpart to the synced service entries (medium)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/canonical_descriptions.py
index 9d039cc44a71..b0844a944391 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/canonical_descriptions.py
@@ -202,6 +202,111 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"work_orders_count": "Number of work orders currently in this status.",
},
},
+ "vehicle_types": {
+ "description": "A classification a vehicle can be given, such as Truck or Trailer. Resolves the vehicle type identifier on each vehicle.",
+ "docs_url": "https://developer.fleetio.com/docs/api/vehicle-types",
+ "columns": {
+ **_COMMON_COLUMNS,
+ "account_id": "Identifier of the Fleetio account the vehicle type belongs to.",
+ "name": "The name of the vehicle type.",
+ "default": "Whether this is the default type applied to new vehicles.",
+ "unarchived_vehicles_count": "Number of unarchived vehicles of this type.",
+ "vehicles_count": "Total number of vehicles of this type.",
+ },
+ },
+ "service_tasks": {
+ "description": "A unit of maintenance work that can be put on a service entry, work order, or service reminder, such as Oil Change or Tire Rotation.",
+ "docs_url": "https://developer.fleetio.com/docs/api/service-tasks",
+ "columns": {
+ **_COMMON_COLUMNS,
+ "name": "The name of the service task.",
+ "description": "A description of the service task.",
+ "archived_at": "Timestamp when the service task was archived, if applicable.",
+ "expected_duration_in_seconds": "How long this service task is expected to take, in seconds.",
+ "service_task_category_id": "Identifier of the category this service task is filed under.",
+ "standard_service_task_id": "Identifier of the standard service task this one is based on.",
+ "merged_service_task_id": "Identifier of the service task this one was merged into.",
+ "standard": "Whether this is one of Fleetio's standard service tasks.",
+ "is_group": "Whether this service task groups other service tasks.",
+ "default_vmrs_assembly_id": "Identifier of the VMRS assembly coded against this service task.",
+ "default_vmrs_component_id": "Identifier of the VMRS component coded against this service task.",
+ "default_vmrs_reason_for_repair_id": "Identifier of the VMRS reason for repair coded against this service task.",
+ "default_vmrs_system_group_id": "Identifier of the VMRS system group coded against this service task.",
+ "default_vmrs_system_id": "Identifier of the VMRS system coded against this service task.",
+ },
+ },
+ "vendors": {
+ "description": "A supplier the fleet buys from — fuel, service, parts, or vehicles. Resolves the vendor identifier on service entries, fuel entries, and purchase orders.",
+ "docs_url": "https://developer.fleetio.com/docs/api/vendors",
+ "columns": {
+ **_COMMON_COLUMNS,
+ "name": "The name of the vendor.",
+ "phone": "The vendor's phone number.",
+ "street_address": "The vendor's street address.",
+ "street_address_line_2": "The second line of the vendor's street address.",
+ "city": "The vendor's city.",
+ "region": "The vendor's region, state, province, or territory.",
+ "postal_code": "The vendor's postal or ZIP code.",
+ "country": "The vendor's country.",
+ "website": "The vendor's website.",
+ "notes": "Additional notes about the vendor.",
+ "fuel": "Whether the vendor supplies fuel.",
+ "service": "Whether the vendor supplies service.",
+ "parts": "Whether the vendor supplies parts.",
+ "vehicle": "Whether the vendor supplies vehicles.",
+ "contact_name": "Name of the contact person at the vendor.",
+ "contact_email": "Email address of the contact person at the vendor.",
+ "contact_phone": "Phone number of the contact person at the vendor.",
+ "latitude": "Latitude of the vendor's location.",
+ "longitude": "Longitude of the vendor's location.",
+ "external_id": "Identifier for the vendor in an external system.",
+ "archived_at": "Timestamp when the vendor was archived, if applicable.",
+ "labels": "Labels applied to the vendor.",
+ },
+ },
+ "purchase_orders": {
+ "description": "An order placed with a vendor to buy parts, with its approval and receiving state and its totals.",
+ "docs_url": "https://developer.fleetio.com/docs/api/purchase-orders",
+ "columns": {
+ **_COMMON_COLUMNS,
+ "number": "The purchase order number.",
+ "description": "A description of the purchase order.",
+ "state": "Where the purchase order sits in its lifecycle, such as draft, approved, or purchased.",
+ "vendor_id": "Identifier of the vendor the order was placed with.",
+ "part_location_id": "Identifier of the part location the order is received into.",
+ "purchase_order_status_id": "Identifier of the purchase order status.",
+ "submitted_for_approval_at": "Timestamp when the purchase order was submitted for approval.",
+ "approved_at": "Timestamp when the purchase order was approved.",
+ "rejected_at": "Timestamp when the purchase order was rejected.",
+ "purchased_at": "Timestamp when the purchase order was purchased.",
+ "received_partial_at": "Timestamp when the purchase order was partially received.",
+ "received_full_at": "Timestamp when the purchase order was received in full.",
+ "closed_at": "Timestamp when the purchase order was closed.",
+ "submitted_for_approval_by_id": "Identifier of the user who submitted the order for approval.",
+ "approved_by_id": "Identifier of the user who approved the order.",
+ "rejected_by_id": "Identifier of the user who rejected the order.",
+ "purchased_by_id": "Identifier of the user who purchased the order.",
+ "received_partial_by_id": "Identifier of the user who partially received the order.",
+ "received_full_by_id": "Identifier of the user who received the order in full.",
+ "closed_by_id": "Identifier of the user who closed the order.",
+ "created_by_id": "Identifier of the user who created the order.",
+ "subtotal_cents": "The pre-tax subtotal of the order, in cents.",
+ "discount_cents": "The discount applied to the order, in cents.",
+ "discount_type": "The kind of discount applied to the order.",
+ "shipping_cents": "The shipping charge on the order, in cents.",
+ "tax_1_cents": "The primary tax on the order, in cents.",
+ "tax_1_percentage": "The primary tax rate applied to the order.",
+ "tax_1_type": "The kind of primary tax applied to the order.",
+ "tax_2_cents": "The secondary tax on the order, in cents.",
+ "tax_2_percentage": "The secondary tax rate applied to the order.",
+ "tax_2_type": "The kind of secondary tax applied to the order.",
+ "total_amount_cents": "The total amount of the order, in cents.",
+ "documents_count": "Number of documents attached to the order.",
+ "comments_count": "Number of comments on the order.",
+ "watchers_count": "Number of people watching the order.",
+ "labels": "Labels applied to the order.",
+ },
+ },
"service_entry_line_items": {
"description": "A single line of a service entry: the labor, part, tax, fee, or free-text charge that makes up its cost.",
"docs_url": "https://developer.fleetio.com/docs/api/service-entry-line-items-index",
@@ -224,4 +329,19 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"vmrs_component": "VMRS component coded against this line item.",
},
},
+ "purchase_order_line_items": {
+ "description": "A single part on a purchase order: what was ordered, how much of it arrived, and what it cost.",
+ "docs_url": "https://developer.fleetio.com/docs/api/purchase-orders-purchase-order-line-items-index",
+ "columns": {
+ **_COMMON_COLUMNS,
+ "purchase_order_id": "Identifier of the purchase order this line item belongs to.",
+ "purchase_order_number": "Number of the purchase order this line item belongs to.",
+ "part_id": "Identifier of the part being purchased.",
+ "part_number": "The part number of the part being purchased.",
+ "quantity": "The quantity of the part ordered.",
+ "total_quantity_received": "The quantity of the part received so far.",
+ "unit_cost": "The cost of one unit of the part.",
+ "subtotal": "The pre-tax subtotal for this line item.",
+ },
+ },
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/settings.py
index 5ece34e2baa6..4d9231b75948 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/settings.py
@@ -65,9 +65,13 @@ FLEETIO_ENDPOINTS: dict[str, FleetioEndpointConfig] = {
"vehicle_assignments": FleetioEndpointConfig(name="vehicle_assignments", path="/vehicle_assignments"),
"expense_entries": FleetioEndpointConfig(name="expense_entries", path="/expense_entries"),
"expense_entry_types": FleetioEndpointConfig(name="expense_entry_types", path="/expense_entry_types"),
- # Lookup tables resolving the status ids carried on every vehicle and work order row.
+ "purchase_orders": FleetioEndpointConfig(name="purchase_orders", path="/purchase_orders"),
+ # Lookup tables resolving the ids carried on the transactional rows above.
"vehicle_statuses": FleetioEndpointConfig(name="vehicle_statuses", path="/vehicle_statuses"),
"work_order_statuses": FleetioEndpointConfig(name="work_order_statuses", path="/work_order_statuses"),
+ "vehicle_types": FleetioEndpointConfig(name="vehicle_types", path="/vehicle_types"),
+ "service_tasks": FleetioEndpointConfig(name="service_tasks", path="/service_tasks"),
+ "vendors": FleetioEndpointConfig(name="vendors", path="/vendors"),
# Line items are only listed per service entry, so fan out over the service entries endpoint.
# The list action takes no `filter` param — only `sort[id]` — so this is full refresh only.
"service_entry_line_items": FleetioEndpointConfig(
@@ -88,6 +92,27 @@ FLEETIO_ENDPOINTS: dict[str, FleetioEndpointConfig] = {
child_params={"sort[id]": "asc"},
),
),
+ # Line items are only listed per purchase order, and the path binds the order's `number`
+ # rather than its id. The list action takes no `filter` param — only `sort[id]` — so this is
+ # full refresh only.
+ "purchase_order_line_items": FleetioEndpointConfig(
+ name="purchase_order_line_items",
+ path="/purchase_orders/{number}/purchase_order_line_items",
+ incremental_fields=[],
+ primary_keys=["purchase_order_id", "id"],
+ fanout=DependentEndpointConfig(
+ parent_name="purchase_orders",
+ resolve_param="number",
+ resolve_field="number",
+ # The line item carries neither identifier of the order it belongs to, so take both
+ # from the parent row: the id to populate the composite primary key and join back to
+ # `purchase_orders`, the number because that is what the path and the Fleetio UI use.
+ include_from_parent=["id", "number"],
+ parent_field_renames={"id": "purchase_order_id", "number": "purchase_order_number"},
+ parent_params={"sort[created_at]": "asc"},
+ child_params={"sort[id]": "asc"},
+ ),
+ ),
}
ENDPOINTS = tuple(FLEETIO_ENDPOINTS.keys())
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/tests/test_fleetio.py b/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/tests/test_fleetio.py
index 17a8920a3731..ca2e793006c8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/tests/test_fleetio.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/fleetio/tests/test_fleetio.py
@@ -379,3 +379,82 @@ class TestServiceEntryLineItemsFanout:
assert [row["service_entry_id"] for row in rows] == [22]
saved = [call.args[0] for call in manager.save_state.call_args_list]
assert any(state.completed is not None and completed in state.completed for state in saved)
+
+
+class TestPurchaseOrderLineItemsFanout:
+ """Line items are listed per purchase order, and the path binds the order's number, not its id."""
+
+ def _wire_fanout(self, session: mock.MagicMock, responses: list[Response]) -> list[str]:
+ session.headers = {}
+ urls: list[str] = []
+
+ def _prepare(request: Any) -> mock.MagicMock:
+ url = request.url
+ for key, value in (request.params or {}).items():
+ url = url.replace("{" + key + "}", str(value))
+ urls.append(url)
+ prepared = mock.MagicMock()
+ prepared.url = url
+ return prepared
+
+ session.prepare_request.side_effect = _prepare
+ session.send.side_effect = responses
+ return urls
+
+ def _line_items(self, manager: mock.MagicMock, **kwargs: Any):
+ return fleetio_source(
+ api_key="k",
+ account_token="a",
+ endpoint="purchase_order_line_items",
+ team_id=1,
+ job_id="j",
+ resumable_source_manager=manager,
+ **kwargs,
+ )
+
+ @mock.patch(CLIENT_SESSION_PATCH)
+ def test_binds_the_parent_number_not_its_id(self, MockSession) -> None:
+ # Fleetio keys this path on the order number. Binding the id instead reads another order's
+ # line items (or 404s), and the rows would still look plausible.
+ session = MockSession.return_value
+ urls = self._wire_fanout(
+ session,
+ [
+ _response([{"id": 11, "number": 4001}, {"id": 22, "number": 4002}], None),
+ _response([{"id": 101, "part_id": 7}], None),
+ _response([{"id": 201}], "CUR2"),
+ _response([{"id": 202}], None),
+ ],
+ )
+
+ rows = _rows(self._line_items(_make_manager()))
+
+ assert urls[0].endswith("/api/purchase_orders")
+ assert urls[1] == "https://secure.fleetio.com/api/purchase_orders/4001/purchase_order_line_items"
+ assert urls[2] == "https://secure.fleetio.com/api/purchase_orders/4002/purchase_order_line_items"
+ # Neither identifier is on the line item itself, so both come from the parent row — the id
+ # populates the composite primary key that keeps line items from colliding on merge.
+ assert [(row["purchase_order_id"], row["purchase_order_number"], row["id"]) for row in rows] == [
+ (11, 4001, 101),
+ (22, 4002, 201),
+ (22, 4002, 202),
+ ]
+
+ @mock.patch(CLIENT_SESSION_PATCH)
+ def test_checkpoints_by_parent_and_resumes_past_completed_parents(self, MockSession) -> None:
+ session = MockSession.return_value
+ self._wire_fanout(
+ session,
+ [
+ _response([{"id": 11, "number": 4001}, {"id": 22, "number": 4002}], None),
+ _response([{"id": 201}], None),
+ ],
+ )
+
+ completed = "/purchase_orders/4001/purchase_order_line_items"
+ manager = _make_manager(FleetioResumeConfig(completed=[completed], current=None, child_state=None))
+ rows = _rows(self._line_items(manager))
+
+ assert [row["purchase_order_number"] for row in rows] == [4002]
+ saved = [call.args[0] for call in manager.save_state.call_args_list]
+ assert any(state.completed is not None and completed in state.completed for state in saved)