[chronojump-server] Added exhibitionData.py



commit 317d510c0cb40b8b7bf6516b0f4ca70b6e3b6a1e
Author: root <root chronojump org>
Date:   Tue Mar 5 18:38:22 2019 +0100

    Added exhibitionData.py

 exhibitionData/exhibitionData.py | 199 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 199 insertions(+)
---
diff --git a/exhibitionData/exhibitionData.py b/exhibitionData/exhibitionData.py
new file mode 100644
index 0000000..46e5cde
--- /dev/null
+++ b/exhibitionData/exhibitionData.py
@@ -0,0 +1,199 @@
+from flask import Flask, render_template, request
+import ConfigParser
+import MySQLdb
+
+config = ConfigParser.ConfigParser()
+config.read('/etc/chronojump.conf')
+
+application = Flask(__name__)
+
+#@application.route("/consulta")
+@application.route("/")
+def my_form():
+    return render_template('my-form.html')
+
+def connect_db():
+    db = MySQLdb.connect(
+        host=config.get("db","server"),
+        user=config.get("db","user"),
+        passwd=config.get("db","password"),
+        db=config.get("db","name")
+        )
+
+    cur = db.cursor()
+    return (db, cur)
+
+def average(testType):
+    (con, cur) = connect_db()
+    cur.execute("SELECT AVG(result) AS result FROM exhibitionTest WHERE testType = '" + testType + "'")
+    rv = cur.fetchall()
+    #return "<li>" + testType + ": " + str(rv[0]) + "</li>"
+    return str(rv)
+
+def averages():
+    s = "<ul>"
+    s += average("JUMP")
+    s += average("RUN")
+    s += average("FORCE_ROPE")
+    s += average("FORCE_SHOT")
+    s += average("INERTIAL")
+    s += "</ul>"
+    return s
+
+def userData1Test(school, group, personId, testType):
+    (con, cur) = connect_db()
+    cur.execute("SELECT AVG(result) AS result FROM exhibitionTest WHERE schoolID = " + school + " AND 
groupID = " + group + " AND personID = " + personId + " AND testType = '" + testType + "'")
+    rv = cur.fetchall()
+    #return "<li>" + testType + ": " + str(rv[0]) + "</li>"
+    return str(rv)
+    
+def findStats(sObject, testType):
+    for row in sObject:
+        if row[0] == testType:
+            return str(row[1])
+    
+def userDataAllTests(school, group, personId):
+    (con, cur) = connect_db()
+
+    sexSQLString = " personId < 100 "
+    sexWord = "Noies"
+    sexAllWord = "Totes&nbsp;les&nbsp;noies"
+    if int(personId) >= 100:
+        sexSQLString = " personId >= 100 "
+        sexWord = "Nois"
+        sexAllWord = "Tots&nbsp;els&nbsp;nois"
+
+    #SUM
+    cur.execute("SELECT testType, round(sum(result),2) AS result, count(result) AS count FROM exhibitionTest 
WHERE schoolID = " + school + " GROUP BY testType ORDER BY testType")
+    sumAllStats = cur.fetchall()
+
+    #AVERAGES
+    #all stats (of this sex)
+    cur.execute("SELECT testType, round(AVG(result),2) AS result FROM exhibitionTest WHERE " + sexSQLString 
+ " GROUP BY testType ORDER BY testType")
+    avgAllStats = cur.fetchall()
+        
+    sexSQLString = " AND " + sexSQLString
+
+    #school stats (of this sex)
+    cur.execute("SELECT testType, round(AVG(result),2) AS result FROM exhibitionTest WHERE schoolID = " + 
school + sexSQLString + " GROUP BY testType ORDER BY testType")
+    sStats = cur.fetchall()
+
+    #group stats (of this sex)
+    cur.execute("SELECT testType, round(AVG(result),2) AS result FROM exhibitionTest WHERE schoolID = " + 
school + " AND groupID = " + group + sexSQLString + " GROUP BY testType ORDER BY testType")
+    gStats = cur.fetchall()
+
+    #person stats
+    cur.execute("SELECT testType, round(AVG(result),2) AS result FROM exhibitionTest WHERE schoolID = " + 
school + " AND groupID = " + group + " AND personID = " + personId + " GROUP BY testType ORDER BY testType")
+    pStats = cur.fetchall()
+
+    results = "<h3>Resultats dels tests amb Chronojump al Yomo BCN 2019</h3>"
+    results += "<table id=\"taula\">"
+    #results += "<tr><th colspan=2>Person</td></tr>"
+    #results += "<tr><td>Id</td><td>" + personId + "</td></tr>"
+    #results += "<tr><td>Grade</td><td>" + group + "</td></tr>"
+    #results += "<tr><td>School</td><td>" + school + "</td></tr>"
+    results += "<tr><th>Id</td><th>Grade</td><th>School</td></tr>"
+    results += "<tr><td>" + personId + "</td><td>" + group + "</td><td>" + school + "</td></tr>"
+    results += "<table><br>"
+    
+    countRow = 0
+    needFirstLine = True
+    for row in pStats:
+        if needFirstLine:
+            results += "<table id=\"taula\">"
+            results += "<tr><th rowspan=2>Test</th><th colspan = 4>Mitjana dels resultats</td></tr>"
+            results += "<tr><th>Resultats</th><th>" + sexWord + "&nbsp;grup</th><th>" + sexWord + 
"&nbsp;escola</th><th>" + sexAllWord + "</th></tr>"
+            needFirstLine = False
+
+        if str(row[0]) == "RUN": #convert run time in 4s to speed
+            results += "<tr><td>" + row[0] + "</td><td style=\"background-color:yellow\";><b>"
+            speed = 4/row[1]
+            results += str(round(speed,2)) + "&nbsp;m/s</b></td><td>"
+            speed = 4/float(findStats(gStats, row[0]))
+            results += str(round(speed,2)) + "&nbsp;m/s</td><td>"
+            speed = 4/float(findStats(sStats, row[0]))
+            results += str(round(speed,2)) + "&nbsp;m/s</td><td>"
+            speed = 4/float(findStats(avgAllStats, row[0]))
+            results += str(round(speed,2)) + "&nbsp;m/s</td></tr>"
+
+        #elif str(row[0]) == "INERTIAL" and row[1] > 10000: #first day inertial data upload was not ok, do 
not show it.
+        #    commented because we have deleted
+        #    results += ""
+        else:
+            results += "<tr><td>" + row[0] + "</td><td style=\"background-color:yellow\";><b>" + str(row[1])
+            if str(row[0]) == "FORCE_ROPE" or str(row[0]) == "FORCE_SHOT":
+                results += "&nbsp;N" + "</b></td><td>" + findStats(gStats, row[0]) + "&nbsp;N</td><td>" + 
findStats(sStats, row[0]) + "&nbsp;N</td><td>" + findStats(avgAllStats, row[0]) + "&nbsp;N"
+            elif str(row[0]) == "INERTIAL":
+                results += "&nbsp;W" + "</b></td><td>" + findStats(gStats, row[0]) + "&nbsp;W</td><td>" + 
findStats(sStats, row[0]) + "&nbsp;W</td><td>" + findStats(avgAllStats, row[0]) + "&nbsp;W"
+            elif str(row[0]) == "JUMP":
+                results += "&nbsp;cm" + "</b></td><td>" + findStats(gStats, "JUMP") + "&nbsp;cm</td><td>" + 
findStats(sStats, "JUMP") + "&nbsp;cm</td><td>" + findStats(avgAllStats, "JUMP") + "&nbsp;cm"
+            results += "</td></tr>"
+        
+    if needFirstLine:
+            results += "No s'han trobat resultats per aquest codi.<br>"
+    else:
+            results += "</table>"
+
+    #estadistiques de suma de tot
+    needFirstLine = True
+    for row in sumAllStats:
+        if needFirstLine:
+            results += "<br><table id=\"taula\" 
style=\"width:600px;\"><tr><th>Test</th><th>Estad&iacute;stiques col.laboratives de escola</th></tr>"
+            needFirstLine = False
+
+        if str(row[0]) == "JUMP":
+            results += "<tr><td>" + row[0] + "</td><td style=\"text-align:left\";>Sumant tots els salts (" + 
str(row[2]) + ") de la teva escola, saltar&iacute;eu un edifici de " + str(round(row[1]/300,1)) + " 
plantes.</td></tr>" #300 pq passem a m i les plantes les fem de 3 metres
+        elif str(row[0]) == "FORCE_ROPE":
+            results += "<tr><td>" + row[0] + "</td><td style=\"text-align:left\";>Sumant tota la F estirant 
la corda dels (" + str(row[2]) + ") tests de corda que heu fet a la vostra escola, aixecar&iacute;u " + 
str(round(row[1]/10000,1)) + " cotxes de 1000 Kg.</td></tr>" #/10000 pq newtons passem a Kg
+        elif str(row[0]) == "FORCE_SHOT":
+            results += "<tr><td>" + row[0] + "</td><td style=\"text-align:left\";>Sumant tota la F xutant la 
pilota en els (" + str(row[2]) + ") tests de xut que heu fet a la vostra escola, una massa de 1 Kg impulsada 
durant 1 segon amb aquesta F arribaria a " + str(round(0.5*((row[1]-9.81)*(row[1]-9.81))/9.81/1000,1)) + " 
Kilometres.</td></tr>"
+        elif str(row[0]) == "INERTIAL":
+            results += "<tr><td>" + row[0] + "</td><td style=\"text-align:left\";>Sumant tota la energia 
dels (" + str(row[2]) + ") tests amb m&agrave;quina inercial que heu fet a la vostra escola, podr&iacute;eu 
il.luminar " + str(round(row[1]/1000/4.5,1)) + " cases amb pot&egrave;ncia de 4,5 KW.</td></tr>"
+
+    results += "</table>"
+    results += "<br>A les estad&iacute;stiques del dissabte les escoles s&oacute;n ciutats o barris de 
Barcelona, i els grups s&oacute;n edats."
+    return results
+    
+
+@application.route('/', methods=['POST'])
+def my_form_post():
+    returnStr = """<!DOCTYPE html>
+<html>
+<head>
+<style>
+#taula {
+  font-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif;
+  border-collapse: collapse;
+  width: 100px;
+}
+
+#taula td, #taula th {
+  border: 1px solid #ddd;
+  text-align: center;
+  padding: 8px;
+}
+
+#taula tr:nth-child(even){background-color: #f2f2f2;}
+
+#taula tr:hover {background-color: #ddd;}
+
+#taula th {
+  padding-top: 12px;
+  padding-bottom: 12px;
+  text-align: center;
+  background-color: #0f2351;
+  color: white;
+}
+</style>
+</head>
+<body>"""
+ 
+    returnStr += "<br><br><a href=\"http://chronojump.org/es/yomo\";><- Tornar</a></body>"
+    school = request.form['school']
+    group = request.form['group']
+    personID = request.form['personID']
+    #return userData1Test(school, group, personId, "RUN")
+    return userDataAllTests(school, group, personID) + returnStr
+    
+if __name__ == "__main__":
+    application.run(debug=True,host='0.0.0.0')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]