Skip to content

Commit

Permalink
Tata parser (#297)
Browse files Browse the repository at this point in the history
* add tata parser

* add tata parser unit tests

* fix bs4.element import-untyped and _parse_time method return-value

* correct black code formatting

* ignore pylint c0104 disallowed-name in circuit_maintenance_parser/parsers/tata.py

* add tata parser in README.md

* avoid computing lower() every time
  • Loading branch information
snitass authored Nov 29, 2024
1 parent 82efe55 commit ffd24f5
Show file tree
Hide file tree
Showing 36 changed files with 1,418 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ By default, there is a `GenericProvider` that supports a `SimpleProcessor` using
- Netflix (AS2906 only)
- Seaborn
- Sparkle
- Tata
- Telstra (\*)
- Turkcell
- Verizon
Expand Down
2 changes: 2 additions & 0 deletions circuit_maintenance_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
PacketFabric,
Seaborn,
Sparkle,
Tata,
Telia,
Telstra,
Turkcell,
Expand Down Expand Up @@ -59,6 +60,7 @@
PacketFabric,
Seaborn,
Sparkle,
Tata,
Telia,
Telstra,
Turkcell,
Expand Down
71 changes: 71 additions & 0 deletions circuit_maintenance_parser/parsers/tata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# pylint: disable=disallowed-name
"""Circuit maintenance parser for Tata Email notifications."""
from typing import List, Dict, Any
from datetime import datetime

from bs4.element import ResultSet # type: ignore
from circuit_maintenance_parser.output import Impact, Status
from circuit_maintenance_parser.parser import Html, EmailSubjectParser


class HtmlParserTata(Html):
"""Custom Parser for HTML portion of Tata circuit maintenance notifications."""

def parse_html(self, soup: ResultSet) -> List[Dict]:
"""Parse Tata circuit maintenance email."""
prev: str = ""
data: Dict[str, Any] = {
"account": "N/A",
"circuits": [],
"organizer": soup.select("a[href^=mailto]")[0].text.strip(),
}
for span in soup.find_all("span"):
curr = span.text.strip()
if curr != prev:
prev_lower = prev.lower()
if prev_lower == "ticket reference - tcl":
data["maintenance_id"] = curr
elif prev_lower == "service id":
for circuit in curr.split(","):
data["circuits"].append(
{
"circuit_id": circuit.strip(),
"impact": Impact.OUTAGE,
}
)
elif prev_lower in ("activity window (gmt)", "revised activity window (gmt)"):
start_end = curr.split("to")
data["start"] = self._parse_time(start_end[0])
data["end"] = self._parse_time(start_end[1])
elif "extended up to time window" in prev_lower:
if "gmt" in curr.lower():
data["end"] = self._parse_time(curr)
prev = span.text.strip()

return [data]

@staticmethod
def _parse_time(string: str) -> int:
"""Convert YYYY-MM-DD HH:MM:SS GMT to epoch."""
return int((datetime.strptime(string.strip(), "%Y-%m-%d %H:%M:%S GMT") - datetime(1970, 1, 1)).total_seconds())


class SubjectParserTata(EmailSubjectParser):
"""Custom Parser for Email subject of Tata circuit maintenance notifications."""

def parse_subject(self, subject: str) -> List[Dict]:
"""Parse Tata Email subject for summary and status."""
data: Dict[str, Any] = {"summary": subject.strip().replace("\n", "")}
subject_lower = subject.lower()
if "completion" in subject_lower:
data["status"] = Status.COMPLETED
elif "reschedule" in subject_lower or "extension" in subject_lower:
data["status"] = Status.RE_SCHEDULED
elif "reminder" in subject_lower:
data["status"] = Status.CONFIRMED
elif "cancellation" in subject_lower:
data["status"] = Status.CANCELLED
else:
data["status"] = Status.CONFIRMED

return [data]
14 changes: 14 additions & 0 deletions circuit_maintenance_parser/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
SubjectParserSeaborn2,
)
from circuit_maintenance_parser.parsers.sparkle import HtmlParserSparkle1
from circuit_maintenance_parser.parsers.tata import HtmlParserTata, SubjectParserTata
from circuit_maintenance_parser.parsers.telstra import HtmlParserTelstra1, HtmlParserTelstra2
from circuit_maintenance_parser.parsers.turkcell import HtmlParserTurkcell1
from circuit_maintenance_parser.parsers.verizon import HtmlParserVerizon1
Expand Down Expand Up @@ -428,6 +429,19 @@ class Sparkle(GenericProvider):
_default_organizer = PrivateAttr("[email protected]")


class Tata(GenericProvider):
"""Tata provider custom class."""

