Skip to content

Commit 1bd1ffe

Browse files
aliex-13chadell
andauthored
Move epoch parser outside of LLM due to inconsistencies (#312)
* Move epoch parser outside of LLM due to inconsistencies * Fix linter errors * Fix exception handling on epoch function * Fix logging debugger * Change logger to error.py Co-authored-by: Christian Adell <chadell@gmail.com> --------- Co-authored-by: Christian Adell <chadell@gmail.com>
1 parent 9a5f20d commit 1bd1ffe

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

circuit_maintenance_parser/parser.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import calendar
66
import datetime
77
import quopri
8+
import hashlib
89
from typing import Dict, List
910
from email.utils import parsedate_tz, mktime_tz
10-
import hashlib
11+
from dateutil.parser import isoparse
1112

1213
import bs4 # type: ignore
1314
from bs4.element import ResultSet # type: ignore
@@ -297,15 +298,15 @@ class LLM(Parser):
297298
_data_types = PrivateAttr(["text/html", "html", "text/plain"])
298299

299300
_llm_question = """Please, could you extract a JSON form without any other comment,
300-
with the following JSON schema (timestamps in EPOCH and taking into account the GMT offset):
301+
with the following JSON schema (start and end times are datetime objects and should be displayed in UTC):
301302
{
302303
"type": "object",
303304
"properties": {
304305
"start": {
305-
"type": "int",
306+
"type": "datetime",
306307
},
307308
"end": {
308-
"type": "int",
309+
"type": "datetime",
309310
},
310311
"account": {
311312
"type": "string",
@@ -408,6 +409,17 @@ def _get_circuit_ids(self, generated_json: dict, impact: Impact):
408409

409410
return circuits
410411

412+
def _convert_str_datetime_to_epoch(self, time_str):
413+
try:
414+
# Using isoparse for flexible ISO8601 parsing
415+
time_dt = isoparse(time_str).astimezone(datetime.timezone.utc)
416+
epoch_time = int(time_dt.timestamp())
417+
return epoch_time
418+
419+
except Exception as error_received:
420+
logger.error("Error parsing dates: %s", error_received)
421+
raise ParserError from error_received
422+
411423
def _get_start(self, generated_json: dict):
412424
"""Method to get the Start Time."""
413425
return generated_json[self.get_key_with_string(generated_json, "start")]
@@ -460,6 +472,10 @@ def parse_content(self, content):
460472
if not generated_json:
461473
return []
462474

475+
if generated_json.get("start") and generated_json.get("end"):
476+
generated_json["start"] = self._convert_str_datetime_to_epoch(generated_json["start"])
477+
generated_json["end"] = self._convert_str_datetime_to_epoch(generated_json["end"])
478+
463479
impact = self._get_impact(generated_json)
464480

465481
data = {

0 commit comments

Comments
 (0)