-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy path_connection_state.py
More file actions
133 lines (107 loc) · 4.32 KB
/
Copy path_connection_state.py
File metadata and controls
133 lines (107 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
"""Pydantic models for connection state management.
These models represent the state of an Airbyte connection, which tracks sync progress
for incremental syncs. The state can be one of several types:
- stream: Per-stream state
- global: Global state with optional per-stream states
- legacy: Legacy state blob
- not_set: No state has been set yet
"""
from __future__ import annotations
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
class StreamDescriptor(BaseModel):
"""Descriptor for a single stream within a connection state."""
name: str = Field(description="The stream name")
namespace: str | None = Field(
default=None,
description="The stream namespace (optional)",
)
class StreamState(BaseModel):
"""State for a single stream."""
model_config = ConfigDict(populate_by_name=True)
stream_descriptor: StreamDescriptor = Field(
alias="streamDescriptor",
description="The stream descriptor (name and namespace)",
)
stream_state: dict[str, Any] | None = Field(
default=None,
alias="streamState",
description="The state blob for this stream",
)
class GlobalState(BaseModel):
"""Global state containing shared state and per-stream states."""
model_config = ConfigDict(populate_by_name=True)
shared_state: dict[str, Any] | None = Field(
default=None,
alias="sharedState",
description="Shared state across all streams",
)
stream_states: list[StreamState] = Field(
default_factory=list,
alias="streamStates",
description="Per-stream states within the global state",
)
class ConnectionStateResponse(BaseModel):
"""Response model for connection state operations.
Represents the state of a connection, which can be one of:
- stream: Per-stream state (streamState field populated)
- global: Global state with optional per-stream states (globalState field populated)
- legacy: Legacy state blob (state field populated)
- not_set: No state has been set yet
"""
model_config = ConfigDict(populate_by_name=True)
state_type: Literal["global", "stream", "legacy", "not_set"] = Field(
alias="stateType",
description="The type of state: global, stream, legacy, or not_set",
)
connection_id: str = Field(
alias="connectionId",
description="The connection ID (UUID)",
)
state: dict[str, Any] | None = Field(
default=None,
description="Legacy state blob (populated when stateType is 'legacy')",
)
stream_state: list[StreamState] | None = Field(
default=None,
alias="streamState",
description="Per-stream states (populated when stateType is 'stream')",
)
global_state: GlobalState | None = Field(
default=None,
alias="globalState",
description="Global state (populated when stateType is 'global')",
)
def __str__(self) -> str:
"""Return a string representation of the connection state."""
stream_count = 0
if self.stream_state:
stream_count = len(self.stream_state)
elif self.global_state and self.global_state.stream_states:
stream_count = len(self.global_state.stream_states)
return (
f"Connection {self.connection_id}: "
f"stateType={self.state_type}, streams={stream_count}"
)
def _match_stream(
stream: StreamState,
stream_name: str,
stream_namespace: str | None = None,
) -> bool:
"""Check if a StreamState matches the given name and namespace.
Namespace matching treats None and "" as equivalent (both mean "no namespace").
There is no wildcard behavior: the caller must match the actual namespace.
"""
if stream.stream_descriptor.name != stream_name:
return False
return (stream.stream_descriptor.namespace or None) == (stream_namespace or None)
def _get_stream_list(
state: ConnectionStateResponse,
) -> list[StreamState]:
"""Extract the stream list from a ConnectionStateResponse regardless of state type."""
if state.state_type == "stream" and state.stream_state:
return state.stream_state
if state.state_type == "global" and state.global_state:
return state.global_state.stream_states
return []