Debian Codegraph @

htrace-htraced/go/web/app/search_view.js

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 var htrace = htrace || {};
21 htrace.SearchView = Backbone.View.extend({
22 initialize : function() {
23 this.predicateViews = [];
24 this.highestPredicateIndex = 0;
25 this.searchInProgress = false;
26 this.searchResults = new htrace.SearchResults();
27 this.resultsView = new htrace.SearchResultsView({
28 searchResults: this.searchResults,
29 el: "#resultsView"
30 });
31 },
32
33 events: {
34 "click #searchButton": "searchHandler",
35 "click #clearButton": "clearHandler",
36 "click .add-field": "dropdownHandler",
37 "blur #begin": "blurBeginHandler",
38 "blur #end": "blurEndHandler",
39 "click #zoomButton": "zoomHandler"
40 },
41
42 searchHandler: function(e){
43 e.preventDefault();
44
45 this.doSearch(e.ctrlKey);
46 },
47
48 clearHandler: function(e){
49 e.preventDefault();
50
51 this.resultsView.clearHandler(true);
52 },
53
54 doSearch: function(showDebug){
55 if (this.searchInProgress) {
56 console.log("Can't start a new search while another one is in " +
57 "progress.");
58 return false;
59 }
60
61 // Check if there are no search criteria.
62 if (this.predicateViews.length == 0) {
63 htrace.showModalWarning("No Search Criteria Specified",
64 "You have not specified any search criteria. " +
65 "Use the 'Add Predicate' button to specify what to search for.");
66 return false;
67 }
68
69 // Build the predicate array.
70 predicates = []
71 var predicateViewsLen = this.predicateViews.length;
72 for (var i = 0; i < predicateViewsLen; i++) {
73 var predicateView = this.predicateViews[i];
74 try {
75 predicates.push(predicateView.getPredicate());
76 } catch(err) {
77 htrace.showModalWarning("Search Field Validation Error",
78 "Invalid search string for the '" + predicateView.ptype.name +
79 "' field.<p/>" + err);
80 return false;
81 }
82 }
83 var lim = Math.min(parseInt($("#search-lim").val()) || 20, 200);
84 $("#search-lim").val(lim.toString());
85 var queryJson = {
86 pred: predicates,
87 lim: lim
88 };
89 // If there are existing search results, we want results which "come after"
90 // those. So pass the last span we saw as a continuation token.
91 if (this.searchResults.size() > 0) {
92 queryJson.prev =
93 this.searchResults.at(this.searchResults.size() - 1).unparse();
94 }
95 $("body").css("cursor", "progress");
96 var searchView = this;
97 var queryResults = new htrace.QueryResults({queryJson: queryJson});
98 console.log("Starting span query " + queryResults.url());
99 this.searchInProgress = true;
100 queryResults.fetch({
101 success: function(model, response, options){
102 $("body").css("cursor", "default");
103 var firstResults = (searchView.searchResults.size() === 0);
104 console.log("Success on span query " + queryResults.url() + ": got " +
105 queryResults.size() + " result(s). firstResults=" + firstResults);
106 searchView.searchResults.add(queryResults.models);
107 if (firstResults) {
108 // After the initial search, zoom to fit everything.
109 // On subsequent searches, we leave the viewport alone.
110 searchView.resultsView.zoomHandler();
111 }
112 searchView.searchInProgress = false;
113 if (showDebug) {
114 htrace.showModalWarning("Search Debug",
115 "This is the search debug box, accessible by holding down the " +
116 "control key while clicking the search button.<p/>" +
117 "<h3>Query JSON</h3><pre>" + queryResults.prettyQueryString() +
118 "</pre><p/><h3>Response JSON</h3><pre>" +
119 JSON.stringify(queryResults, null, 2) + "</pre><p/>");
120 } else if (queryResults.size() == 0) {
121 if (firstResults) {
122 htrace.showModalWarning("No Results Found",
123 "No results were found for your query.<p/>");
124 } else {
125 htrace.showModalWarning("No Additional Results Found",
126 "No additional results were found for your query.<p/>");
127 }
128 }
129 searchView.resultsView.render();
130 },
131 error: function(model, response, options){
132 $("body").css("cursor", "default");
133 searchView.searchResults.reset();
134 var err = "Error " + JSON.stringify(response, null, 2) +
135 " on span query " + queryResults.url();
136 console.log(err);
137 alert(err);
138 searchView.searchInProgress = false;
139 }
140 });
141 return false;
142 },
143
144 dropdownHandler: function(e){
145 e.preventDefault();
146 var text = $(e.target).text();
147 var ptype = htrace.parsePType(text);
148 if (!ptype) {
149 alert("Unable to parse predicate type '" + text + "'");
150 return false;
151 }
152 var index = this.highestPredicateIndex;
153 this.highestPredicateIndex++;
154 var el = "pred" + index;
155 $("#predicates").append('<div id="' + el + '"/></div>');
156 predicateView = new htrace.PredicateView({
157 el: "#" + el,
158 index: index,
159 ptype: ptype,
160 searchView: this
161 });
162 this.predicateViews.push(predicateView);
163 predicateView.render();
164 return true;
165 },
166
167 blurBeginHandler: function(e) {
168 return this.resultsView.handleBeginOrEndChange(e, "begin");
169 },
170
171 blurEndHandler: function(e) {
172 return this.resultsView.handleBeginOrEndChange(e, "end");
173 },
174
175 zoomHandler: function(e) {
176 e.preventDefault();
177 this.resultsView.zoomHandler();
178 },
179
180 removePredicateView: function(predicateView) {
181 this.predicateViews = _.without(this.predicateViews, predicateView);
182 },
183
184 render: function() {
185 this.$el.html(_.template($("#search-view-template").html())
186 ({ model : this.model }))
187 this.resultsView.render();
188 console.log("SearchView#render");
189 return this;
190 },
191
192 close: function() {
193 console.log("SearchView#close")
194 while (this.predicateViews.length > 0) {
195 this.predicateViews[0].remove();
196 }
197 this.resultsView.remove();
198 this.resultsView = null;
199 this.undelegateEvents();
200 }
201 });