Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1.  
  2. def group_user_actions(fund_id, date_from, date_to):
  3.     result_actions = db.session \
  4.         .query(
  5.             FundUser.id.label('fund_user_id'),
  6.             db.func.count(Action.id).label('number_of_actions')
  7.         ) \
  8.         .outerjoin(Action, db.and_(
  9.             Action.fund_user_id == FundUser.id,
  10.             Action.date >= date_from,
  11.             Action.date <= date_to
  12.         )) \
  13.         .filter(
  14.             FundUser.fund_id == fund_id,
  15.             FundUser.role != 'client'
  16.         ) \
  17.         .group_by(FundUser.id) \
  18.         .all()
  19.  
  20.     result_sessions = db.session \
  21.         .query(
  22.             FundUser.id.label('fund_user_id'),
  23.             db.func.sum(Session.duration).label('duration')
  24.         ) \
  25.         .outerjoin(Session, db.and_(
  26.             Session.fund_user_id == FundUser.id,
  27.             Session.end >= date_from,
  28.             Session.end <= date_to
  29.         )) \
  30.         .filter(
  31.             FundUser.fund_id == fund_id,
  32.             FundUser.role != 'client'
  33.         ) \
  34.         .group_by(FundUser.id) \
  35.         .all()
  36.  
  37.     return {'actions': result_actions, 'sessions': result_sessions}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement