Exemple #1
0
 def get(self,tablename):
     try:
         session = Session()
         M = make_table_model(tablename.encode('utf-8'))   
         #filter_args = [ getattr(M,k)==v[0] for k,v in self.request.arguments.items()]
         _params = {}
         [_params.update({k: v[0]}) for k, v in self.request.arguments.items()]
         logger.info(tablename+str(_params))
         filter_args = utils.get_filter_args(M, _params)
         if filter_args:
             models =  session.query(M).filter(*filter_args).order_by(desc('id')).limit(100)
         else:
             models =  session.query(M).order_by(desc('id')).limit(100)
         logger.debug(models)
         models = [ [ getattr(model,c.name) for c in M.__table__._columns] for model in models]
         clms = map(lambda x:x.name, M.__table__._columns)
         # hide the primary_key 
         result = map(lambda x: dict(zip(clms[1:], x[1:])), models)
         for item in result:
             for k in item:
                 if  type(item[k])==datetime:
                     item[k] = item[k].strftime("%Y-%m-%d %H:%M:%S")
                 elif type(item[k])==unicode:
                     item[k] = item[k].strip()
         self.write(json.dumps(result))
     except BaseException, e:
         self.write(json.dumps({'msg':'Request Error'}))
         logger.error(traceback.format_exc(e))
Exemple #2
0
 def get(self, datestr):
     session = Session()
     M = make_table_model("skylight")
     res = session.execute(
         "select sum(count) ,datetime from skylight where date(datetime) = '%s' group by datetime" % datestr
     ).fetchall()
     res = [(str(r[1]), int(r[0])) for r in res]
     res = json.dumps(res)
     self.render("chart/tmp.html", res=res)
Exemple #3
0
    def get(self, tablename):
        session = Session()
        M = make_table_model(tablename.encode("utf-8"))
        filter_args = []
        filter_args = [getattr(M, k) == v[0] for k, v in self.request.arguments.items()]
        if filter_args:
            models = session.query(M).filter(*filter_args).order_by(desc("id")).limit(100)
        else:
            models = session.query(M).order_by(desc("id")).limit(100)

        models = [[getattr(model, c.name) for c in M.__table__._columns] for model in models]
        session.close()
        self.render("chart/model.html", tablename=tablename, models=models, columns=M.__table__._columns)
Exemple #4
0
def save_records(records):
    session = Session()
    for r in records:
        try:
            r = eval(r)
            tablename = r['tablename']
            data = r['data']
            M = make_table_model(tablename.encode('utf-8'))   
            m = M()
            for k,v in data.items():
                setattr(m,k,v)
            session.add(m)
            logger.debug('%s save %s'%(tablename,str(m)))
        except Exception,e:
            logger.error(traceback.format_exc(e))
Exemple #5
0
def make_query(name, params={}, default_conf={}):
    session = Session()
    Schema = make_table_model("system_chartschema")
    schema = session.query(Schema).filter(Schema.name == name).one()
    schema = yaml.load(schema.schema)

    XModel, xfield = get_field(schema[name]["X"])
    y = schema[name]["Y"]
    time_interval = schema[name]["interval"] if schema[name].has_key("interval") else 12

    field_query = []
    filter_args = []
    group_by = []

    for table_field, options in y.items():
        if options is None:
            options = {}
        Model, field = get_field(table_field)
        filter_args = get_filter_args(Model, params, time_interval)
        if options and options.get("func"):
            function = getattr(func, options["func"])
            field = function(field)

        field_query.append(field)
    field_query.append(xfield)
    query = session.query(*field_query)
    filter_function = getattr(query, "filter")

    query = filter_function(*filter_args)
    if schema[name].get("group"):
        fields = get_group_fields(schema[name]["group"])
        query = query.group_by(*fields)
    query = query.order_by(asc("datetime"))
    logger.debug(str(query))
    option = schema[name].get("option", {})
    option.update(default_conf)
    result = _parse_query(query, field_query, option)
    session.close()
    return json.dumps(result)
Exemple #6
0
 def post(self, name):
     session = Session()
     Schema = make_table_model("system_chartschema")
     schema = session.query(Schema).filter(Schema.name == name).one()
     schema.name = self.request.arguments.get("name")[0]
     schema.schema = self.request.arguments.get("schema")[0]
     session.commit()
     session.close()
     return self.redirect("/chart/schema/")
Exemple #7
0
 def post(self,):
     if not self.request.arguments:
         self.render("chart/schema_insert.html")
     else:
         session = Session()
         M = make_table_model("system_chartschema")
         m = M()
         for k, v in self.request.arguments.items():
             setattr(m, k, v[0])
         session.add(m)
         session.commit()
         session.close()
         return self.redirect("/chart/schema/")
Exemple #8
0
 def get(self, name):
     session = Session()
     Schema = make_table_model("system_chartschema")
     schema = session.query(Schema).filter(Schema.name == name).one()
     session.close()
     self.render("chart/schema_edit.html", schema=schema)
Exemple #9
0
 def get(self):
     session = Session()
     Schema = make_table_model("system_chartschema")
     query = session.query(Schema).order_by(desc("id"))
     session.close()
     self.render("chart/schema.html", query=query)