def calculateTask(task, sid, model):
    try:
        generate_mol_image(task)
        suite = SuiteTask.objects.get(sid=sid)
        map_model_name = get_model_name(model['model'])
        smile = task.file_obj.smiles.encode(
            'utf-8') if task.file_obj.file_type != 'mol' else ''

        # smile, mol_fpath 输入只选择一种方式(优先smile)
        mol_fpath = os.path.join(
            settings.SETTINGS_ROOT,
            task.file_obj.file_obj.path) if not smile else None

        try:
            temperature = float(model.get('temperature'))
        except:
            temperature = DEFAULT_TEMPERATURE_ARGS

        chemistry_logger.info(
            'PredictionModel calculating: model name(%s),'
            'smile(%s) mol path(%s) temperature(%s)', map_model_name, smile,
            mol_fpath, temperature)
        # 后台模型计算入口
        predict_results = prediction_model_calculate(map_model_name, smile,
                                                     mol_fpath, temperature)

        if task.file_obj.file_type == 'mol':
            name = os.path.basename(mol_fpath).split('.')[0]
        else:
            name = smile
        result = predict_results[name][map_model_name]
        chemistry_logger.info('[task]result %s' % result)
    except KeyError:
        chemistry_logger.exception('still cannot support this model')
        result = None
        task.result_state = "We don't support this model now"
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    except Exception as e:
        chemistry_logger.exception('failed to submit task to prediction model')
        result = None
        task.result_state = str(e)
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    else:
        chemistry_logger.info("calculate Successfully in celery queue!")
        task.result_state = "Calculate Successfully!"
        task.status = StatusCategory.objects.get(category=STATUS_SUCCESS)
        suite.status_id = StatusCategory.objects.get(category=STATUS_WORKING)

    task.end_time = utils.get_real_now()
    task.results = json.dumps(result)

    suite.save()
    task.save()

    add_counter(suite.sid)

    return result
 def activation_key_expired(self):
     """
     Determine whether this ``RegistrationProfile``'s activation
     key has expired, returning a boolean -- ``True`` if the key
     has expired.
     """
     expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
     return self.activation_key == "ALREADY_ACTIVATED" or \
            (self.user.date_joined + expiration_date <= get_real_now())
def calculateTask(task, sid, model):
    try:
        generate_mol_image(task)
        suite = SuiteTask.objects.get(sid=sid)
        map_model_name = get_model_name(model['model'])
        smile = task.file_obj.smiles.encode('utf-8') if task.file_obj.file_type != 'mol' else ''

        # smile, mol_fpath 输入只选择一种方式(优先smile)
        mol_fpath = os.path.join(settings.SETTINGS_ROOT, task.file_obj.file_obj.path) if not smile else None

        try:
            temperature = float(model.get('temperature'))
        except:
            temperature = DEFAULT_TEMPERATURE_ARGS

        chemistry_logger.info('PredictionModel calculating: model name(%s),'
                              'smile(%s) mol path(%s) temperature(%s)',
                              map_model_name, smile, mol_fpath, temperature)
        # 后台模型计算入口
        predict_results = prediction_model_calculate(map_model_name, smile,
                                                     mol_fpath, temperature)

        if task.file_obj.file_type == 'mol':
            name = os.path.basename(mol_fpath).split('.')[0]
        else:
            name = smile
        result = predict_results[name][map_model_name]
        chemistry_logger.info('[task]result %s' % result)
    except KeyError:
        chemistry_logger.exception('still cannot support this model')
        result = None
        task.result_state = "We don't support this model now"
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    except Exception as e:
        chemistry_logger.exception('failed to submit task to prediction model')
        result = None
        task.result_state = str(e)
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    else:
        chemistry_logger.info("calculate Successfully in celery queue!")
        task.result_state = "Calculate Successfully!"
        task.status = StatusCategory.objects.get(category=STATUS_SUCCESS)
        suite.status_id = StatusCategory.objects.get(category=STATUS_WORKING)

    task.end_time = utils.get_real_now()
    task.results = json.dumps(result)

    suite.save()
    task.save()

    add_counter(suite.sid)

    return result
def generate_calculate_task(models, smile, draw_mol_data, files_id_list,
                            sid, local_search_id):
    from chemistry.tasks import send_email_task
    try:
        for model in models:
            handle_smile_task(smile, model, sid, local_search_id)
            handle_moldraw_task(draw_mol_data, model, sid)
            handle_files_task(files_id_list, model, sid)
    except Exception:
        chemistry_logger.exception('failed to generate suite_task:%s' % sid)
        s = SuiteTask.objects.get(sid=sid)
        s.end_time = utils.get_real_now() 
        s.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
        s.save()
        send_email_task(s.email, s.sid)
