Commits

Matthew Turk committed 8c38429

Adding query methods that access SDB, rather than SQLite.

  • Participants
  • Parent commits 3614eec

Comments (0)

Files changed (4)

 from sqlalchemy import Column, Integer, String, Float, PickleType, \
     ForeignKey, LargeBinary
 from sqlalchemy.orm import relationship
-from ythub.database import Base, db_session
+from ythub.database import Base, db_session, sdb
 from yt.mods import *
 import uuid
 from werkzeug.security import \
     generate_password_hash, check_password_hash
 
-model_registry = {}
-
 class DataProduct(object):
     def __init__(self, id, pf, type, field = "", url = ""):
         self.id = id
     def check_password(self, password):
         return check_password_hash(self.pw_hash, password)
 
-class SimulationOutput(Base):
-    __tablename__ = "sim_outputs"
-    id = Column(Integer, primary_key=True)
-    user_id = Column(Integer, ForeignKey('users.id'))
-    user = relationship(User, primaryjoin=user_id == User.id)
-    name = Column(String(64))
+def _data_results(q):
+    rs = sdb.select(q)
+    data = []
+    for r in rs:
+        data.append(dict(r.items()))
+    return data
 
-    # Now the actual quantities
-    unique_identifier = Column(String(64), unique=True)
-    output_hash = Column(String(64), unique=True)
-    dimensionality = Column(Integer)
-    refine_by = Column(Integer)
-    domain_dimensions = Column(PickleType)
-    current_redshift = Column(Float)
-    current_time = Column(Float)
-    domain_left_edge = Column(PickleType)
-    domain_right_edge = Column(PickleType)
-    cosmological_simulation = Column(Integer)
-    omega_matter = Column(Float)
-    omega_lambda = Column(Float)
-    hubble_constant = Column(Float)
+def data_query(output_hash = None):
+    q = 'select * from `%s`' % (sdb.name)
+    if output_hash is not None:
+        q += ' where output_hash="%s"' % (output_hash)
+    return _data_results(q)
 
-    def __init__(self, **kwargs):
-        self.domain_right_edge = na.array(kwargs.pop('domain_right_edge'),
-                                         dtype='float64')
-        self.domain_left_edge = na.array(kwargs.pop('domain_left_edge'),
-                                         dtype='float64')
-        self.domain_dimensions = na.array(kwargs.pop('domain_dimensions'),
-                                          dtype='int32')
-        super(SimulationOutput, self).__init__(**kwargs)
+def type_query(data_type, output_hash = None):
+    q = 'select * from `%s` where type="%s"' % (sdb.name, data_type)
+    if output_hash is not None:
+        q += ' where output_hash="%s"' % (output_hash)
+    return _data_results(q)
 
-model_registry['simulation_output'] = SimulationOutput
+def obj_query(hub_id):
+    q = 'select * from `%s` where hub_id="%s"' % (sdb.name, hub_id)
+    return _data_results(q)
 
-class VariableMapData(Base):
-    __tablename__ = "vm_data"
-    id = Column(Integer, primary_key=True)
+#model_registry['simulation_output'] = SimulationOutput
+#model_registry['proj'] = VariableMapData
+#model_registry['slice'] = VariableMapData
+#model_registry['image_collection'] = ImageCollection
 
-    output_hash = Column(String(64), ForeignKey('sim_outputs.output_hash'))
-    pf = relationship(SimulationOutput,
-            primaryjoin=output_hash == SimulationOutput.output_hash)
-
-    axis = Column(Integer)
-    field = Column(String(128))
-    weight = Column(String(128))
-    field_data = Column(PickleType)
-    vm_type = Column(String(15))
-
-    @classmethod
-    def products(cls, output_hash = None):
-        # First variable maps
-        data = []
-        if output_hash is not None:
-            vmd = cls.query.filter(cls.output_hash == output_hash).all()
-        else:
-            vmd = cls.query.all()
-        for vm in vmd:
-            data.append(DataProduct(vm.id, vm.pf, vm.vm_type, vm.field,
-                "/map_server/%s" % vm.id))
-        return data
-        
-model_registry['proj'] = VariableMapData
-model_registry['slice'] = VariableMapData
-
-class ImageCollection(Base):
-    __tablename__ = "image_collections"
-    id = Column(Integer, primary_key=True)
-    output_hash = Column(String(64), ForeignKey('sim_outputs.output_hash'))
-    collection_hash = Column(String(64))
-    pf = relationship(SimulationOutput,
-            primaryjoin=output_hash == SimulationOutput.output_hash)
-    name = Column(String(128))
-
-    def __init__(self, **kwargs):
-        images = kwargs.pop("images")
-        kwargs['collection_hash'] = uuid.uuid4().hex
-        super(ImageCollection, self).__init__(**kwargs)
-        for md, data in images.items():
-            print "MAKING NEW IMAGE", self.id
-            im = Image(collection_hash = self.collection_hash,
-                       im_metadata = md, data = data)
-            db_session.add(im)
-        db_session.commit()
-
-    @classmethod
-    def products(cls, output_hash):
-        # First variable maps
-        data = []
-        if output_hash is not None:
-            vmd = cls.query.filter(cls.output_hash == output_hash).all()
-        else:
-            vmd = cls.query.all()
-        for vm in vmd:
-            data.append(DataProduct(vm.id, vm.pf, "Images", "",
-                "/image_collection/%s" % vm.collection_hash))
-        return data
-        
-model_registry['image_collection'] = ImageCollection
-
-class Image(Base):
-    __tablename__ = "image_data"
-    id = Column(Integer, primary_key=True)
-    collection_hash = Column(String(64),
-            ForeignKey('image_collections.collection_hash'))
-    collection = relationship(ImageCollection,
-            primaryjoin=collection_hash == ImageCollection.collection_hash)
-    image_hash = Column(String(64))
-    im_metadata = Column(String(1024))
-    data = Column(LargeBinary)
-
-    def __init__(self, **kwargs):
-        kwargs['image_hash'] = uuid.uuid4().hex
-        super(Image, self).__init__(**kwargs)
-
-# Now we do some user models
-from werkzeug.security import generate_password_hash, \
-     check_password_hash
-

