
    )i                     T    d dl m Z mZ d dlmZ d dlmZ d dlmZ d dlZ G d d      Z	y)    )datetime	timedelta)Count)	TruncDate)timezoneNc                       e Zd Zedd       Zy)DailyCallsServiceNc                    |j                         }|j                         }t        j                         }|r7t        |dd      r*	 t	        j                  |j
                  j                        }t        j                  t        j                  |t        j                  j                               |      }t        j                  t        j                  |t        d      z   t        j                  j                               |      }| j                  ||      }|j                  t        d|            j!                  d	      j                  t#        d
      t#        dd            j%                  d	      }t'        ||z
  j(                  dz         D 	ci c]  }	|t        |	      z   ddd }
}	|D ].  }|d	   }|d   xs d}|d   xs d}|t+        ||z
  d      d|
|<   0 t-        |
j/                               D cg c]  }d|j1                  d      i|
|    }}|S # t        $ r t        j                         }Y w xY wc c}	w c c}w )zF
        Reproduce EXACT logic from old daily-calls endpoint.
        active_companyN   )days)created_at__gtecreated_at__lt
created_at)tzinfo)dayr   idfrom_numberT)distinct)total_callsunique_callersr   )unique
repetitiver   r   datez%b-%d)r   r   get_default_timezonegetattrpytzr   	Exception
make_awarer   combinemintimer   filterannotater   valuesr   order_byranger   maxsortedkeysstrftime)queryset
start_dateend_dateuser
company_tzstart_dtend_dt_exclusivequeryset_in_range	daily_aggi
days_rangerowr   r   totalresults                   H/var/www/html/dp2/backend_v2.1/server/apps/calls/services/daily_calls.pyget_summaryzDailyCallsService.get_summary
   sL     __&
==?224
GD"2D9=!]]4+>+>+G+GH
 &&Z):):)<=

 $..X	q(998<<;L;L;NO

 %OO$+ , 
 X)LDXEVE]X!$K$]TB   Xe_ 	 Hz177!;<
< **q,JJ< 	 

 Ce*C)*/aF&+!E !%&.!4JsO	  joo/0

 1 W-S/ 1 	 
 g  =%::<
=:

s   )H$ II$II)N)__name__
__module____qualname__staticmethodr;        r:   r	   r	      s    ? ?rA   r	   )
r   r   django.db.modelsr   django.db.models.functionsr   django.utilsr   r   r	   r@   rA   r:   <module>rE      s     ( " 0 ! B BrA   