<commit_before>from datetime import datetime, date, timedelta
from dimagi.utils.web import render_to_response
from django.http import HttpResponse
from dimagi.utils.couch.database import get_db
import logging
import itertools
import re
from corehq.apps.domain.decorators import login_and_domain_required

GAP_THRESHOLD = 3 #minutes

def device_list(db, domain):
    device_times = db.view('phonelog/device_log_first_last', group=True, startkey=[domain], endkey=[domain, {}])
    device_users = db.view('phonelog/device_log_users', group=True, startkey=[domain], endkey=[domain, {}])

    dev_users = {}
    for row in device_users:
        dev_id = row['key'][1]
        user = row['key'][2]
        if dev_id not in dev_users:
            dev_users[dev_id] = set()
        dev_users[dev_id].add(user)

    devices = []
    for row in device_times:
        dev = {
            'id': row['key'][1],
            'first': parse_isodate(row['value'][0]),
            'last': parse_isodate(row['value'][1]),
        }
        try:
            dev['users'] = dev_users[dev['id']]
        except KeyError:
            dev['users'] = set()
        devices.append(dev)
    return devices

def overview_list(db, domain):
    devices = device_list(db, domain)
    entries = []
    for dev in devices:
        users = dev['users'] if dev['users'] else set([None])
        for u in users:
            entry = {
                'user': u,
                'device': dev['id'],
                'first': dev['first'],
                'last': dev['last'],
                'other_users': sorted(list(users - set([u]))),
#                'overlaps_with': set(),
            }
            entries.append(entry)

    entries.sort(key=lambda e: (0, e['user'].upper(), e['first']) if e['user'] else (1, e['first']))

#    for u in set(e['user'] for e in entries):
#        user_entries = filter(lambda e: e['user'] == u, entries)
#        for a, b in itertools.combinations(user_entries, 2):
#            pass

    return entries

@login_and_domain_required
def devices(request, domain):
    entries = overview_list(get_db(), domain)
    return render_to_response(request, 'phonelog/devicelist.html', {'entries': entries, 'domain': domain})

@login_and_domain_required
def device_log(request, device, domain):
    db = get_db()

    try:
        limit = int(request.GET.get('limit'))
    except:
        limit = 1000

    try:
        skip = int(request.GET.get('skip'))
    except:
        skip = 0<commit_msg>domain must be 2nd view param<commit_after>from datetime import datetime, date, timedelta
from dimagi.utils.web import render_to_response
from django.http import HttpResponse
from dimagi.utils.couch.database import get_db
import logging
import itertools
import re
from corehq.apps.domain.decorators import login_and_domain_required

GAP_THRESHOLD = 3 #minutes

def device_list(db, domain):
    device_times = db.view('phonelog/device_log_first_last', group=True, startkey=[domain], endkey=[domain, {}])
    device_users = db.view('phonelog/device_log_users', group=True, startkey=[domain], endkey=[domain, {}])

    dev_users = {}
    for row in device_users:
        dev_id = row['key'][1]
        user = row['key'][2]
        if dev_id not in dev_users:
            dev_users[dev_id] = set()
        dev_users[dev_id].add(user)

    devices = []
    for row in device_times:
        dev = {
            'id': row['key'][1],
            'first': parse_isodate(row['value'][0]),
            'last': parse_isodate(row['value'][1]),
        }
        try:
            dev['users'] = dev_users[dev['id']]
        except KeyError:
            dev['users'] = set()
        devices.append(dev)
    return devices

def overview_list(db, domain):
    devices = device_list(db, domain)
    entries = []
    for dev in devices:
        users = dev['users'] if dev['users'] else set([None])
        for u in users:
            entry = {
                'user': u,
                'device': dev['id'],
                'first': dev['first'],
                'last': dev['last'],
                'other_users': sorted(list(users - set([u]))),
#                'overlaps_with': set(),
            }
            entries.append(entry)

    entries.sort(key=lambda e: (0, e['user'].upper(), e['first']) if e['user'] else (1, e['first']))

#    for u in set(e['user'] for e in entries):
#        user_entries = filter(lambda e: e['user'] == u, entries)
#        for a, b in itertools.combinations(user_entries, 2):
#            pass

    return entries

@login_and_domain_required
def devices(request, domain):
    entries = overview_list(get_db(), domain)
    return render_to_response(request, 'phonelog/devicelist.html', {'entries': entries, 'domain': domain})

@login_and_domain_required
def device_log(request, domain, device):
    db = get_db()

    try:
        limit = int(request.GET.get('limit'))
    except:
        limit = 1000

    try:
        skip = int(request.GET.get('skip'))
    except:
        skip = 0
{'fraction_non_alphanumeric': 0.14148296593186374, 'fraction_numerical': 0.005811623246492986, 'mean_word_length': 2.6166666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5477857', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1552, 'n_words': 374}