def estimate_all(): # Estimate ETA for all tasks tms_set = TMS.objects.all() logging.info( 'starting generating ETAs for the \ following TMS entries ({}): {}'.format( len(tms_set), tms_set)) for tms in tms_set: logging.info('generating ETAs for TMS {}'.format(tms)) try: project_set = Project.objects.all().filter(project_tms_id=tms.id) if project_set: logging.info('generating ETAs for TMS {} Projects: {}'.format( tms, project_set)) try: tms_wrapper = TMSlib.TMSWrapper(tms) tms_wrapper.init_ETApredict(project_set) tms_wrapper.estimate_tasks() del tms_wrapper except Exception as e: logging.error('Could not generate ETAs for TMS {} \ Projects {} due to "{}"'.format(tms, project_set, e)) else: logging.info('no projects found for TMS {}'.format(tms)) except Exception as e2: logging.info('Could not generate ETAs for TMS {} due to'.format( tms, e2)) return True
def estimate_ETA_for_TMS(tms, projects_set, **kwargs): """Estimates ETA for a given TMS and projects_set. Arguments: tms - Django model of TMS. Todo: add an option not to refresh velocities https://etabot.atlassian.net/browse/ET-521 """ logging.debug( 'estimate_ETA_for_TMS started for TMS {}, projects: {}'.format( tms, projects_set)) tms_wrapper = TMSlib.TMSWrapper(tms) tms_wrapper.init_ETApredict(projects_set) projects_dict = tms_wrapper.ETApredict_obj.eta_engine.projects project_names = [] for project in projects_set: project.velocities = dc.get_velocity_json( tms_wrapper.ETApredict_obj.user_velocity_per_project, project.name) project_settings = projects_dict.get(project.name, {}).get('project_settings', {}) logging.debug( 'project.project_settings {} before update with {}:'.format( project.project_settings, project_settings)) project.project_settings = project_settings project.save() logging.debug('project.project_settings after save: {}'.format( project.project_settings)) project_names.append(project.name) tms_wrapper.estimate_tasks(project_names=project_names, **kwargs) logging.debug('estimate_ETA_for_TMS finished')
def parse_projects_for_TMS(instance, **kwargs): """Parse projects for the given TMS. Creates new Django model projects objects with parsed data. Arguments: instance - Django TMS object instance """ logging.info('parse_tms started') logging.debug('parse_projects_for_TMS kwargs: {}'.format(kwargs)) existing_projects = Project.objects.filter(project_tms=instance.id) TMS_w1 = TMSlib.TMSWrapper(instance, projects=existing_projects) TMS_w1.init_ETApredict([]) projects_dict = TMS_w1.ETApredict_obj.eta_engine.projects velocities = TMS_w1.ETApredict_obj.user_velocity_per_project logging.debug('parse_tms: velocities found: {}'.format(velocities)) existing_projects_dict = {} for p in existing_projects: existing_projects_dict[p.name] = p logging.info('passing parsed projects info to Django models.') new_projects = [] updated_projects = [] if projects_dict is not None: for project_name, attrs in projects_dict.items(): velocity_json = dc.get_velocity_json(velocities, project_name) if project_name not in existing_projects_dict: new_django_project = Project( owner=instance.owner, project_tms=instance, name=project_name, mode=attrs.get('mode', 'unknown mode'), open_status=attrs.get('open_status', ''), velocities=velocity_json, grace_period=attrs.get('grace_period', 12.0), work_hours=attrs.get('work_hours', {}), vacation_days=attrs.get('vacation_days', {}), project_settings=attrs.get('project_settings', {})) new_django_project.save() new_projects.append(project_name) else: p.velocities = velocity_json p.project_settings = attrs.get('project_settings', p.project_settings) p.mode = attrs.get('mode', p.mode) p.save() updated_projects.append(project_name) logging.info('parse_tms has finished') response_message = '' if len(new_projects) > 0: response_message += "New projects found and parsed: {}.".format( ', '.join(new_projects)) if len(updated_projects) > 0: response_message += " Updated existing projects: {}.".format( ', '.join(updated_projects)) return response_message
def get(self, request, format=None): tms_id = request.query_params.get('tms', None) if tms_id is not None: try: tms_id = int(tms_id) tms_set = TMS.objects.all().filter( owner=self.request.user, id=tms_id) except Exception as e: return Response( 'Invalid tms_id: "{}"'.format(tms_id), status=status.HTTP_400_BAD_REQUEST) else: tms_set = TMS.objects.all().filter(owner=self.request.user) logging.debug('request.query_params: "{}"'.format( request.query_params)) logging.debug('tms_id: "{}"'.format(tms_id)) logging.debug('found tms: {}'.format(tms_set)) # here we need to call an estimate method that takes TMS object which # includes TMS credentials for tms in tms_set: project_id = request.query_params.get('project_id', None) if project_id is not None: project_id = int(project_id) logging.debug('subsetting project_id="{}"'.format(project_id)) projects_set = Project.objects.all().filter( owner=self.request.user, project_tms_id=tms.id, id=project_id) else: projects_set = Project.objects.all().filter( owner=self.request.user, project_tms_id=tms.id) logging.debug('projects_set: "{}"'.format(projects_set)) tms_wrapper = TMSlib.TMSWrapper(tms) tms_wrapper.init_ETApredict(projects_set) project_names = [] for project in projects_set: project_names.append(project.name) tms_wrapper.estimate_tasks(project_names) return Response( 'TMS account to estimate: %s' % tms_set, status=status.HTTP_200_OK)
def parse_tms(sender, instance, **kwargs): logging.debug('parse_tms started') TMS_w1 = TMSlib.TMSWrapper( instance, projects=Project.objects.filter(project_tms=instance.id)) TMS_w1.init_ETApredict([]) projects_dict = TMS_w1.ETApredict_obj.eta_engine.projects if projects_dict is not None: for project_name, attrs in projects_dict.items(): django_project = Project( owner=instance.owner, project_tms=instance, name=project_name, mode=attrs.get('mode', 'unknown mode'), open_status=attrs.get('open_status', ''), grace_period=attrs.get('grace_period', 12.0), work_hours=attrs.get('work_hours', '{}'), vacation_days=attrs.get('vacation_days', '{}')) django_project.save() logging.debug('parse_tms has finished')
def validate_Atlassian_API_key(self, instance): TMS_w1 = TMSlib.TMSWrapper(instance) error = TMS_w1.connect_to_TMS(update_tms=False) if error is not None: logging.debug('Error in validation: {}'.format(error)) if 'Unauthorized (401)' in error: raise serializers.ValidationError( 'Unable to log in due to "Unauthorized (401)"\ error - please check username/email and password') elif 'cannot connnect to TMS JIRA' in error: logging.debug('cannot connnect to TMS JIRA error.') captcha_sig = \ "'X-Authentication-Denied-Reason': 'CAPTCHA_CHALLENGE" if captcha_sig in error: message = 'Need to pass CAPTCHA challenge first. ' login_urls = rr.get_login_url(error) if len(login_urls) > 0: login_url = login_urls[0] logging.debug('login_url: {}'.format(login_url)) else: logging.debug( 'No login url detected, using TMS endpoint.') login_url = instance.endpoint message += 'Please login at <a href="{login_url}">{login_url}</a> \ first and then try again. '.format(login_url=login_url) message += 'If the issue persists, please ask your \ administrator to disable CAPTCHA.' raise serializers.ValidationError(message) else: logging.debug('generic connectivity issue.') raise serializers.ValidationError( 'cannot connnect to TMS JIRA - please check\ inputs and try again. If the issue persists, please report the issue to \ [email protected]') else: raise serializers.ValidationError( 'Unrecognized error has occurred - please check\ inputs and try again. If the issue persists, please report the issue to \ [email protected]')
def validate_tms_credential(sender, instance, **kwargs): logging.debug('validate_tms_credential started') TMS_w1 = TMSlib.TMSWrapper(instance) TMS_w1.connect_to_TMS(instance.password) logging.debug('validate_tms_credential finished')