Ejemplo n.º 1
0
def event_to_minutes(event):
    minutes = IntervalSet.between(0, 0)
    d = {"MO": 0, "TU": 1, "WE": 2, "TH": 3, "FR": 4}
    start, stop = event["DTSTART"].dt, event["DTEND"].dt
    for day in event["RRULE"]["BYDAY"]:
        b = d[day] * 1440 + start.hour * 60 + start.minute
        e = d[day] * 1440 + stop.hour * 60 + stop.minute
        minutes = minutes + IntervalSet.between(b, e)
    return minutes
def calc_break_time(bts_level,t0,t1):
    '''
    根据告警发生时间 t0 和 告警结束时间 t1
    计算出在三个考核时段内告警的时长,单位:minutes
    '''
#    t0 = df_block.loc[0,'告警发生时间']
#    t1 = df_block.loc[0,'告警清除时间']
    date_list = list(rrule(DAILY, dtstart=parse(t0), until=parse(t1)))
    day_list = [x.strftime('%Y-%m-%d') for x in date_list]
    # 创建断站的时间段
    start_time = datetime.strptime(t0, '%Y-%m-%d %H:%M:%S')
    end_time = datetime.strptime(t1, '%Y-%m-%d %H:%M:%S')
    break_interval = IntervalSet.between(start_time, end_time, closed=True)
    break_time_6to8 = 0
    break_time_8to22 = 0
    break_time_22to24 = 0
    for day in day_list:
        # 创建三个考核时间段,分别是6-8点,8-22点,22-24点,
        interval_6to8 = IntervalSet.between(
                datetime.strptime(
                    day + ' ' + '06:00:00',
                    '%Y-%m-%d %H:%M:%S'),
                datetime.strptime(
                    day + ' ' + '08:00:00',
                    '%Y-%m-%d %H:%M:%S'), closed=True)
        interval_8to22 = IntervalSet.between(
            datetime.strptime(
                day + ' ' + '08:00:00',
                '%Y-%m-%d %H:%M:%S'),
            datetime.strptime(
                day + ' ' + '22:00:00',
                '%Y-%m-%d %H:%M:%S'), closed=True)
        interval_22to24 = IntervalSet.between(
            datetime.strptime(
                day + ' ' + '22:00:00',
                '%Y-%m-%d %H:%M:%S'),
            datetime.strptime(
                day + ' ' + '23:59:59',
                '%Y-%m-%d %H:%M:%S'), closed=True)
        break_interval_6to8 = break_interval & interval_6to8
        break_interval_8to22 = break_interval & interval_8to22
        break_interval_22to24 = break_interval & interval_22to24

        break_time_6to8 += calc_time(break_interval_6to8)
        break_time_8to22 += calc_time(break_interval_8to22)
        break_time_22to24 += calc_time(break_interval_22to24)
    if bts_level == 'A' or  bts_level == 'B':
        return (break_time_6to8,break_time_8to22 * 1.2,break_time_22to24)
    else:
        return (break_time_6to8,break_time_8to22,break_time_22to24)
Ejemplo n.º 3
0
def get_working_time(onduty_time, offduty_time, work_date):
    """
    提供上下班打卡时间,计算工作时长
    onduty_time 上班打卡时间, 时间戳毫秒
    offduty_time 下班打卡时间, 时间戳毫秒
    work_date 工作日 '2018-10-10'
    @author: lanxiong
    @time:2018/11/15
    print get_working_time(1538355625000, 1538388501000, '2018-10-01')
    """
    from interval import IntervalSet
    #TODO;从参数表获取
    onduty_time = onduty_time / 1000
    offduty_time = offduty_time / 1000
    work_date = convert_timestamp_to_date(work_date, date_format="%Y-%m-%d")
    am_onduty_time = "00:00"
    am_offduty_time = "12:00"
    pm_onduty_time = "13:30"
    pm_offduty_time = "23:59"
    am_onduty_time_stamp = convert_date_to_timestamp(work_date + ' ' + am_onduty_time, date_format="%Y-%m-%d %H:%M")
    am_offduty_time_stamp = convert_date_to_timestamp(work_date + ' ' + am_offduty_time, date_format="%Y-%m-%d %H:%M")
    pm_onduty_time_stamp = convert_date_to_timestamp(work_date + ' ' + pm_onduty_time, date_format="%Y-%m-%d %H:%M")
    pm_offduty_time_stamp = convert_date_to_timestamp(work_date + ' ' + pm_offduty_time, date_format="%Y-%m-%d %H:%M")

    am_work = IntervalSet.between(am_onduty_time_stamp, am_offduty_time_stamp)
    lunch_time = IntervalSet.between(am_offduty_time_stamp, pm_onduty_time_stamp)
    pm_work = IntervalSet.between(pm_onduty_time_stamp, pm_offduty_time_stamp)

    if offduty_time in am_work:
        return offduty_time - onduty_time
    elif offduty_time in lunch_time:
        return am_offduty_time_stamp - onduty_time
    elif offduty_time in pm_work:
        if onduty_time in am_work:
            return offduty_time - onduty_time - 1.5*60*60
        elif onduty_time in lunch_time:
            return offduty_time - pm_onduty_time_stamp
        elif onduty_time in pm_work:
            return offduty_time - onduty_time
    else:
        # 干到第二天了
        return offduty_time - onduty_time
