graphite: Reorganize the elastic search code so we can put it in chromite.

This change reorganizes the elastic search integration code so that it's
separate from the code that, for instance, reads config information from the
autotest global config. That way, it can be moved from chromite without
breaking any dependencies.

BUG=chromium:446291
TEST=Ran stats_es_functionaltest.py. Ran unit tests. Ran a butterfly-paladin
tryjob with --hwtest.

Change-Id: I0dbf135c4f1732d633e5fc9d5edb9e1f4f7199d5
Reviewed-on: https://chromium-review.googlesource.com/242701
Reviewed-by: Dan Shi <dshi@chromium.org>
Tested-by: Gabe Black <gabeblack@chromium.org>
Commit-Queue: Gabe Black <gabeblack@chromium.org>
diff --git a/site_utils/devserver_history.py b/site_utils/devserver_history.py
index 492a000..af91c25 100755
--- a/site_utils/devserver_history.py
+++ b/site_utils/devserver_history.py
@@ -52,7 +52,7 @@
 import common
 from autotest_lib.client.common_lib import global_config
 from autotest_lib.client.common_lib import time_utils
-from autotest_lib.client.common_lib.cros.graphite import es_utils
+from autotest_lib.client.common_lib.cros.graphite import autotest_es
 
 
 class devserver_call(object):
@@ -62,13 +62,13 @@
     def __init__(self, hit):
         """Retrieve information from a ES query hit.
         """
-        self.devserver = hit['_source']['devserver']
-        self.subname = hit['_source']['subname']
-        self.artifacts = hit['_source']['artifacts'].split(' ')
-        self.image = hit['_source']['image']
-        self.value = hit['_source']['value']
+        self.devserver = hit['devserver']
+        self.subname = hit['subname']
+        self.artifacts = hit['artifacts'].split(' ')
+        self.image = hit['image']
+        self.value = hit['value']
         self.time_recorded = time_utils.epoch_time_to_date_string(
-                hit['_source']['time_recorded'])
+                hit['time_recorded'])
 
 
     def __str__(self):
@@ -100,7 +100,7 @@
             eqs.append(('artifacts', artifact))
     time_start_epoch = time_utils.to_epoch_time(time_start)
     time_end_epoch = time_utils.to_epoch_time(time_end)
-    query = es_utils.create_range_eq_query_multiple(
+    results = autotest_es.query(
             fields_returned=None,
             equality_constraints=eqs,
             range_constraints=[('time_recorded', time_start_epoch,
@@ -108,9 +108,8 @@
             size=size,
             sort_specs=[{'time_recorded': 'desc'}],
             regex_constraints=regex_constraints)
-    results = es_utils.execute_query(query, timeout=30)
     devserver_calls = []
-    for hit in results['hits']['hits']:
+    for hit in results.hits:
         devserver_calls.append(devserver_call(hit))
     logging.info('Found %d calls.', len(devserver_calls))
     return devserver_calls