Ejemplo n.º 1
0
 def test_dateToDayMinutes(self):
     self.assertEqual(
         util.dateToDayMinutes(util.stringToTime("01.02.2017 02:00:00")),
         120)
     self.assertEqual(
         util.dateToDayMinutes(util.stringToTime("01.02.2017 03:00:00")),
         180)
     self.assertEqual(
         util.dateToDayMinutes(util.stringToTime("26.03.2017 04:00:00")),
         240)
Ejemplo n.º 2
0
 def test_stringToTime(self):
     self.assertEqual(
         util.stringToTime("01.02.2017 02:00:00"),
         UtilTest.localizeTime(datetime.datetime(2017, 2, 1, 2, 00, 00)))
     self.assertEqual(
         util.stringToTime("26.03.2017 02:00:00"),
         UtilTest.localizeTime(datetime.datetime(2017, 3, 26, 2, 00, 00)))
     self.assertEqual(
         util.stringToTime("26.03.2017 03:00:00"),
         UtilTest.localizeTime(datetime.datetime(2017, 3, 26, 3, 00, 00)))
Ejemplo n.º 3
0
 def test_dateToTimeString(self):
     self.assertEqual(
         util.dateToTimeString(util.stringToTime("01.02.2017 02:00:00")),
         "2017-02-01T02:00:00+01:00")
     self.assertEqual(
         util.dateToTimeString(util.stringToTime("26.03.2017 02:00:00")),
         "2017-03-26T02:00:00+01:00")
     self.assertEqual(
         util.dateToTimeString(util.stringToTime("26.03.2017 03:00:00")),
         "2017-03-26T03:00:00+02:00")
Ejemplo n.º 4
0
 def test_getNextDay(self):
     testCases = [
         ("23.03.2017 00:00:00", "24.03.2017 00:00:00"),
         ("25.03.2017 00:00:00", "26.03.2017 00:00:00"),
         ("26.03.2017 00:00:00", "27.03.2017 00:00:00"),
     ]
     for testCase in testCases:
         day = util.stringToTime(testCase[0])
         nextDay = util.stringToTime(testCase[1])
         self.assertEqual(util.getNextDay(day), nextDay)
Ejemplo n.º 5
0
 def test_getLatestCompleteTickTime(self):
     self.flow["options"]["granularity"] = 30
     lobAnalyzer = FlowAnalyzer(self.flow)
     testCases = [("02.02.2017 15:35:03", "02.02.2017 15:00:00"),
                  ("02.02.2017 18:03:00", "02.02.2017 17:30:00"),
                  ("02.02.2017 01:00:00", "02.02.2017 00:30:00"),
                  ("02.02.2017 00:15:03", "01.02.2017 23:30:00"),
                  ("02.02.2017 00:00:00", "01.02.2017 23:30:00"),
                  ("01.02.2017 23:59:59", "01.02.2017 23:00:00")]
     for testCase in testCases:
         latestCompleteTickTime = lobAnalyzer._getLatestCompleteTicTime(
             util.stringToTime(testCase[0]))
         self.assertEqual(latestCompleteTickTime,
                          util.stringToTime(testCase[1]))
Ejemplo n.º 6
0
 def test_getNextTic(self):
     testCases = [
         ("26.03.2017 02:00:00", "26.03.2017 04:00:00", 60),
         ("26.03.2017 03:00:00", "26.03.2017 04:00:00", 60),
         ("26.03.2017 01:00:00", "26.03.2017 03:00:00", 180),
         ("26.03.2017 01:55:00", "26.03.2017 03:00:00", 15),
         ("26.03.2017 08:10:00", "26.03.2017 09:00:00", 60),
     ]
     for testCase in testCases:
         time = util.stringToTime(testCase[0])
         expected = util.stringToTime(testCase[1])
         granularity = testCase[2]
         self.assertEqual(util.getNextTic(time, granularity), expected,
                          "time: " + str(time))
Ejemplo n.º 7
0
 def test_getLatestCompleteTickTimeOnDST(self):
     testCases = [("26.03.2017 03:00:00", "26.03.2017 01:00:00", 60),
                  ("29.10.2017 04:00:03", "29.10.2017 02:00:00", 120),
                  ("29.10.2017 03:00:03", "29.10.2017 00:00:00", 120),
                  ("26.03.2017 03:00:00", "26.03.2017 00:00:00", 120),
                  ("27.03.2017 05:00:03", "26.03.2017 00:00:00", 1440),
                  ("26.03.2017 05:00:03", "26.03.2017 00:00:00", 240),
                  ("26.03.2017 05:00:03", "26.03.2017 03:00:00", 120),
                  ("26.03.2017 03:00:03", "26.03.2017 02:55:00", 5)]
     for testCase in testCases:
         self.flow["options"]["granularity"] = testCase[2]
         lobAnalyzer = FlowAnalyzer(self.flow)
         latestClosedIntervalTime = lobAnalyzer._getLatestCompleteTicTime(
             util.stringToTime(testCase[0]))
         expected = util.stringToTime(testCase[1])
         self.assertEqual(latestClosedIntervalTime, expected)
