Ë
    èlÖi6*  ã                   ó€   — d dl m Z mZ d dlmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZ  G d„ d	«      Zy)
é    )ÚdatetimeÚ	timedelta)ÚAnyÚDictÚListÚOptionalN)Ú	BatchSize)Ú	BatchType)Ú	ModelNodeÚ
NodeConfig)ÚDbtInternalErrorÚDbtRuntimeErrorc                   ó6  — e Zd ZdZ	 ddededee   dee   dee   f
d„Zd	„ Z	d
ee   fd„Z
dededee   fd„Zedededeeef   fd„«       Zededededefd„«       Zedededefd„«       Zedededefd„«       Zedededefd„«       Zedededefd„«       Zy)ÚMicrobatchBuilderzTA utility class for building microbatch definitions associated with a specific modelNÚmodelÚis_incrementalÚevent_time_startÚevent_time_endÚdefault_end_timec           	      óN  — |j                   j                  dk7  rt        d|j                  › d�«      ‚|| _        | j                  j                   j
                  €Bt        d| j                  j                  › dt        D �cg c]  }|j                  ‘Œ c}› d�«      ‚|| _	        |r |j                  t        j                  ¬«      nd | _        |r |j                  t        j                  ¬«      nd | _        |xs# t        j                   t        j                  «      | _        y c c}w )NÚ
