
    ZTh                        d dl mZ d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlm Z   G d d      Z!y)    )annotations)datetime)Path)AnyDictListOptionalcast)	FieldMask)CreateReportTemplateRequest%CreateReportTemplateRequestClientKeysGetReportTemplateRequestGetReportTemplateResponseReportTemplateReportTemplateRuleReportTemplateTagUpdateReportTemplateRequest)ReportTemplateServiceStub)to_timestamp_pb)SiftChannel)ReportTemplateConfigReportTemplateUpdate)load_report_templatesc                  t    e Zd ZU dZded<   ddZ	 d	 	 	 ddZ	 d	 	 	 	 	 ddZddZdd	Z	dd
Z
ddZddZy)ReportTemplateServicezv
    A service for managing report templates. Allows for creating, updating, and retrieving report
    templates.
    r   _report_template_service_stubc                $    t        |      | _        y N)r   r   )selfchannels     e/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/report_templates/service.py__init__zReportTemplateService.__init__"   s    -Fw-O*    Nc                    |rD|j                   s%|j                  st        d|j                   d      | j	                  ||       y| j                  |       y)a  
        Create or update a report template via a ReportTemplateConfig. The config must contain a
        template_client_key, otherwise an exception will be raised. If a report template with the
        same client key exists, it will be updated. Otherwise, a new report template will be created.
        See `sift_py.report_templates.config.ReportTemplateConfig` for more information on available
        fields to configure.
        zReport template zG requires either a template_client_key or report_template_id to update.N)template_client_keytemplate_id	Exceptionname_update_report_template_create_report_template)r   configupdatess      r!    create_or_update_report_templatez6ReportTemplateService.create_or_update_report_template%   sV     --f6H6H&v{{m3z{  ((9((0r#   c                   d}|s|r|r|rt        d      |r| j                  |      }n|r| j                  |      }|rt        |j                  |j
                  |j                  |j                  D cg c]  }|j                   c}|j                  |j                  D cg c]  }|j                   c}|j                  r|j                  j                         nd|j                  rd      S d      S dS c c}w c c}w )z
        Retrieve a report template by client key or id. Only one of client_key or id should be
        provided, otherwise an exception will be raised. If a report template is found, it will be
        returned as a ReportTemplateConfig object.
        Nz(One of client_key or id must be providedTF)r(   r&   r%   tagsdescriptionrule_client_keysarchived_datearchived)
ValueError_get_report_template_by_id"_get_report_template_by_client_keyr   r(   report_template_id
client_keyr/   tag_namer0   rulesr2   
ToDatetime)r   r8   idreport_templatetagrules         r!   get_report_templatez)ReportTemplateService.get_report_template8   s     2:"GHH"==bAO"EEjQO  !$))+>>$3$>$>.=.B.BCscllC+77>M>S>S!Td$//!T"00 .;;FFH!0!>!>	
 EJ	
 	

 D!Ts   3C9!C>c                d    t        |      }|D cg c]  }| j                  |       c} |S c c}w )a  
        Load report templates from YAML definitions. The YAML defined report template must have
        a client key. If the report template with the given client key exists, it will be updated,
        otherwise a new report template will be created.
        See `sift_py.yaml.report_templates.load_report_templates` for more information on the YAML
        spec for report templates.
        )r   r-   )r   pathsreport_templatesr=   s       r!   load_report_templates_from_yamlz5ReportTemplateService.load_report_templates_from_yamlZ   s?     17 $4	
 11/B	
  		
s   -c                    t        |      }	 t        t        | j                  j	                  |            }t        t
        |j                        xs d S #  Y y xY w)N)r7   )r   r
   r   r   GetReportTemplater   r=   )r   r7   reqress       r!   r5   z0ReportTemplateService._get_report_template_by_idi   sW    &:LM	)4+M+M+_+_`c+dC (;(;<DD	s   AA Ac                    t        |      }	 t        t        | j                  j	                  |            }|j
                  xs d S #  Y y xY w)Nr8   )r   r
   r   r   rF   r=   )r   r8   rG   rH   s       r!   r6   z8ReportTemplateService._get_report_template_by_client_keys   sO    &*=	)4+M+M+_+_`c+dC &&.$.	s   8A Ac                    t        |j                        }t        |j                  |j                  |j
                  |j                  |      }| j                  j                  |       y )N)r1   )r(   r8   r0   	tag_namesr1   )	r   r1   r   r(   r%   r0   r/   r   CreateReportTemplate)r   r+   client_keys_reqrG   s       r!   r*   z-ReportTemplateService._create_report_template}   sZ    ?#44
 *11**kk,
 	**??Dr#   c                V   i }d|v r|d   |d<   d|v r|d   |d<   d|v r |d   D cg c]  }t        |       c}|d<   d|v r |d   D cg c]  }t        |       c}|d<   d|v r'|d   rt        t        j                               nd	|d
<   t        |j                  xs d|j                  |j                  d|j                        |j                  d|j                        |j                  d|j                        |j                  d|j                  D cg c]  }t        |       c}      |j                  d
|j                              }t        t        |j!                                     }| j"                  j%                  t'        ||             y	c c}w c c}w c c}w )z
        Updates a report template using the provided updates. Only the fields specified in the updates
        will be modified.
        r(   r0   r/   )r9   r1   rJ   r:   r3   Nr2    )r7   r8   r(   r0   r/   r:   r2   )rB   )r=   update_mask)r   r   r   r   nowr   r&   r%   getr(   r0   r/   r1   r2   r   listkeysr   UpdateReportTemplater   )	r   r+   r,   
update_mapr>   r8   keyupdated_report_template
field_masks	            r!   r)   z-ReportTemplateService._update_report_template   s   
 &(
W!(JvG#(/(>J}%WMTU[_!]c"3S"A!]Jv( #**<"=# #j9#Jw  3::3F/D ' #1%117R114"}f6H6HI4..H_H_`,<` %..&:N:NO
#
 T*//*;%<=
**??' 7Z	
1 "^#  as   F
F!F&)r    r   r   )r+   r   r,   zOptional[ReportTemplateUpdate])NN)r8   Optional[str]r<   r[   returnzOptional[ReportTemplateConfig])rB   z
List[Path]r\   zList[ReportTemplateConfig])r7   strr\   Optional[ReportTemplate])r8   r]   r\   r^   )r+   r   )r+   r   r,   r   )__name__
__module____qualname____doc____annotations__r"   r-   r@   rD   r5   r6   r*   r)    r#   r!   r   r      sp    
 $=<P W[1*15S1( EI 
' 
4A 
	' 
D E'
r#   r   N)"
__future__r   r   pathlibr   typingr   r   r   r	   r
   google.protobuf.field_mask_pb2r   -sift.report_templates.v1.report_templates_pb2r   r   r   r   r   r   r   r   2sift.report_templates.v1.report_templates_pb2_grpcr   sift_py._internal.timer   sift_py.grpc.transportr   sift_py.report_templates.configr   r   sift_py.yaml.report_templatesr   r   rd   r#   r!   <module>ro      sD    "   2 2 4	 	 	 Y 2 . V ?W
 W
r#   