src/ldt/ldt/ldt_utils/stat.py
author verrierj
Wed, 01 Feb 2012 15:29:34 +0100
changeset 498 39a7b09be44f
parent 482 c802e00c7131
child 504 32a878a71a80
permissions -rw-r--r--
Stats are updated correctly when a project is unpublished
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
from django.conf import settings
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     2
from ldt.ldt_utils.models import Segment
354
ecd4d57d0b40 Last annotated contents can be retrieved using the field content.last_annotated
verrierj
parents: 351
diff changeset
     3
import datetime
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
     4
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     5
def update_stat_content(content):
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     6
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     7
    nb_division = settings.DIVISIONS_FOR_STAT_ANNOTATION
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     8
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
     9
    segments = Segment.objects.filter(content=content)
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    10
    buckets = [0] * nb_division 
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    11
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    12
    size_division = content.duration / nb_division
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    13
    limits = [x * size_division for x in range(nb_division+1)]
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    14
    nb_annotation = len(segments)
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
    15
    
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
    16
    content.pol_positive = 0
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
    17
    content.pol_negative = 0
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
    18
    content.pol_question = 0
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
    19
    content.pol_reference = 0
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
    20
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    21
    for segment in segments:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    22
        
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    23
        begin = segment.start_ts
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    24
        end = segment.start_ts + segment.duration
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    25
        
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    26
        if segment.is_polemic('OK'):
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    27
            content.pol_positive += 1
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    28
        if segment.is_polemic('KO'):
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    29
            content.pol_negative += 1
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    30
        if segment.is_polemic('Q'):
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    31
            content.pol_question += 1
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    32
        if segment.is_polemic('REF'):
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    33
            content.pol_reference += 1
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    34
        
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    35
        buckets = find_buckets(buckets, limits, begin, end)
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    36
        
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    37
    content.annotation_volume = buckets
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    38
    content.nb_annotation = nb_annotation
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 415
diff changeset
    39
    
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    40
    content.save()     
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    41
 
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    42
   
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    43
def update_stat_project(project, contents_only=[]):
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    44
    
498
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    45
    if contents_only:
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    46
        contents = contents_only
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    47
    else:
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    48
        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
    49
            
498
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    50
    for c in contents:
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    51
        update_stat_content(c)
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 415
diff changeset
    52
            
498
39a7b09be44f Stats are updated correctly when a project is unpublished
verrierj
parents: 482
diff changeset
    53
    return True
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    54
    
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    55
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    56
def add_annotation_to_stat(content, begin, end, polemics=[]):
478
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    57
    nb_division = settings.DIVISIONS_FOR_STAT_ANNOTATION
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    58
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    59
    if not content.nb_annotation or not content.stat_annotation:
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    60
        content.nb_annotation = 1
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    61
        content.stat_annotation = get_empty_stat_vector()
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    62
    
478
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    63
    size_division = content.duration / nb_division
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    64
    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
    65
    
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 474
diff changeset
    66
    buckets = find_buckets(content.annotation_volume, limits, begin, end)
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    67
    content.annotation_volume = buckets
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    68
    content.last_annotated = datetime.datetime.now()
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    69
    
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    70
    for polemic in polemics:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    71
        update_polemic_stat(content, polemic)
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
    72
    
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    73
    content.save()        
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    74
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
    75
def get_string_from_buckets(buckets):
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
    76
    s = "%s" % buckets
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
    77
    s = s[1:-1].replace(' ', '')
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
    78
    return s
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
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    80
def get_buckets_from_string(string):
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    81
    return [int(x) for x in string.split(',')]
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    82
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    83
def get_empty_stat_vector():
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
    84
    return [0] * (settings.DIVISIONS_FOR_STAT_ANNOTATION + 4) # + 4 for polemic syntax
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 424
diff changeset
    85
474
5e7942f049a6 Fix bug in regenerating stat command
verrierj
parents: 468
diff changeset
    86
def get_empty_stat_field():
5e7942f049a6 Fix bug in regenerating stat command
verrierj
parents: 468
diff changeset
    87
    return ("%s" % get_empty_stat_vector())[1:-1].replace(' ', '')
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
    88
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
    89
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
    90
    
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 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
    92
        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
    93
    
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
    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
    95
        
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
    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
    97
        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
    98
            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
    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
                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
   101
                #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
   102
        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
   103
            #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
   104
            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
   105
        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
   106
            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
   107
            
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
   108
    return buckets
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   109
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   110
def update_polemic_stat(content, polemic_hashtag):
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   111
    if polemic_hashtag == "OK":
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   112
        content.pol_positive += 1
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   113
    if polemic_hashtag == "KO":
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   114
        content.pol_negative += 1
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   115
    if polemic_hashtag == "Q":
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   116
        content.pol_question += 1
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   117
    if polemic_hashtag == "REF":
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   118
        content.pol_reference += 1
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   119
        
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   120
    return content
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 478
diff changeset
   121