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='订阅失败')
def unfollow(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() row = sakuya_db.query(Follows).filter_by( cid=id, follower=user['userid']).first() if row is not None: sakuya_db.delete(row) if chart.followers: chart.followers -= 1 sakuya_db.commit() return util.output('ok') except Exception: traceback.print_exc() sakuya_db.rollback() return util.output('error', msg='取消失败')
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='订阅失败')
def haopan(sakuya_db): try: dt = datetime.datetime.strptime(request.forms['time'], '%Y-%m-%d %H:%M') data = json.loads(request.forms['data']) if not isinstance(data[0], list) or len(data[0]) < 6: raise ValueError except Exception: return util.output('error', msg='Invalid parameters.') dtstr = dt.strftime('%Y-%m-%d %H:%M') with open(get_config('webapp')['hp_raw_data'], 'a') as f: f.write('time: %s\n' % dtstr) f.write('data:\n') for item in data: f.write('%s\n' % str(item)) f.write('\n') socket.send(msgpack.packb({ 'time': dtstr, 'data': data })) return util.output('ok', msg='%s, %d items received.' % (dtstr, len(data)))
def solr_delete(sakuya_db, id): row = sakuya_db.query(Charts).filter_by(id=id, cate_id=SOLR_CATE_ID).first() if row is None: return util.output('error', msg='Chart not found.') sakuya_db.delete(row) sakuya_db.commit() storm.delete_rule(id) return util.output('ok')
def solr_add(sakuya_db): # parse parameters try: title = request.forms['title'] if not title: raise ValueError rule_type = request.forms['type'] if rule_type not in ('count', 'ninety'): raise ValueError filters = [] datasource = storm.datasources['access_log'] for filter in json.loads(request.forms['filters']): if filter[0] not in datasource: raise ValueError field_type = datasource[filter[0]][0] if filter[1] not in storm.operators[field_type][0]: raise ValueError if not filter[2]: raise ValueError if field_type == 'string': if filter[1] == 'in': if not isinstance(filter[2], list): raise ValueError filter[2] = [str(i) for i in filter[2]] else: filter[2] = str(filter[2]) elif field_type == 'numeric': if filter[1] in ('in', 'nin'): if not isinstance(filter[2], list): raise ValueError tmp_filter2 = [] for i in filter[2]: i = float(i) if i.is_integer(): i = int(i) tmp_filter2.append(i) filter[2] = tmp_filter2 else: filter[2] = float(filter[2]) if filter[2].is_integer(): filter[2] = int(filter[2]) else: assert False filters.append(filter[:3]) if not filters: raise ValueError except Exception, e: return util.output('error', msg='Invalid parameters.')
def edit_category(sakuya_db, id): if not request.forms.name: return util.output('error', msg='Invalid parameter.') try: row = sakuya_db.query(Categories).get(id) if row is None: return util.output('error', msg='Category not found.') row.name = request.forms.name sakuya_db.commit() return util.output('ok') except Exception: traceback.print_exc() sakuya_db.rollback() return util.output('error', msg='Fail to edit category.')
def recv_warning(sakuya_db): try: chart_id = int(request.params['chart_id']) recv_rules = request.params['recv_rules'] except Exception, e: return util.output('error', msg='参数不正确')
def suite_data(sakuya_db): try: body = json.loads(request.body.getvalue()) category, data, dt = body['category'], body['data'], body['datetime'] if len(category) < 3 or not data: raise ValueError dt = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M') except Exception, e: return util.output('error', msg='Invalid parameters.')
def hide(sakuya_db, id): try: if auth.is_role('admin') is False: return util.output('error', msg='Role Error.') chart = sakuya_db.query(Charts).get(id) if chart is None: return util.output('error', msg='Chart not found.') status = int(request.params['status']) chart.status = status sakuya_db.commit() return util.output('ok') except Exception: traceback.print_exc() sakuya_db.rollback() return util.output('error', msg='隐藏失败')
def haopan_charts(sakuya_db): try: prod = int(request.params['prod']) cities = [int(i) for i in request.params['cities'].split(',')] channels = [int(i) for i in request.params['channels'].split(',')] entries = [int(i) for i in request.params['entries'].split(',')] charts = [int(i) for i in request.params['charts'].split(',')] except Exception: return util.output('error', msg='Invalid parameters.') d_charts = haopan.get_charts() chart_list = [] for k, v in d_charts.iteritems(): if k[0] == prod\ and k[1] in charts\ and k[2] in cities\ and k[3] in channels\ and k[4] in entries: titles = [] if k[2]: titles.append(haopan.D_CITIES[k[2]]) if k[3]: titles.append(haopan.D_CHANNELS[k[3]]) if k[4]: titles.append(haopan.D_ENTRIES[k[4]]) titles.append(haopan.D_CHARTS_DISPLAY[k[1]]) if k[1] == 2: k_ = list(k); k_[1] = 3; k_ = tuple(k_) if k_ in d_charts: chart_list.append((' - '.join(titles), '%d/%d' % (v, d_charts[k_]))) else: chart_list.append((' - '.join(titles), v)) else: chart_list.append((' - '.join(titles), v)) return util.output('ok', charts=chart_list)
def haopan(sakuya_db): try: dt = datetime.datetime.strptime(request.forms['time'], '%Y-%m-%d %H:%M') data = json.loads(request.forms['data']) if not isinstance(data[0], list) or len(data[0]) < 6: raise ValueError except Exception: return util.output('error', msg='Invalid parameters.') dtstr = dt.strftime('%Y-%m-%d %H:%M') with open(get_config('webapp')['hp_raw_data'], 'a') as f: f.write('time: %s\n' % dtstr) f.write('data:\n') for item in data: f.write('%s\n' % str(item)) f.write('\n') socket.send(msgpack.packb({'time': dtstr, 'data': data})) return util.output('ok', msg='%s, %d items received.' % (dtstr, len(data)))
def internal_delete(sakuya_db): try: check_api_key() id = int(request.forms['id']) chart = sakuya_db.query(Charts).get(id) if chart is None: raise ValueError('Chart not found.') if chart.cate_id not in IAPI_CATE_ID: raise ValueError('Authentication failed.') except KeyError, e: return util.output('error', msg='Missing parameter %s.' % e)
def unfollow(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() row = sakuya_db.query(Follows).filter_by(cid=id, follower=user['userid']).first() if row is not None: sakuya_db.delete(row) if chart.followers: chart.followers -= 1 sakuya_db.commit() return util.output('ok') except Exception: traceback.print_exc() sakuya_db.rollback() return util.output('error', msg='取消失败')
def internal_add(sakuya_db): try: check_api_key() name = request.forms['name'] if not name: raise ValueError('Name cannot be empty.') cate_id = int(request.forms['cate_id']) if cate_id not in IAPI_CATE_ID: raise ValueError('Invalid cate_id') except KeyError, e: return util.output('error', msg='Missing parameter %s.' % e)
def chart_data(sakuya_db): try: id_list = [int(i) for i in request.params['id_list'].split(',')] delta_list = [int(i) for i in request.params['delta_list'].split(',')] if len(id_list) != len(delta_list): raise ValueError dt_start = datetime.datetime.strptime(request.params['start'], '%Y-%m-%d %H:%M') dt_end = datetime.datetime.strptime(request.params['end'], '%Y-%m-%d %H:%M') if dt_end - dt_start < datetime.timedelta(minutes=5): dt_end = dt_start + datetime.timedelta(minutes=5) daily = request.params.get('daily') == 'true' if daily and dt_end - dt_start < datetime.timedelta(days=7): dt_end = dt_start + datetime.timedelta(days=7) except Exception, e: return util.output('error', msg='参数不正确')
def add_data(sakuya_db): try: tid = int(request.forms['tid']) chart = sakuya_db.query(Charts).get(tid) if chart is None: raise ValueError dt = datetime.datetime.strptime(request.forms['dt'], '%Y-%m-%d %H:%M') data = request.forms.get('data', None) detail = request.forms.get('detail', None) if data is None and detail is None: raise ValueError if data is not None: data = int(data) if detail is not None and len(detail) == 0: detail = None except Exception, e: return util.output('error', msg='Invalid params.')
@app.get('/chart/recv_warning') @auth.login def recv_warning(sakuya_db): try: chart_id = int(request.params['chart_id']) recv_rules = request.params['recv_rules'] except Exception, e: return util.output('error', msg='参数不正确') user = auth.get_user() row = sakuya_db.query(Follows).filter_by(cid=chart_id, follower=user['userid']).first() if row is None: return util.output('error', msg='没有订阅图表') if recv_rules == 'all': row.recv_warning = True row.recv_rules = 'all' else: tmp = [] for i in recv_rules.split(','): try: tmp.append(int(i)) except Exception: pass row.recv_warning = bool(tmp) row.recv_rules = ','.join(str(i) for i in tmp) sakuya_db.commit()
row.data = data sakuya_db.add(row) if detail is not None: row = LogstuffTmpl.gen(dt.strftime('%Y%m%d'))() row.ds_id = tid row.time = dt row.detail = detail sakuya_db.add(row) sakuya_db.commit() except Exception, e: sakuya_db.rollback() traceback.print_exc() return util.output('error', msg='Internal error.') try: chart.api_ip = util.ip2long(request['REMOTE_ADDR']) chart.api_ts = util.timestamp() sakuya_db.commit() except Exception, e: sakuya_db.rollback() return util.output('ok') socket = zmq.Socket(zmq.Context.instance(), zmq.PUSH) socket.connect(get_config('webapp')['hp_aggregate_connect'])
row.data = data sakuya_db.add(row) if detail is not None: row = LogstuffTmpl.gen(dt.strftime('%Y%m%d'))() row.ds_id = tid row.time = dt row.detail = detail sakuya_db.add(row) sakuya_db.commit() except Exception, e: sakuya_db.rollback() traceback.print_exc() return util.output('error', msg='Internal error.') try: chart.api_ip = util.ip2long(request['REMOTE_ADDR']) chart.api_ts = util.timestamp() sakuya_db.commit() except Exception, e: sakuya_db.rollback() return util.output('ok') socket = zmq.Socket(zmq.Context.instance(), zmq.PUSH) socket.connect(get_config('webapp')['hp_aggregate_connect']) @app.post('/api/haopan')