Skip to content

Commit

Permalink
完成ip分析的distribution和detail两个子命令
Browse files Browse the repository at this point in the history
  • Loading branch information
ljk committed Mar 12, 2018
1 parent 92bcffd commit c8e4844
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
74 changes: 74 additions & 0 deletions common/show/ip_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,77 @@ def base_summary(ip_type, limit, mongo_col, match, total_dict):
format(one_doc['bytes'] / total_dict['total_bytes'] * 100, '.2f').rjust(7),
format(one_doc['time'] / total_dict['total_time'] * 100, '.2f').rjust(7)))


def distribution(mongo_col, arguments):
"""
展示ip统计按照指定period做group聚合的结果
mongo_col: 本次操作对应的集合名称
arguments: docopt解析用户从log_show界面输入的参数而来的dict
"""
groupby = arguments['--group_by'] if arguments['--group_by'] else 'hour'
group_id = group_by_func(groupby)
ip = arguments['<ip>']
limit = int(arguments['--limit'])

match = match_condition(arguments['--server'], arguments['--from'], arguments['--to'], ip=ip)
pipeline = [match['basic_match'], {'$project': {'requests.ips': 1}}, {'$unwind': '$requests'}, {'$unwind': '$requests.ips'},
{'$match': {'$and': [{'requests.ips.ip': ip}]}},
{'$group': {'_id': group_id, 'hits': {'$sum': '$requests.ips.hits'},
'bytes': {'$sum': '$requests.ips.bytes'}, 'time': {'$sum': '$requests.ips.time'}}}]
if limit:
pipeline.extend([{'$sort': {'hits': -1}}, {'$limit': limit}])
# print('distribution pipeline:\n', pipeline) # debug
mongo_result = mongo_col.aggregate(pipeline)

total_project = {'$project': {'requests.ips': 1}}
total_dict = total_info(mongo_col, match, project=total_project, ip=ip)

# 打印表头
print('{0}\nIP: {1}'.format('=' * 20, ip))
print('Total_hits: {} Total_bytes: {}\n{}'.format(total_dict['total_hits'], get_human_size(total_dict['total_bytes']), '=' * 20))
print('{} {} {} {} {}'.format(groupby.rjust(10), 'hits'.rjust(10), 'hits(%)'.rjust(7), 'bytes'.rjust(10), 'bytes(%)'.rjust(8)))
# 打印结果
for one_doc in mongo_result:
hits = one_doc['hits']
bytes_ = one_doc['bytes']
date = one_doc['_id']
print('{} {} {}% {} {}%'.format(date.rjust(10), str(hits).rjust(10),
format(hits / total_dict['total_hits'] * 100, '.2f').rjust(6), get_human_size(bytes_).rjust(10),
format(bytes_ / total_dict['total_bytes'] * 100, '.2f').rjust(7)))


def detail(mongo_col, arguments):
"""
展示指定ip产生的各uri_abs 的hits/bytes/time情况
mongo_col: 本次操作对应的集合名称
arguments: docopt解析用户从log_show界面输入的参数而来的dict
"""
ip = arguments['<ip>']
limit = int(arguments['--limit'])

match = match_condition(arguments['--server'], arguments['--from'], arguments['--to'], ip=ip)
pipeline = [match['basic_match'], {'$project': {'requests.uri_abs': 1, 'requests.ips': 1}},
{'$unwind': '$requests'}, {'$unwind': '$requests.ips'},
{'$match': {'$and': [{'requests.ips.ip': ip}]}},
{'$group': {'_id': '$requests.uri_abs', 'hits': {'$sum': '$requests.ips.hits'},
'bytes': {'$sum': '$requests.ips.bytes'}, 'time': {'$sum': '$requests.ips.time'}}}]
if limit:
pipeline.extend([{'$sort': {'hits': -1}}, {'$limit': limit}])
# print('detail pipeline:\n', pipeline) # debug
mongo_result = mongo_col.aggregate(pipeline)

total_project = {'$project': {'requests.uri_abs': 1, 'requests.ips': 1}}
total_dict = total_info(mongo_col, match, project=total_project, ip=ip)

# 打印表头
print('{}\nIP: {}'.format('=' * 20, ip))
print('Total_hits: {} Total_bytes: {}\n{}'.format(total_dict['total_hits'], get_human_size(total_dict['total_bytes']), '=' * 20))
print('{} {} {} {} {} uri_abs'.format(
'hits'.rjust(8), 'hits(%)'.rjust(7), 'bytes'.rjust(9), 'bytes(%)'.rjust(8), 'time(%)'.rjust(7)))
# 打印结果
for one_doc in mongo_result:
print('{} {}% {} {}% {}% {}'.format(
str(one_doc['hits']).rjust(8), format(one_doc['hits'] / total_dict['total_hits'] * 100, '.2f').rjust(6),
get_human_size(one_doc['bytes']).rjust(9),
format(one_doc['bytes'] / total_dict['total_bytes'] * 100, '.2f').rjust(7),
format(one_doc['time'] / total_dict['total_time'] * 100, '.2f').rjust(6), one_doc['_id']))
4 changes: 2 additions & 2 deletions log_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
if arguments['distribution'] and arguments['request']:
base_show.distribution(arguments['<request>'], arguments['--group_by'], int(arguments['--limit']), mongo_col, arguments)
elif arguments['distribution'] and arguments['ip']:
print('To be implement...')
ip_show.distribution(mongo_col, arguments)
elif arguments['distribution'] and arguments['error']:
print('To be implement...')

elif arguments['detail'] and arguments['request']:
base_show.detail(arguments['<uri>'], int(arguments['--limit']), mongo_col, arguments)
elif arguments['detail'] and arguments['ip']:
print('To be implement...')
ip_show.detail(mongo_col, arguments)
elif arguments['detail'] and arguments['error']:
print('To be implement...')

Expand Down

0 comments on commit c8e4844

Please sign in to comment.