2a6ca0d37f3348c1178ba286521436ef57766166.svn-base
19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!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 … <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">¶</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">'querystring'</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">'timespan'</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">'./common'</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">¶</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">'search?'</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">¶</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">¶</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">¶</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">¶</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">¶</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">¶</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">'Cannot run search without a callback function.'</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">¶</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">' '</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">' '</span><span class="p">,</span> <span class="s1">':'</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">¶</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">¶</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">&&</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">'NOW'</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">'NOW-24HOURS'</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">'NOW'</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">'NOW-24HOURS'</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"><</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">¶</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">'NOW'</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">'NOW-24HOURS'</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>