1. Kayako Download customers: we will continue to develop and support Kayako Download beyond July 2017, alongside the new Kayako for existing customers.

    Find out more.

  2. The forum you are viewing relates to Kayako Classic. If you signed up or upgraded to the new Kayako (after the 4th July 2016), the information in this thread may not apply to you. You can visit the forums for the new Kayako here.

Gettings statistics with PHP API

Discussion in 'Developing, APIs and extending' started by Aaron Lieberman, May 11, 2016.

  1. Aaron Lieberman

    Aaron Lieberman New Member

    I need to collect statistics using PHP API.

    Need to display number of replied tickets per staff (but not number of replies) for selected period.

    There are more than half a million tickets in the system.

    What I have so far is:
    1. Getting all the ticket IDs
    PHP:
    $tickets kyTicket::getAll(
      
    kyDepartment::getAll()
      ->
    filterByTitle(array('Dep1''Dep2''Dep3')) //I don't need all the departments so I filter by department
    )->collectId();
    2. Iterating through ticket IDs and getting all the replies per user
    PHP:
    foreach ($tickets as $ticket){
        
    $tickets_replies kyTicketPost::getAll($ticket)
            ->
    filterByCreatorType(1//filtering only staff replies
            
    ->filterByStaffId(kyStaff::getAll()->filterByStaffGroupId(7)->filterByIsEnabled(1)->collectId()) //filtering enabled staff from selected staff group
            
    ->filterByDateline(array('>''2016-05-10')) //date range
            
    ->filterByDateline(array('<''2016-05-11')); //date range

        
    $sorted_tickets_replies $tickets_replies->orderByStaffId(true); //ordering by staff ID
       
        //iterating through replies, setting their time to 00-00-00 as we don't need to count replies to the same ticket for the same user during the same day and creating array with results
        
    foreach ($sorted_tickets_replies as $sorted_tickets_reply){
            
    $reply_date = new DateTime($sorted_tickets_reply->dateline);
            
    $reply_date->modify('midnight');
            if (!isset(
    $stats[$ticket][$sorted_tickets_reply->staffid])){
                
    $stats[$ticket][$sorted_tickets_reply->staffid][] = $reply_date;
            } else {
                if (!(
    in_array($reply_date$stats[$ticket][$sorted_tickets_reply->staffid]))){
                    
    $stats[$ticket][$sorted_tickets_reply->staffid][] = $reply_date;
                }
            }
        }
    }
    3. Counting replies and getting Staff Full Name
    PHP:
    //counting replies for each user and generating array with stats Saff Full Name as key and number of replies as value
    foreach ($stats as $index => $item){
        foreach (
    $item as $userId => $userData) {
            
    $result[kyStaff::get($userId)->getFullName()] += count($userData);
        }
    }
    Script is timing out. Max_exec_time in PHP is set to 16 minutes.
    I will appreciate any optimization tips on the code above.
     
  2. Tomasz Sawicki

    Tomasz Sawicki Established Member

    If the problem is that the server is timing out, and you don't care how long your script will run, you can do ticket paging with $max_items and $starting_ticket_id parameters of kyTicket::getAll.
    If you want your script to run faster, then with so many tickets I guess you will have to do it directly on database, with SQL.
     

Share This Page