2a6ca0d37f3348c1178ba286521436ef57766166.svn-base 19.7 KB
<!DOCTYPE html>  <html> <head>   <title>search.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="../docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>            <div id="jump_to">         Jump To &hellip;         <div id="jump_wrapper">           <div id="jump_page">                                           <a class="source" href="..//loggly/common.html">                 loggly/common.html               </a>                                           <a class="source" href="..//loggly/config.html">                 loggly/config.html               </a>                                           <a class="source" href="..//loggly/core.html">                 loggly/core.html               </a>                                           <a class="source" href="..//loggly/device.html">                 loggly/device.html               </a>                                           <a class="source" href="..//loggly/facet.html">                 loggly/facet.html               </a>                                           <a class="source" href="..//loggly/input.html">                 loggly/input.html               </a>                                           <a class="source" href="..//loggly/search.html">                 loggly/search.html               </a>                                           <a class="source" href="..//loggly.html">                 loggly.html               </a>                        </div>         </div>       </div>          <table cellpadding="0" cellspacing="0">       <thead>         <tr>           <th class="docs">             <h1>               search.js             </h1>           </th>           <th class="code">           </th>         </tr>       </thead>       <tbody>                               <tr id="section-1">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-1">&#182;</a>               </div>                            </td>             <td class="code">               <div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * search.js: chainable search functions for Loggly</span>
<span class="cm"> *</span>
<span class="cm"> * (C) 2010 Nodejitsu Inc.</span>
<span class="cm"> * MIT LICENSE</span>
<span class="cm"> *</span>
<span class="cm"> */</span>

<span class="kd">var</span> <span class="nx">qs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;querystring&#39;</span><span class="p">),</span>
    <span class="nx">timespan</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;timespan&#39;</span><span class="p">),</span>
    <span class="nx">common</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./common&#39;</span><span class="p">);</span></pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">&#182;</a>               </div>               <p>function Search (query, client, callback) 
  Chainable search object for Loggly API</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Search</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Search</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">query</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">query</span> <span class="o">=</span> <span class="nx">query</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">client</span> <span class="o">=</span> <span class="nx">client</span><span class="p">;</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">baseUrl</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">baseUrl</span> <span class="o">=</span> <span class="s1">&#39;search?&#39;</span><span class="p">;</span>
  <span class="p">}</span>
  </pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">&#182;</a>               </div>               <p>If we're passed a callback, run immediately.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">if</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">callback</span> <span class="o">=</span> <span class="nx">callback</span><span class="p">;</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">run</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-4">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-4">&#182;</a>               </div>               <p>function meta (meta)
  Sets the appropriate metadata for this search query:
  e.g. ip, inputname</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Search</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">meta</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_meta</span> <span class="o">=</span> <span class="nx">meta</span><span class="p">;</span>
  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">&#182;</a>               </div>               <p>function context (context)
  Sets the appropriate context for this search query:
  e.g. rows, start, from, until, order, format, fields</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Search</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">context</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_context</span> <span class="o">=</span> <span class="nx">context</span><span class="p">;</span>
  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">&#182;</a>               </div>               <h3>function run (callback)</h3>

<h4>@callback {function} Continuation to respond to when complete</h4>

<p>Runs the search query for for this instance with the query, metadata,
context, and other parameters that have been configured on it.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Search</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">run</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">rangeError</span><span class="p">;</span>
  </pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">&#182;</a>               </div>               <p>Trim the search query </p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">this</span><span class="p">.</span><span class="nx">query</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span>
  </pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">&#182;</a>               </div>               <p>Update the callback for this instance if it's passed</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">this</span><span class="p">.</span><span class="nx">callback</span> <span class="o">=</span> <span class="nx">callback</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">callback</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;Cannot run search without a callback function.&#39;</span><span class="p">);</span>
  <span class="p">}</span>
  </pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">&#182;</a>               </div>               <p>If meta was passed, update the search query appropriately</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_meta</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">query</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nx">qs</span><span class="p">.</span><span class="nx">unescape</span><span class="p">(</span><span class="nx">qs</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_meta</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">));</span>
  <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-10">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-10">&#182;</a>               </div>               <p>Set the context for the query string</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">this</span><span class="p">.</span><span class="nx">_context</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_context</span> <span class="o">||</span> <span class="p">{};</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">q</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">query</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_checkRange</span><span class="p">();</span>

  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span> <span class="nx">searchOptions</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nx">uri</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">client</span><span class="p">.</span><span class="nx">logglyUrl</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">baseUrl</span> <span class="o">+</span> <span class="nx">qs</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">)),</span>
    <span class="nx">auth</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">client</span><span class="p">.</span><span class="nx">config</span><span class="p">.</span><span class="nx">auth</span>
  <span class="p">};</span>
  
  <span class="nx">common</span><span class="p">.</span><span class="nx">loggly</span><span class="p">(</span><span class="nx">searchOptions</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">callback</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">res</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">self</span><span class="p">.</span><span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">body</span><span class="p">));</span>
  <span class="p">});</span>
  
  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-11">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-11">&#182;</a>               </div>               <h3>function _checkRange ()</h3>

<p>Checks if the range that has been configured for this 
instance is valid and updates if it is not.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Search</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">_checkRange</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span> <span class="o">||</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span><span class="p">))</span> <span class="p">{</span>
    <span class="k">return</span><span class="p">;</span>
  <span class="p">}</span>
  
  <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span> <span class="o">||</span> <span class="s1">&#39;NOW&#39;</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span>  <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span>  <span class="o">||</span> <span class="s1">&#39;NOW-24HOURS&#39;</span><span class="p">;</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">timespan</span><span class="p">.</span><span class="nx">parseDate</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span><span class="p">))</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span> <span class="o">=</span> <span class="s1">&#39;NOW&#39;</span><span class="p">;</span>
  <span class="p">}</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">timespan</span><span class="p">.</span><span class="nx">parseDate</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span><span class="p">))</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span> <span class="o">=</span> <span class="s1">&#39;NOW-24HOURS&#39;</span><span class="p">;</span>
  <span class="p">}</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">timespan</span><span class="p">.</span><span class="nx">fromDates</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span>
    <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span> <span class="o">===</span> <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-12">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-12">&#182;</a>               </div>               <p>If the length of the timespan for this Search instance is
negative then set it to default values</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">until</span> <span class="o">=</span> <span class="s1">&#39;NOW&#39;</span><span class="p">;</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">_context</span><span class="p">.</span><span class="nx">from</span> <span class="o">=</span> <span class="s1">&#39;NOW-24HOURS&#39;</span><span class="p">;</span>
  <span class="p">}</span>
  
  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">};</span>

</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html>