Ejemplo n.º 8
0
 def addTickToData(self, tickTime, value, average, expected):
     tick = util.stringToTime(tickTime)
     self.data[tick] = {
         '_id': tick,
         'value': value,
         'dayAverage': average,
         'expected': expected
     }
Ejemplo n.º 9
0
 def test_minuteDictToDateDict(self):
     baseDate = util.stringToTime("01.02.2017 00:00:00")
     tickDelta = datetime.timedelta(minutes=30)
     dayMinutes = {30: 10, 60: 20, 90: 30}
     metric = "metric"
     dateDict = util.minuteDictToDateDict(baseDate, dayMinutes, metric)
     self.assertEqual(dateDict[baseDate + tickDelta][metric], 10)
     self.assertEqual(dateDict[baseDate + tickDelta * 2][metric], 20)
     self.assertEqual(dateDict[baseDate + tickDelta * 3][metric], 30)
Ejemplo n.º 10
0
 def test_resetDateTimeMidnight(self):
     self.assertEqual(
         util.resetDateTimeMidnight(
             util.stringToTime("01.02.2017 02:00:00")),
         util.stringToTime("01.02.2017 00:00:00"))
     self.assertEqual(
         util.resetDateTimeMidnight(
             util.stringToTime("02.02.2017 03:00:00")),
         util.stringToTime("02.02.2017 00:00:00"))
     self.assertEqual(
         util.resetDateTimeMidnight(
             util.stringToTime("26.03.2017 04:00:00")),
         util.stringToTime("26.03.2017 00:00:00"))
Ejemplo n.º 11
0
 def saveMediationData(self, row):
     """
 :param data:
 dict{"country":"CZ",
 "lobName":"ACI",
 "type":"inputs",
 "flowName":"GSM",
 "dataSize":497464,
 "time":"01.01.2017 00:00:24"}
 :return:
 """
     time = util.stringToTime(row["time"]).replace(second=0)
     updatePath = "data." + row["country"] + "." + row[
         "lobName"] + "." + row["type"] + "."
     try:
         dataSize = int(row["dataSize"])
     except ValueError:
         print("ValueError: " + row)
         dataSize = 0
     updateObj = {
         "$inc": {
             updatePath + row["flowName"]: dataSize,
             updatePath + "sum": dataSize,
             updatePath + "updatesCnt": 1
         }
     }
     try:
         mongo.traffic().update({'_id': time}, updateObj, upsert=True)
         logging.info("Inserted row for " + row["lobName"] + " " +
                      row["flowName"] + " at " + str(time) + " of size " +
                      str(dataSize))
         return True
     except Exception as e:
         logging.error("Error while persisting data " + str(row) +
                       " exception: " + str(e))
         return False
    def setPrecomputedData(self, precomputedData, valueKey):
        for tic in precomputedData:
            id = tic["_id"]
            if valueKey in tic:
                self.tickDict[id] = {
                    "_id": id,
                    "value": tic[valueKey],
                    "expected": tic["expected"],
                    "dayAverage": tic["dayAverage"]
                }
        self.flowAnalyzer.setPrecomputedData(self.tickDict)

    def execute(self):
        d = util.getNextTic(self.fromDate, self.granularity)
        statusList = []
        while d < util.getNextTic(self.toDate, self.granularity):
            self.flowAnalyzer.run(d)
            status = self.flowAnalyzer.status
            statusList.append({
                "_id": self.flowAnalyzer.ticTime,
                "status": status
            })
            d = util.getNextTic(d, self.granularity)
        return statusList


if __name__ == "__main__":
    OutageDateRangeQuery(util.stringToTime("17.01.2017 15:00:00"),
                         util.stringToTime("20.01.2017 15:00:00"), {}, 0)
Ejemplo n.º 13
0
def cumulativeWeightedRunningMean(data, window_size):
    output = []
    for i in range(0, len(data)):
        start = max(0, i - window_size)
        windowData = data[start:i + 1]
        weights = [1 + x * x for x in range(0, len(windowData))]
        s = 0
        for x, y in zip(windowData, weights):
            s += x * y
        average = s / sum(weights)
        output.append(average)
    return output


fromDate = util.stringToTime("02.03.2017 00:00:00")
toDate = util.stringToTime("03.03.2017 00:00:00")
granularity = 15
q = DateRangeGroupQuery(
    fromDate, toDate,
    [MediationConfig.getLobWithCountry("CZ", "GSM")["flows"]["MSSBRN1A"]],
    granularity)
