def handle_overview_question(message): message.reply( "Let me check what's been happening. This may take a second.") seven_days_ago = datetime.datetime.now() - datetime.timedelta(days=7) stories = Story.select().where(Story.tracking_started > seven_days_ago) slugs = Set() for story in stories: print story.name story_slugs = story.slug.split(',') for slug in story_slugs: slugs.add(slug) total_users = analytics.get_user_data(start_date='7daysAgo') total_users = int(total_users['rows'][0][0]) total_users = "{:,}".format(total_users) median = analytics.get_linger_rate(start_date='7daysAgo') linger_rows = analytics.get_linger_rows(start_date='7daysAgo') linger_histogram_url = ChartTools.linger_histogram_link( linger_rows, median) attachments = [{ "fallback": "linger update", "color": "#eeeeee", "title": "Time spent on graphics over the last week", "image_url": linger_histogram_url }] slackTools.send_message( message.body['channel'], "In the past 7 days, I've tracked %s stories and %s graphics." % (len(stories), len(slugs))) slackTools.send_message( message.body['channel'], "%s people looked at graphics on those stories. Here's how much time they spent:" % total_users, attachments, unfurl_links=False) fields = [] for story in stories: print "Adding %s" % story.name fields.append({ "title": story.name.strip(), "value": '<' + story.url + '|' + story.slug.strip() + '>', "short": True }) attachments = [{ "fallback": "linger update", "color": "#eeeeee", # "title": "What we have done", "fields": fields }] slackTools.send_message(message.body['channel'], "Here's everything:", attachments, unfurl_links=False)
def handle_slug_question(message): m = re.search(LINGER_RATE_REGEX, message.body['text']) if not m: return slug = m.group(1) if slug: median = analytics.get_linger_rate(slug) stories = Story.select().where(Story.slug.contains(slug)) message.reply("Ok! I'm looking up %s. This may take a second." % slug) if median: people = "{:,}".format(median['total_people']) time_text = TimeTools.humanist_time_bucket(median) reply = u"*%s* people spent a median *%s* on `%s`." % ( people, time_text, slug) # List the stories this slug appears on reply += '\n\nThis graphic appears in %s %s:' % ( inflector.number_to_words( len(stories)), inflector.plural('story', len(stories))) for story in stories: reply += '\n' + '*<%s|%s>*' % (story.url, story.name.strip()) # Get linger rate data linger_rows = analytics.get_linger_rows(slug) linger_histogram_url = ChartTools.linger_histogram_link( linger_rows, median) all_graphics_rows = analytics.get_linger_rows() all_graphics_median = analytics.get_linger_rate() all_histogram = ChartTools.linger_histogram_link( all_graphics_rows, all_graphics_median) attachments = [{ "fallback": slug + " update", "color": "#eeeeee", "title": slug, "image_url": linger_histogram_url }, { "fallback": slug + " update", "color": "#eeeeee", "title": "How all graphics performed", "image_url": all_histogram }] # Get scroll data, if any. scroll_depth_rows = analytics.get_depth_rate(slug) if scroll_depth_rows: scroll_histogram_url = ChartTools.scroll_histogram_link( scroll_depth_rows) if stories[0].screenshot: scroll_histogram_url = ChartTools.add_screenshot_to_chart( stories[0].screenshot, scroll_histogram_url) attachments.append({ "fallback": slug + " update", "color": "#eeeeee", "title": "How far down did people scroll?", "image_url": scroll_histogram_url }) slackTools.send_message(message.body['channel'], reply, attachments, unfurl_links=False) else: message.reply("I wasn't able to figure out the linger rate of %s" % slug)