Development

Changeset 10436

You must first sign up to be able to contribute.

Changeset 10436

Show
Ignore:
Timestamp:
07/23/08 09:08:20 (5 years ago)
Author:
dwhittle
Message:

ysfAPIClientPlugin: refactored batch handling + added better logging + timing

+ added paralell search (Y! / G) example

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/ysfAPIClientPlugin/trunk/lib/ysfAPIClient.class.php

    r10373 r10436  
    2222{ 
    2323 
    24   protected static $instance = null
     24  protected static $instance = null, $count = 0
    2525 
    2626  protected $context = null; 
     
    2828  private $cache = null, 
    2929          $batches = array(), 
     30          $timer = null, 
    3031          $requests = array(), 
    3132          $responses = array(), 
     
    8081    $this->options = $options; 
    8182 
    82     $this->options['debug'] = isset($this->options['debug']) ? (boolean) $this->options['debug'] : false; 
    83     $this->options['servers'] = isset($this->options['servers']) ? $this->options['servers'] : array(); 
    84  
    8583    $this->loadConfiguration(); 
    8684  } 
     
    9795    } 
    9896 
    99     if(sfConfig::has('ysf_api_cache')) 
    100     { 
    101       $cacheAdapter = sfConfig::get('ysf_api_cache'); 
    102       $this->cache = new $cacheAdapter['class']($cacheAdapter['param']); 
    103     } 
    104     else 
    105     { 
    106       $this->cache = new sfNoCache(); 
    107     } 
    108  
    109     $this->options['servers'] = sfToolkit::arrayDeepMerge($this->options['servers'], sfConfig::get('ysf_api_servers', array())); 
     97    $this->options['enabled'] = sfConfig::get('ysf_api_enabled', true); 
     98    $this->options['debug']   = sfConfig::get('ysf_api_debug', false); 
     99 
     100    $this->options['servers'] = sfToolkit::arrayDeepMerge(isset($this->options['servers']) ? $this->options['servers'] : array(), sfConfig::get('ysf_api_servers', array())); 
     101 
     102    $this->options['cache']   = sfConfig::get('ysf_api_cache', array()); 
     103    $this->cache = isset($this->options['cache']['class']) ? new $this->options['cache']['class'](isset($this->options['cache']['param']) ? $this->options['cache']['param'] : array()) : new sfNoCache(); 
    110104  } 
    111105 
     
    117111  public function setOptions($options) 
    118112  { 
    119     $options['debug'] = isset($options['debug']) ? (boolean) $options['debug'] : $this->options['debug']; 
    120  
    121113    $this->options = $options; 
    122114  } 
     
    157149  public function execute() 
    158150  { 
     151    if($this->options['debug']) 
     152    { 
     153      $this->timer = sfTimerManager::getTimer('API Requests'); 
     154    } 
     155 
     156    $id = time(); // first batch id 
    159157    if(is_array($this->options['servers']) && !empty($this->options['servers'])) 
    160158    { 
     
    169167 
    170168          $options[$options['method']]['uri'] = isset($options[$options['method']]['uri']) ? $options[$options['method']]['uri'] : ''; 
    171  
    172169          $options[$options['method']]['parameter'] = isset($options[$options['method']]['parameter']) ? $options[$options['method']]['parameter'] : ''; 
    173170 
    174171          if(isset($options['method']) && $options['method'] == 'compound') 
    175172          { 
    176             $id = md5($server . 'compound'); 
     173            $id = md5($id.$server); 
    177174            $request = $this->encodeCompound($this->requests[$server], $this->options['servers'][$server]); 
    178175 
     
    180177            { 
    181178              $encodedRequest = $this->encodeRequest($request, $options['format']); 
    182  
    183               $this->batches[$id]['request'] = $encodedRequest; 
    184179 
    185180              $curlRequest = $provider->buildCurlRequest($options[$options['method']]['uri'], array($options[$options['method']]['parameter'] => $encodedRequest)); 
     
    193188          else // single requests (usually rest or similar) 
    194189          { 
    195             foreach($this->requests[$server] as $id => $request) 
     190            foreach($this->requests[$server] as $id => $curlRequest) 
    196191            { 
    197               if(!isset($this->responses[$id]) && ($request != false)) 
     192              if(!isset($this->responses[$id]) && ($curlRequest != false)) 
    198193              { 
    199                 $this->batches[$id]['handle'] = $request; 
     194                $this->batches[$id]['handle'] = $curlRequest; 
    200195              } 
    201196            } 
     
    209204      $mh = curl_multi_init(); 
    210205 
     206      if($this->options['debug']) 
     207      { 
     208        $this->context->getLogger()->info(sprintf("{ysfAPIClient} Executing batch #%s with %s requests", ++self::$count, count($this->batches)), sfLogger::DEBUG); 
     209      } 
     210 
    211211      foreach($this->batches as $id => $batch) 
    212212      { 
    213         if($this->options['debug']) 
    214         { 
    215           $this->context->getLogger()->info(sprintf("executing batch id '%s'", $id), sfLogger::DEBUG); 
    216         } 
    217  
    218213        curl_multi_add_handle($mh, $batch['handle']); 
    219214      } 
     
    251246      } 
    252247      while($still_running); 
     248 
     249      if($this->options['debug']) 
     250      { 
     251        $this->timer->addTime(); 
     252      } 
    253253 
    254254      foreach($this->options['servers'] as $server => $options) 
     
    342342        $serverParameters = $this->options['servers'][$server]; 
    343343 
    344         $id = md5($server . $call . serialize($options)); 
     344        $id = md5($server . $call . serialize($parameters)); 
    345345        if(!isset($this->requests[$server][$id])) 
    346346        { 
     
    353353            if($this->options['debug']) 
    354354            { 
    355               $this->context->getLogger()->info(sprintf("{ysfAPIClient} adding request '%s' with parameters %s", $call, str_replace(array('array (', '0 => ', ",\n  )", ",\n)"), '', var_export($options, true)))); 
    356               $this->context->getLogger()->info(sprintf("{ysfAPIClient} adding request id '%s' for server '%s'", $id, $server)); 
     355              $this->context->getLogger()->info(sprintf("{ysfAPIClient} adding request '%s' to batch with parameters: %s", $call, str_replace(array('array (', '0 => ', ",\n  )", ",\n)", "\n"), '', var_export($parameters, true)))); 
    357356            } 
    358357          } 
  • plugins/ysfAPIClientPlugin/trunk/modules/ysfAPI/actions/actions.class.php

    r10373 r10436  
    2525   * 
    2626   */ 
    27   public function executeIndex($request) 
     27  public function executeSearch($request) 
    2828  { 
    2929 
    30     $timer = sfTimerManager::getTimer('API Requests'); 
     30    // search form 
     31    $this->form = new sfForm(); 
     32    $this->form->setWidgets(array('query' => new sfWidgetFormInput(array(), array('class' => 'search-box')))); 
     33    $this->form->setValidators(array('query' => new sfValidatorString(array('min_length' => 3)))); 
     34    $this->form->getWidgetSchema()->setNameFormat('search[%s]'); 
     35    $this->form->getWidgetSchema()->setFormFormatterName('list'); 
    3136 
    32     $api = ysfAPIClient::getInstance(); 
     37    // search results 
     38    $this->results = array(); 
    3339 
    34     $this->query = $request->getParameter('query', 'symfony'); 
     40    if($request->isMethod('post')) 
     41    { 
     42      // bind posted form 
     43      $this->form->bind($request->getParameter('search')); 
    3544 
    36     $ysearch = $api->addRequest('yahoo.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my Y! search')); 
    37     $gsearch = $api->addRequest('google.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my G search')); 
     45      if($this->form->isValid()) 
     46      { 
     47        $this->query = $this->form->getValue('query'); 
    3848 
    39     if($api->execute()) 
    40     { 
    41       $yJson = $api->getData($ysearch); 
    42       $gJson = $api->getData($gsearch); 
     49        $api = ysfAPIClient::getInstance(); 
    4350 
    44       // normalization logic can be moved to provier 
    45       $this->results = array(); 
    46       foreach (array_merge($yJson->ysearchresponse->resultset_web, $gJson->responseData->results) as $data) 
    47       { 
    48         $result = new stdClass(); 
    49         $result->title = $data->title; 
    50         $result->abstract = isset($data->abstract) ? $data->abstract : $data->content; 
    51         $result->url = $data->url; 
     51        // parallel 
     52        $ysearch = $api->addRequest('yahoo.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my Y! search')); 
     53        $gsearch = $api->addRequest('google.search', array('query' => $this->query), array(CURLOPT_USERAGENT => 'my G search')); 
    5254 
    53         array_push($this->results, $result); 
     55        if($api->execute()) 
     56        { 
     57          $yJson = $api->getData($ysearch); 
     58          $gJson = $api->getData($gsearch); 
     59 
     60          if(isset($yJson->ysearchresponse) && isset($gJson->responseData)) 
     61          { 
     62            // normalization logic could be moved to each provider 
     63            foreach (array_merge($yJson->ysearchresponse->resultset_web, $gJson->responseData->results) as $data) 
     64            { 
     65              $result = new stdClass(); 
     66              $result->title = $data->title; 
     67              $result->abstract = isset($data->abstract) ? $data->abstract : $data->content; 
     68              $result->url = $data->url; 
     69 
     70              array_push($this->results, $result); 
     71            } 
     72          } 
     73        } 
    5474      } 
     75 
    5576    } 
    56     else 
    57     { 
    58       $this->results = array(); 
    59     } 
    60     $timer->addTime(); 
    6177 
    6278    return sfView::SUCCESS;