Exemplo n.º 1
0
def compute_collections():
    """Update collection caches, including the global stats cache."""
    authz = Authz.from_role(None)
    schemata = defaultdict(int)
    countries = defaultdict(int)
    categories = defaultdict(int)

    for collection in Collection.all():
        compute_collection(collection)

        if authz.can(collection.id, authz.READ):
            categories[collection.category] += 1
            things = index.get_collection_things(collection.id)
            for schema, count in things.items():
                schemata[schema] += count
            for country in collection.countries:
                countries[country] += 1

    log.info("Updating global statistics cache...")
    data = {
        "collections": sum(categories.values()),
        "schemata": dict(schemata),
        "countries": dict(countries),
        "categories": dict(categories),
        "things": sum(schemata.values()),
    }
    key = cache.key(cache.STATISTICS)
    cache.set_complex(key, data, expires=cache.EXPIRE)
Exemplo n.º 2
0
def reconcile_index(collection=None):
    domain = settings.APP_UI_URL.strip("/")
    label = settings.APP_TITLE
    suggest_query = []
    schemata = list(model)
    if collection is not None:
        label = "%s (%s)" % (collection.get("label"), label)
        suggest_query.append(("filter:collection_id", collection.get("id")))
        things = get_collection_things(collection.get("id"))
        schemata = [model.get(s) for s in things.keys()]
    return jsonify({
        "name":
        label,
        "identifierSpace":
        "http://rdf.freebase.com/ns/type.object.id",
        "schemaSpace":
        "http://rdf.freebase.com/ns/type.object.id",
        "view": {
            "url": entity_url("{{id}}")
        },
        "preview": {
            "url": entity_url("{{id}}"),
            "width": 800,
            "height": 400
        },
        "suggest": {
            "entity": {
                "service_url":
                domain,
                "service_path":
                url_for(
                    "reconcile_api.suggest_entity",
                    _query=suggest_query,
                    _authz=request.authz,
                    _relative=True,
                ),
            },
            "type": {
                "service_url":
                domain,
                "service_path":
                url_for("reconcile_api.suggest_type", _relative=True),
            },
            "property": {
                "service_url":
                domain,
                "service_path":
                url_for("reconcile_api.suggest_property", _relative=True),
            },
        },
        "defaultTypes":
        [get_freebase_type(s) for s in schemata if s.matchable],
    })
Exemplo n.º 3
0
def reconcile_index(collection=None):
    domain = settings.APP_UI_URL.strip('/')
    label = settings.APP_TITLE
    suggest_query = []
    schemata = list(model)
    if collection is not None:
        label = '%s (%s)' % (collection.get('label'), label)
        suggest_query.append(('filter:collection_id', collection.get('id')))
        things = get_collection_things(collection.get('id'))
        schemata = [model.get(s) for s in things.keys()]
    return jsonify({
        'name':
        label,
        'identifierSpace':
        'http://rdf.freebase.com/ns/type.object.id',
        'schemaSpace':
        'http://rdf.freebase.com/ns/type.object.id',
        'view': {
            'url': entity_url('{{id}}')
        },
        'preview': {
            'url': entity_url('{{id}}'),
            'width': 800,
            'height': 400
        },
        'suggest': {
            'entity': {
                'service_url':
                domain,
                'service_path':
                url_for('reconcile_api.suggest_entity',
                        _query=suggest_query,
                        _authorize=True,
                        _relative=True)
            },
            'type': {
                'service_url':
                domain,
                'service_path':
                url_for('reconcile_api.suggest_type', _relative=True)
            },
            'property': {
                'service_url':
                domain,
                'service_path':
                url_for('reconcile_api.suggest_property', _relative=True)
            }
        },
        'defaultTypes':
        [get_freebase_type(s) for s in schemata if s.matchable]
    })
Exemplo n.º 4
0
def statistics():
    """Get a summary of the data acessible to the current user.
    ---
    get:
      summary: System-wide user statistics.
      description: >
        Get a summary of the data acessible to the current user.
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
      tags:
      - System
    """
    enable_cache()
    collections = request.authz.collections(request.authz.READ)
    for collection_id in collections:
        resolver.queue(request, Collection, collection_id)
    resolver.resolve(request)

    # Summarise stats. This is meant for display, so the counting is a bit
    # inconsistent between counting all collections, and source collections
    # only.
    schemata = defaultdict(int)
    countries = defaultdict(int)
    categories = defaultdict(int)
    for collection_id in collections:
        data = resolver.get(request, Collection, collection_id)
        if data is None or data.get('casefile'):
            continue
        categories[data.get('category')] += 1
        things = get_collection_things(collection_id)
        for schema, count in things.items():
            schemata[schema] += count
        for country in data.get('countries', []):
            countries[country] += 1

    return jsonify({
        'collections': len(collections),
        'schemata': dict(schemata),
        'countries': dict(countries),
        'categories': dict(categories),
        'things': sum(schemata.values()),
    })