Esempio n. 1
0
    def __search_metadata(
            self, selector: SeriesSelector) -> Generator[Metadata, None, None]:
        connection = self.connect()
        cursor = connection.cursor()
        dictionary_cursor = None
        if self._config.dictionary_query is not None:
            dictionary_cursor = self.connect().cursor()

        cursor.execute(self._config.list_query)
        series_name_index = None
        for i, name in enumerate(self._config.list_columns):
            if name == "series name":
                series_name_index = i
        if series_name_index is None:
            raise InvalidMetadataError('column "series name" not found')
        for row in cursor:
            selector = SeriesSelector(selector.source, row[series_name_index])
            metadata = Metadata(selector)
            for i, name in enumerate(self._config.list_columns):
                if i == series_name_index:
                    continue
                if row[i] is None:
                    continue
                metadata.set_field(
                    name,
                    self._metadata_value_mapper.from_source(name, row[i]))
            if metadata.dictionary_name is not None and dictionary_cursor is not None:
                metadata.dictionary = self.__query_dictionary(
                    dictionary_cursor, metadata.dictionary_name)
            yield metadata
Esempio n. 2
0
    def get_metadata(self, selector: SeriesSelector) -> Metadata:
        """Read metadata from the DB-API connection."""
        metadata = Metadata(selector)
        if self._config.metadata_query is None:
            return metadata
        connection = self.connect()
        cursor = connection.cursor()

        query = self._config.metadata_query
        params = [selector.name]
        if self._config.query_string_parameters:
            query = query.format(*params)
            params = []
        cursor.execute(query, params)
        row = cursor.fetchone()
        if row:
            for i, name in enumerate(self._config.metadata_columns):
                if row[i] is not None:
                    metadata.set_field(
                        name,
                        self._metadata_value_mapper.from_source(name, row[i]))
        if metadata.dictionary_name is not None:
            metadata.dictionary = self.__query_dictionary(
                cursor, metadata.dictionary_name)
        return metadata
Esempio n. 3
0
    def get_metadata(self, selector: SeriesSelector) -> Metadata:
        """Read metadata, taking any configured metadata mapping into account."""
        metadata = Metadata(selector)
        if self.__loaders.metadata is None:
            return metadata

        with self.__loaders.metadata.open() as metadata_file:
            reader = csv.DictReader(metadata_file)
            for row in reader:
                if self.__metadata_mapper.from_kukur("series name") not in row:
                    raise InvalidMetadataError(
                        'column "series name" not found')
                if (row[self.__metadata_mapper.from_kukur("series name")] !=
                        selector.name):
                    continue
                for field, _ in metadata:
                    if self.__metadata_mapper.from_kukur(field) in row:
                        try:
                            value = row[self.__metadata_mapper.from_kukur(
                                field)]
                            metadata.set_field(
                                field,
                                self.__metadata_value_mapper.from_source(
                                    field, value),
                            )
                        except ValueError:
                            pass

            if metadata.dictionary_name is not None:
                metadata.dictionary = self.__get_dictionary(
                    metadata.dictionary_name)

        return metadata
Esempio n. 4
0
 def search(self,
            selector: SeriesSelector) -> Generator[Metadata, None, None]:
     """Search for series matching the given selector."""
     many_series = self.__client.get_list_series()
     fields = self.__client.query("SHOW FIELD KEYS")
     for series in many_series:
         series_name = series.replace("\\", "")
         measurement = series_name.split(",")[0]
         for field in fields.get_points(measurement=measurement):
             yield Metadata(
                 SeriesSelector(selector.source,
                                f'{series_name}::{field["fieldKey"]}'))
Esempio n. 5
0
def _read_metadata(data: Dict[str, Any]) -> Metadata:
    series = SeriesSelector(data["series"]["source"], data["series"]["name"])
    metadata = Metadata(series)
    for k, v in data.items():
        if v is None:
            continue
        if k == "series":
            continue
        if k == "dictionary":
            metadata.set_field(k, Dictionary(dict(v)))
            continue
        metadata.set_field(k, v)
    return metadata
Esempio n. 6
0
    def search(self,
               selector: SeriesSelector) -> Generator[Metadata, None, None]:
        """Search for series matching the given selector."""
        if self.__loaders.metadata is None:
            return

        with self.__loaders.metadata.open() as metadata_file:
            reader = csv.DictReader(metadata_file)
            for row in reader:
                if self.__metadata_mapper.from_kukur("series name") not in row:
                    raise InvalidMetadataError(
                        'column "series name" not found')
                series_name = row[self.__metadata_mapper.from_kukur(
                    "series name")]
                metadata = None
                if selector.name is not None:
                    if series_name == selector.name:
                        metadata = Metadata(
                            SeriesSelector(selector.source, series_name))
                else:
                    metadata = Metadata(
                        SeriesSelector(selector.source, series_name))

                if metadata is not None:
                    for field, _ in metadata:
                        if self.__metadata_mapper.from_kukur(field) in row:
                            try:
                                value = row[self.__metadata_mapper.from_kukur(
                                    field)]
                                metadata.set_field(
                                    field,
                                    self.__metadata_value_mapper.from_source(
                                        field, value),
                                )
                            except ValueError:
                                pass
                    if metadata.dictionary_name is not None:
                        metadata.dictionary = self.__get_dictionary(
                            metadata.dictionary_name)
                    yield metadata
Esempio n. 7
0
    def get_metadata(self, selector: SeriesSelector) -> Metadata:
        """Return the metadata for the given series.

        The resulting metadata is the combination of the metadata in the source itself and any additional
        metadata sources. Metadata sources earlier in the list of sources take precendence over later ones."""
        metadata = Metadata(selector)
        if selector.name is None:
            return metadata
        for metadata_source in list(reversed(self.__metadata)) + [
            MetadataSource(self.__source.metadata)
        ]:
            received_metadata = metadata_source.source.get_metadata(selector)
            if len(metadata_source.fields) == 0:
                for k, v in received_metadata:
                    if v is not None and v != "":
                        metadata.set_field(k, v)
            else:
                for field_name in metadata_source.fields:
                    field_value = received_metadata.get_field(field_name)
                    if field_value is not None and field_value != "":
                        metadata.set_field(field_name, field_value)
        return metadata
Esempio n. 8
0
 def get_metadata(self, selector: SeriesSelector) -> Metadata:
     """Feather currently always returns empty metadata."""
     return Metadata(selector)