Example #1
0
def _user_count(db, username=None):
    params = Params()
    if username:
        params.users = username.split(",")

    if not params.users:
        return

    res = query._count(db, params, ['class.level'], ['class.level'])
    ret = {1: 0, 2: 0, 3: 0}
    for (l, c) in res:
        ret[l] = c

    return ret
Example #2
0
def matrix(db, lang):
    params = Params(default_limit=None)
    errors_groups = query._count(db, params, [
        "markers.item", "markers.class", "sources.country", "items.menu->'en'"
    ])
    analysers = defaultdict(lambda: defaultdict(int))
    analysers_sum = defaultdict(int)
    countries_sum = defaultdict(int)
    total = 0
    for row in errors_groups:
        item, class_, country, menu, count = row
        analyser = '{}/{} {}'.format(item, class_, menu)
        analysers[analyser][country] += count
        analysers_sum[analyser] += count
        countries_sum[country] += count
        total += count

    return dict(total=total,
                countries_sum=countries_sum,
                analysers_sum=analysers_sum,
                analysers=analysers)
def index(db, lang, format=None):
    if "false-positive" in request.path:
        title = _("False positives")
        gen = "false-positive"
    elif "done" in request.path:
        title = _("Fixed issues")
        gen = "done"
    else:
        title = _("Informations")
        gen = "error"

    if not format in ('rss', 'gpx', 'kml', 'josm', 'csv'):
        format = None

    countries = query_meta._countries(db, lang) if format == None else None
    items = query_meta._items(db, lang)

    params = Params()
    params.status = {
        "error": "open",
        "false-positive": "false",
        "done": "done"
    }[gen]
    params.limit = None
    params.fixable = None

    if format == None and params.item:
        errors_groups = query._count(
            db,
            params,
            [
                "dynpoi_class.item", "marker.source", "marker.class",
                "source.country", "source.analyser",
                "dynpoi_update_last.timestamp"
            ],
            ["dynpoi_item", "class"],
            [
                "min(dynpoi_item.menu::text)::jsonb AS menu",
                "min(class.title::text)::jsonb AS title"
            ],
        )

        total = 0
        for res in errors_groups:
            if res["count"] != -1:
                total += res["count"]
    else:
        errors_groups = []
        total = 0

    params.limit = request.params.get('limit', type=int, default=100)
    if params.limit > 10000:
        params.limit = 10000

    if (total > 0 and total < 1000) or params.limit:
        params.full = True
        errors = query._gets(db, params)
        if gen in ("false-positive", "done"):
            opt_date = "date"
        else:
            opt_date = "-1"
    else:
        opt_date = None
        errors = None

    if format == 'rss':
        response.content_type = 'application/rss+xml'
        tpl = 'errors/list.rss'
    elif format == 'gpx':
        response.content_type = 'application/gpx+xml'
        tpl = 'errors/list.gpx'
    elif format == 'kml':
        response.content_type = 'application/vnd.google-earth.kml+xml'
        tpl = 'errors/list.kml'
    elif format == 'josm':
        objects = set(
            sum(
                map(
                    lambda error: list(
                        map(
                            lambda elem: elem['type'].lower() + str(elem[
                                'id']), error['elems'] or [])), errors), []))
        response.status = 302
        response.set_header(
            'Location',
            'http://localhost:8111/load_object?objects=%s' % ','.join(objects))
        return
    elif format == 'csv':
        output = io.StringIO()
        writer = csv.writer(output)
        h = [
            'uuid', 'source', 'item', 'class', 'level', 'title', 'subtitle',
            'country', 'analyser', 'timestamp', 'username', 'lat', 'lon',
            'elems'
        ]
        writer.writerow(h)
        for res in errors:
            usernames = list(
                map(lambda elem: elem.get("username", ""), res['elems'] or []))
            elems = '_'.join(
                map(
                    lambda elem: {
                        'N': 'node',
                        'W': 'way',
                        'R': 'relation'
                    }[elem['type']] + str(elem['id']), res['elems'] or []))
            writer.writerow(
                list(
                    map(
                        lambda a: usernames if a == 'username' else elems
                        if a == 'elems' else res[a], h)))
        response.content_type = 'text/csv'
        return output.getvalue()
    else:
        tpl = 'errors/index'

    return template(tpl,
                    countries=countries,
                    items=items,
                    errors_groups=errors_groups,
                    total=total,
                    errors=errors,
                    query=request.query_string,
                    country=params.country,
                    item=params.item,
                    level=params.level,
                    lang=lang[0],
                    translate=translator(lang),
                    gen=gen,
                    opt_date=opt_date,
                    title=title,
                    website=utils.website,
                    main_website=utils.main_website,
                    remote_url_read=utils.remote_url_read)
