
    iA,                         d dl Z d dlZd dlmZ d dlmZmZmZmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZm Z   G d d      Z!y)	    N)datetime)ListDictAnyOptional   )EMPTY_ACTIVITY_SIGNALSEMPTY_ORDER_SIGNALSEMPTY_IMPRESSION_SIGNALSEMPTY_SENTIMENT_SIGNALSMIN_ORDERS_WITH_LOCATIONMIN_DISTINCT_LOCATIONSMIN_LOCATION_TIME_PAIRSTOP_SEARCHES_NTOP_RESTAURANTS_NTOP_ITEMS_NTOP_ITEM_PAIRS_NTOP_CATEGORY_PAIRS_NTOP_LOCATION_ORDER_NTOP_LOCATION_TIME_NTOP_LOCATION_TRIPLET_NACTIVITY_VOLUME_LOW_THRESHOLDORDER_VOLUME_LOW_THRESHOLDPOSITIVE_KEYWORDSNEGATIVE_KEYWORDS)
parse_datecalculate_reorder_ratiotemporal_distributionpair_counts_from_groupsc                       e Zd ZdZdedee   fdZdee	e
ef      de	e
ef   fdZdee	e
ef      dee	e
ef      de	e
ef   fd	Zd
ee	e
ef      de	e
ef   fdZdee	e
ef      de	e
ef   fdZy)SignalProcessorz
    Computes deterministic behavioral signals from raw data.
    Strictly returning evidence (counts, ratios), not conclusions.
    date_valreturnc                     t        |      S N)r   )selfr"   s     J/var/www/html/userprofiledev.eatanceapp.com/signal_processing/processor.py_parse_datezSignalProcessor._parse_date)   s    (##    raw_activitiesc                    	 |st        t              S t        j                  |D cg c]%  }|j	                  d      s|j	                  d      ' c}      }|D cg c]%  }|j	                  d      s|j	                  d      ' }}t        | j                        \  }}t        j                         	t        	 fd|D              }t        d |D              t        d |D              t        d |D              t        d |D              d	}t        |      t        |      t        j                  |      j                  t              ||d
|t        t        j                  |D cg c]%  }|j	                  d      xs dj                         ' c}            d|rt        |t        |      z  d      ndidt        |      t        k  idS c c}w c c}w c c}w )Nscreensearch_keywordparse_fnc              3      K   | ]V  }j                  |j                  d             r4j                  |j                  d             z
  j                  dk  rd X yw)
created_at   r   N)r(   getdays).0anowr&   s     r'   	<genexpr>z;SignalProcessor.process_activity_signals.<locals>.<genexpr>6   sZ       
%!l 34#@P@PQRQVQVWcQd@e:e9k9kop9p ~s   AAc              3   J   K   | ]  }|j                  d       dk(  sd  yw)pure_veg_filter1r   Nr3   r5   r6   s     r'   r8   z;SignalProcessor.process_activity_signals.<locals>.<genexpr><   s#     !_^quuEV?W[^?^!^   ##c              3   J   K   | ]  }|j                  d       dk(  sd  yw)offers_filterr;   r   Nr<   r=   s     r'   r8   z;SignalProcessor.process_activity_signals.<locals>.<genexpr>=   s"     [>aQUU?=SWZ=Z>r>   c              3   J   K   | ]  }|j                  d       dk(  sd  yw)fast_delivery_filterr;   r   Nr<   r=   s     r'   r8   z;SignalProcessor.process_activity_signals.<locals>.<genexpr>>   s#     &i.QAEEJ`DaehDhq.r>   c              3   J   K   | ]  }|j                  d       dk(  sd  yw)rating4plus_filterr;   r   Nr<   r=   s     r'   r8   z;SignalProcessor.process_activity_signals.<locals>.<genexpr>?   s#     %fA155I]C^beCear>   )pure_veg_countoffers_countfast_delivery_countrating_4plus_counthourdayplatformunknownrecent_activity_ratio_7d   r   activity_volume_low)total_eventsscreen_visitstop_searchesr   filtersplatform_usagerecency	stability)dictr	   collectionsCounterr3   r   r(   r   r7   sumlenmost_commonr   lowerroundr   )
r&   r*   r6   rR   search_keywords	hour_distday_distactivity_7d_countfilter_signalsr7   s
   `        @r'   process_activity_signalsz(SignalProcessor.process_activity_signals,   s   .//#++n,hnXYX]X]^fXgQUU8_n,hi<JfNqaeeTdNe155!12Nf3NTM]M]^	8lln  
% 
 
 "!_^!__[>[[#&&i.&i#i"%%f%f"f	
  /!-0'//@LL^\.7%I%";#6#6jx7yjxefz9J9Wi8^8^8`jx7y#z{*aoE2Cc.FY2Y[\,]uv 0^1DGd1de
 	
