-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·266 lines (234 loc) · 9.34 KB
/
index.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python
# -*- coding: utf8 -*-
import cgi
import cgitb; cgitb.enable()
import views
import DBtransactions
form = cgi.FieldStorage()
def printjquery(graph, history):
graphJson = DBtransactions.graphToJson(graph)
jq = """<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="/becher/static/js/cytoscape.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-qtip/2.2.5/cytoscape-qtip.js"></script>
<script>
$(function(){
"""
jq = jq + graphJson+graphStyle(history)
return jq
print "Content-Type: text/html"
print """
<html>
<head>
<meta charset="utf-8">
<title>DB[transactions]</title>
<link rel="stylesheet" type="text/css" href="/becher/static/css/bootstrapcosmo.min.css" />
<script src="/becher/static/js/jquery-1.11.3.min.js"></script>
<script src="/becher/static/js/bootstrap.min.js"></script>
</head>"""
def html(history):
return """<body>
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-6">
"""+views.getHeading()+"""</div>
<div class="col-md-6">
<p class="text-right"><button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#helpModal">
<span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span> Hilfe</button>
</p>
</div>
</div>
<br/>
<div class="panel panel-default">
<div class="panel-body">
<span class="label label-success">DB-Fragen? DB fragen:</span> <a href="mailto:david.becher@mytum.de">david.becher@mytum.de</a>
</div>
</div>
<br/>
<form class="form" action="index.py" method="POST">
<div class="form-group">
<h4>Historie eingeben</h4>
<input type="text" class="form-control" name="history" value="
""" + history+ """
"></input>
</div>
<div class="form-group">
<a href="index.py" class="btn btn-default">Neue Historie generieren</a>
</div>
"""
htmlend="""
<!-- Modal: help -->
<div id="helpModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Hilfe</h3>
</div>
<div class="modal-body">
<p>
<h4>Historie eingeben</h4>
<p>Historien in folgender Form eingeben: <code>w1[x],r2[y],r2[x],c1,a2</code></p>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
</body>
</html>
"""
def graphStyle(history):
edgeTooltips = views.conflictOperationsTooltip(history)
return """
var graph = cytoscape({
container: document.getElementById('graph'),
style: cytoscape.stylesheet()
.selector('node')
.css({
'background-color': '#2780E3',
'color': '#ffffff',
'text-outline-width': 2,
'text-outline-color': '#2780E3',
'shape': 'rectangle',
'width': 'mapData(0, 0, 30, 30, 0)',
'height': 'mapData(0, 0, 20, 20, 50)',
'content': 'data(id)',
'text-valign': 'center',
'text-halign': 'center'
})
.selector('edge')
.css({
'width': 'mapData(0, 0, 10, 5, 9)',
'line-color': 'data(color)',
'target-arrow-color': 'data(color)',
'target-arrow-shape': 'triangle',
'opacity': 0.5
}),
elements: elesJson,
layout: {
name: 'circle',
directed: true,
padding: 10
},
minZoom: 0.5,
maxZoom: 2,
autoungrabify: false,
userPanningEnabled: false,
zoomingEnabled: true,
userZoomingEnabled: false,
}); """ + edgeTooltips + """
});
</script>
"""
def printResults(string, answers={}):
history = DBtransactions.parseInput(string)
if not isinstance(history, basestring):
historyString = views.historyToString(history)
historyTable = views.historyToTable(history)
result = DBtransactions.computeEverything(history)
conflictOperations = views.conflictOperationsToString(result['conflictOperations'])
readingTAs = views.readingTAsToString(result['readingTAs'])
committedTransactions = views.transactionListToString(result['committedTAs'])
abortedTransactions = views.transactionListToString(result['abortedTAs'])
graph = result['graph']
isSR = result['SR']
operationsNotRC = result['operationsNotRC']
isRC = result['RC']
operationsNotACA = result['operationsNotACA']
isACA = result['ACA']
operationsNotST = result['operationsNotST']
isST = result['ST']
resultString = historyTable+views.wrapInPanel("Konfliktoperationen", conflictOperations,3)+views.wrapInPanel("Lesende Transaktionen", readingTAs, 3)+views.wrapInPanel("Committete Transaktionen", committedTransactions, 3)+views.wrapInPanel("Abortete Transaktionen", abortedTransactions, 3)+views.htmlGraph()+"<div>"+views.wrapInPanel("Eigenschaften von H := "+historyString, views.booleanPropertyToString("serialisierbar", isSR)+views.propertyToString("rücksetzbar", history, operationsNotRC)+views.propertyToString("vermeidet kaskadierendes Rücksetzen", history, operationsNotACA)+views.propertyToString("strikt", history, operationsNotST), 12)+"</div>"+printjquery(graph, history)
if answers:
if answers['SR'] == isSR and answers['RC'] == isRC and answers['ACA'] == isACA and answers['ST'] == isST:
returnString = printCheckboxes(answers) + views.getMessageBox("Richtig!","thumbs-up") + resultString
else:
returnString = printCheckboxes(answers) + views.getMessageBox("Leider falsch!","thumbs-down")
else:
returnString = printCheckboxes(result) + resultString
else:
returnString = printCheckboxes(answers) + history
return returnString
def getUserAnswers(form):
formKeys = form.keys();
input = {"SR": "SR" in formKeys, "RC": "RC" in formKeys, "ACA": "ACA" in formKeys, "ST": "ST" in formKeys}
return input
def printCheckboxes(checked={}):
returnString = ""
checkedStrings = {"SR": "", "RC": "", "ACA": "", "ST": ""}
for k,v in checked.iteritems():
if v:
checkedStrings[k] = " checked"
else:
checkedStrings[k] = ""
returnString = """ <br/><h4>Welche Eigenschaften erfüllt diese Historie?</h4>
<div class="form-group">
<div id="toggleButtons" class="btn-group" data-toggle="buttons">
<label id="SR" type="toggleButton">
<input type="checkbox" name="SR" autocomplete="off"
"""+checkedStrings['SR'] +"""/>
<span></span>
</label>
<label id="RC" type="toggleButton">
<input type="checkbox" name="RC" autocomplete="off"
"""+checkedStrings['RC'] +"""/>
<span></span>
</label>
<label id="ACA" type="toggleButton">
<input type="checkbox" name="ACA" autocomplete="off"
"""+checkedStrings['ACA'] +"""/>
<span></span>
</label>
<label id="ST" type="toggleButton">
<input type="checkbox" name="ST" autocomplete="off"
"""+checkedStrings['ST'] +"""/>
<span></span>
</label>
</div>
<button id="quizbutton" name="quizbutton" type="submit" class="btn btn-primary" value="true">Überprüfen</button>
</div>
<div class="form-group">
<button id="submitbutton" type="submit" class="btn btn-default btn-sm" value="send">Ergebnis anzeigen</button>
</div>
</div>
</form>
<script>
$('label[type="toggleButton"]').on("change click", function () {
var checked = $('input', this).is(':checked');
if (checked){
$(this).removeClass().addClass("btn btn-success active");
$('span', this).html('<span class="glyphicon glyphicon-ok" aria-hidden="true" onclick="return iconclicked('+this.id+');"></span>'+this.id);
}
else{
$(this).removeClass().addClass("btn btn-danger");
$('span', this).html('<span class="glyphicon glyphicon-flash" aria-hidden="true" onclick="return iconclicked('+this.id+');"></span>'+this.id);
}
}).change();
</script>
<script>
function iconclicked(label){
$("#"+label.id).click();
};
</script>
</div>
</div>
"""
return returnString
try:
history = str(form['history'].value)
try:
quizButton = form['quizbutton'].value
#quiz has been sent
print html(history) + printResults(history, getUserAnswers(form)) + htmlend
except KeyError:
#just show the results
print html(history) + printResults(history) + htmlend
except KeyError:
history = views.historyToString(DBtransactions.generateHistory(), False)
print html(history)+printCheckboxes()+htmlend