_include_filter = PrivateAttr({EMAIL_HEADER_SUBJECT: ["Planned Work Notification"]})

_processors: List[GenericProcessor] = PrivateAttr(
[
CombinedProcessor(data_parsers=[HtmlParserTata, SubjectParserTata, EmailDateParser]),
]
)
_default_organizer = PrivateAttr("[email protected]")


class Telia(Arelion):
"""Telia provider custom class."""

Expand Down
102 changes: 102 additions & 0 deletions tests/unit/data/tata/tata_body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="background-color:#FFEB9C; width:100%; padding:2pt; font-size:10pt; line-height:12pt; font-family:'Calibri'; color:Black; text-align: left;">
<span style="color:#9C6500; font-weight:bold;">CAUTION:</span> This email has been sent from an external source. Do not click links, open attachments, or provide sensitive business information unless you can verify the sender’s legitimacy.
</div>
<br>
<div>
<p></p>
<p></p>
<p></p>
<p><span style="color: #000000; font-size: 10pt; font-family: tahoma, arial, helvetica, sans-serif;">Dear&nbsp;Customer,</span></p>
<p>&nbsp;</p>
<p><span style="color: #000000; font-size: 10pt; font-family: tahoma, arial, helvetica, sans-serif;">Tata Communication is in receipt of maintenance activity. The details of maintenance activity are as below. Your below mentioned service would experience an
outage of “8 Days 23 Hours 58 Minutes” during the activity window.</span></p>
<p>&nbsp;</p>
<table style="height: 42px; border-color: #000000;" border="0" width="623" cellspacing="0">
<tbody>
<tr style="background-color: #4b85c5;">
<td style="border: none; text-align: left; padding-left: 5px;"><span style="font-size: 11pt; color: #ffffff; font-family: helvetica;"><strong>TATA COMMUNICATIONS</strong></span></td>
<td style="border: 0.5px #bbbbbb;">&nbsp;<img style="align: baseline;" title="" src="cid:[email protected]" alt="" width="44" height="38" align="right" border="" hspace="10" vspace=""></td>
</tr>
</tbody>
</table>
<table style="height: 38px; border-color: #000000;" border="1" width="620" cellspacing="0">
<tbody>
<tr>
<td style="background-color: #4b85c5;" colspan="2"><span style="color: #ffffff; font-family: arial, helvetica, sans-serif; font-size: 12pt;">&nbsp;Planned Work Notification</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">Ticket Reference - TCL</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">CHGP0123456</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">Service ID</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">A012345678901234567, B012345678901234567</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">NIMS ID</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">F0123-45678-901, F0123-45678-901</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Maintenance Type</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Normal</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Activity Status</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Scheduled</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Execution Owner</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">International Cables</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Location of activity</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Mumbai</span></td>
</tr>
<tr>
<td style="background-color: #4b85c5;" colspan="2">&nbsp;</td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Activity Window (IST)</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">2024-12-22 05:31:00 IST to 2024-12-31 05:29:00 IST</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Activity Window (GMT)</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">2024-12-22 00:01:00 GMT to 2024-12-30 23:59:00 GMT</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Expected Impact Duration(DD:HH:MM) :</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">8 Days 23 Hours 58 Minutes</span></td>
</tr>
<tr>
<td style="background-color: #4b85c5;" colspan="2">&nbsp;</td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">Activity Description</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Planned activity for shunt fault repair of Seg 11 near Mumbai in TGNEA network.</span></td>
</tr>
<tr>
<td colspan="2">
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">We request you to reschedule sensitive operations at your end accordingly.<br>
We apologize for any inconvenience due to this event and resulting downtime.<br>
If you have any queries with respect to this activity, feel free to contact us on the coordinates mentioned below:<br>
Mail ID :<a href="mailto:[email protected]">[email protected]</a><br>
Contact Number : + 91 20 6614 4444 &amp; Toll Free no: 1-8002660660<br>
We look forward to your co-operation and a long term synergic association.<br>
Best Regards,<br>
Customer Service<br>
TATA COMMUNICATIONS</span></p>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<div>&nbsp;</div>
<div style="display:inline">Ref:MSGTCL000142929801</div>
</div>
</body>
</html>