Example #4
0
def index(db, lang, format):
    if "false-positive" in request.path:
        title = _("False positives")
        gen = "false-positive"
    elif "done" in request.path:
        title = _("Fixed issues")
        gen = "done"
    else:
        title = _("Information")
        gen = "error"

    params = Params()
    params.status = {
        "error": "open",
        "false-positive": "false",
        "done": "done"
    }[gen]
    params.fixable = None

    items = query_meta._items_menu(db, lang)
    for res in items:
        if params.item == str(res["item"]):
            title += ' - ' + res['menu']['auto']

    params.limit = request.params.get('limit', type=int, default=100)
    if params.limit > 10000:
        params.limit = 10000

    params.full = True
    errors = query._gets(db, params)
    for error in errors:
        error["subtitle"] = i10n_select_auto(error["subtitle"], lang)
        error["title"] = i10n_select_auto(error["title"], lang)
        error["menu"] = i10n_select_auto(error["menu"], lang)

    if format == 'rss':
        response.content_type = 'application/rss+xml'
        tpl = 'errors/list.rss'
    elif format == 'gpx':
        response.content_type = 'application/gpx+xml'
        tpl = 'errors/list.gpx'
    elif format == 'kml':
        response.content_type = 'application/vnd.google-earth.kml+xml'
        tpl = 'errors/list.kml'
    elif format == 'josm':
        objects = set(
            sum(
                map(
                    lambda error: list(
                        map(
                            lambda elem: elem['type'].lower() + str(elem[
                                'id']), error['elems'] or [])), errors), []))
        response.status = 302
        response.set_header(
            'Location',
            'http://localhost:8111/load_object?objects=%s' % ','.join(objects))
        return
    elif format == 'csv':
        output = io.StringIO()
        writer = csv.writer(output)
        h = [
            'uuid', 'source', 'item', 'class', 'level', 'title', 'subtitle',
            'country', 'analyser', 'timestamp', 'username', 'lat', 'lon',
            'elems'
        ]
        hh = {'source': 'source_id'}
        writer.writerow(h)
        for res in errors:
            usernames = list(
                map(lambda elem: elem.get("username", ""), res['elems'] or []))
            elems = '_'.join(
                map(
                    lambda elem: {
                        'N': 'node',
                        'W': 'way',
                        'R': 'relation'
                    }[elem['type']] + str(elem['id']), res['elems'] or []))
            writer.writerow(
                list(
                    map(
                        lambda a: usernames if a == 'username' else elems
                        if a == 'elems' else res[a],
                        map(lambda y: hh.get(y, y), h))))
        response.content_type = 'text/csv'
        return output.getvalue()
    else:
        countries = query_meta._countries(db)
        items = list(map(dict, items))

        if params.item:
            params.limit = None
            errors_groups = query._count(
                db,
                params,
                [
                    "markers_counts.item", "markers.source_id",
                    "markers.class", "sources.country", "sources.analyser",
                    "updates_last.timestamp"
                ],
                ["items", "class"],
                [
                    "min(items.menu::text)::jsonb AS menu",
                    "min(class.title::text)::jsonb AS title"
                ],
            )

            total = 0
            for res in errors_groups:
                res["title"] = i10n_select_auto(res["title"], lang)
                res["menu"] = i10n_select_auto(res["menu"], lang)
                if res["count"] != -1:
                    total += res["count"]
        else:
            errors_groups = []
            total = 0

        if gen in ("false-positive", "done"):
            opt_date = "date"
        else:
            opt_date = None

        errors_groups = list(map(dict, errors_groups))
        for res in errors_groups:
            res['timestamp'] = str(res['timestamp'])
        errors = list(map(dict, errors))
        for res in errors:
            res['timestamp'] = str(res['timestamp'])
            if 'date' in res:
                res['date'] = str(res['date'])
        return dict(countries=countries,
                    items=items,
                    errors_groups=errors_groups,
                    total=total,
                    errors=errors,
                    gen=gen,
                    opt_date=opt_date,
                    website=utils.website,
                    main_website=utils.main_website,
                    remote_url_read=utils.remote_url_read)

    return template(tpl,
                    items=items,
                    errors=errors,
                    query=request.query_string,
                    lang=lang[0],
                    gen=gen,
                    title=title,
                    website=utils.website,
                    main_website=utils.main_website,
                    remote_url_read=utils.remote_url_read)