aboutsummaryrefslogtreecommitdiffstats
path: root/compute_json.py
blob: 2196ebdc77f034fc807afa2ff6fa470f88f7d58e (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/python

import json
from datetime import datetime, timedelta
import numpy as np
import scipy as sp
import scipy.stats
import sys
import os.path
from scipy import stats
from math import sqrt


cols = [
		{
			"type": "string",
			"id": "commits",
			"label": "Commit name"
		},
		{
			"type": "datetime",
			"id": "average",
			"label": "Average time"
		},
		{
			"type": "datetime",
			"id": "i0",
			"role": "interval",
			"label": "Interval1"
		},
		{
			"type": "datetime",
			"id": "i1",
			"role": "interval",
			"label": "Interval2"
	},
{
			"type": "datetime",
			"id": "i2",
			"role": "interval",
			"label": "Point1"
		},
		{
			"type": "datetime",
			"id": "i2",
			"role": "interval",
			"label": "Point2"
		},
		{
			"type": "datetime",
			"id": "i2",
			"role": "interval",
			"label": "Point3"
		},
		{
			"role": "interval",
			"type": "datetime",
			"id": "i2",
			"label": "Point4"
		},
		{
			"role": "interval",
			"type": "datetime",
			"id": "i2",
			"label": "Point5"
		},
		{
			"role": "interval",
			"type": "datetime",
			"id": "i2",
			"label": "Point6"
		},
		{
			"role": "interval",
			"type": "datetime",
			"id": "i2",
			"label": "Point7"
		},
		{
			"role": "interval",
			"type": "datetime",
			"id": "i2",
			"label": "Point8"
		},
		{
			"role": "interval",
			"type": "datetime",
			"id": "i2",
			"label": "Point9"
		}
	]

#
#Usage
#

if len(sys.argv)!= 3:
	print "./compute_json.py accepts only one argument"
	print "Usage: ./compute_json.py <arg> <input_file>"
	exit(1)
elif type(int(sys.argv[1])) is not int:
	print " Usage: ./copmute_json.py <arg> <input_file>"
	print " where [arg] is int"
	exit (1)
elif not os.path.isfile(sys.argv[2]):
	print "USage: ./compute_json.py <arg> <input_file>"
	exit (1)
	
	
option = int(sys.argv[1])
input = sys.argv[2]

#function check if line creates new column or adds to existing column

def check_col(rows, items):
	commit = items[2]
#	values = items[8].split(":")
# the items column is passed as a python script argument sys.argv[1]
	timed   = items[int(option)].split(".")
	val = timed[0].split(":")
	if len(val) == 2:
		val.insert(0,"0")

	values = val
#	print "Values", values

	build_time = timedelta(hours=int(values[0]), minutes=int(values[1]), seconds=int(values[2]))
	for row in rows:
		current_commit = row["c"][0]["v"]
		
		if current_commit == commit:
			row["sum"] += build_time
			row["runs"] += 1
			row["build_times"].append(build_time.total_seconds())
			date_obj = datetime.now()
			date_obj = date_obj.replace(hour=int(values[0]), minute=int(values[1]), second=int(values[2]))
			fmt = "Date(%Y,%m,%d,%H,%M,%S,0)"
#printing point values to the HTML
			point_format = timedelta(hours=int(val[0]), minutes=int(values[1]), seconds=int(values[2]))

#			row["c"].append({"f": "Point{}".format(row["runs"]), "v": date_obj.strftime(fmt)}) 
			row["c"].append({"f": "P" + str(row["runs"]) + ": " + str(point_format), "v": date_obj.strftime(fmt)}) 
			return row 
	row = {"c": [{"v": commit}], "sum": timedelta(), "runs": 0, "build_times": []}
	rows.append(row)
	return check_col(rows, items)


rows = []

#with open ("input.csv", "r") as f:
#with open ("input.csv", "r") as f:

with open(input, "r") as f:
	for line in f:
		items = line.split(",")
		commit = items[1]
		check_col(rows, items)

for row in rows:
#compute confidence intervals based on standard deviation
## check if enugh runs are available to compute the average.

# TODO - this fails when there are diffrent git describe than git commits. the checking of no of commits is larger than 3 is done by clean-csv.py
	if row["runs"] < 3:
		print "current commit/git describe: " + row["c"][0]["v"]
		print "Failed with no of runs under 3:  " +  str(row["runs"])
		exit(1)

	avg_td = row["sum"] // row["runs"]
	avg = avg_td.total_seconds()

	variance = map(lambda x: (x - avg) ** 2, row["build_times"])
	std_dev = sqrt(sum(variance) / row["runs"])

	se = stats.sem(row["build_times"])
	h = se * stats.t._ppf((1 + 0.95) / 2., row["runs"] - 1)
#	print(row["build_times"] + [std_dev, avg, avg-h, avg+h])

	high_int = avg + h
	# print high_int
	low_int = avg -h
	# print low_int
#make low_int = 0 if negative
	if low_int < 0: 
		low_int = 0

#	avg, low_int, high_int = mean_confidence_interval(row)
#	low_int, high_int = stats.norm.interval(0.05, loc=avg, scale=std_dev)
#	print(row["build_times"] + [std_dev, avg, low_int, high_int])
#second_attempt std dev


#	mean_confidence_interval(row)

#write average time to JSON	
	hours, remainder = divmod(int(avg), 3600)
	minutes, seconds = divmod(remainder, 60)
	date_obj = datetime.now()
	date_obj = date_obj.replace(hour=hours, minute=minutes, second=seconds)
	fmt = "Date(%Y,%m,%d,%H,%M,%S,0)"
	fmt_print = "%H:%M:%S"
	row["c"].insert(1, {"f": date_obj.strftime(fmt_print), "v": date_obj.strftime(fmt)})

#compute and write top an bottom intervals based on the confidence intervals
	# print
	# print line
	# print high_int
	# print avg
	hours, remainder = divmod(int(high_int), 3600)
	minutes, seconds = divmod(remainder, 60)
	date_obj = datetime.now()
	date_obj = date_obj.replace(hour=hours, minute=minutes, second=seconds)	
	row["c"].insert(2, {"f": "C1: " + date_obj.strftime(fmt_print), "v": date_obj.strftime(fmt)})
	
	hours, remainder = divmod(int(low_int), 3600)
	minutes, seconds = divmod(remainder, 60)
	date_obj = datetime.now()
	date_obj = date_obj.replace(hour=hours, minute=minutes, second=seconds)	
	row["c"].insert(3, {"f": "C2: " + date_obj.strftime(fmt_print), "v": date_obj.strftime(fmt)})
	
	del row["sum"]
	del row["runs"]
	del row["build_times"]
#	break
print(json.dumps({"rows": rows, "cols": cols}))