use of net.opentsdb.search.SearchQuery in project opentsdb by OpenTSDB.
the class Search method lookup.
/**
* Performs a time series lookup given a query like "metric tagk=tagv" where
* a list of all time series containing the given metric and tag pair will be
* dumped to standard out. Tag pairs can be given with empty tagk or tagvs to
* and the metric is option. E.g. a query of "=web01" will return all time
* series with a tag value of "web01".
* By default the lookup is performed against the tsdb-meta table. If the
* "--use_data_table" flag is supplied, the main data table will be scanned.
* @param tsdb The TSDB to use for communication
* @param use_data_table Whether or not lookups should be done on the full
* data table
* @param args Arguments to parse
* @return An exit code
*/
private static int lookup(final TSDB tsdb, final boolean use_data_table, final String[] args) throws Exception {
if (!use_data_table) {
tsdb.getClient().ensureTableExists(tsdb.getConfig().getString("tsd.storage.hbase.meta_table")).joinUninterruptibly();
}
final SearchQuery query = new SearchQuery();
query.setType(SearchType.LOOKUP);
int index = 1;
if (!args[index].contains("=")) {
query.setMetric(args[index++]);
}
final List<Pair<String, String>> tags = new ArrayList<Pair<String, String>>(args.length - index);
for (; index < args.length; index++) {
Tags.parse(tags, args[index]);
}
query.setTags(tags);
if (use_data_table) {
query.setUseMeta(false);
LOG.warn("NOTE: Scanning the full data table may take a long time");
}
final TimeSeriesLookup lookup = new TimeSeriesLookup(tsdb, query);
lookup.setToStdout(true);
lookup.lookup();
return 0;
}
use of net.opentsdb.search.SearchQuery in project opentsdb by OpenTSDB.
the class SearchRpc method parseQueryString.
/**
* Parses required search values from the query string
* @param query The HTTP query to work with
* @param type The type of search query requested
* @return A parsed SearchQuery object
*/
private final SearchQuery parseQueryString(final HttpQuery query, final SearchType type) {
final SearchQuery search_query = new SearchQuery();
if (type == SearchType.LOOKUP) {
final String query_string = query.getRequiredQueryStringParam("m");
search_query.setTags(new ArrayList<Pair<String, String>>());
try {
search_query.setMetric(Tags.parseWithMetric(query_string, search_query.getTags()));
} catch (IllegalArgumentException e) {
throw new BadRequestException("Unable to parse query", e);
}
if (query.hasQueryStringParam("limit")) {
final String limit = query.getQueryStringParam("limit");
try {
search_query.setLimit(Integer.parseInt(limit));
} catch (NumberFormatException e) {
throw new BadRequestException("Unable to convert 'limit' to a valid number");
}
}
return search_query;
}
// process a regular search query
search_query.setQuery(query.getRequiredQueryStringParam("query"));
if (query.hasQueryStringParam("limit")) {
final String limit = query.getQueryStringParam("limit");
try {
search_query.setLimit(Integer.parseInt(limit));
} catch (NumberFormatException e) {
throw new BadRequestException("Unable to convert 'limit' to a valid number");
}
}
if (query.hasQueryStringParam("start_index")) {
final String idx = query.getQueryStringParam("start_index");
try {
search_query.setStartIndex(Integer.parseInt(idx));
} catch (NumberFormatException e) {
throw new BadRequestException("Unable to convert 'start_index' to a valid number");
}
}
return search_query;
}
use of net.opentsdb.search.SearchQuery in project opentsdb by OpenTSDB.
the class SearchRpc method execute.
/**
* Handles the /api/search/<type> endpoint
* @param tsdb The TSDB to which we belong
* @param query The HTTP query to work with
*/
@Override
public void execute(TSDB tsdb, HttpQuery query) {
final HttpMethod method = query.getAPIMethod();
if (method != HttpMethod.GET && method != HttpMethod.POST) {
throw new BadRequestException("Unsupported method: " + method.getName());
}
// the uri will be /api/vX/search/<type> or /api/search/<type>
final String[] uri = query.explodeAPIPath();
final String endpoint = uri.length > 1 ? uri[1] : "";
final SearchType type;
final SearchQuery search_query;
try {
type = SearchQuery.parseSearchType(endpoint);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid search query type supplied", e);
}
if (query.hasContent()) {
search_query = query.serializer().parseSearchQueryV1();
} else {
search_query = parseQueryString(query, type);
}
search_query.setType(type);
if (type == SearchType.LOOKUP) {
processLookup(tsdb, query, search_query);
return;
}
try {
final SearchQuery results = tsdb.executeSearch(search_query).joinUninterruptibly();
query.sendReply(query.serializer().formatSearchResultsV1(results));
} catch (IllegalStateException e) {
throw new BadRequestException("Searching is not enabled", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of net.opentsdb.search.SearchQuery in project opentsdb by OpenTSDB.
the class TestSearchRpc method setupAnswerSearchQuery.
/**
* Configures an Answer to respond with when the tests call
* tsdb.executeSearch(), responding to the type of query requested with valid
* responses for parsing tests.
*/
private void setupAnswerSearchQuery() {
mock_plugin = mock(SearchPlugin.class);
Whitebox.setInternalState(tsdb, "search", mock_plugin);
when(mock_plugin.executeQuery((SearchQuery) any())).thenAnswer(new Answer<Deferred<SearchQuery>>() {
@Override
public Deferred<SearchQuery> answer(InvocationOnMock invocation) throws Throwable {
final Object[] args = invocation.getArguments();
search_query = (SearchQuery) args[0];
List<Object> results = new ArrayList<Object>(1);
// if we want an empty response, return an empty response
if (search_query.getQuery().toUpperCase().equals("EMTPY")) {
search_query.setResults(results);
search_query.setTotalResults(0);
return Deferred.fromResult(search_query);
}
switch(search_query.getType()) {
case TSMETA:
final TSMeta meta = new TSMeta("000001000001000001");
meta.setCreated(1356998400);
meta.setDescription("System CPU metric");
UIDMeta uid = new UIDMeta(UniqueIdType.METRIC, "000001");
final Field uid_name = UIDMeta.class.getDeclaredField("name");
uid_name.setAccessible(true);
uid_name.set(uid, "sys.cpu.0");
final Field metric = TSMeta.class.getDeclaredField("metric");
metric.setAccessible(true);
metric.set(meta, uid);
final ArrayList<UIDMeta> tags = new ArrayList<UIDMeta>(2);
uid = new UIDMeta(UniqueIdType.TAGK, "000001");
uid_name.set(uid, "host");
tags.add(uid);
uid = new UIDMeta(UniqueIdType.TAGV, "000001");
uid_name.set(uid, "web01");
tags.add(uid);
final Field tags_field = TSMeta.class.getDeclaredField("tags");
tags_field.setAccessible(true);
tags_field.set(meta, tags);
results.add(meta);
break;
case LOOKUP:
case TSMETA_SUMMARY:
final HashMap<String, Object> ts = new HashMap<String, Object>(1);
ts.put("metric", "sys.cpu.0");
final HashMap<String, String> tag_map = new HashMap<String, String>(2);
tag_map.put("host", "web01");
tag_map.put("owner", "ops");
ts.put("tags", tag_map);
ts.put("tsuid", "000001000001000001");
results.add(ts);
break;
case TSUIDS:
results.add("000001000001000001");
results.add("000002000002000002");
break;
case UIDMETA:
UIDMeta uid2 = new UIDMeta(UniqueIdType.METRIC, "000001");
final Field name_field = UIDMeta.class.getDeclaredField("name");
name_field.setAccessible(true);
name_field.set(uid2, "sys.cpu.0");
results.add(uid2);
uid2 = new UIDMeta(UniqueIdType.TAGK, "000001");
name_field.set(uid2, "host");
results.add(uid2);
break;
case ANNOTATION:
final Annotation note = new Annotation();
note.setStartTime(1356998400);
note.setEndTime(1356998460);
note.setDescription("Something went pear shaped");
note.setTSUID("000001000001000001");
results.add(note);
break;
}
search_query.setResults(results);
search_query.setTotalResults(results.size());
search_query.setTime(0.42F);
return Deferred.fromResult(search_query);
}
});
}
Aggregations