Skip to content

Commit

Permalink
Fixes per pre-commit messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwjordan committed Aug 21, 2024
1 parent 9a37a7f commit 60de2cd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
74 changes: 50 additions & 24 deletions backend/danswer/connectors/zendesk/connector.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Any
import time

import requests
from retry import retry
from zenpy import Zenpy # type: ignore
from zenpy.lib.api_objects import Ticket, Comment # type: ignore
from zenpy.lib.api_objects import Ticket
from zenpy.lib.api_objects.help_centre_objects import Article # type: ignore

from danswer.configs.app_configs import INDEX_BATCH_SIZE
Expand All @@ -25,6 +24,7 @@
# Define a constant for the time delay
TIME_DELAY_SECONDS = 5


def _article_to_document(article: Article, content_tags: dict[str, str]) -> Document:
author = BasicExpertInfo(
display_name=article.author.name, email=article.author.email
Expand Down Expand Up @@ -56,15 +56,16 @@ def _article_to_document(article: Article, content_tags: dict[str, str]) -> Docu
metadata=metadata,
)


class ZendeskClientNotSetUpError(PermissionError):
def __init__(self) -> None:
super().__init__("Zendesk Client is not set up, was load_credentials called?")


class ZendeskConnector(LoadConnector, PollConnector):
def __init__(
self,
batch_size: int = INDEX_BATCH_SIZE,
self,
batch_size: int = INDEX_BATCH_SIZE,
content_type: str = "articles",
) -> None:
self.batch_size = batch_size
Expand Down Expand Up @@ -135,12 +136,11 @@ def _ticket_to_document(self, ticket: Ticket) -> Document:
raise ZendeskClientNotSetUpError()

requester = BasicExpertInfo(
display_name=ticket.requester.name,
email=ticket.requester.email
display_name=ticket.requester.name, email=ticket.requester.email
)
update_time = time_str_to_utc(ticket.updated_at)

metadata: dict[str, Union[str, list[str]]] = {
metadata: dict[str, str | list[str]] = {
"status": ticket.status,
"priority": ticket.priority,
"tags": ticket.tags,
Expand All @@ -154,19 +154,30 @@ def _ticket_to_document(self, ticket: Ticket) -> Document:
comments = self.zendesk_client.tickets.comments(ticket=ticket)

# Combine all comments into a single text
comments_text = "\n\n".join([
f"Comment by {comment.author.name} at {comment.created_at}:\n{comment.body}"
for comment in comments
])
comments_text = "\n\n".join(
[
f"Comment by {comment.author.name} at {comment.created_at}:\n{comment.body}"
for comment in comments
]
)

# Combine ticket description and comments
full_text = f"Ticket Description:\n{ticket.description}\n\nComments:\n{comments_text}"
description = (
ticket.description
if hasattr(ticket, "description") and ticket.description
else ""
)
full_text = f"Ticket Description:\n{description}\n\nComments:\n{comments_text}"

# Extract subdomain from ticket.url
subdomain = ticket.url.split("//")[1].split(".zendesk.com")[0]

# Build the html url for the ticket
ticket_url = f"https://{subdomain}.zendesk.com/agent/tickets/{ticket.id}"

return Document(
id=f"ticket:{ticket.id}",
sections=[
Section(link=ticket.url, text=full_text)
],
sections=[Section(link=ticket_url, text=full_text)],
source=DocumentSource.ZENDESK,
semantic_identifier=f"Ticket #{ticket.id}: {ticket.subject}",
doc_updated_at=update_time,
Expand All @@ -187,7 +198,9 @@ def poll_source(
else:
raise ValueError(f"Unsupported content_type: {self.content_type}")

def _poll_articles(self, start: SecondsSinceUnixEpoch | None) -> GenerateDocumentsOutput:
def _poll_articles(
self, start: SecondsSinceUnixEpoch | None
) -> GenerateDocumentsOutput:
articles = (
self.zendesk_client.help_center.articles(cursor_pagination=True)
if start is None
Expand Down Expand Up @@ -215,7 +228,9 @@ def _poll_articles(self, start: SecondsSinceUnixEpoch | None) -> GenerateDocumen
if doc_batch:
yield doc_batch

def _poll_tickets(self, start: SecondsSinceUnixEpoch | None) -> GenerateDocumentsOutput:
def _poll_tickets(
self, start: SecondsSinceUnixEpoch | None
) -> GenerateDocumentsOutput:
if self.zendesk_client is None:
raise ZendeskClientNotSetUpError()

Expand All @@ -224,33 +239,44 @@ def _poll_tickets(self, start: SecondsSinceUnixEpoch | None) -> GenerateDocument
start = 0

try:
ticket_generator = self.zendesk_client.tickets.incremental(start_time=int(start))

ticket_generator = self.zendesk_client.tickets.incremental(
start_time=int(start)
)

total_processed = 0
while True:
doc_batch = []
for _ in range(self.batch_size):
try:
ticket = next(ticket_generator)

# Log out the ticket details
print(f"Processing ticket: {ticket.id}")

# Check if the ticket status is deleted
if ticket.status == "deleted":
print(f"Skipping deleted ticket: {ticket.id}")
continue

doc_batch.append(self._ticket_to_document(ticket))
total_processed += 1
except StopIteration:
# No more tickets to process
if doc_batch:
yield doc_batch
return

if doc_batch:
yield doc_batch

time.sleep(TIME_DELAY_SECONDS) # 5-second delay between batches to rate-limit calling the Zendesk API

except Exception as e:
except Exception:
raise


if __name__ == "__main__":
import os
# import time

import time

connector = ZendeskConnector()
connector.load_credentials(
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/connectors/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,10 @@ For example, specifying .*-support.* as a "channel" will cause the connector to
optional: false,
options: [
{ name: "articles", value: "articles" },
{ name: "tickets", value: "tickets" }
{ name: "tickets", value: "tickets" },
],
default: 0,
}
},
],
},
linear: {
Expand Down

0 comments on commit 60de2cd

Please sign in to comment.