ythub/templates/index.html

               </thead>
               <tbody>
                 {% for d in data %}
-                <tr><td>{{ d.id }}</td>
-                    <td>{{ d.pf.user.name }}</td>
-                    <td>{{ d.type }}</td>
-                    <td>{{ d.field }}</td>
-                    <td><a href="simulation_outputs/{{d.pf.output_hash}}">{{ d.pf.name }}</a></td>
-                    <td><a href="map_server/{{ d.id }}">Map it!</a></td>
+                <tr><td>{{ d['id'] }}</td>
+                    <td></td>
+                    <td>{{ d['obj_type'] }}</td>
+                    <td></td>
+                    <td><a href="simulation_outputs/{{d['output_hash']}}"></a></td>
+                    <td><a href="map_server/{{ d['hub_id'] }}">Map it!</a></td>
                 </tr>
                 {% endfor %}
               </tbody>

ythub/uploader.py

 from tempfile import TemporaryFile
 from flask import jsonify
-from ythub.models import model_registry
+#from ythub.models import model_registry
 from ythub.database import db_session, sdb, bucket
 from yt.mods import *
 import uuid
 from flask import render_template, session, redirect, url_for, escape, \
     request, make_response
 from flask import g, flash, abort, Response, jsonify, json
-from ythub.models import VariableMapData, User, SimulationOutput, \
-    ImageCollection, Image
+from ythub.models import User, data_query, type_query, obj_query
 from ythub.data_proxies import MapDataProxy, container
 from yt.mods import *
 from ythub.map_view import MapTileView
 
 CHUNK_SIZE = 1024*1024*16
 
-def data_products(output_hash = None):
-    data = []
-    data += ImageCollection.products(output_hash)
-    data += VariableMapData.products(output_hash)
-    return data
-
 @app.route("/")
 def index():
-    data = VariableMapData.products()[-20:]
+    data = data_query()
     return render_template("index.html", data = data)
 
 @app.route("/create_user", methods=["GET", "POST"])
 
 @app.route("/map_server/<map_id>")
 def mapserver(map_id = None):
-    map = VariableMapData.query.filter(VariableMapData.id == map_id).first()
+    map = obj_query(map_id)
+    if len(map) == 0:
+        return make_response("MAP NOT FOUND", 404)
+    map = map[0]
     map_proxy = MapDataProxy(map)
-    container['map_server_%s' % map_id] = MapTileView(map_proxy, map.field)
+    container['map_server_%s' % map_id] = MapTileView(map_proxy, map['field'])
     return render_template("mapserver.html", map_id = map_id), 200
 
 @app.route("/image_collection/<collection_hash>")
 def image_collection(collection_hash = None):
     if collection_hash is None:
         return make_response("IMAGE COLLECTION NOT FOUND", 404)
-    collection = ImageCollection.query.filter(
-        ImageCollection.collection_hash == collection_hash).all()
+    collection = obj_query(collection_hash)
     if len(collection) == 0:
         return make_response("IMAGE COLLECTION NOT FOUND", 404)
     images = Image.query.filter(Image.collection_hash == collection_hash).all()
 
 @app.route("/simulation_outputs/<output_hash>")
 def simulation_display(output_hash = None):
-    results = SimulationOutput.query.filter_by(output_hash = output_hash).first()
-    data = data_products(output_hash)
-    if results is None: return make_response("SORRY NOT FOUND", 404)
+    results = obj_query(output_hash)
+    if len(results) == 0: return make_response("SORRY NOT FOUND", 404)
+    data = data_query(output_hash)
     return render_template("simulation_display.html", s = results, data=data)
 
 @app.route("/upload", methods = ["POST", "GET"])