Ejemplo n.º 4
0
volume4 = Interval.between("Spade", "Zygote")

encyclopedia = IntervalSet([volume1, volume2, volume3, volume4])

mySet = IntervalSet([volume1, volume3, volume4])

"Meteor" in encyclopedia
"Goose" in encyclopedia

"Goose" in mySet
volume2 in (encyclopedia ^ mySet)

print [(_.lower_bound, _.upper_bound) for _ in IntervalSet(encyclopedia)]


OfficeHours = IntervalSet.between("08:00", "17:00")
myLunch = IntervalSet.between("11:30", "12:30")
myHours = IntervalSet.between("08:30", "19:30") - myLunch
myHours.issubset(OfficeHours)

"12:00" in myHours

"15:30" in myHours

inOffice = OfficeHours & myHours

overtime = myHours - OfficeHours



Ejemplo n.º 5
0
if __name__ == "__main__":
    urls = []

    while True:
        url = raw_input("Enter a Ninja Courses URL here (enter '.' to stop): ")
        if url == '.':
            break
        url = coerce_to_ics(url)
        if url:
            urls.append(url)
        else:
            print "ERROR: Could not extract 's' and 't' from the provided URL"
            print "Make sure your URL is in the form http://ninjacourses.com/schedule/view/#######/?t=######## or http://ninjacourses.com/schedule/export/schedule.ics?s=#######&t=########"

    calendars = [Calendar.from_ical(requests.get(url).text) for url in urls]
    events = sum((calendar.walk() for calendar in calendars), [])
    events = [event for event in events if isinstance(event, Event) and "RRULE" in event]
    events = [event for event in events if event["RRULE"]["FREQ"] == ["WEEKLY"]]

    minutes = IntervalSet.between(0, 7200)
    for event in events[::-1]:
        minutes = minutes - event_to_minutes(event)

    dow = "Monday Tuesday Wednesday Thursday Friday".split()
    for interval in minutes:
        b, e = interval.lower_bound, interval.upper_bound
        b = datetime(2013, 10, 7, 0, 0) + timedelta(minutes=b)
        e = datetime(2013, 10, 7, 0, 0) + timedelta(minutes=e)
        print "You are all free from %s to %s!" % \
                (b.strftime("%a %I:%M %p"), e.strftime("%a %I:%M %p"))
Ejemplo n.º 6
0
def call_on(call_user, subject):
    convert_subject = tool.ip_convert(subject)
    call_subject = convert_subject.split(':')[2].strip().replace('.', '点')
    url = 'http://apiserver:8765/phoneWarn/callUp/v1/gen'
    payload = {
        'tos': call_user['1']["phone"],
        'name': call_user['1']["name"],
        'content': call_subject
    }
    result = requests.post(url, data=payload)
    alert_data = {
        "alert_data": subject.split(':')[2].strip(),
        "alert_name": call_user[num]['name']
    }
    log_info = {}
    log_info["info"] = alert_data
    log_info["result"] = json.loads(result.text.strip())
    logger.info(json.dumps(log_info))


if __name__ == '__main__':
    subject = sys.argv[1]
    start_time = IntervalSet.between("21:00", "23:59")
    end_time = IntervalSet.between("00:00", "10:30")
    now_time = datetime.now().strftime('%H:%M')
    alert_time = datetime.now().strftime("%Y%m%d")
    if now_time in start_time or now_time in end_time:
        call_on(call_user, subject)
    else:
        logger.info('非电话报警时间,报警取消..')