|
| 1 | +"""Apple peering parser.""" |
| 2 | +import email |
| 3 | +import re |
| 4 | + |
| 5 | +from datetime import datetime, timezone |
| 6 | +from typing import Dict, List |
| 7 | + |
| 8 | +from circuit_maintenance_parser.output import Impact, Status |
| 9 | +from circuit_maintenance_parser.parser import EmailSubjectParser, Text, CircuitImpact |
| 10 | + |
| 11 | + |
| 12 | +class SubjectParserApple(EmailSubjectParser): |
| 13 | + """Subject parser for Apple notification.""" |
| 14 | + |
| 15 | + def parse_subject(self, subject: str) -> List[Dict]: |
| 16 | + """Use the subject of the email as summary. |
| 17 | +
|
| 18 | + Args: |
| 19 | + subject (str): Message subjects |
| 20 | +
|
| 21 | + Returns: |
| 22 | + List[Dict]: List of attributes for Maintenance object |
| 23 | + """ |
| 24 | + return [{"summary": subject}] |
| 25 | + |
| 26 | + |
| 27 | +class TextParserApple(Text): |
| 28 | + """Parse the plaintext content of an Apple notification. |
| 29 | +
|
| 30 | + Args: |
| 31 | + Text (str): Plaintext message |
| 32 | + """ |
| 33 | + |
| 34 | + def parse_text(self, text: str) -> List[Dict]: |
| 35 | + """Extract attributes from an Apple notification email. |
| 36 | +
|
| 37 | + Args: |
| 38 | + text (str): plaintext message |
| 39 | +
|
| 40 | + Returns: |
| 41 | + List[Dict]: List of attributes for Maintenance object |
| 42 | + """ |
| 43 | + data = { |
| 44 | + "circuits": self._circuits(text), |
| 45 | + "maintenance_id": self._maintenance_id(text), |
| 46 | + "start": self._start_time(text), |
| 47 | + "stamp": self._start_time(text), |
| 48 | + "end": self._end_time(text), |
| 49 | + "status": Status.CONFIRMED, # Have yet to see anything but confirmation. |
| 50 | + |
| 51 | + "provider": "apple", |
| 52 | + "account": "Customer info unavailable", |
| 53 | + } |
| 54 | + return [data] |
| 55 | + |
| 56 | + def _circuits(self, text): |
| 57 | + pattern = r"Peer AS: (\d*)" |
| 58 | + match = re.search(pattern, text) |
| 59 | + return [CircuitImpact(circuit_id=f"AS{match.group(1)}", impact=Impact.OUTAGE)] |
| 60 | + |
| 61 | + def _maintenance_id(self, text): |
| 62 | + # Apple ticket numbers always starts with "CHG". |
| 63 | + pattern = r"CHG(\d*)" |
| 64 | + match = re.search(pattern, text) |
| 65 | + return match.group(0) |
| 66 | + |
| 67 | + def _get_time(self, pattern, text): |
| 68 | + # Apple sends timestamps as RFC2822 for the US |
| 69 | + # but a custom format for EU datacenters. |
| 70 | + match = re.search(pattern, text) |
| 71 | + try: |
| 72 | + # Try EU timestamp |
| 73 | + return int( |
| 74 | + datetime.strptime(match.group(1), "%Y-%m-%d(%a) %H:%M %Z").replace(tzinfo=timezone.utc).timestamp() |
| 75 | + ) |
| 76 | + except ValueError: |
| 77 | + # Try RFC2822 - US timestamp |
| 78 | + rfc2822 = match.group(1) |
| 79 | + time_tuple = email.utils.parsedate_tz(rfc2822) |
| 80 | + return email.utils.mktime_tz(time_tuple) |
| 81 | + |
| 82 | + def _start_time(self, text): |
| 83 | + pattern = "Start Time: ([a-zA-Z0-9 :()-]*)" |
| 84 | + return self._get_time(pattern, text) |
| 85 | + |
| 86 | + def _end_time(self, text): |
| 87 | + pattern = "End Time: ([a-zA-Z0-9 :()-]*)" |
| 88 | + return self._get_time(pattern, text) |
0 commit comments