Skip to content

Commit

Permalink
Merge pull request #19 from PinoutLTD/development
Browse files Browse the repository at this point in the history
add reports. search for a ticket by description
  • Loading branch information
tubleronchik authored Jul 3, 2024
2 parents 1239b20 + 59e72af commit d547e6d
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 31 deletions.
5 changes: 3 additions & 2 deletions rrs_operator/rrs_operator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from rrs_operator.src.robonomics import RobonomicsHelper
from rrs_operator.src.odoo import Odoo


class Operator:
def __init__(self) -> None:
self.odoo = Odoo()
self.robonomics = RobonomicsHelper(self.odoo)
self.robonomics.subscribe()

def get_robonomics_add_user_callback(self) -> None:
return self.robonomics.add_user_callback
return self.robonomics.add_user_callback
17 changes: 12 additions & 5 deletions rrs_operator/src/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from tenacity import *
from helpers.logger import Logger
from utils.decryption import decrypt_message
from rrs_operator.utils.reports import ReportsFabric

load_dotenv()

Expand Down Expand Up @@ -63,16 +64,22 @@ def _download_logs(self, hash: str) -> None:
for i in range(1, pictures_count + 1):
self._download_file(hash, f"picture{i}")

def parse_logs(self, hash) -> tuple:
def parse_logs(self, hash) -> list:
"""Parse description file."""
self._download_logs(hash)
self._logger.info(f"IPFS: Parsing logs... Hash: {hash}")
with open(f"{self.temp_dir}/issue_description.json") as f:
issue = json.load(f)
phone = issue["phone_number"]
description = issue["description"]
return phone, description
if isinstance(issue["description"], dict):
description_type = issue["description"]["type"]
unparsed_description = issue["description"]["description"]
else:
description_type = "errors"
unparsed_description = issue["description"]
report = ReportsFabric.get_report(description_type)
description = report.get_descriptions(unparsed_description)
return description

def clean_temp_dir(self) -> None:
"""Remove the temporary directory and its content"""
shutil.rmtree(self.temp_dir)
shutil.rmtree(self.temp_dir)
26 changes: 18 additions & 8 deletions rrs_operator/src/odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@

class Odoo:
"""Odoo for Operator"""

def __init__(self) -> None:
self.helper = OdooHelper("operator")
self._logger = Logger("odoo-registar")

@retry(wait=wait_fixed(5))
def create_ticket(
self, email: str, robonomics_address: str, phone: str, description: str, ipfs_hash: str
) -> tp.Optional[int]:
def create_ticket(self, email: str, robonomics_address: str, description: str, ipfs_hash: str) -> tp.Optional[int]:
"""Creates ticket in Helpdesk module
:param email: Customer's email address
:param robonomics_address: Customer's address in Robonomics parachain
:param phone: Customer's phone number
:param description: Problem's description from cusotmer
:return: Ticket id
Expand All @@ -39,7 +37,6 @@ def create_ticket(
"priority": priority,
"channel_id": channel_id,
"partner_email": email,
"phone": phone,
},
)
self._logger.debug(f"Ticket created. Ticket id: {ticket_id}")
Expand Down Expand Up @@ -97,7 +94,7 @@ def find_user_email(self, address) -> tp.Optional[str]:
self._logger.debug(f"user id: {user_id}")
if user_id:
user_data = self.helper.read(model="rrs.register", record_ids=user_id, fields=["customer_email"])
email = user_data[0]['customer_email']
email = user_data[0]["customer_email"]
self._logger.debug(f"Find user's email: {email}")
return email
else:
Expand All @@ -106,7 +103,6 @@ def find_user_email(self, address) -> tp.Optional[str]:
except Exception as e:
self._logger.error(f"Couldn't find email {e}")
raise Exception("Failed to find email")


def _find_user_id(self, address: str) -> list:
"""Find a user id by the parachain address. This id is used to retrive the user's email.
Expand All @@ -116,4 +112,18 @@ def _find_user_id(self, address: str) -> list:
"""
id = self.helper.search(model="rrs.register", search_domains=[("address", "=", address)])
self._logger.debug(f"Find user with id: {id}")
return id
return id

def find_ticket_with_description(self, description: str, email: str) -> int:
""" """
description = f"Issue from HA: {description}"
self._logger.debug(f"Looking for a ticket for email: {email}, description: {description}")
ticket_ids = self.helper.search(
model="helpdesk.ticket", search_domains=[("description", "=", description), ("partner_email", "=", email)]
)
self._logger.debug(f"Ticket ids: {ticket_ids}")

if ticket_ids:
self._logger.debug(f"Found tickets with the description: {ticket_ids}")
return ticket_ids[0]
self._logger.debug(f"No ticket found")
35 changes: 19 additions & 16 deletions rrs_operator/src/robonomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, odoo) -> None:
self.odoo = odoo
self.users = self.rws.get_devices(ADMIN_ADDRESS)
self._track_free_weight()

def add_user_callback(self, address: str) -> None:
"""Updates the list of users to track new datalogs for
:param address: Address to add
Expand All @@ -47,8 +47,6 @@ def _on_new_record(self, data: tp.Tuple[tp.Union[str, tp.List[str]]]) -> None:
"""

try:
self._logger.debug(f"New datalogs: {data}")
self._logger.debug(f"users: {self.users}")
if data[0] in self.users:
hash = data[2]
self._logger.info(f"Ipfs hash: {hash}")
Expand All @@ -71,17 +69,20 @@ def _handle_data(self, ipfs_hash: str, robonomics_address_from: str) -> None:
"""
ipfs = IPFSHelpder(robonomics_address_from)
email = self.odoo.find_user_email(robonomics_address_from)
phone, description = ipfs.parse_logs(ipfs_hash)
self._logger.debug(f"Data from ipfs: {email}, {phone}, {description}")
ticket_id = self.odoo.create_ticket(email, robonomics_address_from, phone, description, ipfs_hash)
if len(os.listdir(ipfs.temp_dir)) > 1:
for f in os.listdir(ipfs.temp_dir):
if f == "issue_description.json":
pass
else:
file_name = f
file_path = f"{ipfs.temp_dir}/{f}"
self.odoo.create_note_with_attachment(ticket_id, file_name, file_path)
descriptions_list = ipfs.parse_logs(ipfs_hash)
self._logger.debug(f"Data from ipfs: {email}, {descriptions_list}")
for description in descriptions_list:
ticket_id = self.odoo.find_ticket_with_description(description, email)
if not ticket_id:
ticket_id = self.odoo.create_ticket(email, robonomics_address_from, description, ipfs_hash)
if len(os.listdir(ipfs.temp_dir)) > 1:
for f in os.listdir(ipfs.temp_dir):
if f == "issue_description.json":
pass
else:
file_name = f
file_path = f"{ipfs.temp_dir}/{f}"
self.odoo.create_note_with_attachment(ticket_id, file_name, file_path)
ipfs.clean_temp_dir()