def submit_calculate_task(user, smile=None, draw_mol_data=None,
                          task_notes=None, task_name=None,
                          files_id_list=None, models=None,
                          local_search_id=None):

    chemistry_logger.info("smile: %s" % smile)
    chemistry_logger.info("draw_mol_data: %s" % draw_mol_data)
    chemistry_logger.info("files_id_list: %s" % files_id_list)
    chemistry_logger.info("models: %s" % models)

    tasks_num = calculate_tasks(files_id_list, smile, draw_mol_data, models)

    if tasks_num == 0:
        status = False
        info = "请至少选择一种输入方式和计算模型!"
        id = None
        return (status, info, id)

    try:
        s = SuiteTask()
        s.sid = id = str(uuid.uuid4())
        s.user = UserProfile.objects.get(user=user)
        s.total_tasks = tasks_num
        s.has_finished_tasks = 0
        s.start_time = utils.get_real_now()
        s.name = task_name
        s.notes = task_notes
        s.models_str, s.models_category_str = parse_models(models)
        s.status = StatusCategory.objects.get(category=STATUS_WORKING)
        s.email = user.email
        s.save()
        
        chemistry_logger.info('~~~~~~~~ s:%s' % s.start_time)

        generate_calculate_task.delay(models, smile, draw_mol_data,
                                      files_id_list, id, local_search_id)
    except:
        chemistry_logger.exception('failed to generate suite_task')
        s.delete()
        status = False
        info = "计算任务添加不成功,将重试或联系网站管理员!"
        id = None
    else:
        status = True
        info = "恭喜,计算任务已经提交!"

    return (status, info, id)
def save_record(f, model, sid, source_type, smile=None, local_search_id=None):
    from chemistry.tasks import calculateTask, DEFAULT_TEMPERATURE_ARGS
    task = SingleTask()
    task.start_time = utils.get_real_now() 
    task.sid = SuiteTask.objects.get(sid=sid)
    task.pid = str(uuid.uuid4())
    task.model = ModelCategory.objects.get(category=model['model'])

    temperature = model.get('temperature')
    task.temperature = float(temperature) if temperature else DEFAULT_TEMPERATURE_ARGS

    if source_type == ORIGIN_UPLOAD:
        # here, f is ProcessedFile record instance
        f.file_source = FileSourceCategory.objects.get(category=source_type)
        f.file_type = "mol"
        task.file_obj = f
        f.save()
    elif source_type in (ORIGIN_SMILE, ORIGIN_DRAW):
        # here, f is a file path
        processed_f = ProcessedFile()
        obj = File(open(f, "r"))
        processed_f.title = os.path.basename(obj.name)
        processed_f.file_type = source_type
        processed_f.file_source = FileSourceCategory.objects.get(category=source_type)
        processed_f.file_obj = obj
        if smile:
            processed_f.smiles = smile

        if source_type == ORIGIN_SMILE and local_search_id is not None:
            processed_f.local_search_id = int(local_search_id)

        processed_f.save()
        task.file_obj = processed_f
        obj.close()
    else:
        raise ErrorCalculateType('Cannot recongize this source type')

    task.status = StatusCategory.objects.get(category=STATUS_WORKING)
    task.save()

    chemistry_logger.info('~~~~~~~~ t:%s' % task.start_time)

    calculateTask.delay(task, sid, model)
def add_counter(suite_id):
    """
    use filter to get task numbers
    """
    finished_count = SingleTask.objects.filter(sid=suite_id)\
                                       .exclude(status=StatusCategory.objects.get(category=STATUS_WORKING))\
                                       .count()
    suite = SuiteTask.objects.get(sid=suite_id)

    if finished_count == suite.total_tasks:
        suite.has_finished_tasks = suite.total_tasks
        suite.status_id = StatusCategory.objects.get(category=STATUS_SUCCESS)
        suite.end_time = utils.get_real_now()
        suite.save()

        send_email_task(suite.email, suite.sid)
    else:
        suite.has_finished_tasks = finished_count
        suite.save()

    chemistry_logger.info('~~~~~~~~ s-e:%s' % suite.end_time)
def add_counter(suite_id):
    """
    use filter to get task numbers
    """
    finished_count = SingleTask.objects.filter(sid=suite_id)\
                                       .exclude(status=StatusCategory.objects.get(category=STATUS_WORKING))\
                                       .count()
    suite = SuiteTask.objects.get(sid=suite_id)

    if finished_count == suite.total_tasks:
        suite.has_finished_tasks = suite.total_tasks
        suite.status_id = StatusCategory.objects.get(category=STATUS_SUCCESS)
        suite.end_time = utils.get_real_now()
        suite.save()

        send_email_task(suite.email, suite.sid)
    else:
        suite.has_finished_tasks = finished_count
        suite.save()

    chemistry_logger.info('~~~~~~~~ s-e:%s' % suite.end_time)