Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug 1241661] Calculate CSAT metrics #2788

Merged
merged 4 commits into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion kitsune/kpi/cron.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import operator
from datetime import datetime, date, timedelta
from functools import reduce
Expand All @@ -6,6 +7,7 @@
from django.db.models import Count, F, Q

import cronjobs
import requests
from statsd import statsd

from kitsune.customercare.models import Reply
Expand All @@ -17,7 +19,10 @@
SUPPORT_FORUM_CONTRIBUTORS_METRIC_CODE, VISITORS_METRIC_CODE, SEARCH_SEARCHES_METRIC_CODE,
SEARCH_CLICKS_METRIC_CODE, EXIT_SURVEY_YES_CODE, EXIT_SURVEY_NO_CODE,
EXIT_SURVEY_DONT_KNOW_CODE, CONTRIBUTOR_COHORT_CODE, KB_ENUS_CONTRIBUTOR_COHORT_CODE,
KB_L10N_CONTRIBUTOR_COHORT_CODE, SUPPORT_FORUM_HELPER_COHORT_CODE, AOA_CONTRIBUTOR_COHORT_CODE)
KB_L10N_CONTRIBUTOR_COHORT_CODE, SUPPORT_FORUM_HELPER_COHORT_CODE, AOA_CONTRIBUTOR_COHORT_CODE,
CONTRIBUTORS_CSAT_METRIC_CODE, AOA_CONTRIBUTORS_CSAT_METRIC_CODE,
KB_ENUS_CONTRIBUTORS_CSAT_METRIC_CODE, KB_L10N_CONTRIBUTORS_CSAT_METRIC_CODE,
SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe all these codes should be in some sort of shared object? Something like this? Or do you think it is preferable to import them all like this?

from kitsune.kpi import config
config.SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE

from kitsune.kpi.surveygizmo_utils import (
get_email_addresses, add_email_to_campaign, get_exit_survey_results,
SURVEYS)
Expand Down Expand Up @@ -581,3 +586,85 @@ def is_in_cohort(u):
cohort |= set(filter(is_in_cohort, potential_users))

return cohort


@cronjobs.register
def calculate_csat_metrics():
user = settings.SURVEYGIZMO_USER
password = settings.SURVEYGIZMO_PASSWORD
startdate = date.today() - timedelta(days=2)
enddate = date.today() - timedelta(days=1)
page = 1
more_pages = True
survey_id = SURVEYS['general']['community_health']

csat = {
CONTRIBUTORS_CSAT_METRIC_CODE: 0,
SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
AOA_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
KB_ENUS_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
KB_L10N_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
}

counts = {
CONTRIBUTORS_CSAT_METRIC_CODE: 0,
SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
AOA_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
KB_ENUS_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
KB_L10N_CONTRIBUTORS_CSAT_METRIC_CODE: 0,
}

while more_pages:
response = requests.get(
'https://restapi.surveygizmo.com/v2/survey/{survey}'
'/surveyresponse?'
'filter[field][0]=datesubmitted'
'&filter[operator][0]=>=&filter[value][0]={start}+0:0:0'
'filter[field][1]=datesubmitted'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have an '&' at the beginning here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, yes!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funny that it doesn't complain about this when i run the cronjob

'&filter[operator][1]=<&filter[value][1]={end}+0:0:0'
'&filter[field][2]=status&filter[operator][2]=='
'&filter[value][2]=Complete'
'&resultsperpage=500'
'&page={page}'
'&user:pass={user}:{password}'.format(
survey=survey_id, start=startdate,
end=enddate, page=page, user=user, password=password),
timeout=300)

results = json.loads(response.content)
total_pages = results['total_pages']
more_pages = page < total_pages

for r in results['data']:
try:
rating = int(r['[question(3)]'])
except ValueError:
# CSAT question was not answered
pass
else:
csat[CONTRIBUTORS_CSAT_METRIC_CODE] += rating
counts[CONTRIBUTORS_CSAT_METRIC_CODE] += 1

if len(r['[question(4), option(10010)]']): # Army of Awesome
csat[AOA_CONTRIBUTORS_CSAT_METRIC_CODE] += rating
counts[AOA_CONTRIBUTORS_CSAT_METRIC_CODE] += 1

if len(r['[question(4), option(10011)]']): # Support Forum
csat[SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE] += rating
counts[SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE] += 1

if len(r['[question(4), option(10012)]']): # KB EN-US
csat[KB_ENUS_CONTRIBUTORS_CSAT_METRIC_CODE] += rating
counts[KB_ENUS_CONTRIBUTORS_CSAT_METRIC_CODE] += 1

if len(r['[question(4), option(10013)]']): # KB L10N
csat[KB_L10N_CONTRIBUTORS_CSAT_METRIC_CODE] += rating
counts[KB_L10N_CONTRIBUTORS_CSAT_METRIC_CODE] += 1

page += 1

for code in csat:
metric_kind, _ = MetricKind.objects.get_or_create(code=code)
value = csat[code] / counts[code] if counts[code] else 50 # If no responses assume neutral
Metric.objects.update_or_create(kind=metric_kind, start=startdate, end=enddate,
defaults={'value': value})
6 changes: 6 additions & 0 deletions kitsune/kpi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
EXIT_SURVEY_NO_CODE = 'exit-survey:no'
EXIT_SURVEY_DONT_KNOW_CODE = 'exit-survey:dont-know'

CONTRIBUTORS_CSAT_METRIC_CODE = 'csat contributors'
AOA_CONTRIBUTORS_CSAT_METRIC_CODE = 'csat contributors:aoa'
SUPPORT_FORUM_CONTRIBUTORS_CSAT_METRIC_CODE = 'csat contributors:supportforum'
KB_ENUS_CONTRIBUTORS_CSAT_METRIC_CODE = 'csat contributors:kb:en-US'
KB_L10N_CONTRIBUTORS_CSAT_METRIC_CODE = 'csat contributors:kb-l10n'

CONTRIBUTOR_COHORT_CODE = 'contributor'
KB_ENUS_CONTRIBUTOR_COHORT_CODE = 'contributor:kb:en-US'
KB_L10N_CONTRIBUTOR_COHORT_CODE = 'contributor:kb:l10n'
Expand Down
1 change: 1 addition & 0 deletions kitsune/kpi/surveygizmo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'email_collection_survey_id': 1002970,
'exit_survey_id': 991425,
'exit_survey_campaign_id': 878533,
'community_health': 2466367,
},
'questions': { # This is for users that are browsing questions.
'email_collection_survey_id': 1717268,
Expand Down
1 change: 1 addition & 0 deletions scripts/crontab/crontab.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ HOME = /tmp
00 00 * * * {{ cron }} rebuild_kb
42 00 * * * {{ cron }} update_top_contributors
00 01 * * * {{ cron }} update_l10n_coverage_metrics
00 01 * * * {{ cron }} calculate_csat_metrics
11 01 * * * {{ cron }} report_employee_answers
30 01 * * * {{ cron }} reindex_users_that_contributed_yesterday
40 01 * * * {{ cron }} update_weekly_votes
Expand Down