microbatchzModel 'z1' does not use 'microbatch' incremental_strategy.úMicrobatch model 'z.' does not have a 'batch_size' config (one of z) specificed.)Útzinfo)ÚconfigÚincremental_strategyr   Únamer   Ú
batch_sizer   r	   Úvaluer   ÚreplaceÚpytzÚUTCr   r   r   Únowr   )Úselfr   r   r   r   r   r   s          úp/var/www/html/strategist-ai/venv_dbt/lib/python3.12/site-packages/dbt/materializations/incremental/microbatch.pyÚ__init__zMicrobatchBuilder.__init__   s  € ð �<‰<×,Ñ,°Ò<Ü"Ø˜%Ÿ*™*˜Ð%VÐWóð ð ˆŒ
à�:‰:×Ñ×'Ñ'Ð/Ü!Ø$ T§Z¡Z§_¡_Ð$5Ð5cô  IRö  eSð  {EÐeo×euÓeuò  eSð  dTð  Tað  bóð ð -ˆÔá9IÐ×$Ñ$¬D¯H©HÐ$Ô5Ètð 	Ôñ JX˜n×4Ñ4¼D¿H¹HÐ4ÔEÐ]aˆÔØ 0Ò J´H·L±LÄÇÁÓ4JˆÕùò eSs   Á?D"c                 óž   — | j                   xs | j                  }t        j                  || j                  j
                  j                  «      S )z`Defaults the end_time to the current time in UTC unless a non `None` event_time_end was provided)r   r   r   Úceiling_timestampr   r   r   )r#   Úend_times     r$   Úbuild_end_timez MicrobatchBuilder.build_end_time)   s;   € à×&Ñ&Ò?¨$×*?Ñ*?ˆÜ ×2Ñ2°8¸T¿Z¹Z×=NÑ=N×=YÑ=YÓZÐZó    Ú
checkpointc                 ó   — t        | j                  j                  t        «      sJ ‚| j                  j                  j                  }| j
                  r t        j                  | j
                  |«      S | j                  r|€w| j                  j                  j                  s#t        d| j                  j                  › d�«      ‚t        j                  | j                  j                  j                  |«      S | j                  j                  j                  }|t        j                  ||«      k(  r|dz  }t        j                  ||d|z  «      S )a»  Create a start time based off the passed in checkpoint.

        If the checkpoint is `None`, or this is the first run of a microbatch model, then the
        model's configured `begin` value will be returned as a checkpoint is necessary
        to build a start time. This is because we build the start time relative to the checkpoint
        via the batchsize and offset, and we cannot offset a checkpoint if there is no checkpoint.
        r   z#' requires a 'begin' configuration.é   éÿÿÿÿ)Ú
isinstancer   r   r   r   r   r   Útruncate_timestampr   Úbeginr   r   ÚlookbackÚoffset_timestamp)r#   r+   r   r2   s       r$   Úbuild_start_timez"MicrobatchBuilder.build_start_time.   s  € ô ˜$Ÿ*™*×+Ñ+¬ZÔ8Ð8Ð8Ø—Z‘Z×&Ñ&×1Ñ1ˆ
ð × Ò Ü$×7Ñ7¸×8MÑ8MÈzÓZÐZð ×"Ò" jÐ&8Ø—:‘:×$Ñ$×*Ò*Ü%Ø(¨¯©¯©Ð(9Ð9\Ð]óð ô %×7Ñ7¸¿
¹
×8IÑ8I×8OÑ8OÐQ[Ó\Ð\à—:‘:×$Ñ$×-Ñ-ˆð Ô*×=Ñ=¸jÈ*ÓUÒUØ˜‰MˆHä ×1Ñ1°*¸jÈ"ÈxÉ-ÓXÐXr*   ÚstartÚendÚreturnc                 ó
  — | j                   j                  j                  }|}t        j	                  ||d«      }||fg}||k  r2|}t        j	                  ||d«      }|j                  ||f«       ||k  rŒ2|d   d   |f|d<   |S )zŠ
        Given a start and end datetime, builds a list of batches where each batch is
        the size of the model's batch_size.
        r-   r.   r   )r   r   r   r   r3   Úappend)r#   r5   r6   r   Úcurr_batch_startÚcurr_batch_endÚbatchess          r$   Úbuild_batcheszMicrobatchBuilder.build_batchesQ   s£   € ð
 —Z‘Z×&Ñ&×1Ñ1ˆ
Ø%*ÐÜ#4×#EÑ#EØ˜j¨!ó$
ˆð &6°~Ð$FÐ#GˆØ˜sÒ"Ø-ÐÜ.×?Ñ?Ð@PÐR\Ð^_Ó`ˆNØ�N‰NÐ,¨nÐ=Ô>ð ˜sÓ"ð ˜r‘{ 1‘~ sÐ+ˆ�‰àˆr*   Úincremental_batchc                 óˆ   — i }| j                  «       |d<   | j                  |d<   | j                  |d<   |rd„ |d<   d„ |d<   |S )zÂ
        Create context with entries that reflect microbatch model + incremental execution state

        Assumes self.model has been (re)-compiled with necessary batch filters applied.
        r   ÚsqlÚcompiled_codec                   ó   — y)NT© rC   r*   r$   ú<lambda>zAMicrobatchBuilder.build_jinja_context_for_batch.<locals>.<lambda>w   ó   � r*   r   c                   ó   — y)NFrC   rC   r*   r$   rD   zAMicrobatchBuilder.build_jinja_context_for_batch.<locals>.<lambda>x   rE   r*   Úshould_full_refresh)Úto_dictrA   )r   r>   Újinja_contexts      r$   Úbuild_jinja_context_for_batchz/MicrobatchBuilder.build_jinja_context_for_batchg   s\   € ð )+ˆð "'§¡£ˆ�gÑØ$×2Ñ2ˆ�eÑØ).×)<Ñ)<ˆ�oÑ&ñ Ù.:ˆMÐ*Ñ+Ù3@ˆMÐ/Ñ0àÐr*   Ú	timestampr   Úoffsetc                 óú  — t         j                  | |«      }|t        j                  k(  r|t	        |¬«      z   }|S |t        j
                  k(  r|t	        |¬«      z   }|S |t        j                  k(  rW|}t        t        |«      «      D ]<  }|dk  r|t	        d¬«      z
  }n|t	        d¬«      z   }t         j                  ||«      }Œ> |S |t        j                  k(  r|j                  |j                  |z   ¬«      }S )a~  Truncates the passed in timestamp based on the batch_size and then applies the offset by the batch_size.

        Note: It's important to understand that the offset applies to the truncated timestamp, not
        the origin timestamp. Thus being offset by a day isn't relative to the any given hour that day,
        but relative to the start of the day. So if the timestamp is the very end of a day, 2024-09-17 23:59:59,
        you have a batch size of a day, and an offset of +1, then the returned value ends up being only one
        second later, 2024-09-18 00:00:00.

        2024-09-17 16:06:00 + Batchsize.hour -1 -> 2024-09-17 15:00:00
        2024-09-17 16:06:00 + Batchsize.hour +1 -> 2024-09-17 17:00:00
        2024-09-17 16:06:00 + Batchsize.day -1 -> 2024-09-16 00:00:00
        2024-09-17 16:06:00 + Batchsize.day +1 -> 2024-09-18 00:00:00
        2024-09-17 16:06:00 + Batchsize.month -1 -> 2024-08-01 00:00:00
        2024-09-17 16:06:00 + Batchsize.month +1 -> 2024-10-01 00:00:00
        2024-09-17 16:06:00 + Batchsize.year -1 -> 2023-01-01 00:00:00
        2024-09-17 16:06:00 + Batchsize.year +1 -> 2025-01-01 00:00:00
        )Úhours)Údaysr   r-   é   )Úyear)r   r0   r	   Úhourr   ÚdayÚmonthÚrangeÚabsrQ   r   )rK   r   rL   Ú	truncatedr3   Ú_s         r$   r3   z"MicrobatchBuilder.offset_timestamp|   s  € ô& &×8Ñ8¸ÀJÓOˆ	ð œŸ™Ò'Ø(¬9¸6Ô+BÑBÐð   Ðð œ9Ÿ=™=Ò(Ø(¬9¸&Ô+AÑAÐð  Ðð œ9Ÿ?™?Ò*Ø(ÐÜœ3˜v›;Ó'ò �Ø˜A’:Ø'7¼)ÈÔ:KÑ'KÑ$à'7¼)ÈÔ:LÑ'LÐ$Ü#4×#GÑ#GØ$ jó$Ñ ðð  Ðð œ9Ÿ>™>Ò)Ø(×0Ñ0°i·n±nÀvÑ6MÐ0ÓNÐàÐr*   c           
      ór  — |t         j                  k(  rJt        | j                  | j                  | j
                  | j                  dddt        j                  «      }|S |t         j
                  k(  r@t        | j                  | j                  | j
                  ddddt        j                  «      }|S |t         j                  k(  r6t        | j                  | j                  dddddt        j                  «      }|S |t         j                  k(  r*t        | j                  ddddddt        j                  «      }S )aT  Truncates the passed in timestamp based on the batch_size.

        2024-09-17 16:06:00 + Batchsize.hour -> 2024-09-17 16:00:00
        2024-09-17 16:06:00 + Batchsize.day -> 2024-09-17 00:00:00
        2024-09-17 16:06:00 + Batchsize.month -> 2024-09-01 00:00:00
        2024-09-17 16:06:00 + Batchsize.year -> 2024-01-01 00:00:00
        r   r-   )r	   rR   r   rQ   rT   rS   r    Úutc)rK   r   rW   s      r$   r0   z$MicrobatchBuilder.truncate_timestamp¥   s   € ð œŸ™Ò'Ü Ø—‘Ø—‘Ø—‘Ø—‘ØØØÜ—‘ó	ˆIð& Ðð œ9Ÿ=™=Ò(Ü Ø—‘ 	§¡°·±ÀÀ1ÀaÈÌDÏHÉHóˆIð Ðð œ9Ÿ?™?Ò*Ü  §¡°·±À!ÀQÈÈ1ÈaÔQU×QYÑQYÓZˆIð Ðð œ9Ÿ>™>Ò)Ü  §¡°°A°q¸!¸QÀÄ4Ç8Á8ÓLˆIàÐr*   Ú
