|
| 1 | +"""RETN parser.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import re |
| 5 | +from typing import Dict, List |
| 6 | + |
| 7 | +from bs4.element import ResultSet # type: ignore |
| 8 | +from dateutil import parser |
| 9 | + |
| 10 | +from circuit_maintenance_parser.parser import EmailSubjectParser, Html, Impact, Status |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class HtmlParserRETN1(Html): |
| 16 | + """Notifications Parser for RETN notifications.""" |
| 17 | + |
| 18 | + def parse_html(self, soup: ResultSet) -> List[Dict]: |
| 19 | + """Execute parsing.""" |
| 20 | + data: Dict[str] = {"circuits": []} |
| 21 | + self.parse_bold(soup.find_all("b"), data) |
| 22 | + self.parse_h3(soup.find_all("h3"), data) |
| 23 | + self.parse_h4(soup.find_all("h4"), data) |
| 24 | + |
| 25 | + return [data] |
| 26 | + |
| 27 | + def parse_h3(self, headers: ResultSet, data: Dict): |
| 28 | + """Parse H3 elements to find the status.""" |
| 29 | + for header in headers: |
| 30 | + if "Ticket Opened" in header.text: |
| 31 | + data["status"] = Status("CONFIRMED") |
| 32 | + elif "Ticket Resolved" in header.text: |
| 33 | + data["status"] = Status("COMPLETED") |
| 34 | + elif "New Update" in header.text: |
| 35 | + data["status"] = Status("IN-PROCESS") |
| 36 | + elif "Rescheduling Update" in header.text: |
| 37 | + data["status"] = Status("RE-SCHEDULED") |
| 38 | + |
| 39 | + if "status" not in data: |
| 40 | + data["status"] = Status("NO-CHANGE") |
| 41 | + |
| 42 | + def parse_h4(self, headers: ResultSet, data: Dict): |
| 43 | + """Parse H4 elements to find impact, CID's and summary.""" |
| 44 | + for header in headers: |
| 45 | + # CID's will follow and impact inside of this element |
| 46 | + if "service" in header.text.lower(): |
| 47 | + impact = Impact("NO-IMPACT") |
| 48 | + if "complete loss" in header.text: |
| 49 | + impact = Impact("OUTAGE") |
| 50 | + elif "50 ms flaps" in header.text: |
| 51 | + impact = Impact("REDUCED-REDUNDANCY") |
| 52 | + elif "services at risk" in header.text: |
| 53 | + impact = Impact("REDUCED-REDUNDANCY") |
| 54 | + elif "RTT increasing" in header.text: |
| 55 | + impact = Impact("REDUCED-REDUNDANCY") |
| 56 | + |
| 57 | + # Find CID's |
| 58 | + header_next = header.next_sibling |
| 59 | + while header_next: |
| 60 | + text = header_next.text.strip() |
| 61 | + if "description" in text.lower(): |
| 62 | + break |
| 63 | + |
| 64 | + if header_next.text.strip() != "": |
| 65 | + data["circuits"].append({"circuit_id": text, "impact": impact}) |
| 66 | + |
| 67 | + header_next = header_next.next_sibling |
| 68 | + |
| 69 | + # Summary will follow |
| 70 | + elif "Description" in header.text: |
| 71 | + data["summary"] = header.next_sibling.text.strip() |
| 72 | + |
| 73 | + def parse_bold(self, bolds: ResultSet, data: Dict): |
| 74 | + """Parse B (bold) elements to find start and end time.""" |
| 75 | + for bold in bolds: |
| 76 | + if "planned start time" in bold.text.lower(): |
| 77 | + data["start"] = self.dt2ts(parser.parse(bold.next_sibling.text.strip(), dayfirst=True)) |
| 78 | + elif "planned end time" in bold.text.lower(): |
| 79 | + data["end"] = self.dt2ts(parser.parse(bold.next_sibling.text.strip(), dayfirst=True)) |
| 80 | + |
| 81 | + |
| 82 | +class SubjectParserRETN1(EmailSubjectParser): |
| 83 | + """Parse the subject of an RETN circuit maintenance email. The subject contains the maintenance ID and account.""" |
| 84 | + |
| 85 | + def parse_subject(self, subject: str) -> List[Dict]: |
| 86 | + """Parse the RETN Email subject for maintenance ID and account. |
| 87 | +
|
| 88 | + Example subject line: [RETN.NET Ticket#PW-40135152]: Planned Works Notification CID#133337 |
| 89 | +
|
| 90 | + """ |
| 91 | + data = {} |
| 92 | + parse_subject = re.search(r"Ticket#([A-Z]+-[0-9]+)](.*)CID#([0-9]+)", subject) |
| 93 | + if parse_subject: |
| 94 | + data["maintenance_id"] = parse_subject[1] |
| 95 | + data["account"] = parse_subject[3] |
| 96 | + |
| 97 | + return [data] |
0 commit comments