miranda-schedule-checker.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import collections
  2. import fileinput
  3. import string
  4. class Color:
  5. HEADER = '\033[95m'
  6. OKBLUE = '\033[94m'
  7. OKGREEN = '\033[92m'
  8. WARNING = '\033[93m'
  9. FAIL = '\033[91m'
  10. ENDC = '\033[0m'
  11. BOLD = '\033[1m'
  12. UNDERLINE = '\033[4m'
  13. COLORS = {
  14. 1: Color.OKBLUE + '%s' + Color.ENDC,
  15. 2: Color.OKGREEN + '%s' + Color.ENDC,
  16. 3: Color.WARNING + '%s' + Color.ENDC,
  17. 3: Color.FAIL + '%s' + Color.ENDC,
  18. 4: Color.FAIL + '%s' + Color.ENDC,
  19. 5: Color.FAIL + '%s' + Color.ENDC,
  20. 6: Color.FAIL + '%s' + Color.ENDC,
  21. 7: Color.FAIL + '%s' + Color.ENDC,
  22. 8: Color.FAIL + '%s' + Color.ENDC,
  23. 9: Color.FAIL + '%s' + Color.ENDC,
  24. 10: Color.FAIL + '%s' + Color.ENDC,
  25. }
  26. _HRS = {
  27. 'N': (21, 9),
  28. }
  29. _IGNORED = {
  30. 'R',
  31. }
  32. _LEAVE_CODES = {
  33. 'AL',
  34. 'A/L',
  35. 'SD',
  36. 'BH',
  37. }
  38. _COLUMNS = list(string.ascii_uppercase[2:]) + ['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH']
  39. _DRIVERS = {
  40. 'John Garbo PPIM/SN',
  41. 'Nonyelum Ifediora SCW',
  42. 'Katie Coyle SCW',
  43. 'Stanford Sokole SCW',
  44. 'Keith Duffy HCA',
  45. }
  46. totals_by_day = collections.defaultdict(int)
  47. totals_by_hours = {}
  48. drivers_by_hours = {}
  49. for line in fileinput.input():
  50. fs = line.split('\t')
  51. fs = [x.strip() for x in fs]
  52. if not any(fs):
  53. continue
  54. try:
  55. target = float(fs[1])
  56. except ValueError, e:
  57. print 'bad target hrs: %s' % fs
  58. continue
  59. name = fs[0].strip()
  60. driver = name in _DRIVERS
  61. total = 0
  62. prev_end = None
  63. errors = []
  64. for i, k in enumerate(fs[2:]):
  65. if not k:
  66. prev_end = None
  67. continue
  68. start = 0
  69. end = 0
  70. if k in _LEAVE_CODES:
  71. total += 8
  72. prev_end = None
  73. continue
  74. elif k in _IGNORED:
  75. prev_end = None
  76. continue
  77. elif k in _HRS:
  78. start, end = _HRS[k]
  79. elif k.startswith('T/'):
  80. hrs = float(k[2:])
  81. total += hrs
  82. continue
  83. elif '/' in k:
  84. start, end = k.split('/')
  85. start = int(start.strip())
  86. end = int(end.strip())
  87. else:
  88. errors.append('\tUnrecognized code: %s' % k)
  89. prev_end = None
  90. continue
  91. if end < start:
  92. end += 24
  93. total += end - start
  94. if end <= 24:
  95. hrs = [(i, j) for j in range(start, end)]
  96. else:
  97. hrs = [(i, j) for j in range(start, 24)] + [(i+1, j) for j in range(0, end-24)]
  98. for day, hr in hrs:
  99. totals_by_day[day] += 1
  100. if day not in totals_by_hours:
  101. totals_by_hours[day] = collections.defaultdict(int)
  102. drivers_by_hours[day] = collections.defaultdict(int)
  103. totals_by_hours[day][hr] += 1
  104. if driver:
  105. drivers_by_hours[day][hr] += 1
  106. if prev_end == 23 and start == 7:
  107. pass
  108. elif prev_end and (start + 24) < prev_end + 10:
  109. errors.append('\tWARNING: Possible bad start time for day %s' % _COLUMNS[i])
  110. prev_end = end
  111. delta = total-target
  112. delta_str = ('+' if delta > 0 else '') + str(delta)
  113. if -3 <= delta <= 4:
  114. print Color.OKGREEN + name + ': %s' % delta_str
  115. elif -6 <= delta <= 8:
  116. print Color.WARNING + name + ': %s' % delta_str
  117. else:
  118. print Color.FAIL + name + ': %s' % delta_str
  119. #print name
  120. # print '\tTarget: ', target
  121. # print '\tTotal: ', total
  122. # print '\tDelta: %s%s' % ('+' if delta > 0 else '', delta)
  123. print Color.ENDC
  124. if errors:
  125. print Color.FAIL
  126. for error in errors:
  127. print error
  128. print Color.ENDC
  129. print ''
  130. print ' Cover by hour Driver cover by hour Cover by day'
  131. for k, v in sorted(totals_by_day.items()):
  132. by_hr = ''.join([COLORS.get(totals_by_hours[k].get(hr), '%s') % totals_by_hours[k].get(hr, ' ') for hr in range(24)])
  133. drivers_by_hr = ''.join([COLORS.get(drivers_by_hours[k].get(hr), '%s') % drivers_by_hours[k].get(hr, ' ') for hr in range(24)])
  134. print ' '.join([
  135. _COLUMNS[k] + (' ' if len(_COLUMNS[k]) == 1 else ''),
  136. # '%02d' % v,
  137. by_hr,
  138. drivers_by_hr,
  139. '.' * (v/2),
  140. ])
  141. # print '%s%s %02d %s %s %s' % (_COLUMNS[k], v, by_hr, drivers_by_hr, '.' * (v/2))