CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Max API Results
Pages: [1]

Author Topic: Max API Results  (Read 1530 times)

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Max API Results
August 18, 2011, 03:18:35 pm
I see in the documentation that the default maximum API results is 25.

Is there anyway to get more results?

Basically, I have a couple of ACLs to restrict who can view / edit custom groups. Although you can't use smart groups for ACL control, I'd like to make this 'almost' a smart list with a cron job running a script every hour and looking at a certain field to decide whether to add the user or remove them from the group in question.

I would imagine that at peak usage time, I could have a need to get up to 500 results from the API.

Brian

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Max API Results
August 18, 2011, 10:00:36 pm
Hi,

We have been looking at moving to a format of holding 'special' params in one of 3 arrays

'options'
'filters'
'return'

Where options might contain

'limit'
'sort'
'offset'

Filters contain 'custom' filters - e.g. 'is_full' for events

Return should contain return what you want returned.

I need to check where we are with this as it won't work on all API - which one are you using? Here's an example

http://svn.civicrm.org/civicrm/branches/v3.4/api/v3/examples/OptionValue/SortOption.php

The alternate syntax might be $params['isCurrent'] but we decided we wanted to distinguish between 'real fields' & various special bits & pieces. There is a thread somewhere. Getting tests in place is the main bottleneck here.

Here is an example of the test for 'limit'

        /**
     *  Test limit param
     */
     function testGetOptionValueLimit()
     {
         $params = array( 'version'         =>  $this->_apiversion,);
         $result =& civicrm_api('option_value','getcount',$params);       
         $this->assertGreaterThan(1, $result, "Check more than one exists In line " . __LINE__ );
         $params['options']['limit'] = 1;
         $result =& civicrm_api('option_value','getcount',$params);   
         $this->assertEquals(1, $result, "Check only 1 retrieved " . __LINE__ );
 
     }

Ideally we would figure out how to make an automated test on this that checks all API. The complexity is the data (we'd need to be able to easily ensure each API had more than one entry)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

fen

  • I post frequently
  • ***
  • Posts: 216
  • Karma: 13
    • CivicActions
  • CiviCRM version: 3.3-4.3
  • CMS version: Drupal 6/7
  • MySQL version: 5.1/5.5
  • PHP version: 5.3/5.4
Re: Max API Results
October 26, 2011, 07:51:09 am
I'd really like to see this move forward.  There are times I need to pull all contacts but since the API only returns 25, I use pure SQL to get all the ids and then pull them one at a time.  Having the same issue with getting all groups, too.  (And I'd love an option to get just smart groups.)

How can I help?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Max API Results
October 26, 2011, 01:30:17 pm
Just to clarify - every API should be able to return more than 25 - unfortunately the syntax in inconsistent (and since we are really far into APIv3 I suspect we'll have to consolidate on supporting ALL of them).

Currently more than half the API support (all the ones that use basic_get or set_filter)

'options' => array('limit' => 200)

(the same ones also support option.limit => 200 from memory - there is a test / example somewhere)

There are a few that support 'rowCount' => 200 (contact_get, contribution_get, pledge_get and a couple of others)

From memory website_get Doesn't deal with limits of any type - I want to replace it with 'basic_get' but need a test first
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Max API Results
October 26, 2011, 03:17:13 pm
For contacts, rowCount does work.

I would be super useful if you could modify the api/v3/Contact.php to rowCount=option.limit

(and test)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Max API Results
October 26, 2011, 03:30:31 pm
Actually - it should be modified to use the same conversion function as pledge does & that should support both formats

http://issues.civicrm.org/jira/browse/CRM-9110
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

fen

  • I post frequently
  • ***
  • Posts: 216
  • Karma: 13
    • CivicActions
  • CiviCRM version: 3.3-4.3
  • CMS version: Drupal 6/7
  • MySQL version: 5.1/5.5
  • PHP version: 5.3/5.4
Re: Max API Results
October 26, 2011, 06:35:15 pm
I don't see 'offset' being mentioned in this conversation, but it is important so we can have loops to process all Contacts (Groups, Activities, etc.) like:
Code: [Select]
$params = array('version' => 3, 'options' => array('offset' => 0, 'limit' => 25));
while (TRUE) {
    $results = civicrm_api('Contact','get',$params);
    if ($results['count'] == 0) { break; }
    $params['options']['offset'] += $results['count'];
    // process batch of contacts
}

I'm not really sure how many different formats you need to keep around, as I don't think many are being used yet.  It sounds like rowCount may need to stick around for the Contact API.  While options as a separate array is programmatically cleaner:
Code: [Select]
'options'=>array('offset'=>0, 'limit'=>25)it makes it more difficult to exercise the options in the AJAX API Explorer, where this would be easier:
Code: [Select]
version=3&option.offset=0&option.limit=25

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Max API Results
October 26, 2011, 06:59:56 pm
Yes, the difficulty of the different formats in different frameworks is why we got into multiple types. In fact I don't think supporting multiple types is the biggest difficulty - we need to funnel all use of these options through 1 or 2 functions so that whatever we do support is applied to all. Working through those changes (& ensuring tests are in place) is the biggest challenge here
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Max API Results
October 26, 2011, 08:34:27 pm

Note that calling the same query with n queries with different offsets and same limit is n times less efficient (i.e limit does not really improve query performance)

there is a php memory / mysql query performance trade-off that needs to be balanced when dealing with large datasets. In recent code, we've been using more temp tables to keep memory usage down

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

fen

  • I post frequently
  • ***
  • Posts: 216
  • Karma: 13
    • CivicActions
  • CiviCRM version: 3.3-4.3
  • CMS version: Drupal 6/7
  • MySQL version: 5.1/5.5
  • PHP version: 5.3/5.4
Re: Max API Results
October 27, 2011, 06:17:41 am
What I do now if I need to process (say) 20% of 100K contacts, I perform a pure SQL query that returns the contact_ids of the group I want and then use the API to get each Contact separately.  This seems efficient enough to me, but my concern is that I try to minimize such pure sql queries and use the API whenever possible.  With offset and limit I could process contacts in batches of (say) 25 which is better than pulling the full contact records for all 20,000 in one call.  But if this is known to leak memory, then I'll stick with the pure SQL method and just be sure to document the dependencies.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Max API Results
October 27, 2011, 01:35:52 pm
In my experience working with 20,000 entities through the API can happen lickety-split or it can take a while - it depends on the complexity of the API & how much checking it does.

It also depends what the script is for. I have a script that runs once a month. I don't care if it takes one minute or 6.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Max API Results

This forum was archived on 2017-11-26.