示例#1
0
def follow(sakuya_db, id):

    try:
        chart = sakuya_db.query(Charts).get(id)
        if chart is None:
            return util.output('error', msg='Chart not found.')

        user = auth.get_user()

        follow = sakuya_db.query(Follows).filter_by(cid=id, follower=user['userid']).first()
        if follow is None:
            follow = Follows()
            follow.cid = id
            follow.follower = user['userid']
            follow.recv_warning = False
            sakuya_db.add(follow)

            if not chart.followers:
                chart.followers = 1
            else:
                chart.followers += 1

            sakuya_db.commit()

        return util.output('ok')

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return util.output('error', msg='订阅失败')
示例#2
0
def follow(sakuya_db, id):

    try:
        chart = sakuya_db.query(Charts).get(id)
        if chart is None:
            return util.output('error', msg='Chart not found.')

        user = auth.get_user()

        follow = sakuya_db.query(Follows).filter_by(
            cid=id, follower=user['userid']).first()
        if follow is None:
            follow = Follows()
            follow.cid = id
            follow.follower = user['userid']
            follow.recv_warning = False
            sakuya_db.add(follow)

            if not chart.followers:
                chart.followers = 1
            else:
                chart.followers += 1

            sakuya_db.commit()

        return util.output('ok')

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return util.output('error', msg='订阅失败')
示例#3
0
def add_chart(cate_id, title, rule_type, host, api):

    if session.query(Charts).filter_by(name=title).count():
        print title, 'exists'
        return

    # make rule
    rule = {
        'datasource':
        'access_log',
        'logging':
        False,
        'filters': [['host', 'equals', False, host],
                    ['request_url', 'regex', False,
                     '.*?%s.*' % api]]
    }
    if rule_type == 'count':
        rule['rule_type'] = 'count'
        rule['field'] = None
    elif rule_type == 'ninety':
        rule['rule_type'] = 'ninety'
        rule['field'] = 'upstream_response_time'
    else:
        assert False

    # make warn
    warn = [["00:00", "08:59", "RANGE", "200", "400", "30", "50", "60", "5"],
            ["09:00", "20:59", "RANGE", "200", "400", "30", "50", "5", "2"],
            ["21:00", "23:59", "RANGE", "200", "400", "30", "50", "60", "5"]]

    # add chart
    row = Charts()
    row.name = title
    row.cate_id = cate_id
    row.owner = 'mingshi'
    row.ext_info = json.dumps({'rule': rule})
    row.alert_enable = 0
    row.createtime = datetime.datetime.now()
    session.add(row)
    session.flush()
    rowid = row.id

    # add follow
    follow = Follows()
    follow.cid = rowid
    follow.follower = 19
    follow.recv_warning = True
    session.add(follow)

    session.commit()

    # storm
    storm.set_rule(rowid, json.dumps(rule))

    print title, 'added'
示例#4
0
def add_chart(cate_id, title, rule_type, host, api):

    if session.query(Charts).filter_by(name=title).count():
        print title, "exists"
        return

    # make rule
    rule = {
        "datasource": "access_log",
        "logging": False,
        "filters": [["host", "equals", False, host], ["request_url", "regex", False, ".*?%s.*" % api]],
    }
    if rule_type == "count":
        rule["rule_type"] = "count"
        rule["field"] = None
    elif rule_type == "ninety":
        rule["rule_type"] = "ninety"
        rule["field"] = "upstream_response_time"
    else:
        assert False

    # make warn
    warn = [
        ["00:00", "08:59", "RANGE", "200", "400", "30", "50", "60", "5"],
        ["09:00", "20:59", "RANGE", "200", "400", "30", "50", "5", "2"],
        ["21:00", "23:59", "RANGE", "200", "400", "30", "50", "60", "5"],
    ]

    # add chart
    row = Charts()
    row.name = title
    row.cate_id = cate_id
    row.owner = "mingshi"
    row.ext_info = json.dumps({"rule": rule})
    row.alert_enable = 0
    row.createtime = datetime.datetime.now()
    session.add(row)
    session.flush()
    rowid = row.id

    # add follow
    follow = Follows()
    follow.cid = rowid
    follow.follower = 19
    follow.recv_warning = True
    session.add(follow)

    session.commit()

    # storm
    storm.set_rule(rowid, json.dumps(rule))

    print title, "added"