Adds a column with the UTC timestamp of the last row update Args: refresh_rate (pw.Duration, optional): The interval at which the UTC timestamp is refreshed. Defaults to 1 second. update_timestamp_column_name (str, optional): The name of the column to sto
(
self: pw.Table,
refresh_rate: pw.Duration = pw.Duration(seconds=1),
update_timestamp_column_name: str = "updated_timestamp_utc",
)
| 189 | @check_arg_types |
| 190 | @trace_user_frame |
| 191 | def add_update_timestamp_utc( |
| 192 | self: pw.Table, |
| 193 | refresh_rate: pw.Duration = pw.Duration(seconds=1), |
| 194 | update_timestamp_column_name: str = "updated_timestamp_utc", |
| 195 | ) -> pw.Table: |
| 196 | """Adds a column with the UTC timestamp of the last row update |
| 197 | |
| 198 | Args: |
| 199 | refresh_rate (pw.Duration, optional): The interval at which the UTC |
| 200 | timestamp is refreshed. Defaults to 1 second. |
| 201 | update_timestamp_column_name (str, optional): The name of the column to |
| 202 | store the update timestamp. Defaults to "updated_timestamp_utc". |
| 203 | |
| 204 | Returns: |
| 205 | pw.Table: A new table with an additional column containing the UTC |
| 206 | timestamp of the last update for each row. The id column is preserved. |
| 207 | """ |
| 208 | utc_now_single_row = utc_now(refresh_rate=refresh_rate).reduce( |
| 209 | timestamp_utc=pw.reducers.latest(pw.this.timestamp_utc) |
| 210 | ) |
| 211 | |
| 212 | stream = self.with_columns(_id=pw.this.id).to_stream() |
| 213 | |
| 214 | stream_joined = stream.asof_now_join_left(utc_now_single_row) |
| 215 | new_cols = { |
| 216 | update_timestamp_column_name: pw.coalesce( |
| 217 | pw.right.timestamp_utc, |
| 218 | pw.DateTimeUtc(datetime.datetime.now(tz=datetime.timezone.utc)), |
| 219 | ) |
| 220 | } |
| 221 | stream_with_update_time = stream_joined.select(*pw.left, **new_cols) |
| 222 | |
| 223 | result = ( |
| 224 | stream_with_update_time.stream_to_table(pw.this.is_upsert) |
| 225 | .without(pw.this.is_upsert) |
| 226 | .with_id(pw.this._id) |
| 227 | .without(pw.this._id) |
| 228 | ) |
| 229 | return result |
nothing calls this directly
no test coverage detected