|
| 1 | +"""Circuit Maintenance Parser for Equinix Email Notifications.""" |
| 2 | +from typing import Any, Dict, List |
| 3 | +import re |
| 4 | + |
| 5 | +from bs4.element import ResultSet # type: ignore |
| 6 | +from dateutil import parser |
| 7 | + |
| 8 | +from circuit_maintenance_parser.output import Impact |
| 9 | +from circuit_maintenance_parser.parser import Html, EmailSubjectParser, Status |
| 10 | + |
| 11 | + |
| 12 | +class HtmlParserEquinix(Html): |
| 13 | + """Custom Parser for HTML portion of Equinix circuit maintenance notifications.""" |
| 14 | + |
| 15 | + def parse_html(self, soup: ResultSet) -> List[Dict]: |
| 16 | + """Parse an equinix circuit maintenance email. |
| 17 | +
|
| 18 | + Args: |
| 19 | + soup (ResultSet): beautiful soup object containing the html portion of an email. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + Dict: The data dict containing circuit maintenance data. |
| 23 | + """ |
| 24 | + data: Dict[str, Any] = {"circuits": list()} |
| 25 | + |
| 26 | + impact = self._parse_b(soup.find_all("b"), data) |
| 27 | + self._parse_table(soup.find_all("th"), data, impact) |
| 28 | + return [data] |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def _isascii(string): |
| 32 | + """Python 3.6 compatible way to determine if string is only english characters. |
| 33 | +
|
| 34 | + Args: |
| 35 | + string (str): string to test if only ascii chars. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + bool: Returns True if string is ascii only, returns false if the string contains extended unicode characters. |
| 39 | + """ |
| 40 | + try: |
| 41 | + string.encode("ascii") |
| 42 | + return True |
| 43 | + except UnicodeEncodeError: |
| 44 | + return False |
| 45 | + |
| 46 | + def _parse_b(self, b_elements, data): |
| 47 | + """Parse the <b> elements from the notification to capture start and end times, description, and impact. |
| 48 | +
|
| 49 | + Args: |
| 50 | + b_elements (): resulting soup object with all <b> elements |
| 51 | + data (Dict): data from the circuit maintenance |
| 52 | +
|
| 53 | + Returns: |
| 54 | + impact (Status object): impact of the maintenance notification (used in the parse table function to assign an impact for each circuit). |
| 55 | + """ |
| 56 | + for b_elem in b_elements: |
| 57 | + if "UTC:" in b_elem: |
| 58 | + raw_time = b_elem.next_sibling |
| 59 | + # for non english equinix notifications |
| 60 | + # english section is usually at the bottom |
| 61 | + # this skips the non english line at the top |
| 62 | + if not self._isascii(raw_time): |
| 63 | + continue |
| 64 | + start_end_time = raw_time.split("-") |
| 65 | + if len(start_end_time) == 2: |
| 66 | + data["start"] = self.dt2ts(parser.parse(raw_time.split("-")[0].strip())) |
| 67 | + data["end"] = self.dt2ts(parser.parse(raw_time.split("-")[1].strip())) |
| 68 | + # all circuits in the notification share the same impact |
| 69 | + if "IMPACT:" in b_elem: |
| 70 | + impact_line = b_elem.next_sibling |
| 71 | + if "No impact to your service" in impact_line: |
| 72 | + impact = Impact.NO_IMPACT |
| 73 | + elif "There will be service interruptions" in impact_line.next_sibling.text: |
| 74 | + impact = Impact.OUTAGE |
| 75 | + return impact |
| 76 | + |
| 77 | + def _parse_table(self, theader_elements, data, impact): # pylint: disable=no-self-use |
| 78 | + for th_elem in theader_elements: |
| 79 | + if "Account #" in th_elem: |
| 80 | + circuit_table = th_elem.find_parent("table") |
| 81 | + for tr_elem in circuit_table.find_all("tr"): |
| 82 | + if tr_elem.find(th_elem): |
| 83 | + continue |
| 84 | + circuit_info = list(tr_elem.find_all("td")) |
| 85 | + if circuit_info: |
| 86 | + account, _, circuit = circuit_info # pylint: disable=unused-variable |
| 87 | + data["circuits"].append( |
| 88 | + {"circuit_id": circuit.text, "impact": impact,} |
| 89 | + ) |
| 90 | + data["account"] = account.text |
| 91 | + |
| 92 | + |
| 93 | +class SubjectParserEquinix(EmailSubjectParser): |
| 94 | + """Parse the subject of an equinix circuit maintenance email. The subject contains the maintenance ID and status.""" |
| 95 | + |
| 96 | + def parse_subject(self, subject: str) -> List[Dict]: |
| 97 | + """Parse the Equinix Email subject for summary and status. |
| 98 | +
|
| 99 | + Args: |
| 100 | + subject (str): subject of email |
| 101 | + e.g. 'Scheduled software upgrade in metro connect platform-SG Metro Area Network Maintenance -19-OCT-2021 [5-212760022356]'. |
| 102 | +
|
| 103 | +
|
| 104 | + Returns: |
| 105 | + List[Dict]: Returns the data object with summary and status fields. |
| 106 | + """ |
| 107 | + data = {} |
| 108 | + maintenance_id = re.search(r"\[(.*)\]$", subject) |
| 109 | + if maintenance_id: |
| 110 | + data["maintenance_id"] = maintenance_id[1] |
| 111 | + data["summary"] = subject.strip().replace("\n", "") |
| 112 | + if "COMPLETED" in subject: |
| 113 | + data["status"] = Status.COMPLETED |
| 114 | + if "SCHEDULED" in subject or "REMINDER" in subject: |
| 115 | + data["status"] = Status.CONFIRMED |
| 116 | + return [data] |
0 commit comments