Exemplo n.º 1
0
    def new_chart(self, k):

        try:
            row = Charts()
            row.name = 'hp-' + '-'.join(str(i) for i in k)
            row.owner = ''
            row.cate_id = CATEGORY_ID
            row.ds_id = 0
            row.ds_tbl_name = ''
            row.ext_info = ''
            row.followers = 0
            row.createtime = datetime.datetime.now()
            self.sakuya_db.add(row)
            self.sakuya_db.commit()

            logging.info('Create chart %d - %s' % (row.id, row.name))

            return row.id

        except Exception:
            self.sakuya_db.rollback()
            logging.exception('Fail to create chart %s' % str(k))
            return None

        finally:
            try:
                f.close()
            except Exception:
                pass
Exemplo n.º 2
0
    def new_chart(self, k):

        try:
            row = Charts()
            row.name = 'hp-' + '-'.join(str(i) for i in k)
            row.owner = ''
            row.cate_id = CATEGORY_ID
            row.ds_id = 0
            row.ds_tbl_name = ''
            row.ext_info = ''
            row.followers = 0
            row.createtime = datetime.datetime.now()
            self.sakuya_db.add(row)
            self.sakuya_db.commit()

            logging.info('Create chart %d - %s' % (row.id, row.name))

            return row.id

        except Exception:
            self.sakuya_db.rollback()
            logging.exception('Fail to create chart %s' % str(k))
            return None

        finally:
            try:
                f.close()
            except Exception:
                pass
Exemplo n.º 3
0
def add(sakuya_db):

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        rt['warn_only'] = False
        rt['is_haopan'] = False
        return rt

    try:
        use_storm = request.forms['use_storm'] == '1'
        if use_storm:
            rule = make_rule()
            if not rule['datasource'] or not rule['filters']:
                raise ValueError

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        name = request.forms['name']
        if not name:
            raise ValueError

        cid = int(request.forms['cid'])
        if not sakuya_db.\
               query(Categories).\
               filter_by(id=cid, is_parent=False).\
               count():
            raise ValueError

        owner = request.forms['owner']
        if not sakuya_db.\
               query(Users).\
               filter_by(username=owner).\
               count():
            raise ValueError

    except Exception:
        return error('参数错误。')

    # add chart
    try:
        row = Charts()
        row.name = name
        row.owner = owner
        row.cate_id = cid
        row.root_category = get_parent_category(sakuya_db, cid)
        row.ds_id = 0
        row.ds_tbl_name = ''
        row.ext_info = ''
        row.followers = 0
        row.createtime = datetime.datetime.now()
        row.alert_enable = int(alert_enable)

        ext = {}
        if use_storm:
            ext['rule'] = rule
        row.ext_info = json.dumps(ext)

        sakuya_db.add(row)
        sakuya_db.flush()
        rowid = row.id
        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表添加失败。')

    process_warn_rules(sakuya_db, rowid, warn)

    if use_storm:
        storm.set_rule(rowid, json.dumps(rule))
        redirect('/admin/category/%d' % cid)
    else:
        redirect('/admin/chart/success/%d' % rowid)
Exemplo n.º 4
0
def add(sakuya_db):

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        rt['warn_only'] = False
        rt['is_haopan'] = False
        return rt

    try:
        use_storm = request.forms['use_storm'] == '1'
        if use_storm:
            rule = make_rule()
            if not rule['datasource'] or not rule['filters']:
                raise ValueError

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        name = request.forms['name']
        if not name:
            raise ValueError

        cid = int(request.forms['cid'])
        if not sakuya_db.\
               query(Categories).\
               filter_by(id=cid, is_parent=False).\
               count():
            raise ValueError

        owner = request.forms['owner']
        if not sakuya_db.\
               query(Users).\
               filter_by(username=owner).\
               count():
            raise ValueError

    except Exception:
        return error('参数错误。')

    # add chart
    try:
        row = Charts()
        row.name = name
        row.owner = owner
        row.cate_id = cid
        row.root_category = get_parent_category(sakuya_db, cid)
        row.ds_id = 0
        row.ds_tbl_name = ''
        row.ext_info = ''
        row.followers = 0
        row.createtime = datetime.datetime.now()
        row.alert_enable = int(alert_enable)

        ext = {}
        if use_storm:
            ext['rule'] = rule
        row.ext_info = json.dumps(ext)

        sakuya_db.add(row)
        sakuya_db.flush()
        rowid = row.id
        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表添加失败。')

    process_warn_rules(sakuya_db, rowid, warn)

    if use_storm:
        storm.set_rule(rowid, json.dumps(rule))
        redirect('/admin/category/%d' % cid)
    else:
        redirect('/admin/chart/success/%d' % rowid)
Exemplo n.º 5
0
            chart.cate_id = pid
            chart.createtime = datetime.datetime.now()
            sakuya_db.add(chart)
            sakuya_db.commit()

        if isinstance(v, list):
            try:
                value, detail = v
            except Exception, e:
                continue

        else:
            value, detail = v, None

        row = Chartdata()
        row.ds_id = chart.id
        row.time = dt
        row.data = value
        sakuya_db.add(row)

        if detail is not None:
            row = Logstuff()
            row.ds_id = chart.id
            row.time = dt
            row.detail = detail
            sakuya_db.add(row)

        sakuya_db.commit()

    return util.output('ok')
Exemplo n.º 6
0
            chart.cate_id = pid
            chart.createtime = datetime.datetime.now()
            sakuya_db.add(chart)
            sakuya_db.commit()

        if isinstance(v, list):
            try:
                value, detail = v
            except Exception, e:
                continue

        else:
            value, detail = v, None

        row = Chartdata()
        row.ds_id = chart.id
        row.time = dt
        row.data = value
        sakuya_db.add(row)

        if detail is not None:
            row = Logstuff()
            row.ds_id = chart.id
            row.time = dt
            row.detail = detail
            sakuya_db.add(row)

        sakuya_db.commit()

    return util.output('ok')