# Path: moira/graphite/datalib.py
# def unpackTimeSeries(dataList, retention, startTime, endTime, bootstrap, allowRealTimeAlerting):
# 
#     def getTimeSlot(timestamp):
#         return int((timestamp - startTime) / retention)
# 
#     valuesList = []
#     for data in dataList:
#         points = {}
#         for value, timestamp in data:
#             points[getTimeSlot(timestamp)] = float(value.split()[1])
# 
#         lastTimeSlot = getTimeSlot(endTime)
# 
#         values = []
#         # note that right boundary is exclusive
#         for timeSlot in range(0, lastTimeSlot):
#             values.append(points.get(timeSlot))
# 
#         lastPoint = points.get(lastTimeSlot)
#         if bootstrap or (allowRealTimeAlerting and lastPoint is not None):
#             values.append(lastPoint)
# 
#         valuesList.append(values)
#     return valuesList


from twisted.trial import unittest
from moira.graphite.datalib import unpackTimeSeries
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 0, bootstrap=False, allowRealTimeAlerting=False), [[], []])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 0, bootstrap=False, allowRealTimeAlerting=True), [[100.], []])

        # time == 5
        dataList[1].append(self.generateRedisDataPoint(5, 150.))
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 5, bootstrap=False, allowRealTimeAlerting=False), [[], []])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 5, bootstrap=False, allowRealTimeAlerting=True), [[100.], [150.]])

        # time == 9, 10
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 9, bootstrap=False, allowRealTimeAlerting=False), [[], []])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 9, bootstrap=False, allowRealTimeAlerting=True), [[100.], [150.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 10, bootstrap=False, allowRealTimeAlerting=False), [[100.], [150.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 10, bootstrap=False, allowRealTimeAlerting=True), [[100.], [150.]])
        dataList[0].append(self.generateRedisDataPoint(10, 200.))

        # time == 15
        dataList[1].append(self.generateRedisDataPoint(15, 250.))

        # time == 20
        dataList[0].append(self.generateRedisDataPoint(20, 300.))
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 20, bootstrap=False, allowRealTimeAlerting=False), [[100., 200.], [150.,250.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 20, bootstrap=False, allowRealTimeAlerting=True), [[100., 200., 300.], [150.,250.]])

        # time == 25
        dataList[1].append(self.generateRedisDataPoint(25, 350.))

        # time == 29, 30
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 29, bootstrap=False, allowRealTimeAlerting=False), [[100., 200.], [150., 250.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 29, bootstrap=False, allowRealTimeAlerting=True), [[100., 200., 300.], [150., 250., 350.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 30, bootstrap=False, allowRealTimeAlerting=False), [[100., 200., 300.], [150., 250., 350.]])

# Path: moira/graphite/datalib.py
# def unpackTimeSeries(dataList, retention, startTime, endTime, bootstrap, allowRealTimeAlerting):
# 
#     def getTimeSlot(timestamp):
#         return int((timestamp - startTime) / retention)
# 
#     valuesList = []
#     for data in dataList:
#         points = {}
#         for value, timestamp in data:
#             points[getTimeSlot(timestamp)] = float(value.split()[1])
# 
#         lastTimeSlot = getTimeSlot(endTime)
# 
#         values = []
#         # note that right boundary is exclusive
#         for timeSlot in range(0, lastTimeSlot):
#             values.append(points.get(timeSlot))
# 
#         lastPoint = points.get(lastTimeSlot)
#         if bootstrap or (allowRealTimeAlerting and lastPoint is not None):
#             values.append(lastPoint)
# 
#         valuesList.append(values)
#     return valuesList


# Path: tests/unit/test_fetchdata.py
from twisted.trial import unittest
from moira.graphite.datalib import unpackTimeSeries

        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 0, bootstrap=False, allowRealTimeAlerting=False), [[], []])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 0, bootstrap=False, allowRealTimeAlerting=True), [[100.], []])

        # time == 5
        dataList[1].append(self.generateRedisDataPoint(5, 150.))
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 5, bootstrap=False, allowRealTimeAlerting=False), [[], []])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 5, bootstrap=False, allowRealTimeAlerting=True), [[100.], [150.]])

        # time == 9, 10
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 9, bootstrap=False, allowRealTimeAlerting=False), [[], []])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 9, bootstrap=False, allowRealTimeAlerting=True), [[100.], [150.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 10, bootstrap=False, allowRealTimeAlerting=False), [[100.], [150.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 10, bootstrap=False, allowRealTimeAlerting=True), [[100.], [150.]])
        dataList[0].append(self.generateRedisDataPoint(10, 200.))

        # time == 15
        dataList[1].append(self.generateRedisDataPoint(15, 250.))

        # time == 20
        dataList[0].append(self.generateRedisDataPoint(20, 300.))
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 20, bootstrap=False, allowRealTimeAlerting=False), [[100., 200.], [150.,250.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 20, bootstrap=False, allowRealTimeAlerting=True), [[100., 200., 300.], [150.,250.]])

        # time == 25
        dataList[1].append(self.generateRedisDataPoint(25, 350.))

        # time == 29, 30
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 29, bootstrap=False, allowRealTimeAlerting=False), [[100., 200.], [150., 250.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 29, bootstrap=False, allowRealTimeAlerting=True), [[100., 200., 300.], [150., 250., 350.]])
        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 30, bootstrap=False, allowRealTimeAlerting=False), [[100., 200., 300.], [150., 250., 350.]])

        self.assertEqual(unpackTimeSeries(dataList, retention, startTime, 30, bootstrap=False, allowRealTimeAlerting=True), [[100., 200., 300.], [150., 250., 350.]])