% -if. 8zs   GGG3G**G
raw_orders	raw_itemsc                   3 |st        t              S t        j                  t              }|D ]%  }||j                  d         j                  |       ' t        d |D              }t        || j                        \  }}t        |d t        dd      }t        |d t        dd	      }	d
t        t        t        f   dt        fd}
|D cg c]  } |
|      s| |
|      f }}t        j                   d |D              }|j#                  t$              D cg c]
  \  }}||d }}}t        j                          }t        j                          }|D ]d  \  }}| j                  |j                  d            }|s)|||j'                  d      fxx   dz  cc<   |||j'                  d      fxx   dz  cc<   f |j#                  t(              D cg c]  \  \  }}}|||d }}}}|j#                  t(              D cg c]  \  \  }}}|||d }}}}t+        |      }t+        |      }t+        |      t+        |      z   }|t,        k\  xr	 |t.        k\  }|t0        k\  }|xr |}g }|r|rt        j                          } |D ]  \  }}| j                  |j                  d            }|s)|j'                  d      }!|j                  |j                  d      g       }"t3        d |"D              }#|#D ]  }$|$s| ||!|$fxx   dz  cc<     | j#                  t4              D $cg c]  \  \  }}}$}|||$|d }}$}}}t        j                   |D cg c]%  }|j                  d      s|j                  d      ' c}      }%|%r|%j#                  d      d   d   nd}&|D cg c]H  }|j                  d      s|j                  d      r$|j                  d      xs |j                  d      J }'}t        j                   |'      }(|(j#                  t6              })|)r|)d   d   nd}*|D cg c]+  }|j                  d      s|j                  d      dk7  s*|- }+}t9        |D cg c]C  }| j                  |j                  d            s$| j                  |j                  d            E c}      },d }-d }.d }/t+        |,      dkD  rt;        dt+        |,            D 0cg c]  }0|,|0   |,|0dz
     z
  j<                   }1}0t?        t        |1      t+        |1      z  d      }-t        |1      t+        |1      z  3t        3fd|1D              t+        |1      z  }2t?        tA        jB                  |2      d      }.|,r$tE        jF                         |,d   z
  j<                  }/t+        |      ||rt?        |t+        |      z  d      ndd t+        |%      |%j#                  tH              tK        |%      d!|)t+        |      d"t+        |+      |r!t?        t+        |+      t+        |      z  d      ndd#|-|.|/d$|rt?        |&t+        |      z  d      nd|rt?        |*t+        |      z  d      ndd%d&t+        |      tL        k  i||	||d'|||d(||d)S c c}w c c}}w c c}}}w c c}}}w c c}}$}}w c c}w c c}w c c}w c c}w c c}0w )*Norder_idc              3   R   K   | ]  }t        |j                  d d             ! yw)	sub_totalr   N)floatr3   )r5   os     r'   r8   z8SignalProcessor.process_order_signals.<locals>.<genexpr>W   s!     K
1%k1 56
s   %'r.   c                 J    | j                  d      xs | j                  d      S )N	menu_name	item_namer<   is    r'   <lambda>z7SignalProcessor.process_order_signals.<locals>.<lambda>]   s    aeeK(>AEE+,>>r)   item_aitem_bc                 $    | j                  d      S )Ncategory_namer<   rq   s    r'   rs   z7SignalProcessor.process_order_signals.<locals>.<lambda>d   s    aeeO,r)   
category_a
category_brm   r#   c                     | j                  d      xs dj                         }| j                  d      xs dj                         }|xs |xs dS )N
order_area 
order_cityr3   strip)rm   r6   cs      r'   _order_locationz>SignalProcessor.process_order_signals.<locals>._order_locationj   sH    |$*113A|$*113A<<Rr)   c              3   &   K   | ]	  \  }}|  y wr%    )r5   _locs      r'   r8   z8SignalProcessor.process_order_signals.<locals>.<genexpr>p   s     3[FZFAsCFZs   )locationorder_countr1   z%Hr   z%A)r   rJ   count)r   rK   r   idc              3      K   | ]J  }|j                  d       xs dj                         r%|j                  d       xs dj                          L yw)rw   r|   Nr~   )r5   items     r'   r8   z8SignalProcessor.process_order_signals.<locals>.<genexpr>   sI       %17R>>@ XXo.4";;= %s   AA)r   rJ   categoryr   restaurant_namer   ro   rp   	coupon_idc              3   .   K   | ]  }|z
  d z    yw)rO   Nr   )r5   xmeans     r'   r8   z8SignalProcessor.process_order_signals.<locals>.<genexpr>   s     >IqAH?Is   rO   )total_orderstotal_spentavg_order_value)unique_restaurants_counttop_restaurantsreorder_ratio)	top_itemstotal_items_ordered)coupon_order_countcoupon_ratio)avg_days_between_ordersstd_dev_days_between_orderslast_order_days_ago)top_restaurant_sharetop_item_shareorder_volume_lowrI   )by_hourby_day)metricsloyalty	inventory	financialvelocityconcentrationrW   
item_pairscategory_pairsorder_temporallocation_orderlocation_timelocation_tripletlocation_significant)'rX   r
   rY   defaultdictlistr3   appendr[   r   r(   r   r   r   r   strr   rZ   r]   r   strftimer   r\   r   r   r   setr   r   sortedranger4   r_   mathsqrtr   r7   r   r   r   )4r&   rf   rg   items_by_orderr   r   
