def test_group_by_start_end(self):
        """Test group by start-end"""
        data = utils.get_data()
        self.assertIsNotNone(data)
        result = utils.group_by_start_end(data[11])
        self.assertIsNotNone(result)
        self.assertIsInstance(data, dict)
        self.assertEqual(len(result), 7)
        self.assertDictEqual(result, {
            0: {'start': [33134], 'end': [57257]},
            1: {'start': [33590], 'end': [50154]},
            2: {'start': [33206], 'end': [58527]},
            3: {'start': [37116, 34088], 'end': [60085, 57087]},
            4: {'start': [47816], 'end': [54242]},
            5: {'start': [], 'end': []},
            6: {'start': [], 'end': []}
            }
            )

        result = utils.group_by_start_end(data[10])
        self.assertIsNotNone(result)
        self.assertIsInstance(data, dict)
        self.assertEqual(len(result), 7)
        self.assertDictEqual(result, {
            0: {'start': [], 'end': []},
            1: {'start': [34745], 'end': [64792]},
            2: {'start': [33592], 'end': [58057]},
            3: {'start': [38926], 'end': [62631]},
            4: {'start': [], 'end': []},
            5: {'start': [], 'end': []},
            6: {'start': [], 'end': []}
            }
            )
    def test_group_by_start_end(self):
        """
        Test group_by_start_end.
        """
        testResult = utils.group_by_start_end(utils.get_data()[10])
        testResult2 = utils.group_by_start_end(utils.get_data()[11])
        testResult3 = utils.group_by_start_end([])

        self.assertListEqual(
            testResult, [
                [[], []],
                [[34745], [64792]],
                [[33592], [58057]],
                [[38926], [62631]],
                [[], []],
                [[], []],
                [[], []]
            ]

        )
        self.assertListEqual(
            testResult2, [
                [[33134], [57257]],
                [[33590], [50154]],
                [[33206], [58527]],
                [[37116, 34088], [60085, 57087]],
                [[47816], [54242]],
                [[], []],
                [[], []],
            ]
        )
        self.assertListEqual(
            testResult3, [
                [[], []],
                [[], []],
                [[], []],
                [[], []],
                [[], []],
                [[], []],
                [[], []]
            ]
        )
def presence_start_end_view(user_id):
    """Returns mean start and end time of given user"""
    data = get_data()
    if user_id not in data:
        log.debug('User {0} not found!'.format(user_id))
        return []

    weekdays = group_by_start_end(data[user_id])
    result = [
        (calendar.day_abbr[weekday], mean(times['start']), mean(times['end']))
        for weekday, times in weekdays.items()]

    return result
def presence_start_end_view(user_id):
    """
    Returns mean presence time of given
    user grouped by weekday and start/end hour.
    """
    data = get_data()
    if user_id not in data:
        return 0

    weekdays = group_by_start_end(data[user_id])
    result = [(calendar.day_abbr[weekday], mean(start_end[0]),
               mean(start_end[1]))
              for weekday, start_end in enumerate(weekdays)]
    return result
 def test_group_by_start_end(self):
     """
     Test grouping user time by start/end.
     """
     data = utils.get_data()
     for user in data.itervalues():
         current_user = utils.group_by_start_end(user)
         self.assertIsInstance(current_user, dict, msg=str(user))
         self.assertEqual(len(current_user), 7, msg=str(user))
         self.assertItemsEqual(current_user.keys(), range(7),
                               msg=str(current_user))
         for item in current_user.itervalues():
             self.assertIsInstance(item, dict, msg=str(item))
             self.assertEqual(len(item), 2)
             self.assertItemsEqual(item.keys(), ['start_list', 'end_list'],
                                   msg=str(item))
def presence_start_end_view(user_id):
    """
    Returns presence average time.
    """
    data = get_data()
    if user_id not in data:
        log.debug('User %s not found!', user_id)
        return []

    weekdays = group_by_start_end(data[user_id])
    result = []

    for (weekday, presence_dict) in weekdays.items():
        start = mean(presence_dict['start_list'])
        end = mean(presence_dict['end_list'])
        result.append([calendar.day_abbr[weekday], start, end])
    return result