Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User stats #303

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
eb18ddd
initial work on stats implementation
tayoogunbiyi Aug 4, 2020
a0b2ec5
renamed stats test file
tayoogunbiyi Aug 5, 2020
101649d
refactored stats class
tayoogunbiyi Aug 5, 2020
70ad6ef
added args processing to main script flow
tayoogunbiyi Aug 5, 2020
d374b5a
ensured testing stats don't get mixed with main stats
tayoogunbiyi Aug 5, 2020
11f71c6
expanded time management to store both current date and hour of day w…
tayoogunbiyi Aug 6, 2020
f4ee9ed
added functionality to keep track of search engine
tayoogunbiyi Aug 6, 2020
d2ad290
refactored hour, words, queries tracking to use collections.counter
tayoogunbiyi Aug 6, 2020
29bbe6d
added functionality to keep track of links discovered
tayoogunbiyi Aug 7, 2020
85aa818
added logic and tests for keeping track of cache hits and misses
tayoogunbiyi Aug 7, 2020
5172403
added call to record cache hti
tayoogunbiyi Aug 7, 2020
33e931e
added way to keep track of success and failure results
tayoogunbiyi Aug 7, 2020
1dfced0
renamed valid constant to sucess
tayoogunbiyi Aug 7, 2020
a577a90
added new termgraph dependency
tayoogunbiyi Aug 10, 2020
f28a6b9
completed rough implementation of the user stats rendering using term…
tayoogunbiyi Aug 12, 2020
aae6680
added way to get top_N statistics
tayoogunbiyi Aug 12, 2020
cf4f50b
refactored stats reporting into it's own class
tayoogunbiyi Aug 13, 2020
4003abd
began writing tests for stats reporter
tayoogunbiyi Aug 13, 2020
7ad4d23
added more test for statistics
tayoogunbiyi Aug 17, 2020
5e79d8f
moved util func into utils.py
tayoogunbiyi Aug 17, 2020
58e9ac7
replaced divisions to use safe_divide
tayoogunbiyi Aug 17, 2020
131e8b2
refactored stats.py to smaller functions
tayoogunbiyi Aug 17, 2020
0f002e4
changed queries to make sure the new tests use cached results
tayoogunbiyi Aug 17, 2020
c2b72a3
added more tests for the stats
tayoogunbiyi Aug 17, 2020
ffa9da6
added flag for using stats from the cli
tayoogunbiyi Aug 18, 2020
2475706
improved py2.7 support
tayoogunbiyi Aug 18, 2020
9c7f399
cleaned up variable and function names, removed unused comments
tayoogunbiyi Aug 18, 2020
8577c03
Merge branch 'master' into user-stats
tayoogunbiyi Aug 18, 2020
1c88947
fixed py3.5 support for termgraph
tayoogunbiyi Aug 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# .coveragerc to control coverage.py
[run]
omit =
env/*
test_*

34 changes: 28 additions & 6 deletions howdoi/howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from requests.exceptions import ConnectionError
from requests.exceptions import SSLError

from .stats import Stats

# Handle imports for Python 2 and 3
if sys.version < '3':
Expand Down Expand Up @@ -124,6 +125,14 @@ def _print_dbg(x): print("[DEBUG] " + x) # noqa: E302
else:
cache = FileSystemCache(CACHE_DIR, CACHE_ENTRY_MAX, default_timeout=0)

DEFAULT_STORE_DIR = appdirs.user_cache_dir('howdoi-stats')

if os.getenv('HOWDOI_DISABLE_STATS_COLLECTIONS'):
stats_cache = NullCache()
else:
stats_cache = FileSystemCache(DEFAULT_STORE_DIR, default_timeout=0)

stats_obj = Stats(stats_cache)
howdoi_session = requests.session()


Expand Down Expand Up @@ -353,6 +362,7 @@ def _get_links_with_cache(query):

question_links = _get_questions(links)
cache.set(cache_key, question_links or CACHE_EMPTY_VAL)
stats_obj.process_discovered_links(question_links)

return question_links

Expand Down Expand Up @@ -447,7 +457,7 @@ def _get_cache_key(args):
return str(args) + __version__


def format_stash_item(fields, index = -1):
def format_stash_item(fields, index=-1):
title = fields['alias']
description = fields['desc']
item_num = index + 1
Expand All @@ -467,12 +477,13 @@ def format_stash_item(fields, index = -1):
description=description)


def print_stash(stash_list = []):
def print_stash(stash_list=[]):
if len(stash_list) == 0:
stash_list = ['\nSTASH LIST:']
commands = keep_utils.read_commands()
if commands is None or len(commands.items()) == 0:
print('No commands found in stash. Add a command with "howdoi --{stash_save} <query>".'.format(stash_save=STASH_SAVE))
print(
'No commands found in stash. Add a command with "howdoi --{stash_save} <query>".'.format(stash_save=STASH_SAVE))
return
for cmd, fields in commands.items():
stash_list.append(format_stash_item(fields))
Expand All @@ -483,7 +494,7 @@ def print_stash(stash_list = []):

def _get_stash_key(args):
stash_args = {}
ignore_keys = [STASH_SAVE, STASH_VIEW, STASH_REMOVE, STASH_EMPTY, 'tags'] # ignore these for stash key.
ignore_keys = [STASH_SAVE, STASH_VIEW, STASH_REMOVE, STASH_EMPTY, 'tags'] # ignore these for stash key.
for key in args:
if not (key in ignore_keys):
stash_args[key] = args[key]
Expand Down Expand Up @@ -543,9 +554,13 @@ def howdoi(raw_query):
if _is_help_query(args['query']):
return _get_help_instructions() + '\n'

stats_obj.process_args(args)

res = cache.get(cache_key)

if res:
stats_obj.record_cache_hit()
stats_obj.process_response(res)
return _parse_cmd(args, res)

try:
Expand All @@ -557,6 +572,7 @@ def howdoi(raw_query):
res = {'error': 'Unable to reach {search_engine}. Do you need to use a proxy?\n'.format(
search_engine=args['search_engine'])}

stats_obj.process_response(res)
return _parse_cmd(args, res)


Expand Down Expand Up @@ -584,10 +600,11 @@ def get_parser():
action='store_true'),
parser.add_argument('--empty', help='empty your stash',
action='store_true')
parser.add_argument('--stats', help='view your howdoi usage statistics', action='store_true')
return parser


def prompt_stash_remove(args, stash_list, view_stash = True):
def prompt_stash_remove(args, stash_list, view_stash=True):
if view_stash:
print_stash(stash_list)

Expand Down Expand Up @@ -642,12 +659,17 @@ def command_line_runner():
if args[STASH_REMOVE] and len(args['query']) == 0:
commands = keep_utils.read_commands()
if commands is None or len(commands.items()) == 0:
print('No commands found in stash. Add a command with "howdoi --{stash_save} <query>".'.format(stash_save=STASH_SAVE))
print(
'No commands found in stash. Add a command with "howdoi --{stash_save} <query>".'.format(stash_save=STASH_SAVE))
return
stash_list = [{'command': cmd, 'fields': field} for cmd, field in commands.items()]
prompt_stash_remove(args, stash_list)
return

if args['stats']:
stats_obj.render_stats()
return

if not args['query']:
parser.print_help()
return
Expand Down
Loading