order_hour	order_dayr   r   r   rm   orders_with_locationlocation_order_countsr   r   r   loc_time_hourloc_time_daydthr   location_time_by_hourdlocation_time_by_dayorders_with_loc_countdistinct_locationslocation_time_pairslocation_order_significantlocation_time_significantr   r   triplet_countshour_bucketitemscatscatrestaurantstop_restaurant_count
item_namesitem_countsr   top_item_countcoupon_ordersorder_datesavg_velocitystd_dev_velocitylast_order_daysrr   	intervalsvariancer   s4                                                      @r'   process_order_signalsz%SignalProcessor.process_order_signalsO   s   +,,$006D488J/077=  K
KK 5j4K[K[ \
I,>

 1, 
	 tCH~ 	 # 	 
 BLbA_`OaOA$6 7b + 3 33[FZ3[ [ 4??@TU
U
U U3U 	 

 $++-"**,*FAs!!!%%"56BsBKK$5671<7c2;;t#456!;6	 +  -889LM!
Ma! a!4M 	 !
  ,778KL 
La! Q3L 	  

 !$$8 9 !67!-03|3DD!%==nBTXnBn 	# %8;R$R!9W>WN(002N.3%%aeeL&9: kk$/&**155;;  % 
  C&[#'>?1D?   /  )7(B(BCY(Z (Z$MS!S1 !!qI(Z   
 "))Z*tZ[\[`[`ar[s1551B+CZ*tuCN{66q9!<Q?TU "
!%+)> HH[!:TXXk%::! 	 

 "))*5++K8	,51a1$.aJq!%%2D{I[_`I`Ja<FpJq$JZJZ[\[`[`am[nJoTaeeL12Jp
 {aMRSTVYZeVfMghMg+a.;q1u+==CCMgIh Y#i.!@!DLy>C	N2D>I>>YOH$TYYx%8!<'||~B?EEO !$J*NX5s:)F#J^_ -0,<#.#:#:;L#M!8!E
 (1YX&)-&8R\c-&83z?&JA Nbc
 ,8/?'6 ]g.BS_.TVW(XlmOX%Y(G"K^_ -c*o@Z.Z[$,'1)D,)>J^_ 0$8A!
 !	
g  c
!
 
> 
 +u
 b q is[   ZZZZ Z'-Z.
Z61Z6*AZ;3[ 
[ [ .%["[([
raw_impressionsc           
      v   |st        t              S t        j                  |D cg c]%  }|j	                  d      s|j	                  d      ' c}      }t        t        |D cg c]%  }|j	                  d      s|j	                  d      ' c}            }t        |      |t        |      dS c c}w c c}w )Nr,   restaurant_id)total_impressionsunique_restaurants_seenscreen_exposure)rX   r   rY   rZ   r3   r\   r   )r&   r   rr   screensrestaurants_seens        r'   process_impression_signalsz*SignalProcessor.process_impression_signals   s    011%%&c1STSXSXYaSbquuX&cdsO#nOqWXW\W\]lWmAEE/$:O#nop!$_!5'7#G}
 	
 'd#ns   B1B1$B6;B6raw_reviewsc                 n   |st        t              S |D cg c].  }|j                  d      t        |j                  d            0 }}dj	                  |D cg c])  }|j                  d      s|j                  d      xs d+ c}      j                         }|j                         }t        d |D              }t        d |D              }t        |      |r!t        t        |      t        |      z  d      ndt        t        j                  |            ||t        |      d	d
S c c}w c c}w )Nrating reviewr|   c              3   2   K   | ]  }|t         v sd   ywr   N)r   r5   ws     r'   r8   z<SignalProcessor.process_sentiment_signals.<locals>.<genexpr>        K5aA9J4J5   c              3   2   K   | ]  }|t         v sd   ywr   )r   r   s     r'   r8   z<SignalProcessor.process_sentiment_signals.<locals>.<genexpr>   r   r   rO   r   )positive_keyword_countnegative_keyword_counttotal_word_count)review_count
avg_ratingrating_distributiontext_evidence)rX   r   r3   rl   joinr^   splitr[   r\   r_   rY   rZ   )r&   r   rratingsall_textwordspositive_evidencenegative_evidences           r'   process_sentiment_signalsz)SignalProcessor.process_sentiment_signals   s   /003>^;a!%%/B]5x);^88kekaeeT\oacaeeHo33keflln K5KKK5KK,CJ%Gs7| ;Q?PQ#'(;(;G(D#E*;*;$'J		
 		
 _es   D-D-.D2N)__name__
__module____qualname____doc__r   r   r   r(   r   r   r   re   r   r   r  r   r)   r'   r!   r!   #   s    
$C $HX,> $!
tDcN7K !
PTUXZ]U]P^ !
FT
T#s(^0D T
QUVZ[^`c[cVdQe T
jnortwowjx T
l	
$tCH~:N 	
SWX[]`X`Sa 	

T$sCx.5I 
dSVX[S[n 
r)   r!   )"rY   r   r   typingr   r   r   r   	constantsr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   helpersr   r   r   r   r!   r   r)   r'   <module>r     sH       , ,     * ^
 ^
r)   