# Path: osprofiler/drivers/mongodb.py
# class MongoDB(base.Driver):
#     def __init__(self, connection_str, db_name="osprofiler", project=None,
#                  service=None, host=None, **kwargs):
#         """MongoDB driver for OSProfiler."""
# 
#         super(MongoDB, self).__init__(connection_str, project=project,
#                                       service=service, host=host, **kwargs)
#         try:
#             from pymongo import MongoClient
#         except ImportError:
#             raise exc.CommandError(
#                 "To use OSProfiler with MongoDB driver, "
#                 "please install `pymongo` library. "
#                 "To install with pip:\n `pip install pymongo`.")
# 
#         client = MongoClient(self.connection_str, connect=False)
#         self.db = client[db_name]
# 
#     @classmethod
#     def get_name(cls):
#         return "mongodb"
# 
#     def notify(self, info):
#         """Send notifications to MongoDB.
# 
#         :param info:  Contains information about trace element.
#                       In payload dict there are always 3 ids:
#                       "base_id" - uuid that is common for all notifications
#                                   related to one trace. Used to simplify
#                                   retrieving of all trace elements from
#                                   MongoDB.
#                       "parent_id" - uuid of parent element in trace
#                       "trace_id" - uuid of current element in trace
# 
#                       With parent_id and trace_id it's quite simple to build
#                       tree of trace elements, which simplify analyze of trace.
# 
#         """
#         data = info.copy()
#         data["project"] = self.project
#         data["service"] = self.service
#         self.db.profiler.insert_one(data)
# 
#         if (self.filter_error_trace
#                 and data.get("info", {}).get("etype") is not None):
#             self.notify_error_trace(data)
# 
#     def notify_error_trace(self, data):
#         """Store base_id and timestamp of error trace to a separate db."""
#         self.db.profiler_error.update(
#             {"base_id": data["base_id"]},
#             {"base_id": data["base_id"], "timestamp": data["timestamp"]},
#             upsert=True
#         )
# 
#     def list_traces(self, fields=None):
#         """Query all traces from the storage.
# 
#         :param fields: Set of trace fields to return. Defaults to 'base_id'
#                and 'timestamp'
#         :return List of traces, where each trace is a dictionary containing
#                 at least `base_id` and `timestamp`.
#         """
#         fields = set(fields or self.default_trace_fields)
#         ids = self.db.profiler.find({}).distinct("base_id")
#         out_format = {"base_id": 1, "timestamp": 1, "_id": 0}
#         out_format.update({i: 1 for i in fields})
#         return [self.db.profiler.find(
#                 {"base_id": i}, out_format).sort("timestamp")[0] for i in ids]
# 
#     def list_error_traces(self):
#         """Returns all traces that have error/exception."""
#         out_format = {"base_id": 1, "timestamp": 1, "_id": 0}
#         return self.db.profiler_error.find({}, out_format)
# 
#     def get_report(self, base_id):
#         """Retrieves and parses notification from MongoDB.
# 
#         :param base_id: Base id of trace elements.
#         """
#         for n in self.db.profiler.find({"base_id": base_id}, {"_id": 0}):
#             trace_id = n["trace_id"]
#             parent_id = n["parent_id"]
#             name = n["name"]
#             project = n["project"]
#             service = n["service"]
#             host = n["info"]["host"]
#             timestamp = n["timestamp"]
# 
#             self._append_results(trace_id, parent_id, name, project, service,
#                                  host, timestamp, n)
# 
#         return self._parse_results()
# 
# Path: osprofiler/tests/test.py
# class TestCase(testcase.TestCase):
# class FunctionalTestCase(TestCase):
#     def setUp(self):