def _resubscribe(self) -> None:
Expand All @@ -104,7 +105,9 @@ def _is_subscription_alive(self) -> None:

def _track_free_weight(self) -> None:
"""Track free weight of the subscription"""
threading.Timer(60, self._track_free_weight,).start()
threading.Timer(
60,
self._track_free_weight,
).start()
free_weight = self.rws.get_ledger(ADMIN_ADDRESS)
self._logger.debug(f"Free weight in subscription: {free_weight}")

1 change: 1 addition & 0 deletions rrs_operator/utils/reports/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .reports_fabric import ReportsFabric
15 changes: 15 additions & 0 deletions rrs_operator/utils/reports/reports_fabric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .src import Report
from .src import WarningsReport, ErrorsReport, UnrespondedDevicesReport


class ReportsFabric:
"""Fabric to select Report based on its type"""

@staticmethod
def get_report(type: str) -> Report:
if type == "warnings":
return WarningsReport()
if type == "errors":
return ErrorsReport()
if type == "unresponded_devices":
return UnrespondedDevicesReport()
4 changes: 4 additions & 0 deletions rrs_operator/utils/reports/src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .report import Report
from .warnings import WarningsReport
from .errors import ErrorsReport
from .unresponded_devices import UnrespondedDevicesReport
8 changes: 8 additions & 0 deletions rrs_operator/utils/reports/src/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .report import Report


class ErrorsReport(Report):
"""Class for reports with errors"""

def get_descriptions(self, unparsed_description: str) -> list:
return super().get_descriptions(unparsed_description)
9 changes: 9 additions & 0 deletions rrs_operator/utils/reports/src/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from abc import ABC, abstractmethod


class Report(ABC):
"""Base class (interface) for reports"""

@abstractmethod
def get_descriptions(self, unparsed_description: str) -> list:
return [unparsed_description]
9 changes: 9 additions & 0 deletions rrs_operator/utils/reports/src/unresponded_devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .report import Report


class UnrespondedDevicesReport(Report):
"""Class for reports with unresponded devices"""

def get_descriptions(self, unparsed_description: str) -> list:
devices = unparsed_description.split("*")
return devices
9 changes: 9 additions & 0 deletions rrs_operator/utils/reports/src/warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .report import Report


class WarningsReport(Report):
"""Class for reports with warnings"""

def get_descriptions(self, unparsed_description: str) -> list:
warnigns = unparsed_description.split("*")
return warnigns

0 comments on commit d547e6d

Please sign in to comment.