Files
appointment_tool/logs/LogSender.py
T
2022-04-20 18:51:52 +02:00

124 lines
4.5 KiB
Python

import datetime
import logging
import sys
import oci
from oci.loggingingestion import LoggingClient
from oci.loggingingestion.models import PutLogsDetails, LogEntryBatch, LogEntry
import definitions
import params
from logs.AppLogging import init_logger
from pojo import ReserveResultPojo
from pojo.ReserveResultPojo import PublishType
# Log subjects
from pojo.serial_modem import SerialModem
LOG_SUBJECT_EVENT = "EVENT"
LOG_SUBJECT_SMS = "SMS"
SUBJECT_SIM_INFO = "sim_card"
# Log type
TYPE_EVENT_CHECK_RESULTS = "EVENT_CHECK_RESULTS"
TYPE_EVENT_RESET_ALL_SIM_CARDS = "EVENT_RESET_ALL_SIM_CARDS"
TYPE_EVENT_CHANGE_SLOT = "EVENT_CHANGE_SLOT"
TYPE_SMS_RECEIVED = "TYPE_SMS_RECEIVED"
LOG_ERROR = "ERROR"
LOG_TYPE_INFO = "INFO"
LOG_APPOINTMENT_ERROR = "APPOINTMENT_ERROR"
LOG_APPOINTMENT_TIMEOUT = "TIMEOUT"
LOG_APPOINTMENT_CONTACT_NOT_FOUND = "CONTACT_NOT_FOUND"
LOG_APPOINTMENT_SUCCESS = "SUCCESS"
custom_retry_strategy = oci.retry.RetryStrategyBuilder(
# Make up to 10 service calls
max_attempts_check=True,
max_attempts=10,
# Don't exceed a total of 600 seconds for all service calls
total_elapsed_time_check=True,
total_elapsed_time_seconds=600,
# Wait 45 seconds between attempts
retry_max_wait_between_calls_seconds=45,
# Use 2 seconds as the base number for doing sleep time calculations
retry_base_sleep_time_seconds=2,
# Retry on certain service errors:
#
# - 5xx code received for the request
# - Any 429 (this is signified by the empty array in the retry config)
# - 400s where the code is QuotaExceeded or LimitExceeded
service_error_check=True,
service_error_retry_on_any_5xx=True,
service_error_retry_config={
400: ['QuotaExceeded', 'LimitExceeded'],
429: []
},
# Use exponential backoff and retry with full jitter, but on throttles use
# exponential backoff and retry with equal jitter
backoff_type=oci.retry.BACKOFF_FULL_JITTER_EQUAL_ON_THROTTLE_VALUE
).get_retry_strategy()
class LogSender:
def __init__(self):
self._config = oci.config.from_file("~/.oci/logger_config_appointment")
self._identity = oci.identity.IdentityClient(self._config)
self._loggingingestion_client = LoggingClient(self._config, timeout=60.0, retry_strategy=custom_retry_strategy)
def send_appoint_result(self, result: ReserveResultPojo):
if result.type == PublishType.SUCCESS:
# get id
self.send_log(result.id, type=LOG_APPOINTMENT_SUCCESS)
else:
msg = "{}, email: {}".format(result.message, result.email)
self.send_log(msg, type=LOG_APPOINTMENT_ERROR)
def send_error(self, msg: str):
self.send_log(msg=msg, type=LOG_ERROR)
def send_log(self, msg: str, source=definitions.LOG_SOURCE, subject="appointment", type: str = "INFO"):
log_id = "ocid1.log.oc1.eu-frankfurt-1.amaaaaaas4ft22ya3ub6glkltqqbnmkxo3ui7xwq3dxtjd2scdhme4deyu2q"
response = self._loggingingestion_client.put_logs(
log_id=log_id,
put_logs_details=PutLogsDetails(
specversion="1.0",
log_entry_batches=[
LogEntryBatch(
entries=[LogEntry(
data=msg,
id=log_id,
time=datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"))],
source=source,
type=type,
defaultlogentrytime=datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
subject=subject)])
)
return response
def send_sms_reception_log(self, phone, sms_text, ccid):
msg = "from:{}, sms:{}, ccid:{}".format(phone, sms_text, ccid)
self.send_log(msg=msg, subject=LOG_SUBJECT_SMS, type=TYPE_SMS_RECEIVED)
def send_timeout_log(self, serial_modem: SerialModem):
msg = "phone:{}, ccid:{}".format(serial_modem.phone_number, serial_modem.ccid)
self.send_log(msg, type=LOG_APPOINTMENT_TIMEOUT)
def send_contact_not_found(self, msg: str):
self.send_log(msg, subject=SUBJECT_SIM_INFO, type=LOG_APPOINTMENT_CONTACT_NOT_FOUND)
def send_wait_sms_log(self):
self.send_log("等待短信", subject=LOG_SUBJECT_EVENT, type=TYPE_SMS_RECEIVED)
if __name__ == '__main__':
init_logger()
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
log_sender = LogSender()
response = log_sender.send_log("test")
print(response)