start_timec                 óN   — t         j                  | |«      j                  dd«      S )Nú-Ú )r   Úformat_batch_startr   )r[   r   s     r$   Úbatch_idzMicrobatchBuilder.batch_idÄ   s$   € ä ×3Ñ3°JÀ
ÓK×SÑSÐTWÐY[Ó\Ð\r*   Úbatch_startc                 óü   — |t         j                  k(  r| j                  d«      S |t         j                  k(  r| j                  d«      S |t         j                  k(  r| j                  d«      S | j                  d«      S )a*  Format the passed in datetime based on the batch_size.

        2024-09-17 16:06:00 + Batchsize.hour  -> 2024-09-17T16
        2024-09-17 16:06:00 + Batchsize.day   -> 2024-09-17
        2024-09-17 16:06:00 + Batchsize.month -> 2024-09
        2024-09-17 16:06:00 + Batchsize.year  -> 2024
        z%Yz%Y-%mz%Y-%m-%dz%Y-%m-%dT%H)r	   rQ   ÚstrftimerT   rS   )ra   r   s     r$   r_   z$MicrobatchBuilder.format_batch_startÈ   sl   € ð œŸ™Ò'Ø×'Ñ'¨Ó-Ð-Øœ9Ÿ?™?Ò*Ø×'Ñ'¨Ó0Ð0Øœ9Ÿ=™=Ò(Ø×'Ñ'¨
Ó3Ð3à×'Ñ'¨Ó6Ð6r*   c                 ón   — t         j                  | |«      x}}|| k7  rt         j                  ||d«      }|S )aÌ  Takes the given timestamp and moves it to the ceiling for the given batch size

        Note, if the timestamp is already the batch size ceiling, that is returned
        2024-09-17 16:06:00 + BatchSize.hour -> 2024-09-17 17:00:00
        2024-09-17 16:00:00 + BatchSize.hour -> 2024-09-17 16:00:00
        2024-09-17 16:06:00 + BatchSize.day -> 2024-09-18 00:00:00
        2024-09-17 00:00:00 + BatchSize.day -> 2024-09-17 00:00:00
        2024-09-17 16:06:00 + BatchSize.month -> 2024-10-01 00:00:00
        2024-09-01 00:00:00 + BatchSize.month -> 2024-09-01 00:00:00
        2024-09-17 16:06:00 + BatchSize.year -> 2025-01-01 00:00:00
        2024-01-01 00:00:00 + BatchSize.year -> 2024-01-01 00:00:00

        r-   )r   r0   r3   )rK   r   ÚceilingrW   s       r$   r'   z#MicrobatchBuilder.ceiling_timestampÚ   s=   € ô 0×BÑBÀ9ÈjÓYÐYˆ�)Ø˜	Ò!Ü'×8Ñ8¸ÀJÐPQÓRˆGØˆr*   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úboolr   r   r%   r)   r4   r   r
   r=   Ústaticmethodr   Ústrr   rJ   r	   Úintr3   r0   r`   r_   r'   rC   r*   r$   r   r      s�  „ Ù^ð 04ñKàðKð ðKð # 8Ñ,ð	Kð
 ! Ñ*ðKð # 8Ñ,óKò4[ð
!Y¨8°HÑ+=ó !YðF 8ð °(ð ¸tÀI¹ó ð, ð¨Yð È4ð ÐTXÐY\Ð^aÐYaÑTbò ó ðð( ð&  Hð & ¸)ð & ÈSð & ÐU]ò & ó ð& ðP ð hð ¸Ið È(ò ó ðð< ð]˜Xð ]°9ð ]Àò ]ó ð]ð ð7¨ð 7¸ið 7ÈCò 7ó ð7ð" ð Xð ¸9ð Èò ó ñr*   r   )r   r   Útypingr   r   r   r   r    Údbt.artifacts.resources.typesr	   Ú#dbt.artifacts.schemas.batch_resultsr
   Údbt.contracts.graph.nodesr   r   Údbt.exceptionsr   r   r   rC   r*   r$   ú<module>rs      s)   ðß (ß ,Ó ,ã å 3Ý 9ß ;ß <÷`ò `r*   