src/ldt/ldt/ldt_utils/stat.py
author cavaliet
Thu, 28 Nov 2013 16:44:30 +0100
changeset 1264 77e0dbc58104
parent 667 9e64be35704f
permissions -rw-r--r--
v1.51.16 debug multisegments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
     1
from django.conf import settings #@UnresolvedImport
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
     2
from ldt.ldt_utils.models import Segment, ContentStat, POL_INDICES
354
ecd4d57d0b40 Last annotated contents can be retrieved using the field content.last_annotated
verrierj
parents: 351
diff changeset
     3
import datetime
665
f4fed46efbde correct warnings about timezones, remove dependance on pytz
ymh <ymh.work@gmail.com>
parents: 662
diff changeset
     4
from django.utils import timezone
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
     5
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     6
def update_stat_content(content):
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     7
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     8
    nb_division = settings.DIVISIONS_FOR_STAT_ANNOTATION
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     9
    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
    10
    segments = Segment.objects.filter(content=content).select_related('project_obj').only('start_ts','duration','polemics','project_obj__modification_date') #@UndefinedVariable
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    11
    buckets = [0] * nb_division 
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    12
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    13
    size_division = content.duration / nb_division
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    14
    limits = [x * size_division for x in range(nb_division+1)]
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
    15
    nb_annotation = segments.count()
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
    16
    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
    17
    contentstat, created = ContentStat.objects.get_or_create(content=content) #@UnusedVariable @UndefinedVariable
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    18
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    19
    pol_stats = [0]*len(POL_INDICES)
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    20
    
667
9e64be35704f correct timezone problem
ymh <ymh.work@gmail.com>
parents: 665
diff changeset
    21
    min_ts = datetime.datetime.min
9e64be35704f correct timezone problem
ymh <ymh.work@gmail.com>
parents: 665
diff changeset
    22
    if settings.USE_TZ:
9e64be35704f correct timezone problem
ymh <ymh.work@gmail.com>
parents: 665
diff changeset
    23
        min_ts = min_ts.replace(tzinfo=timezone.utc)
9e64be35704f correct timezone problem
ymh <ymh.work@gmail.com>
parents: 665
diff changeset
    24
        
662
73b94f485ed2 correct stat to work with non naive datetime
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    25
    min_update_ts = min_ts #@UndefinedVariable
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
    26
    
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    27
    for segment in segments:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    28
        
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    29
        begin = segment.start_ts
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    30
        end = segment.start_ts + segment.duration
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    31
        
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    32
        if segment.is_polemic('OK'):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    33
            pol_stats[POL_INDICES['pol_positive']] += 1
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    34
        if segment.is_polemic('KO'):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    35
            pol_stats[POL_INDICES['pol_negative']] += 1
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    36
        if segment.is_polemic('Q'):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    37
            pol_stats[POL_INDICES['pol_question']] += 1
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    38
        if segment.is_polemic('REF'):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    39
            pol_stats[POL_INDICES['pol_reference']] += 1
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    40
        
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    41
        buckets = find_buckets(buckets, limits, begin, end)
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
    42
        if segment.project_obj is not None and segment.project_obj.modification_date > min_update_ts:
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    43
            min_update_ts = segment.project_obj.modification_date
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
    44
            
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    45
    contentstat.polemics_volume = pol_stats
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    46
    contentstat.annotation_volume = buckets
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    47
    contentstat.nb_annotations = nb_annotation
662
73b94f485ed2 correct stat to work with non naive datetime
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    48
    contentstat.last_annotated = min_update_ts if min_update_ts > min_ts else content.update_date #@UndefinedVariable
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    49
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    50
    contentstat.save()
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    51
 
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    52
   
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    53
def update_stat_project(project, contents_only=[]):
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    54
    
498
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    55
    if contents_only:
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    56
        contents = contents_only
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    57
    else:
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    58
        contents = project.contents.all()
351
74f898bd0983 Fix bug when computing stat on a project where the media is referenced in the xml but is not included in the contents field
verrierj
parents: 343
diff changeset
    59
            
498
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    60
    for c in contents:
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    61
        update_stat_content(c)
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 415
diff changeset
    62
            
498
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    63
    return True
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    64
    
504
32a878a71a80 Users arrive in front pages by default + bugfixes
verrierj
parents: 498
diff changeset
    65
def add_annotation_to_stat(content, begin, end):
478
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    66
    nb_division = settings.DIVISIONS_FOR_STAT_ANNOTATION
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    67
    
478
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    68
    size_division = content.duration / nb_division
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    69
    limits = [x * size_division for x in range(nb_division+1)]    
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    70
    
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    71
    buckets = find_buckets(content.annotation_volume, limits, begin, end)
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    72
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    73
    contentstat, created = ContentStat.objects.get_or_create(content=content) #@UnusedVariable @UndefinedVariable
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    74
    contentstat.annotation_volume = buckets
665
f4fed46efbde correct warnings about timezones, remove dependance on pytz
ymh <ymh.work@gmail.com>
parents: 662
diff changeset
    75
    contentstat.last_annotated = timezone.now()
504
32a878a71a80 Users arrive in front pages by default + bugfixes
verrierj
parents: 498
diff changeset
    76
      
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    77
    contentstat.save()        
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    78
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    79
def get_string_from_buckets(buckets):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
    80
    return ','.join([str(b) for b in buckets])
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    81
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    82
def find_buckets(buckets, limits, begin, end):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    83
    
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    84
    if len(buckets)+1 != len(limits):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    85
        raise ValueError("There should be as many buckets as those defined by limits.")
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    86
    
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    87
    has_started = False  
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    88
        
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    89
    for i in range(len(limits)-1):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    90
        if not has_started:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    91
            if limits[i] <= begin and begin <= limits[i+1]:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    92
                buckets[i] += 1
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    93
                has_started = True
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    94
                #print "Starts after timecode %s" % limits[i]
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    95
        elif limits[i] > end:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    96
            #print "Ends before timecode %s" % limits[i]
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    97
            break
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    98
        else:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
    99
            buckets[i] += 1
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
   100
            
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
diff changeset
   101
    return buckets