예제 #1
0
def saveLRStats(resource, action, request=None):
    """
    Saves the actions on a resource.
    
    Takes into account the session to avoid to increment more than one time the
    stats counter. Returns whether the stats counter was incremented or not.
    """
    result = False
    LOGGER.debug("saveLRStats action=%s lrid=%s", action,
                 resource.storage_object.identifier)
    if not hasattr(resource,
                   'storage_object') or resource.storage_object is None:
        return result

    # Manage statistics according with the resource status
    lrid = resource.storage_object.identifier
    ignored = False
    if action == INGEST_STAT:
        ignored = True
        LRStats.objects.filter(lrid=lrid).update(ignored=ignored)
        UsageStats.objects.filter(lrid=lrid).delete()
    if action == DELETE_STAT:
        UsageStats.objects.filter(lrid=lrid).delete()
        LRStats.objects.filter(lrid=lrid).delete()
        return result
    if (resource.storage_object.publication_status != PUBLISHED):
        return result

    userid = _get_userid(request)
    sessid = _get_sessionid(request)
    lrset = LRStats.objects.filter(userid=userid,
                                   lrid=lrid,
                                   sessid=sessid,
                                   action=action)
    if (lrset.count() > 0):
        record = lrset[0]
        record.ignored = ignored
        record.save(force_update=True)
        #LOGGER.debug('UPDATESTATS: Saved LR {0}, {1} action={2} ({3}).'.format(lrid, sessid, action, record.lasttime))
    else:
        record = LRStats()
        record.userid = userid
        record.lrid = lrid
        record.action = action
        record.sessid = sessid
        record.geoinfo = getcountry_code(_get_ipaddress(request))
        record.ignored = ignored
        record.save(force_insert=True)
        #LOGGER.debug('SAVESTATS: Saved LR {0}, {1} action={2}.'.format(lrid, sessid, action))
        result = True
    if action == UPDATE_STAT:
        if (resource.storage_object.published):
            UsageStats.objects.filter(lrid=lrid).delete()
            update_usage_stats(lrid, resource.export_to_elementtree())
            #LOGGER.debug('STATS: Updating usage statistics: resource {0} updated'.format(lrid))
    return result
예제 #2
0
def saveQueryStats(query, facets, found, exectime=0, request=None):
    stat = QueryStats()
    stat.userid = _get_userid(request)
    stat.geoinfo = getcountry_code(_get_ipaddress(request))
    stat.query = query
    stat.facets = facets
    stat.found = found
    stat.exectime = exectime
    stat.save()
    LOGGER.debug(u"saveQueryStats q={0}.".format(query))
예제 #3
0
def saveQueryStats(query, facets, found, exectime=0, request=None):
    stat = QueryStats()
    stat.userid = _get_userid(request)
    stat.geoinfo = getcountry_code(_get_ipaddress(request))
    stat.query = query
    stat.facets = facets
    stat.found = found
    stat.exectime = exectime
    stat.save()
    LOGGER.debug(u'saveQueryStats q={0}.'.format(query))
예제 #4
0
def saveLRStats(resource, action, request=None): 
    """
    Saves the actions on a resource.
    
    Takes into account the session to avoid to increment more than one time the
    stats counter. Returns whether the stats counter was incremented or not.
    """
    result = False
    LOGGER.debug("saveLRStats %s %s", action,
                 resource.storage_object.identifier)
    if not hasattr(resource, 'storage_object') or resource.storage_object is None:
        return result
        
    # Manage statistics according with the resource status
    lrid = resource.storage_object.identifier
    ignored = False
    if action == INGEST_STAT:
        ignored = True
        LRStats.objects.filter(lrid=lrid).update(ignored=ignored)
        UsageStats.objects.filter(lrid=lrid).delete()
    if action == DELETE_STAT:
        UsageStats.objects.filter(lrid=lrid).delete()
        LRStats.objects.filter(lrid=lrid).delete()
        return
    if (resource.storage_object.publication_status != PUBLISHED):
        return False
    
    userid = _get_userid(request)
    sessid = _get_sessionid(request)
    lrset = LRStats.objects.filter(userid=userid, lrid=lrid, sessid=sessid, action=action)
    if (lrset.count() > 0):
        record = lrset[0]
        record.lasttime = datetime.now()
        record.ignored = ignored
        record.save(force_update=True)
        LOGGER.debug('UPDATESTATS: Saved LR {0}, {1} action={2} ({3}).'.format(lrid, sessid, action, record.lasttime))
    else:       
        record = LRStats()
        record.userid = userid
        record.lrid = lrid
        record.action = action
        record.sessid = sessid
        record.geoinfo = getcountry_code(_get_ipaddress(request))
        record.ignored = ignored
        record.save(force_insert=True)
        LOGGER.debug('SAVESTATS: Saved LR {0}, {1} action={2}.'.format(lrid, sessid, action))
        result = True
    if action == UPDATE_STAT:
        if (resource.storage_object.published):
            UsageStats.objects.filter(lrid=lrid).delete()
            _update_usage_stats(lrid, resource.export_to_elementtree())
            LOGGER.debug('STATS: Updating usage statistics: resource {0} updated'.format(lrid))
    return result