Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add admin

  • Loading branch information...
commit a31ba4c19656fab39dd11335d099725c96bf9a10 1 parent 9a333b2
authored May 03, 2010
29  Server/db.py
@@ -5,11 +5,36 @@
5 5
 def getConnection():
6 6
     return database.Connection("/tmp/mysql.sock", user="root", database="dpgpjs")
7 7
 
  8
+
8 9
 def getProblems():
9 10
     c=getConnection()
  11
+    problems = [problem for problem in c.query("SELECT * FROM problems")]
  12
+    for problem in problems:
  13
+        program = c.query("SELECT * FROM programs WHERE problem = %s ORDER BY fitness DESC LIMIT 1" % problem.id)
  14
+        if(len(program) != 0):
  15
+            problem.bestFitness = program[0].fitness
  16
+            problem.bestProgram = program[0].program_string
  17
+        else:
  18
+            problem.bestFitness = "none"
  19
+            problem.bestProgram = "none"
  20
+    return problems
  21
+
  22
+def getProblem(problem_id):
  23
+    c=getConnection()
  24
+    problem = c.query("SELECT * FROM problems WHERE id = %s" % problem_id)[0]
  25
+    program = c.query("SELECT * FROM programs WHERE problem = %s ORDER BY fitness DESC LIMIT 1" % problem.id)
  26
+    if(len(program) < 0):
  27
+        problem.bestFitness = program.fitness
  28
+        problem.bestProgram = program[0].program_string
  29
+    else:    
  30
+            problem.bestFitness = "none"
  31
+            problem.bestProgram = "none"
  32
+    return problem
  33
+
  34
+def updateProblem(problem_id,name,comments,start_population,max_population,tournament_size,crossover_probability,mutation_probability,clone_probability):
  35
+    c=getConnection()
10 36
     
11  
-    return [problem for problem in c.query("SELECT * FROM problems")]
12  
-        
  37
+    return c.execute("UPDATE problems SET name = \"%s\", comments = \"%s\", start_population = \"%s\", max_population = \"%s\", tournament_size = \"%s\", crossover_probability = \"%s\", mutation_probability = \"%s\", clone_probability = \"%s\" WHERE id = %s LIMIT 1" % (name, comments, start_population,max_population,tournament_size,crossover_probability,mutation_probability, clone_probability, problem_id))
13 38
 def getFitnessCases(problem_id):
14 39
     c=getConnection()
15 40
     
26  Server/dpgp.py
@@ -16,6 +16,7 @@ def get(self):
16 16
         self.render("templates/upload.html")
17 17
 
18 18
 class UploadFormHandler(tornado.web.RequestHandler):
  19
+    # TODO: after git merge, switch upload to admin
19 20
     def get(self):
20 21
         self.render("templates/error.html")
21 22
     
@@ -32,11 +33,32 @@ def get(self,worker_id):
32 33
 class WorkerJSHandler(tornado.web.RequestHandler):
33 34
     def get(self,worker_id):
34 35
         self.render("templates/gp_worker.js", fitness_cases=db.getFitnessCases(worker_id))
  36
+
35 37
 class ResultsUploadHandler(tornado.web.RequestHandler):
36 38
     def post(self):
37 39
         # uploadedData = json.loads(self.ge)
38 40
         print self.request.arguments
39 41
 
  42
+class AdminHandler(tornado.web.RequestHandler):
  43
+    def get(self):
  44
+        self.render("templates/admin/index.html",problems=db.getProblems())
  45
+
  46
+class AdminEditHandler(tornado.web.RequestHandler):
  47
+    def get(self,problem_id):
  48
+        self.render("templates/admin/edit.html",problem=db.getProblem(problem_id))
  49
+    def post(self,problem_id):
  50
+        name = self.get_argument('name')
  51
+        comments = self.get_argument('comments')
  52
+        start_population = self.get_argument('start_population')
  53
+        max_population = self.get_argument('max_population')
  54
+        tournament_size = self.get_argument('tournament_size')
  55
+        crossover_probability = self.get_argument('crossover_probability')
  56
+        mutation_probability = self.get_argument('mutation_probability')
  57
+        clone_probability = self.get_argument('clone_probability')
  58
+        db.updateProblem(problem_id,name,comments,start_population,max_population,tournament_size,crossover_probability,mutation_probability,clone_probability)
  59
+        self.render("templates/admin/edit.html", problem=db.getProblem(problem_id))
  60
+
  61
