
    tj
                        d Z ddlmZ ddlZddlmZmZmZ ddlmZ ddl	m
Z
 e
dz  dz  Zdd	ZddZdddZddZedk(  r= e       Z ed eed
                  eded   xs d         eded    d       yy)u  Count consecutive days on which an UNATTENDED brief succeeded.

The definition of done for this rail is "a brief lands on his phone for fourteen
consecutive days with no action from him". That is a claim about the past, and the
only honest way to make it is to have been counting at the time — reconstructing it
afterwards from memory is exactly the kind of thing this whole system exists to stop.

Hand-fired runs do not count and are not recorded here. Only `brief.py --scheduled`,
which is what the timer invokes.

Deliberately a sorted list of dates rather than a counter: a counter can only tell you
what it believes, a list can be read and argued with.
    )annotationsN)datedatetime	timedelta)Any)ROOTstatezunattended.jsonc            	     2   t         j                         sg S 	 t        j                  t         j	                  d            } | j                  dg       }t        |D ch c]  }t        |t              s| c}      S # t
        t        f$ r g cY S w xY wc c}w )Nutf-8encodingdays)STREAK_PATHexistsjsonloads	read_text
ValueErrorOSErrorgetsorted
isinstancestr)valuer   ds      /root/personal-cos/streak.py_loadr      s    	

;00'0BC 99VR Dd9jC&819::   	 :s   )A= B2B=BBr   c                    | syt        | D ch c]  }t        j                  |       c}      }d}t        ||dd       D ]  \  }}||z
  t	        d      k(  r|dz   nd}  |S c c}w )z1Consecutive days ending at the most recent entry.r      N)r   )r   r   fromisoformatzipr   )r   r   parsedrunearlierlaters         r   _run_lengthr&   %   st    D9qT''*9:F
CffQRj1 EI1,==cAg1EJ	 :s   A$c                   | xs0 t        j                         j                         j                         } t	               }| j                         |vr3|j                  | j                                t        t        |            }t        j                  j                  dd       |t        |      d}t        j                  d      }|j                  t        j                   |d      dz   d	       |j#                  t               |S )
z>Record an unattended success for `day` (default: local today).T)parentsexist_ok)r   streakz	.json.tmp   )indent
r   r   )r   now
astimezoner   r   	isoformatappendr   setr   parentmkdirr&   with_suffix
write_textr   dumpsreplace)dayr   payload	temporarys       r   recordr<   0   s    

3**,113C7D
}}d"CMMO$c$i TD9{4'89G''4IGA6=Pk"N    c                     t               } | r| d   nd }t        |       }|rWt        j                         j	                         j                         t        j                  |      z
  j                  }|dkD  rd}| ||dS )Nr   r   )r   r*   last)r   r&   r   r.   r/   r   r    r   )r   r@   r#   gaps       r   currentrB   ?   so    7D48D
d
C||~((*//1D4F4Ft4LLRR7CC66r=   __main__zunattended briefs recorded : zlast                       : r@   neverzcurrent streak             : r*   z / 14)return	list[str])r   rF   rE   int)N)r9   zdate | NonerE   dict[str, Any])rE   rH   )__doc__
__future__r   r   r   r   r   typingr   notifyr   r   r   r&   r<   rB   __name__r	   printlen r=   r   <module>rQ      s    #  . .  Wn00;	7 zIE	)#eFm*<)=
>?	)%-*B7)C
DE	)%/):%
@A	 r=   