Example #1
0
def filter_sessions(state, mac_to_name, macs, allsessions=True, no_headers=False):
    """
    Show (all|latest) sessions for the clients in macs[]
    """
    data = []
    for mac in state['macs']:
        if mac in macs:
            try:
                name = mac_to_name[mac]
            except KeyError:
                name = '-' # (unknown)
            if allsessions:
                for info in state['macs'][mac]:
                    if info['session_end'] == None:
                        info['session_end'] = ''
                    data.append([mac, info['ip'], name, info['session_start'], info['session_end']])
            else:
                info = state['macs'][mac][-1]
                if info['session_end'] == None:
                    info['session_end'] = ''
                data.append([mac, info['ip'], name, info['session_start'], info['session_end']])

    if no_headers:
        return printutil.to_smart_columns(data)
    else:
        headers = ['MAC', 'IP', 'name', 'session start', 'session end']
        return printutil.to_smart_columns(data, headers)
Example #2
0
def current_sessions(macfile):
    """
    Show currently open sessions
    """
    state, mac_to_name = read_state(macfile)

    data = []
    for mac in state['macs']:
        try:
            name = mac_to_name[mac]
        except KeyError:
            name = '-' # (unknown)
        info = state['macs'][mac][-1]
        if info['session_end'] == None:
            data.append([mac, info['ip'], name, info['session_start']])

    headers = ['MAC', 'IP', 'name', 'session start']
    print(printutil.to_smart_columns(data, headers))
Example #3
0
def showstats():
    """
    Show statistics about the collection
    """
    # Size of progress-bar
    COLUMNS = 40
    result = []

    session = get_db_connection()
    #session.query(Article).
    #items = session.query(extract('year', Article.time_read).label('year')).distinct().subquery()
    #items = session.query(extract('year', Article.time_read).label('year'), func.count(Article.id)).distinct().order_by('year')

    # Numbers
    items_total = nr_total(session)
    items_read = nr_read(session)
    items_unread = nr_unread(session)
    items_favourited = nr_favourited(session)
    items_deleted = nr_deleted(session)

    result.append(['Total items', str(items_total)])
    result.append(['Total read', str(items_read)])
    result.append(['Total unread', str(items_unread)])
    result.append(['Total favourited', str(items_favourited)])
    result.append(['Total deleted', str(items_deleted)])
    result.append([])

    # Progress bar
    result.append(['progress', printutil.progress_bar(items_total, items_read, COLUMNS, '.', '#', True)])
    result.append(['favourites', printutil.progress_bar(items_total, items_favourited, COLUMNS, ' ', '*')])

    result.append([])

    # Read articles per yer
    result.append(['year', 'amount of articles read'])
    items = session.query(extract('year', Article.time_read).label('year'), func.count(Article.id)).group_by('year')
    for item in items:
        if item[0] is None:
            result.append(['unknown', str(item[1])])
        elif item[0] == 1970:
            result.append(['unread', str(item[1])])
        else:
            result.append([str(item[0]), str(item[1])])

    result.append([])
    print(printutil.to_smart_columns(result))

    # List of number of items read, per date
    items_read = session.query(func.date(Article.time_read).label('thedate'), func.count(Article.id)).group_by('thedate')
    # firstseen_time_updated comes as close to 'time added' as we can get from the Pocket API
    items_added = session.query(func.date(Article.firstseen_time_updated).label('thedate'), func.count(Article.id)).group_by('thedate')
    #for item in items_added:
    #    print item
    # TODO: plot added-vs-read graph
    items_added_per_month = session.query(extract('year', Article.firstseen_time_updated).label('year'), extract('month', Article.firstseen_time_updated).label('month'), func.count(Article.id)).group_by('year', 'month')
    items_read_per_month = session.query(extract('year', Article.time_read).label('year'), extract('month', Article.time_read).label('month'), func.count(Article.id)).group_by('year', 'month')
    read_vs_added = printutil.x_vs_y(items_read_per_month, items_added_per_month, filter_none=True)
    #for item in read_vs_added:
    #    result.append(item)
    print(read_vs_added)

    # Tags
    # TODO: loop over Articles, get amount of articles/tag

    # Finally, print the stats
    #print(printutil.to_smart_columns(result))
    return

    items = session.query(extract('year', Article.time_read).label('year')).distinct()
    for item in items:
        print(item)

    per_date = session.query(func.count(Article.id), extract('date', Article.time_read).label('h')).group_by('h')
    for item in per_date:
        print(item)
    per_hour = session.query(extract('hour', Article.time_read).label('h')).group_by('h')
    for item in per_hour:
        print(item)
Example #4
0
def showstats():
    """
    Show statistics about the collection
    """
    # Size of progress-bar
    COLUMNS = 40
    result = []

    session = get_db_connection()
    #session.query(Article).
    #items = session.query(extract('year', Article.time_read).label('year')).distinct().subquery()
    #items = session.query(extract('year', Article.time_read).label('year'), func.count(Article.id)).distinct().order_by('year')

    # Numbers
    items_total = nr_total(session)
    items_read = nr_read(session)
    items_unread = nr_unread(session)
    items_favourited = nr_favourited(session)
    items_deleted = nr_deleted(session)

    result.append(['Total items', str(items_total)])
    result.append(['Total read', str(items_read)])
    result.append(['Total unread', str(items_unread)])
    result.append(['Total favourited', str(items_favourited)])
    result.append(['Total deleted', str(items_deleted)])
    result.append([])

    # Progress bar
    result.append(['progress', printutil.progress_bar(items_total, items_read, COLUMNS, '.', '#', True)])
    result.append(['favourites', printutil.progress_bar(items_total, items_favourited, COLUMNS, ' ', '*')])

    result.append([])

    # Read articles per yer
    result.append(['year', 'amount of articles read'])
    items = session.query(extract('year', Article.time_read).label('year'), func.count(Article.id)).group_by('year')
    for item in items:
        if item[0] is None:
            result.append(['unknown', str(item[1])])
        elif item[0] == 1970:
            result.append(['unread', str(item[1])])
        else:
            result.append([str(item[0]), str(item[1])])

    result.append([])
    print(printutil.to_smart_columns(result))

    # List of number of items read, per date
    items_read = session.query(func.date(Article.time_read).label('thedate'), func.count(Article.id)).group_by('thedate')
    # firstseen_time_updated comes as close to 'time added' as we can get from the Pocket API
    items_added = session.query(func.date(Article.firstseen_time_updated).label('thedate'), func.count(Article.id)).group_by('thedate')
    #for item in items_added:
    #    print item
    # TODO: plot added-vs-read graph
    items_added_per_month = session.query(extract('year', Article.firstseen_time_updated).label('year'), extract('month', Article.firstseen_time_updated).label('month'), func.count(Article.id)).group_by('year', 'month')
    items_read_per_month = session.query(extract('year', Article.time_read).label('year'), extract('month', Article.time_read).label('month'), func.count(Article.id)).group_by('year', 'month')
    read_vs_added = printutil.x_vs_y(items_read_per_month, items_added_per_month, filter_none=True)
    #for item in read_vs_added:
    #    result.append(item)
    print read_vs_added

    # Tags
    # TODO: loop over Articles, get amount of articles/tag

    # Finally, print the stats
    #print(printutil.to_smart_columns(result))
    return

    items = session.query(extract('year', Article.time_read).label('year')).distinct()
    for item in items:
        print item

    per_date = session.query(func.count(Article.id), extract('date', Article.time_read).label('h')).group_by('h')
    for item in per_date:
        print item
    per_hour = session.query(extract('hour', Article.time_read).label('h')).group_by('h')
    for item in per_hour:
        print item