+
40 62
 settings = {"static_path": os.path.join(os.path.dirname(__file__), "static") }
41 63
 
42 64
 application = tornado.web.Application([
@@ -45,7 +67,9 @@ def post(self):
45 67
     (r"/new_problem", UploadFormHandler),
46 68
     (r"/gp_worker([0-9]+)\.js", WorkerJSHandler),
47 69
     (r"/worker([0-9]+)", WorkerHandler),
48  
-    (r"/uploadresults", ResultsUploadHandler)
  70
+    (r"/uploadresults", ResultsUploadHandler),
  71
+    (r"/admin", AdminHandler),
  72
+    (r"/admin/edit([0-9]+)", AdminEditHandler)
49 73
 
50 74
 ], **settings)
51 75
 
32  Server/templates/admin/edit.html
... ...
@@ -0,0 +1,32 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  3
+	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  4
+
  5
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  6
+<head>
  7
+	<title>dPGP.js Admin</title>
  8
+	
  9
+</head>
  10
+
  11
+<body>
  12
+	<a href="/">Back to index</a><br/>
  13
+	<a href="/admin">Back to admin index</a>
  14
+	<form action="/admin/edit{{ problem.id }}" method="post" accept-charset="utf-8">
  15
+		<div style="border-style:solid;border-width:2px;padding:3px">
  16
+			<div>Name: <input type="text" name="name" value="{{ problem.name}}" id="name" /><div>
  17
+			<div>Comments: <input type="text" name="comments" value="{{ problem.comments }}" id="comments" /></div>
  18
+			<div>Evaluated Stack: {{ problem.evaluated_stack }}</div>
  19
+			<div>Start Population: <input type="text" name="start_population" value="{{ problem.start_population }}" id="start_population" /></div>
  20
+			<div>Max Population: <input type="text" name="max_population" value="{{ problem.max_population }}" id="max_population" /></div>
  21
+			<div>Tournament Size: <input type="text" name="tournament_size" value="{{ problem.tournament_size }}" id="tournament_size" /></div>
  22
+			<div>Crossover Probability: <input type="text" name="crossover_probability" value="{{ problem.crossover_probability }}" id="crossover_probability" /></div>
  23
+			<div>Mutation Probability: <input type="text" name="mutation_probability" value="{{ problem.mutation_probability }}" id="mutation_probability" /></div>
  24
+			<div>Clone Probability: <input type="text" name="clone_probability" value="{{ problem.clone_probability }}" id="clone_probability" /></div>
  25
+			<div>Best fitness: {{ problem.bestFitness }}</div>
  26
+			<div><a href="/worker{{ problem.id }}">Worker link</a></div>
  27
+			<div><input type="submit" value="Edit"></div>
  28
+		</div>
  29
+	</form>
  30
+
  31
+</body>
  32
+</html>
24  Server/templates/admin/index.html
... ...
@@ -0,0 +1,24 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  3
+	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  4
+
  5
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  6
+<head>
  7
+	<title>dPGP.js Admin</title>
  8
+	
  9
+</head>
  10
+
  11
+<body>
  12
+{% for problem in problems %}
  13
+<div style="border-style:solid;border-width:2px;padding:3px">
  14
+	<div>Name: {{ problem.name }}</div>
  15
+	<div>Comments: {{ problem.comments }}</div>
  16
+    <div>Best fitness: {{ problem.bestFitness }}</div>
  17
+    <div>Best program: {{ problem.bestProgram }}</div>
  18
+	<div><a href="/worker{{ problem.id }}">Worker link</a></div>
  19
+	<div><a href="/admin/edit{{ problem.id }}">Edit</a></div>
  20
+</div>
  21
+{% end %}
  22
+
  23
+</body>
  24
+</html>
4  Server/templates/main.html
@@ -15,8 +15,8 @@
15 15
         <div style="border-style:solid;border-width:2px;padding:3px">
16 16
             <div>Name: {{ problem.name }}</div>
17 17
             <div>Comments: {{ problem.comments }}</div>
18  
-            
19  
-            <div>Best fitness: undefined</div>
  18
+            <div>Best fitness: {{ problem.bestFitness }}</div>
  19
+            <div>Best program: {{ problem.bestProgram }}</div>
20 20
             <div><a href="/worker{{ problem.id }}">Worker link</a></div>
21 21
         </div>
22 22
         

0 notes on commit a31ba4c

Please sign in to comment.
Something went wrong with that request. Please try again.