from unittest import mock
from osprofiler.drivers.mongodb import MongoDB
from osprofiler.tests import test
                        "query": ""
                    },
                    "service": None
                },
                "name": "wsgi-start",
                "service": "main",
                "timestamp": "2015-12-23T14:02:22.338776",
                "trace_id": "06320327-2c2c-45ae-923a-515de890276a",
                "project": "keystone",
                "parent_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4",
                "base_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4"
            },

            {
                "info": {
                    "project": None,
                    "host": "ubuntu",
                    "service": None
                },
                "name": "wsgi-stop",
                "service": "main",
                "timestamp": "2015-12-23T14:02:22.380405",
                "trace_id": "839ca3f1-afcb-45be-a4a1-679124c552bf",
                "project": "keystone",
                "parent_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4",
                "base_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4"
            },

            {
                "info": {

# Path: osprofiler/drivers/mongodb.py
# class MongoDB(base.Driver):
#     def __init__(self, connection_str, db_name="osprofiler", project=None,
#                  service=None, host=None, **kwargs):
#         """MongoDB driver for OSProfiler."""
# 
#         super(MongoDB, self).__init__(connection_str, project=project,
#                                       service=service, host=host, **kwargs)
#         try:
#             from pymongo import MongoClient
#         except ImportError:
#             raise exc.CommandError(
#                 "To use OSProfiler with MongoDB driver, "
#                 "please install `pymongo` library. "
#                 "To install with pip:\n `pip install pymongo`.")
# 
#         client = MongoClient(self.connection_str, connect=False)
#         self.db = client[db_name]
# 
#     @classmethod
#     def get_name(cls):
#         return "mongodb"
# 
#     def notify(self, info):
#         """Send notifications to MongoDB.
# 
#         :param info:  Contains information about trace element.
#                       In payload dict there are always 3 ids:
#                       "base_id" - uuid that is common for all notifications
#                                   related to one trace. Used to simplify
#                                   retrieving of all trace elements from
#                                   MongoDB.
#                       "parent_id" - uuid of parent element in trace
#                       "trace_id" - uuid of current element in trace
# 
#                       With parent_id and trace_id it's quite simple to build
#                       tree of trace elements, which simplify analyze of trace.
# 
#         """
#         data = info.copy()
#         data["project"] = self.project
#         data["service"] = self.service
#         self.db.profiler.insert_one(data)
# 
#         if (self.filter_error_trace
#                 and data.get("info", {}).get("etype") is not None):
#             self.notify_error_trace(data)
# 
#     def notify_error_trace(self, data):
#         """Store base_id and timestamp of error trace to a separate db."""
#         self.db.profiler_error.update(
#             {"base_id": data["base_id"]},
#             {"base_id": data["base_id"], "timestamp": data["timestamp"]},
#             upsert=True
#         )
# 
#     def list_traces(self, fields=None):
#         """Query all traces from the storage.
# 
#         :param fields: Set of trace fields to return. Defaults to 'base_id'
#                and 'timestamp'
#         :return List of traces, where each trace is a dictionary containing
#                 at least `base_id` and `timestamp`.
#         """
#         fields = set(fields or self.default_trace_fields)
#         ids = self.db.profiler.find({}).distinct("base_id")
#         out_format = {"base_id": 1, "timestamp": 1, "_id": 0}
#         out_format.update({i: 1 for i in fields})
#         return [self.db.profiler.find(
#                 {"base_id": i}, out_format).sort("timestamp")[0] for i in ids]
# 
#     def list_error_traces(self):
#         """Returns all traces that have error/exception."""
#         out_format = {"base_id": 1, "timestamp": 1, "_id": 0}
#         return self.db.profiler_error.find({}, out_format)
# 
#     def get_report(self, base_id):
#         """Retrieves and parses notification from MongoDB.
# 
#         :param base_id: Base id of trace elements.
#         """
#         for n in self.db.profiler.find({"base_id": base_id}, {"_id": 0}):
#             trace_id = n["trace_id"]
#             parent_id = n["parent_id"]
#             name = n["name"]
#             project = n["project"]
#             service = n["service"]
#             host = n["info"]["host"]
#             timestamp = n["timestamp"]
# 
#             self._append_results(trace_id, parent_id, name, project, service,
#                                  host, timestamp, n)
# 
#         return self._parse_results()
# 
# Path: osprofiler/tests/test.py
# class TestCase(testcase.TestCase):
# class FunctionalTestCase(TestCase):
#     def setUp(self):


# Path: osprofiler/tests/unit/drivers/test_mongodb.py
from unittest import mock
from osprofiler.drivers.mongodb import MongoDB
from osprofiler.tests import test

                        "query": ""
                    },
                    "service": None
                },
                "name": "wsgi-start",
                "service": "main",
                "timestamp": "2015-12-23T14:02:22.338776",
                "trace_id": "06320327-2c2c-45ae-923a-515de890276a",
                "project": "keystone",
                "parent_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4",
                "base_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4"
            },

            {
                "info": {
                    "project": None,
                    "host": "ubuntu",
                    "service": None
                },
                "name": "wsgi-stop",
                "service": "main",
                "timestamp": "2015-12-23T14:02:22.380405",
                "trace_id": "839ca3f1-afcb-45be-a4a1-679124c552bf",
                "project": "keystone",
                "parent_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4",
                "base_id": "7253ca8c-33b3-4f84-b4f1-f5a4311ddfa4"
            },

            {
                "info": {

                    "project": None,