def databaseinfra_notification(self, user=None): task_history = TaskHistory.register(request=self.request, user=user) threshold_infra_notification = Configuration.get_by_name_as_int("threshold_infra_notification", default=0) if threshold_infra_notification <= 0: LOG.warning("database infra notification is disabled") return # Sum capacity per databseinfra with parameter plan, environment and engine infras = DatabaseInfra.objects.values('plan__name', 'environment__name', 'engine__engine_type__name', 'plan__provider').annotate(capacity=Sum('capacity')) for infra in infras: # total database created in databaseinfra per plan, environment and engine used = DatabaseInfra.objects.filter(plan__name=infra['plan__name'], environment__name=infra['environment__name'], engine__engine_type__name=infra['engine__engine_type__name']).aggregate( used=Count('databases')) # calculate the percentage percent = int(used['used'] * 100 / infra['capacity']) if percent >= threshold_infra_notification and infra['plan__provider'] != Plan.CLOUDSTACK: LOG.info('Plan %s in environment %s with %s%% occupied' % ( infra['plan__name'], infra['environment__name'], percent)) LOG.info("Sending database infra notification...") context = {} context['plan'] = infra['plan__name'] context['environment'] = infra['environment__name'] context['used'] = used['used'] context['capacity'] = infra['capacity'] context['percent'] = percent email_notifications.databaseinfra_ending(context=context) task_history.update_status_for(TaskHistory.STATUS_SUCCESS, details='Databaseinfra Notification successfully sent to dbaas admins!') return
def databaseinfra_notification(self, user=None): worker_name = get_worker_name() task_history = TaskHistory.register(request=self.request, user=user, worker_name=worker_name) threshold_infra_notification = Configuration.get_by_name_as_int( "threshold_infra_notification", default=0) if threshold_infra_notification <= 0: LOG.warning("database infra notification is disabled") return # Sum capacity per databseinfra with parameter plan, environment and engine infras = DatabaseInfra.objects.values( 'plan__name', 'environment__name', 'engine__engine_type__name', 'plan__provider').annotate(capacity=Sum('capacity')) for infra in infras: try: database = infra.databases.get() except Database.MultipleObjectsReturned: pass else: if database.is_in_quarantine: continue if not database.subscribe_to_email_events: continue used = DatabaseInfra.objects.filter( plan__name=infra['plan__name'], environment__name=infra['environment__name'], engine__engine_type__name=infra['engine__engine_type__name'] ).aggregate(used=Count('databases')) # calculate the percentage percent = int(used['used'] * 100 / infra['capacity']) if percent >= threshold_infra_notification and infra[ 'plan__provider'] != Plan.CLOUDSTACK: LOG.info( 'Plan %s in environment %s with %s%% occupied' % (infra['plan__name'], infra['environment__name'], percent)) LOG.info("Sending database infra notification...") context = {} context['plan'] = infra['plan__name'] context['environment'] = infra['environment__name'] context['used'] = used['used'] context['capacity'] = infra['capacity'] context['percent'] = percent email_notifications.databaseinfra_ending(context=context) task_history.update_status_for( TaskHistory.STATUS_SUCCESS, details= 'Databaseinfra Notification successfully sent to dbaas admins!') return
def databaseinfra_notification(): # Sum capacity per databseinfra with parameter plan, environment and engine infras = DatabaseInfra.objects.values('plan__name', 'environment__name', 'engine__engine_type__name').annotate(capacity=Sum('capacity')) for infra in infras: # total database created in databaseinfra per plan, environment and engine used = DatabaseInfra.objects.filter(plan__name=infra['plan__name'], environment__name=infra['environment__name'], engine__engine_type__name=infra['engine__engine_type__name']).aggregate(used=Count('databases')) # calculate the percentage percent = int(used['used'] * 100 / infra['capacity']) if percent >= Configuration.get_by_name_as_int("threshold_infra_notification", default=50): LOG.info('Plan %s in environment %s with %s%% occupied' % (infra['plan__name'], infra['environment__name'],percent)) LOG.info("Sending notification...") email_notifications.databaseinfra_ending(infra['plan__name'], infra['environment__name'], used['used'],infra['capacity'],percent) return