19 changes: 19 additions & 0 deletions tests/unit/data/tata/tata_body_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"account": "N/A",
"circuits": [
{
"circuit_id": "A012345678901234567",
"impact": "OUTAGE"
},
{
"circuit_id": "B012345678901234567",
"impact": "OUTAGE"
}
],
"end": 1735603140,
"maintenance_id": "CHGP0123456",
"organizer": "[email protected]",
"start": 1734825660
}
]
103 changes: 103 additions & 0 deletions tests/unit/data/tata/tata_cancellation_body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="background-color:#FFEB9C; width:100%; padding:2pt; font-size:10pt; line-height:12pt; font-family:'Calibri'; color:Black; text-align: left;">
<span style="color:#9C6500; font-weight:bold;">CAUTION:</span> This email has been sent from an external source. Do not click links, open attachments, or provide sensitive business information unless you can verify the sender’s legitimacy.
</div>
<br>
<div>
<p></p>
<p></p>
<p></p>
<p><span style="color: black; font-family: tahoma, arial, helvetica, sans-serif; font-size: 10pt;">Dear Customer,</span></p>
<p><span style="color: black; font-family: tahoma, arial, helvetica, sans-serif; font-size: 10pt;">Please note that this activity has been cancelled.
</span></p>
<p><span style="color: black; font-family: tahoma, arial, helvetica, sans-serif; font-size: 10pt;">The details for the same have been mentioned below:</span><strong><span style="color: black;">&nbsp;
</span></strong></p>
<table style="height: 42px; border-color: #000000;" border="0" width="623" cellspacing="0">
<tbody>
<tr style="background-color: #4b85c5;">
<td style="border: none; text-align: left; padding-left: 5px;"><span style="font-size: 11pt; color: #ffffff; font-family: helvetica;"><strong>TATA COMMUNICATIONS</strong></span></td>
<td style="border: 0.5px #bbbbbb;">&nbsp;<img style="align: baseline;" title="" src="cid:[email protected]" alt="" width="44" height="38" align="right" border="" hspace="10" vspace=""></td>
</tr>
</tbody>
</table>
<table style="height: 38px; border-color: #000000;" border="1" width="620" cellspacing="0">
<tbody>
<tr>
<td style="background-color: #4b85c5;" colspan="2"><span style="color: #ffffff; font-family: arial, helvetica, sans-serif; font-size: 12pt;">&nbsp;Planned Work Notification</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">Ticket Reference - TCL</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">CHGP0123456</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">Service ID</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">01234567890123456789</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">NIMS ID</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"></span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Maintenance Type</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Normal</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Activity Status</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Canceled</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Execution Owner</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">3rd party service Provider</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Location of activity</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">LONDON</span></td>
</tr>
<tr>
<td style="background-color: #4b85c5;" colspan="2">&nbsp;</td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Activity Window (IST)</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">2024-11-21 04:30:00 IST to 2024-11-22 10:30:00 IST</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Activity Window (GMT)</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">2024-11-20 23:00:00 GMT to 2024-11-22 05:00:00 GMT</span></td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><strong><span style="color: black;">Expected Impact Duration(DD:HH:MM) :</span></strong></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">6 Hours</span></td>
</tr>
<tr>
<td style="background-color: #4b85c5;" colspan="2">&nbsp;</td>
</tr>
<tr>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong><span style="color: black;">Activity Description</span></strong></span></td>
<td><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">&nbsp;Third party service provider will execute network maintenance activity for Network redevelopment.<br>
Services will be impacted for 6 hrs on each Days.</span></td>
</tr>
<tr>
<td colspan="2">
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">We request you to reschedule sensitive operations at your end accordingly.<br>
We apologize for any inconvenience due to this event and resulting downtime.<br>
If you have any queries with respect to this activity, feel free to contact us on the coordinates mentioned below:<br>
Mail ID :<a href="mailto:[email protected]">[email protected]</a><br>
Contact Number : + 91 20 6614 4444 &amp; Toll Free no: 1-8002660660<br>
We look forward to your co-operation and a long term synergic association.<br>
Best Regards,<br>
Customer Service<br>
TATA COMMUNICATIONS</span></p>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<div>&nbsp;</div>
<div style="display:inline">Ref:MSGTCL000143017654</div>
</div>
</body>
</html>

15 changes: 15 additions & 0 deletions tests/unit/data/tata/tata_cancellation_body_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"account": "N/A",
"circuits": [
{
"circuit_id": "01234567890123456789",
"impact": "OUTAGE"
}
],
"end": 1732251600,
"maintenance_id": "CHGP0123456",
"organizer": "[email protected]",
"start": 1732143600
}
]
1 change: 1 addition & 0 deletions tests/unit/data/tata/tata_cancellation_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cancellation Notification /TCL TT: "CHGP0123456" / "Normal" / "Canceled" / Planned Work Notification
6 changes: 6 additions & 0 deletions tests/unit/data/tata/tata_cancellation_subject_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"status": "CANCELLED",
"summary": "Cancellation Notification /TCL TT: \"CHGP0123456\" / \"Normal\" / \"Canceled\" / Planned Work Notification"
}
]
Loading

0 comments on commit ffd24f5

Please sign in to comment.