Exemple #1
0
def campaign_count(campaign_id):
    campaign = Campaign.query.filter_by(id=campaign_id).first_or_404()

    # number of calls completed in campaign
    calls_completed = db.session.query(func.Count(Call.id)).filter_by(
        campaign_id=campaign.id, status='completed')

    # list of sessions with at least two completed calls
    # grouped by referral_code, include count
    referrers = db.session.query(
        Session.referral_code,
        func.Count(Call.id)
    ).join(Call).filter(
        Call.campaign_id==campaign.id,
        Call.status=='completed'
    ).group_by(Session.referral_code)\
    .having(func.count(Call.id) > 2)

    return jsonify({
        'completed':
        calls_completed.scalar(),
        'last_24h':
        calls_completed.filter(
            Call.timestamp >= datetime.now() - timedelta(hours=24)).scalar(),
        'last_week':
        calls_completed.filter(
            Call.timestamp >= datetime.now() - timedelta(days=7)).scalar(),
        'referral_codes':
        dict(referrers)
    })
Exemple #2
0
def campaign_count(campaign_id):
    campaign = Campaign.query.filter_by(id=campaign_id).first_or_404()

    # number of calls completed in campaign
    calls_completed = db.session.query(func.Count(Call.id)).filter_by(
        campaign_id=campaign.id, status='completed').scalar()

    return jsonify({'completed': calls_completed})
Exemple #3
0
                    adapted_data = None
            else:
                current_app.logger.error('no target_data for %s: %s' % (target_uid, e))
                adapted_data = None

        if adapted_data:    
            targets[target_uid]['title'] = adapted_data.get('title')
            targets[target_uid]['name'] = adapted_data.get('name')
            targets[target_uid]['district'] = adapted_data.get('district')
        else:
            targets[target_uid]['title'] = target_title
            targets[target_uid]['name'] = target_name
            targets[target_uid]['district'] = target_uid

    # query calls to count status
    query_target_status = query_call_targets.group_by(Call.status).with_entities(Call.status, Target.uid, func.Count(Call.id))

    for (call_status, target_uid, count) in query_target_status:
        if call_status in TWILIO_CALL_STATUS:
            # combine calls status for each target
            targets[target_uid][call_status] = targets.get(target_uid, {}).get(call_status, 0) + count

    return jsonify({'objects': targets})


# returns twilio call sids made to a particular phone number
# searches phone_hash if available, otherwise the twilio api
@api.route('/twilio/calls/to/<phone>/', methods=['GET'])
@api_key_or_auth_required
def call_sids_for_number(phone):
Exemple #4
0
                current_app.logger.error('no target_data for %s: %s' %
                                         (target_uid, e))

        if adapted_data:
            targets[target_uid]['title'] = adapted_data.get('title')
            targets[target_uid]['name'] = adapted_data.get('name')
            targets[target_uid]['district'] = adapted_data.get('district')
        else:
            targets[target_uid]['title'] = target_title
            targets[target_uid]['name'] = target_name
            targets[target_uid]['district'] = target_uid

    # query calls to count status
    query_target_status = query_call_targets.group_by(
        Call.status).with_entities(Call.status, Target.key,
                                   func.Count(Call.id))

    for (call_status, target_uid, count) in query_target_status:
        if call_status in TWILIO_CALL_STATUS:
            # combine calls status for each target
            targets[target_uid][call_status] = targets.get(target_uid, {}).get(
                call_status, 0) + count

    return jsonify({'objects': targets})


# returns twilio call sids made to a particular phone number
# searches phone_hash if available, otherwise the twilio api
@api.route('/twilio/calls/to/<phone>/', methods=['GET'])
@api_key_or_auth_required
def call_sids_for_number(phone):
Exemple #5
0
def campaign_target_calls(campaign_id):
    start = request.values.get('start')
    end = request.values.get('end')

    campaign = Campaign.query.filter_by(id=campaign_id).first_or_404()

    query_call_targets = (db.session.query(
        Target.title, Target.name, Target.key).join(Call).filter(
            Call.campaign_id == int(campaign.id)).group_by(
                Target.title).group_by(Target.name).group_by(Target.key))

    if start:
        try:
            startDate = dateutil.parser.parse(start)
        except ValueError:
            abort(400, 'start should be in isostring format')
        query_call_targets = query_call_targets.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_call_targets = query_call_targets.filter(
            Call.timestamp <= endDate)

    targets = defaultdict(dict)
    political_data = campaign.get_campaign_data().data_provider

    for (target_title, target_name, target_uid) in query_call_targets:
        # get more target_data from political_data cache
        try:
            target_data = political_data.cache_get(target_uid)[0]
        except (KeyError, IndexError):
            target_data = political_data.cache_get(target_uid)
        except Exception as e:
            current_app.logger.error('unable to cache_get for %s: %s' %
                                     (target_uid, e))
            target_data = None

        # use adapter to get title, name and district
        adapted_data = None
        if ':' in target_uid:
            data_adapter = adapt_by_key(target_uid)
            try:
                if target_data:
                    adapted_data = data_adapter.target(target_data)
                else:
                    adapted_data = data_adapter.target({
                        'title': target_title,
                        'name': target_name,
                        'uid': target_uid
                    })
            except AttributeError:
                current_app.logger.error(
                    'unable to adapt target_data for %s: %s' %
                    (target_uid, target_data))

        elif political_data.country_code.lower(
        ) == 'us' and campaign.campaign_type == 'congress':
            # fall back to USData, which uses bioguide
            if not target_data:
                try:
                    target_data = political_data.get_bioguide(target_uid)[0]
                except Exception as e:
                    current_app.logger.error(
                        'unable to get_bioguide for %s: %s' % (target_uid, e))
            if target_data:
                try:
                    data_adapter = UnitedStatesData()
                    adapted_data = data_adapter.target(target_data)
                except AttributeError:
                    current_app.logger.error(
                        'unable to adapt target_data for %s: %s' %
                        (target_uid, target_data))
            else:
                current_app.logger.error('no target_data for %s' % target_uid)

        if adapted_data:
            targets[target_uid]['title'] = adapted_data.get('title')
            targets[target_uid]['name'] = adapted_data.get('name')
            targets[target_uid]['district'] = adapted_data.get('district')
        else:
            targets[target_uid]['title'] = target_title
            targets[target_uid]['name'] = target_name
            targets[target_uid]['district'] = target_uid

    # query calls to count status
    query_target_status = query_call_targets.group_by(
        Call.status).with_entities(Call.status, Target.key,
                                   func.Count(Call.id))

    for (call_status, target_uid, count) in query_target_status:
        if call_status in TWILIO_CALL_STATUS:
            # combine calls status for each target
            targets[target_uid][call_status] = targets.get(target_uid, {}).get(
                call_status, 0) + count

    return jsonify({'objects': targets})