<s> from collections import OrderedDict <EOL> from mongoengine import Q <EOL> from vulyk . app import TASKS_TYPES <EOL> from vulyk . models . tasks import Batch , AbstractTask <EOL> def batch_completeness ( batch_name , task_type ) : <EOL> """<STR_LIT>""" <EOL> batches = OrderedDict ( ) <EOL> rs = lambda batch : AbstractTask . objects ( batch = batch ) <EOL> percent = lambda done , total : ( float ( done ) / ( total or done or <NUM_LIT:1> ) ) * <NUM_LIT:100> <EOL> query = Q ( id = batch_name ) if batch_name else Q ( ) <EOL> query &= Q ( task_type = task_type ) if task_type else Q ( ) <EOL> for b in Batch . objects ( query ) . order_by ( '<STR_LIT:id>' ) : <EOL> batches [ b . id ] = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if len ( rs ( b . id ) ) > <NUM_LIT:0> : <EOL> rs_task = TASKS_TYPES [ rs ( b . id ) . first ( ) . task_type ] <EOL> answers = rs ( b . id ) . filter ( closed = False ) . sum ( '<STR_LIT>' ) <EOL> answers_all = rs ( b . id ) . sum ( '<STR_LIT>' ) <EOL> answers_mix = answers + ( rs_task . redundancy * b . tasks_processed ) <EOL> answers_needed = b . tasks_count * rs_task . redundancy <EOL> batches [ b . id ] = { <EOL> '<STR_LIT>' : b . tasks_count , <EOL> '<STR_LIT>' : b . tasks_processed , <EOL> '<STR_LIT>' : percent ( b . tasks_processed , b . tasks_count ) , <EOL> '<STR_LIT>' : answers_all , <EOL> '<STR_LIT>' : percent ( answers_mix , answers_needed ) , <EOL> '<STR_LIT>' : _breakdown_by_processed ( b . id ) <EOL> } <EOL> return batches <EOL> def _breakdown_by_processed ( batch ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> rs = AbstractTask . objects ( batch = batch ) <EOL> for i in rs . item_frequencies ( '<STR_LIT>' ) . items ( ) : <EOL> result . append ( '<STR_LIT>' . format ( * i ) ) <EOL> return '<STR_LIT:\n>' . join ( result ) </s>
