def campaign_date_calls(campaign_id): start = request.values.get('start') end = request.values.get('end') timespan = request.values.get('timespan', 'day') if timespan not in API_TIMESPANS.keys(): abort(400, 'timespan should be one of %s' % ','.join(API_TIMESPANS)) else: timespan_strf, timespan_to_char = API_TIMESPANS[timespan] campaign = Campaign.query.filter_by(id=campaign_id).first_or_404() timestamp_to_char = func.to_char(Call.timestamp, timespan_to_char).label(timespan) query = ( db.session.query( func.min(Call.timestamp.label('date')), timestamp_to_char, Call.status, func.count(distinct(Call.id)).label('calls_count') ) .filter(Call.campaign_id == int(campaign.id)) .group_by(timestamp_to_char) .order_by(timespan) .group_by(Call.status) ) if start: try: startDate = dateutil.parser.parse(start) except ValueError: abort(400, 'start should be in isostring format') query = query.filter(Call.timestamp >= startDate) if end: try: endDate = dateutil.parser.parse(end) if start: if endDate < startDate: abort(400, 'end should be after start') if endDate == startDate: endDate = startDate + timedelta(days=1) except ValueError: abort(400, 'end should be in isostring format') query = query.filter(Call.timestamp <= endDate) dates = defaultdict(dict) for (date, timespan, call_status, count) in query.all(): # combine status values by date for status in TWILIO_CALL_STATUS: if call_status == status: date_string = date.strftime(timespan_strf) dates[date_string][status] = count sorted_dates = OrderedDict(sorted(dates.items())) return jsonify({'objects': sorted_dates})
def campaign_date_calls(campaign_id): start = request.values.get('start') end = request.values.get('end') timespan = request.values.get('timespan', 'day') if timespan not in API_TIMESPANS.keys(): abort(400, 'timespan should be one of %s' % ','.join(API_TIMESPANS)) else: timespan_strf = API_TIMESPANS[timespan] campaign = Campaign.query.filter_by(id=campaign_id).first_or_404() timespan_extract = extract(timespan, Call.timestamp).label(timespan) query = ( db.session.query( func.min(Call.timestamp.label('date')), timespan_extract, Call.status, func.count(distinct(Call.id)).label('calls_count') ) .filter(Call.campaign_id == int(campaign.id)) .group_by(timespan_extract) .order_by(timespan) .group_by(Call.status) ) if start: try: startDate = dateutil.parser.parse(start) except ValueError: abort(400, 'start should be in isostring format') query = query.filter(Call.timestamp >= startDate) if end: try: endDate = dateutil.parser.parse(end) if endDate < startDate: abort(400, 'end should be after start') if endDate == startDate: endDate = startDate + timedelta(days=1) except ValueError: abort(400, 'end should be in isostring format') query = query.filter(Call.timestamp <= endDate) dates = defaultdict(dict) for (date, timespan, call_status, count) in query.all(): # combine status values by date for status in TWILIO_CALL_STATUS: if call_status == status: date_string = date.strftime(timespan_strf) dates[date_string][status] = count sorted_dates = OrderedDict(sorted(dates.items())) return Response(json.dumps(sorted_dates), mimetype='application/json')
def campaigns_overall(): start = request.values.get('start') end = request.values.get('end') timespan = request.values.get('timespan', 'day') if timespan not in API_TIMESPANS.keys(): abort(400, 'timespan should be one of %s' % ','.join(API_TIMESPANS)) else: timespan_strf, timespan_to_char = API_TIMESPANS[timespan] timestamp_to_char = func.to_char(Call.timestamp, timespan_to_char).label(timespan) query = (db.session.query( func.min(Call.timestamp.label('date')), Call.campaign_id, timestamp_to_char, func.count(distinct(Call.id)).label('calls_count')).group_by( Call.campaign_id).group_by(timestamp_to_char).order_by(timespan)) completed_query = db.session.query(Call.timestamp, Call.id).filter_by(status='completed') if start: try: startDate = dateutil.parser.parse(start) except ValueError: abort(400, 'start should be in isostring format') query = query.filter(Call.timestamp >= startDate) completed_query = completed_query.filter(Call.timestamp >= startDate) if end: try: endDate = dateutil.parser.parse(end) if start: if endDate < startDate: abort(400, 'end should be after start') if endDate == startDate: endDate = startDate + timedelta(days=1) except ValueError: abort(400, 'end should be in isostring format') query = query.filter(Call.timestamp <= endDate) completed_query = completed_query.filter(Call.timestamp <= endDate) dates = defaultdict(dict) for (date, campaign_id, timespan, count) in query.all(): date_string = date.strftime(timespan_strf) dates[date_string][int(campaign_id)] = count sorted_dates = OrderedDict(sorted(dates.items())) meta = {'calls_completed': completed_query.count()} return jsonify({'meta': meta, 'objects': sorted_dates})