Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/148999

Change subject: Setup QueryRun model and API endpoints
......................................................................

Setup QueryRun model and API endpoints

* Setup QueryRun model
* Add endpoint that submits a QueryRun
* Setup constants for the different statuses

Change-Id: If3a14025cd88c707972f4137c153dda3d3261596
---
M quarry/web/app.py
M quarry/web/models/query.py
M quarry/web/static/js/query/view.js
M tables.sql
4 files changed, 83 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/quarry/web 
refs/changes/99/148999/1

diff --git a/quarry/web/app.py b/quarry/web/app.py
index 4276f9f..3b9c07d 100644
--- a/quarry/web/app.py
+++ b/quarry/web/app.py
@@ -2,7 +2,7 @@
 from flask_mwoauth import MWOAuth
 import oursql
 from models.user import User
-from models.query import Query, QueryRevision
+from models.query import Query, QueryRevision, QueryRun
 import json
 
 
@@ -85,7 +85,19 @@
     query_rev.save_new()
     query.latest_rev = query_rev
     query.save()
-    return json.dumps({'id': query.id})
+    return json.dumps({'id': query_rev.id})
+
+
+@app.route('/api/query/run', methods=['POST'])
+def api_run_query():
+    if g.user is None:
+        return "Authentication required", 401
+    print request.form.get('query_rev_id', '')
+    query_rev = QueryRevision.get_by_id(request.form['query_rev_id'])
+    query_run = QueryRun()
+    query_run.query_rev = query_rev
+    query_run.save_new()
+    return json.dumps({'id': query_run.id})
 
 
 @app.route("/query/all")
diff --git a/quarry/web/models/query.py b/quarry/web/models/query.py
index 3a4acac..4befb87 100644
--- a/quarry/web/models/query.py
+++ b/quarry/web/models/query.py
@@ -100,3 +100,58 @@
                 (self.query_id, self.text)
             )
             self.id = cur.lastrowid
+
+
+class QueryRun(object):
+    STATUS_QUEUED = 0
+    STATUS_SCHEDULED = 1
+    STATUS_RUNNING = 2
+    STATUS_KILLED = 3
+    STATUS_COMPLETE = 4
+    STATUS_SUPERSEDED = 5
+
+    def __init__(self, id=None, query_rev_id=None, status=None, 
timestamp=None):
+        self.id = id
+        self.query_rev_id = query_rev_id
+        self.status = status
+        self.timestamp = timestamp
+
+    @classmethod
+    def get_by_id(cls, id):
+        with g.conn.cursor() as cur:
+            cur.execute(
+                """SELECT id, query_rev_id, status, timestamp
+                FROM query_run WHERE id = ?""",
+                (id, )
+            )
+            result = cur.fetchone()
+        if result is None:
+            return None
+
+        return cls(result[0], result[1], result[2], result[3])
+
+    def save_new(self):
+        with g.conn.cursor() as cur:
+            cur.execute(
+                "INSERT INTO query_run (query_rev_id) VALUES (?)",
+                (self.query_rev_id, )
+            )
+            self.id = cur.lastrowid
+
+    def save(self):
+        with g.conn.cursor() as cur:
+            cur.execute(
+                "UPDATE query RUN SET status=?",
+                (self.status, )
+            )
+
+    @property
+    def query_rev(self):
+        if not hasattr(self, '_query_rev'):
+            self._query = QueryRevision.get_by_id(self.query_rev_id)
+        return self._query
+
+    @query_rev.setter
+    def query_rev(self, value):
+        self._query_rev = value
+        self.query_rev_id = value.id
diff --git a/quarry/web/static/js/query/view.js 
b/quarry/web/static/js/query/view.js
index 0dfc9f6..0c0c853 100644
--- a/quarry/web/static/js/query/view.js
+++ b/quarry/web/static/js/query/view.js
@@ -10,7 +10,12 @@
             text: editor.getValue(),
             query_id: vars.query_id
         }).done( function( data ) {
-            alert( data );
+            var d = JSON.parse(data);
+            $.post( "/api/query/run", {
+                query_rev_id: d.id,
+            }).done( function( data ) {
+                alert( data );
+            });
         } );
     } );
 
diff --git a/tables.sql b/tables.sql
index 3010738..f0a0784 100644
--- a/tables.sql
+++ b/tables.sql
@@ -16,3 +16,11 @@
     query_id INT UNSIGNED NOT NULL,
     timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );
+
+CREATE TABLE query_run(
+    id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
+    query_rev_id INT UNSIGNED NOT NULL,
+    status TINYINT UNSIGNED NOT NULL DEFAULT 0,
+    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE 
CURRENT_TIMESTAMP
+);

-- 
To view, visit https://gerrit.wikimedia.org/r/148999
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If3a14025cd88c707972f4137c153dda3d3261596
Gerrit-PatchSet: 1
Gerrit-Project: analytics/quarry/web
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <yuvipa...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to