Auto-generated patch by python-black
[integration/test.git] / tools / clustering / cluster-debugging / transaction-tracking / process.py
1 #!/usr/bin/env python
2
3 from datetime import datetime
4 import collections
5
6
7 class Transaction:
8     def __init__(self, txnId, startTime, operations):
9         self.txnId = txnId
10         self.operations = operations
11         self.startTime = datetime.strptime(startTime, "%Y-%m-%d,%H:%M:%S,%f")
12         self.reachedTime = None
13         self.completeTime = None
14
15     def setReachedTime(self, reachedTime):
16         self.reachedTime = datetime.strptime(reachedTime, "%Y-%m-%d,%H:%M:%S,%f")
17
18     def setCompleteTime(self, completeTime):
19         self.completeTime = datetime.strptime(completeTime, "%Y-%m-%d,%H:%M:%S,%f")
20
21     def totalTime(self):
22         return Transaction.diffInMicros(self.startTime, self.completeTime)
23
24     def transferTime(self):
25         return Transaction.diffInMicros(self.startTime, self.reachedTime)
26
27     @staticmethod
28     def diffInMicros(start, end):
29         if end is not None and start is not None:
30             delta = end - start
31             seconds = delta.seconds
32             microseconds = delta.microseconds
33             return (seconds * 1000000 + microseconds) / 1000
34         return -1
35
36     def __str__(self):
37         return (
38             "transactionId = "
39             + self.txnId
40             + ", "
41             + "operations = "
42             + unicode(self.operations)
43             + ", "
44             + "startTime = "
45             + unicode(self.startTime)
46             + ", "
47             + "reachedTime = "
48             + unicode(self.reachedTime)
49             + ", "
50             + "completeTime = "
51             + unicode(self.completeTime)
52             + ", "
53             + "transferTime = "
54             + unicode(self.transferTime())
55             + ", "
56             + "totalTime = "
57             + unicode(self.totalTime())
58         )
59
60     def csv(self):
61         return (
62             unicode(self.startTime)
63             + ","
64             + self.txnId
65             + ","
66             + unicode(self.operations)
67             + ","
68             + unicode(self.transferTime())
69             + ","
70             + unicode(self.totalTime())
71         )
72
73     @staticmethod
74     def csv_header():
75         return "Start Time,Transaction Id,Operations,Transfer Time," "Complete Time"
76
77
78 def processFiles():
79     txns = collections.OrderedDict()
80     txnBegin = open("txnbegin.txt", "r")
81     for line in txnBegin:
82         arr = line.split(",")
83         txns[arr[3]] = Transaction(
84             arr[3], arr[0] + "," + arr[1] + "," + arr[2], int(arr[4])
85         )
86
87     txnReached = open("txnreached.txt", "r")
88     for line in txnReached:
89         arr = line.split(",")
90         txnId = arr[3].strip()
91         if txnId in txns:
92             txn = txns[txnId]
93             txn.setReachedTime(arr[0] + "," + arr[1] + "," + arr[2])
94
95     txnComplete = open("txnend.txt", "r")
96     for line in txnComplete:
97         arr = line.split(",")
98         txnId = arr[3].strip()
99         if txnId in txns:
100             txn = txns[txnId]
101             txn.setCompleteTime(arr[0] + "," + arr[1] + "," + arr[2])
102
103     return txns
104
105
106 def filterTransactionsByTimeToComplete(timeToComplete):
107     txns = processFiles()
108     totalTime = 0
109     for txn in txns:
110         if txns[txn].totalTime() > timeToComplete:
111             print(txns[txn])
112             totalTime += txns[txn].totalTime()
113
114     print("Total time for these transactions = ", unicode(totalTime))
115
116
117 def csv():
118     txns = processFiles()
119     print(Transaction.csv_header())
120     for txn in txns:
121         print(txns[txn].csv())