예제 #1
0
    async def get_i3histogram(
        self,
        database_name: str,
        collection_name: str,
        histogram_name: str,
        remove_id: bool = True,
    ) -> Optional[I3Histogram]:
        """Return I3Histogram object.

        Also type-checks the required I3Histogram attributes.
        """
        collection = self.md_mc.get_collection(database_name, collection_name)

        if remove_id:
            mongo_histogram = await collection.find_one(
                {"name": histogram_name}, projection=REMOVE_ID)
        else:
            mongo_histogram = await collection.find_one(
                {"name": histogram_name})

        if not mongo_histogram:
            return None

        try:
            histogram = I3Histogram.from_dict(mongo_histogram)
        except (NameError, AttributeError, TypeError) as e:
            raise tornado.web.HTTPError(500, reason=str(e))

        return histogram
예제 #2
0
    def test_30() -> None:
        """Test from_dict() and to_dict()."""
        name = "test"
        xmax = 100.1
        xmin = 0.023
        overflow = 5
        underflow = 3
        nan_count = 12
        bin_values = [0, 2, 4.02, 5, 9.486, 8, 5]
        history = [10.25, 300]
        extra_value = "extra"
        dict_ = {
            "name": name,
            "xmax": xmax,
            "xmin": xmin,
            "overflow": overflow,
            "underflow": underflow,
            "nan_count": nan_count,
            "bin_values": bin_values,
            "history": history,
            "extra_value": extra_value,
        }

        histogram = I3Histogram.from_dict(dict_)  # type: ignore
        TestI3Histogram.assert_values(histogram, name, xmax, xmin, overflow,
                                      underflow, nan_count, bin_values)

        out_dict = histogram.to_dict()
        assert dict_ == out_dict
예제 #3
0
    async def insert_histogram(self, database_name: str, collection_name: str,
                               histogram: I3Histogram) -> None:
        """Insert the novel histogram.

        Write back to output buffer.
        """
        histogram.add_to_history()  # record when this happened

        # put in DB
        collection = await self.md_mc.get_create_collection(
            database_name, collection_name)
        await collection.insert_one(histogram.to_dict())

        # write
        self.write({
            "database": database_name,
            "collection": collection_name,
            "histogram": histogram.to_dict(exclude=EXCLUDE_KEYS),
            "history": histogram.history,
            "updated": False,
        })
예제 #4
0
    def test_11() -> None:
        """Test alternative types."""
        name = "test"
        xmax = 100.1
        xmin = 0.023
        overflow = 5
        underflow = 3
        nan_count = 12
        bin_values = [0, 2, 4.02, 5, 9.486, 8, 5]  # type: List[Num]

        histogram = I3Histogram(name, xmax, xmin, overflow, underflow,
                                nan_count, bin_values)
        TestI3Histogram.assert_values(histogram, name, xmax, xmin, overflow,
                                      underflow, nan_count, bin_values)

        history = [10.25, 300]
        histogram.history = history
        assert histogram.history == history
예제 #5
0
    def test_10() -> None:
        """Test basic functionality."""
        name = "test"
        xmax = 10
        xmin = 0
        overflow = 5
        underflow = 3
        nan_count = 12
        bin_values = [0, 2, 4, 5, 9, 8, 5]  # type: List[Num]

        histogram = I3Histogram(name, xmax, xmin, overflow, underflow,
                                nan_count, bin_values)
        TestI3Histogram.assert_values(histogram, name, xmax, xmin, overflow,
                                      underflow, nan_count, bin_values)

        history = [200, 500]  # type: List[Num]
        histogram.history = history
        assert histogram.history == history
예제 #6
0
    async def get_mongo_histograms_in_collection(
            self, database_name: str,
            collection_name: str) -> List[MongoHistogram]:
        """Return collection's histograms as dicts."""
        collection = self.get_collection(database_name, collection_name)

        mongo_histos = [
            o async for o in collection.find(projection=REMOVE_ID)
            if o["name"] != "filelist"
        ]

        # type check
        try:
            for histo in mongo_histos:
                _ = I3Histogram.from_dict(histo)
        except (NameError, AttributeError, TypeError) as e:
            raise tornado.web.HTTPError(500, reason=str(e))

        return mongo_histos
예제 #7
0
    def test_31() -> None:
        """Test to_dict().

        Test with 'exclude' keys and dynamically-added attributes.
        """
        extra = 5
        addl = ["a"]
        keeps = 2.0

        histo = I3Histogram("test", 0, 0, 0, 0, 0, [])
        histo.extra = extra  # type: ignore
        histo.addl = addl  # type: ignore
        histo.keeps = keeps  # type: ignore
        dict_ = histo.to_dict(exclude=["extra", "addl"])

        assert histo.extra == extra  # type: ignore
        assert "extra" not in dict_

        assert histo.addl == addl  # type: ignore
        assert "addl" not in dict_

        assert "keeps" in dict_
        assert dict_["keeps"] == keeps == histo.keeps  # type: ignore
예제 #8
0
    async def post(self) -> None:
        """Handle POST."""
        database_name = self.get_required_argument("database")
        collection_name = self.get_required_argument("collection")
        mongo_histogram = self.get_required_argument("histogram")
        update = self.get_optional_argument("update", default=False)

        # check reserved key(s)
        if "history" in mongo_histogram:
            raise tornado.web.HTTPError(
                400, reason="histogram cannot define the field 'history'")

        # check type and structure
        try:
            histogram = I3Histogram.from_dict(mongo_histogram)
        except (NameError, AttributeError, TypeError) as e:
            raise tornado.web.HTTPError(400, reason=str(e))

        # is the histogram already in the collection?
        async def histogram_exists() -> bool:
            return bool(await
                        self.get_i3histogram(database_name, collection_name,
                                             histogram.name))

        # update/insert
        if await histogram_exists():
            if not update:
                raise tornado.web.HTTPError(
                    409,
                    reason=f"histogram already in collection ({histogram.name})"
                )
            await self.update_histogram(database_name, collection_name,
                                        histogram)
        else:
            await self.insert_histogram(database_name, collection_name,
                                        histogram)
예제 #9
0
 def test_20() -> None:
     """Fail-test name attribute."""
     with pytest.raises(NameError):
         _ = I3Histogram("filelist", 0, 0, 0, 0, 0, [])