#!/usr/bin/env python
# -*- coding: utf-8 -*-
from .send_email import MonitEmialSender
from .send_message import MonitMessageSender
import datetime
from sense_core import config
import sense_core as sd

sd.log_init_config(root_path=sd.config('log_path'))


class Monit_lib(object):
	
	__send_msg_dict = {}
	__send_email_dict = {}
	
	@classmethod
	def send_message(cls, module, title = '日志出现错误', phone_list=None):
		global __send_msg_dict
		_now = datetime.datetime.now()
		monitor_msg = MonitMessageSender()
		if module not in cls.__send_msg_dict:
			cls.__send_msg_dict[module] = _now
			monitor_msg._send_err_message(module, title, phone_list)
		else:
			last_send_time = cls.__send_msg_dict[module]
			if last_send_time < (_now + datetime.timedelta(minutes=-20)):
				cls.__send_msg_dict[module] = _now
				monitor_msg._send_err_message(module, title, phone_list)
			else:
				sd.log_info('The module of {0} has send err message in last 20 minutes'.format(module))
				return None
	
	@classmethod
	def send_email(cls, module, title, receiver_email=None):
		global __send_email_dict
		_now = datetime.datetime.now()
		monitor_email = MonitEmialSender()
		if module not in cls.__send_email_dict:
			cls.__send_email_dict[module] = _now
			monitor_email._send_err_email(module, title, receiver_email)
		else:
			last_send_time = cls.__send_email_dict[module]
			if last_send_time < (_now + datetime.timedelta(minutes=-20)):
				cls.__send_email_dict[module] = _now
				monitor_email._send_err_email(module, title, receiver_email)
			else:
				sd.log_info('The module of {0} has send err email in last 20 minutes'.format(module))
				return None
		
		
		
		
		
		