summaryrefslogtreecommitdiff
path: root/jobstate.py
blob: 8ae05d0f5ac68b437db796bff76424b72f49ecd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python

from builtins import str
from builtins import object
import time
import logging
import datetime

class JobState(object):
	def __init__(self, name, friendlyname):
		self.name = name
		self.friendlyname = friendlyname
		self.CurrentStateSuccess = True
		self.FirstFailureTime = 0
		self.LastNotifyTime = 0
		self.NumFailures = 0

	def markFailedAndNotify(self):
		# Confusing: In this function 'self' represents the lastRunStatus
		# and we know the current run has failed
		if self.CurrentStateSuccess:
			self.CurrentStateSuccess = False
			self.FirstFailureTime = time.time()
			self.LastNotifyTime = self.FirstFailureTime
			self.NumFailures = 1
		else:
			self.LastNotifyTime = time.time()
			self.NumFailures += 1

	def markFailedNoNotify(self):
		# Confusing: In this function 'self' represents the lastRunStatus
		# and we know the current run has failed
		if self.CurrentStateSuccess:
			self.CurrentStateSuccess = False
			self.FirstFailureTime = time.time()
			self.LastNotifyTime = 0
			self.NumFailures = 1
		else:
			self.NumFailures += 1

	def markSuccessful(self):
		# Confusing: In this function 'self' represents the lastRunStatus
		# and we know the current run has succeeded
		if self.CurrentStateSuccess:
			pass
		else:
			self.CurrentStateSuccess = True
			self.FirstFailureTime = 0
			self.LastNotifyTime = 0
			self.NumFailures = 0

	def serialize(self):
		ret  = self.name + "|" 
		ret += "Succeeding" if self.CurrentStateSuccess else "Failing"
		ret += "|" + str(self.FirstFailureTime)
		ret += "|" + str(self.LastNotifyTime) + "|"
		ret += self.friendlyname.replace("|", "#") #Why yes, this is ugly!
		ret += "|" + str(self.NumFailures) + "\n"
		return ret

	@staticmethod
	def Parse(line):
		s = JobState("", "")

		line = line.strip()
		parts = line.split("|")

		s.name = parts[0]
		s.CurrentStateSuccess = True if parts[1] == "Succeeding" else False
		s.FirstFailureTime = float(parts[2])
		s.LastNotifyTime = float(parts[3])
		s.friendlyname = parts[4].replace("#", "|")

		if len(parts) > 5:
			s.NumFailures = int(parts[5])

		return s

	@staticmethod
	def Empty(name, friendlyname):
		s = JobState(name, friendlyname)
		return s