# q = DateRangeGroupQuery(fromDate, toDate, [MediationConfig.getLobWithCountry("CZ","ACI")["flows"]["GSM"]], granularity)
lob1Data = util.dateDataListToList(q.execute(), "MSSBRN1A")
lob1Data.append(0)
lob1Data.append(0)
lob1Data.append(0)
lob1Data.append(0)
dates = list(
    map(
        lambda x: util.dateToTimeString(x).split("+")[0].split("T")[1].split(
Ejemplo n.º 14
0
    'type': 'inputs',
    'dataPath': 'CZ.GSM.inputs.MSSCEB1B',
    'name': 'MSSCEB1B',
    'gName': 'CZ_GSM_MSSCEB1B',
    'options': {
        'difference': 'day',
        'hardAlarmLevel': 0.5,
        'minimalExpectation': 1,
        'enabled': True,
        'lazyDayDifference': 0.7,
        'granularity': 60,
        'softAlarmLevel': 0.75
    },
    'country': 'CZ'
}
CURRENT_TIME = util.stringToTime("17.01.2017 15:03:00")
"""
Class for testing flow analyzer and its results
"""


class FlowAnalyzerTest(unittest.TestCase):
    def setUp(self):
        self.flowAnalyzer = FlowAnalyzer(FLOW)
        self.data = {}
        self.flowAnalyzer.setPrecomputedData(self.data)

    def test_easyOK(self):
        self.addTickToData("17.01.2017 14:00:00", 1000, 50, 100)
        self.assertStatus(OK)
Ejemplo n.º 15
0
import datetime

import pytz
from dateutil import tz

from common import util
prg = pytz.timezone('Europe/Prague')
dateutilPrg = tz.gettz('Europe/Prague')
utc = pytz.timezone('UTC')
cet = pytz.timezone('CET')
# t = util.stringToTime("01.01.2017 01:00:00")
# print(t)
day = util.resetDateTimeMidnight(util.stringToTime("26.03.2017 07:00:00"))
day2 = util.resetDateTimeMidnight(
    util.stringToTime("26.03.2017 00:00:00") + datetime.timedelta(days=1))
print(day.astimezone(utc))
print(
    util.resetDateTimeMidnight(day +
                               datetime.timedelta(days=1)).astimezone(utc))
x = (util.stringToTime("27.03.2017 00:00:00") +
     datetime.timedelta(days=1)).astimezone(utc)

# print(prg.localize(datetime.datetime(2017, 3, 26, 1, 0, 0, 0)))
# print(prg.localize(datetime.datetime(2017, 3, 26, 2, 0, 0, 0)))
# print(prg.localize(datetime.datetime(2017, 3, 26, 3, 0, 0, 0)))
# print(prg.localize(datetime.datetime(2017, 3, 26, 2, 0, 0, 0)) == prg.localize(datetime.datetime(2017, 3, 26, 3, 0, 0, 0)))
# print("--")
# print(datetime.datetime(2017, 3, 26, 0, 0, 0, 0,cet).astimezone(prg))
# print(datetime.datetime(2017, 3, 26, 1, 0, 0, 0,cet).astimezone(prg))
# print(datetime.datetime(2017, 3, 26, 2, 0, 0, 0,cet).astimezone(prg))
# print(datetime.datetime(2017, 3, 26, 3, 0, 0, 0,cet).astimezone(prg))
Ejemplo n.º 16
0
      latestClosedIntervalTime = time - datetime.timedelta(minutes=minutesOverInterval + granularity)
      latestClosedIntervalTime = latestClosedIntervalTime.replace(second=0, microsecond=0)
      return util.getTicTime(latestClosedIntervalTime.astimezone(AppConfig.getTimezone()), granularity)
    else:
      naiveTime = time.replace(tzinfo=None)
      latestClosedIntervalNaiveTime = naiveTime - datetime.timedelta(minutes=minutesOverInterval + granularity)
      latestClosedIntervalNaiveTime = latestClosedIntervalNaiveTime.replace(second=0, microsecond=0)
      localized = AppConfig.getTimezone().localize(latestClosedIntervalNaiveTime).astimezone(AppConfig.getTimezone())
      return util.getTicTime(localized, granularity)

  def getResult(self):
    """
    returns result. It's up to caller to save it to db!
    :return:
    """
    return self.status, self.difference

  def setPrecomputedData(self, precomputedData):
    self.precomputedData = precomputedData
    self.outageDetector.setPrecomputedData(self.precomputedData)


if __name__ == "__main__":
  logging.basicConfig(format='%(levelname)s [%(module)s]: %(message)s', level=logging.DEBUG)
  gsm = MediationConfig.getLobWithCountry("CZ", "TIT")
  analyzer = FlowAnalyzer(gsm["flows"]["CLH"])
  analyzer.run(util.stringToTime("17.01.2017 15:00:00"))
  isOutage, traffic = analyzer.getResult()
  print(isOutage)
  print(traffic)