Exemplo n.º 1
0
    def respond(self, message):
        """
        Respond to requests about the last seven days of data
        TODO: Loop over all stories and report stats on each
        """
        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)

        try:
            team = self.config.get_team_for_story(stories[0])
        except:
            team = self.config.get_default_team()

        total_users = self.get_user_data(team=team, start_date='7daysAgo')
        total_users = int(total_users['rows'][0][0])
        total_users = "{:,}".format(total_users)

        npr_linger = NPRLingerRate()
        linger_rows = npr_linger.get_linger_data(team=team, start_date='7daysAgo')
        median = NPRLingerRate.get_median(linger_rows)
        linger_histogram_url = npr_linger.get_histogram_url(linger_rows, median)

        attachments = [{
            "fallback": "linger update",
            "color": "#eeeeee",
            "title": "Time spent on graphics over the last week",
            "image_url": linger_histogram_url
        }]

        text = "In the past 7 days, I've tracked {0} stories and {1} graphics.".format(len(stories), len(slugs))
        text += "\n\n"
        text += "{0} people looked at graphics on the property. Here's how much time they spent:".format(total_users)

        fields = []
        for story in stories:
            fields.append({
                "title": story.name.strip(),
                "value": "<{0}|{1}>".format(story.url, story.slug.strip()),
                "short": True
            })

        attachments.append({
            "fallback": "linger update",
            "color": "#eeeeee",
            # "title": "What we have done",
            "fields": fields
        })

        return {
            'text': text,
            'attachments': attachments
        }
Exemplo n.º 2
0
 def test_median_of_time_buckets(self):
     test_data = [
         [1, 10],
         [2, 20],
         [3, 10]
     ]
     median = NPRLingerRate.median_of_time_buckets(test_data)
     self.assertEqual(median, 2)
Exemplo n.º 3
0
    def test_handle_slug_inquiry(self,
                                 mock_upload,
                                 mock_histogram,
                                 mock_linger,
                                ):

        # Set some fake analytics
        linger_data = [
            [10, 10],
            [20, 10],
            [30, 10],
            [40, 10],
            [50, 10],
            [60, 10],
            [120, 10],
            [180, 10],
            [240, 10],
            [300, 10],
        ]
        mock_linger.return_value = linger_data
        mock_histogram.return_value = 'http://image-url-here'
        mock_upload.return_value = 'http://image-url-here'

        slug = 'x-y-z'
        linger = NPRLingerRate()
        class FakeMessage(object):
            body = {
                'text': 'check slug ' + slug
            }

        clear_stories()
        Story.create(
            name = 'example',
            slug = slug,
            date = datetime.datetime.now(),
            url = 'example.com',
            team = 'deafult'
        )

        message = linger.handle_slug_inquiry(FakeMessage)
        print message
        assert u'*100* people spent a median *55 seconds* on `x-y-z`' in message['text']
        self.assertEqual(message['attachments'][0]['title'], slug)
Exemplo n.º 4
0
    def test_handle_slug_inquiry(
        self,
        mock_upload,
        mock_histogram,
        mock_linger,
    ):

        # Set some fake analytics
        linger_data = [
            [10, 10],
            [20, 10],
            [30, 10],
            [40, 10],
            [50, 10],
            [60, 10],
            [120, 10],
            [180, 10],
            [240, 10],
            [300, 10],
        ]
        mock_linger.return_value = linger_data
        mock_histogram.return_value = 'http://image-url-here'
        mock_upload.return_value = 'http://image-url-here'

        slug = 'x-y-z'
        linger = NPRLingerRate()

        class FakeMessage(object):
            body = {'text': 'check slug ' + slug}

        clear_stories()
        Story.create(name='example',
                     slug=slug,
                     date=datetime.datetime.now(),
                     url='example.com',
                     team='deafult')

        message = linger.handle_slug_inquiry(FakeMessage)
        print message
        assert u'*100* people spent a median *55 seconds* on `x-y-z`' in message[
            'text']
        self.assertEqual(message['attachments'][0]['title'], slug)
Exemplo n.º 5
0
 def test_get_median(self):
     test_data = [
         [60, 25],
         [90, 50],
         [120, 25],
     ]
     median = NPRLingerRate.get_median(test_data)
     self.assertEqual(median['total_people'], 100)
     self.assertEqual(median['raw_avg_seconds'], 90)
     self.assertEqual(median['minutes'], 1)
     self.assertEqual(median['seconds'], 30)
Exemplo n.º 6
0
 def test_get_median(self):
     test_data = [
         [60, 25],
         [90, 50],
         [120, 25],
     ]
     median = NPRLingerRate.get_median(test_data)
     self.assertEqual(median['total_people'], 100)
     self.assertEqual(median['raw_avg_seconds'], 90)
     self.assertEqual(median['minutes'], 1)
     self.assertEqual(median['seconds'], 30)
Exemplo n.º 7
0
 def test_median_of_time_buckets(self):
     test_data = [[1, 10], [2, 20], [3, 10]]
     median = NPRLingerRate.median_of_time_buckets(test_data)
     self.assertEqual(median, 2)
Exemplo n.º 8
0
    def respond(self, message):
        """
        Respond to requests about the last seven days of data
        TODO: Loop over all stories and report stats on each
        """
        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)

        try:
            team = self.config.get_team_for_story(stories[0])
        except:
            team = self.config.get_default_team()

        total_users = self.get_user_data(team=team, start_date='7daysAgo')
        total_users = int(total_users['rows'][0][0])
        total_users = "{:,}".format(total_users)

        npr_linger = NPRLingerRate()
        linger_rows = npr_linger.get_linger_data(team=team,
                                                 start_date='7daysAgo')
        median = NPRLingerRate.get_median(linger_rows)
        linger_histogram_url = npr_linger.get_histogram_url(
            linger_rows, median)

        attachments = [{
            "fallback": "linger update",
            "color": "#eeeeee",
            "title": "Time spent on graphics over the last week",
            "image_url": linger_histogram_url
        }]

        text = "In the past 7 days, I've tracked {0} stories and {1} graphics.".format(
            len(stories), len(slugs))
        text += "\n\n"
        text += "{0} people looked at graphics on the property. Here's how much time they spent:".format(
            total_users)

        fields = []
        for story in stories:
            fields.append({
                "title":
                story.name.strip(),
                "value":
                "<{0}|{1}>".format(story.url, story.slug.strip()),
                "short":
                True
            })

        attachments.append({
            "fallback": "linger update",
            "color": "#eeeeee",
            # "title": "What we have done",
            "fields": fields
        })

        return {'text': text, 'attachments': attachments}