Recent searches


No recent searches

Search API - solved tickets limited to max 1000 tickets



Posted May 02, 2024

Hi, 
i am trying to retrieve 2000 solved tickets using search API , but i am unable to get 2000 solved tickets.

first query - i made to get the solved tickets , i used pagination ,so i made 10 calls to get 1000 tickets which are updated less than current date , each page giving me 100 tickets each .

interestingly ,
i took the first query results for my second and subsequent API calls,  for second query i took last ticket  updated at field  of first query and made API calls (i.e. ticket updated less than this date and status as solved ). but it either returned few ticket or nothing and after that tickets in the search results are empty

any help to get the 2000 tickets which are solved?


thanks 
Rahul


0

4

4 comments

Hi Rahul,

 

Could you please share the queries you're using?

 

Ned

1


hi Ned Petrov  we are searching for closed tickets week by week,here is my python code .

end_date = datetime.utcnow()

        start_date = end_date - timedelta(days=1)

        results = []

        seen_ticket_ids = set()

        while len(results) < max_tickets and start_date > datetime(2023, 1, 1):  # Example: lower bound date

            params = {

                'query': f"type:ticket updated_at>={start_date.strftime('%Y-%m-%d')} updated_at<={end_date.strftime('%Y-%m-%d')}  status:solved or status:closed",

                'sort_by': 'updated_at',

                'sort_order': 'desc',

            }

            tickets,seen_ticket_ids,next_page = get_unique_tickets(seen_ticket_ids,max_tickets,zendesk_headers,url,params)#first call

            results += tickets

            while next_page and len(results) < max_tickets:#paginated query

                tickets,seen_ticket_ids,next_page = get_unique_tickets(seen_ticket_ids,max_tickets,zendesk_headers,next_page,params)

                results += tickets

            end_date = start_date

            start_date -= timedelta(days=7)        # Decrement the date by one day

        return results

i know this takes lot of time to get 2000 closed tickets.

let me know, if there is a  better way to get all the solved 2000 or n tickets.

0


I am not a python dev, but I have hit this 1000 hard limit in my C# apps too.
Here's how we cleverly bypass this restriction:

  1. Time Windowing: Instead of trying to fetch all tickets at once, we use a moving time window.
    • Start with the current date and time as the end of our window.
    • Set a start date some time in the past (e.g., 30 days ago).
    • Fetch tickets within this time window.
  2. Pagination Within Windows: For each time window, we use pagination to get all results.
    • This ensures we get all tickets within a specific time range.
  3. Moving Backwards: If we haven't reached our desired number of tickets:
    • Move the time window back (i.e., set the end date to our previous start date).
    • Repeat the process with the new time window.
  4. API Limit Detection: We check for the specific API limit message in responses.
    • If hit, we adjust our time window and continue.
  5. Deduplication: We keep track of seen ticket IDs to avoid duplicates.
    • This is crucial as time windows may overlap.
  6. Sorting: We sort results by 'updated_at' in descending order.
    • This ensures we always get the most recently updated tickets first.
       

I tried to make a python script with this approach, please keep in mind the code is untested and I am not a python expert!

 

import requests
from datetime import datetime, timedelta
from typing import List, Dict, Any, Tuple
import logging
import sys
import time

class ZendeskTicketFetcher:
   def __init__(self, subdomain: str, email: str, api_token: str):
       self.base_url = f"https://{subdomain}.zendesk.com/api/v2"
       self.auth = (f"{email}/token", api_token)
       self.logger = logging.getLogger(__name__)

   def get_solved_tickets(self, max_tickets: int = 2000) -> List[Dict[str, Any]]:
       end_date = datetime.utcnow()
       start_date = end_date - timedelta(days=30)  # Start with a 30-day window
       results = []
       seen_ticket_ids = set()

       while len(results) < max_tickets and start_date > datetime(2000, 1, 1):  # Arbitrary lower bound
           tickets, next_page, api_limit_hit = self.fetch_tickets(start_date, end_date)
           if api_limit_hit:
               end_date = start_date
               start_date -= timedelta(days=30)
               continue

           new_tickets = [t for t in tickets if t['id'] not in seen_ticket_ids]
           results.extend(new_tickets)
           seen_ticket_ids.update(t['id'] for t in new_tickets)

           while next_page and len(results) < max_tickets:
               tickets, next_page, api_limit_hit = self.fetch_tickets(start_date, end_date, next_page)
               if api_limit_hit:
                   break
               new_tickets = [t for t in tickets if t['id'] not in seen_ticket_ids]
               results.extend(new_tickets)
               seen_ticket_ids.update(t['id'] for t in new_tickets)

           if len(results) < max_tickets:
               end_date = start_date
               start_date -= timedelta(days=30)

       return results[:max_tickets]

   def fetch_tickets(self, start_date: datetime, end_date: datetime, page_url: str = None) -> Tuple[List[Dict[str, Any]], str, bool]:
       try:
           if page_url:
               response = requests.get(page_url, auth=self.auth)
           else:
               url = f"{self.base_url}/search.json"
               query = f"type:ticket updated>{start_date.isoformat()} updated<{end_date.isoformat()} status:solved"
               params = {
                   'query': query,
                   'sort_by': 'updated_at',
                   'sort_order': 'desc'
               }
               response = requests.get(url, auth=self.auth, params=params)

           response.raise_for_status()
           data = response.json()
           
           api_limit_hit = self.check_api_limit(response.status_code, response.text)
           if api_limit_hit:
               return [], None, True

           return data['results'], data.get('next_page'), False

       except requests.RequestException as e:
           self.logger.error(f"Failed to fetch tickets: {str(e)}")
           sys.exit(1)

   def check_api_limit(self, status_code: int, response_content: str) -> bool:
       if status_code == 200:
           return False

       if "Requested response size was greater than Search Response Limits" in response_content:
           self.logger.warning("API limit hit. Adjusting time window.")
           return True

       self.logger.error(f"Failed to get ticket data from Zendesk. Status code: {status_code}")
       self.logger.error(f"Response content: {response_content}")
       time.sleep(2)
       sys.exit(1)

# Usage
logging.basicConfig(level=logging.INFO)
fetcher = ZendeskTicketFetcher('your_subdomain', 'your_email', 'your_api_token')
solved_tickets = fetcher.get_solved_tickets(2000)
print(f"Retrieved {len(solved_tickets)} solved tickets")

0


hi 이지훈(Lee jihoon),

Even i was moving back 1 week instead of 1 month, Although i was searching for one single api which returns n solved tickets.

Thanks for your response, i will move 1 month back
 

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post