<commit_before>import configparser
import datetime
import logging

import click
import influxdb
import phue

import akari.log_config  # noqa: F401


log = logging.getLogger(__name__)


def connect_to_hue_bridge(hue_config: configparser.SectionProxy) -> phue.Bridge:
    return phue.Bridge(
        ip=hue_config['ip_address'],
        username=hue_config['username'],
    )


def connect_to_influxdb(influxdb_config: configparser.SectionProxy) -> influxdb.InfluxDBClient:
    return influxdb.InfluxDBClient(
        host=influxdb_config['host'],
        port=int(influxdb_config['port']),
        database=influxdb_config['database'],
        username=influxdb_config['username'],
        password=influxdb_config['password'],
        ssl=influxdb_config.getboolean('use_ssl'),
    )


class InfluxdbPointBuilder:
    def __init__(self, measurement: str, utc_timestamp: datetime.datetime):
        self.measurement = measurement
        self.formatted_utc_timestamp = '{0}Z'.format(utc_timestamp.isoformat())

        # `InfluxDBClient.write_points`-friendly stored data
        self.points = []

    def add_point(
            self,
            *,
            light_name: str,
            light_unique_id: str,
            room_name: str,
            room_class: str,
            configured_brightness: int,
            effective_brightness: int,
            is_on: bool,
            is_reachable: bool) -> None:
        """
        Note that the brightness of a bulb is in the range [1, 254].
        However, if the bulb is off, then the effective brightness is 0.

        :param light_name: the human-provided name for this bulb
        :param light_unique_id: a long string like 'fc00:db20:35b:7399::5-0b'
        :param room_name: if in a room, the human-provided name of the room
        :param room_class: if in a room, the human-specified class of the room
        :param configured_brightness: how bright the bulb is set to be, if on
        :param effective_brightness: how bright the bulb actually is
        :param is_on: whether the bulb is on
        :param is_reachable: whether the bulb is connected to the bridge
        """
        self.points.append({
            'timestamp': self.formatted_utc_timestamp,
            'measurement': self.measurement,
            'tags': {
                'light_name': light_name,
                'light_unique_id': light_unique_id,
                'room_name': room_name,
                'room_class': room_class,
            },
            'fields': {
                'configured_brightness': configured_brightness,
                'effective_brightness': effective_brightness,
                'is_on': is_on,
                'is_reachable': is_reachable,
            }
        })


@click.command()
@click.option('--config', 'config_path',
              type=click.Path(exists=True, readable=True, resolve_path=True))
def main(config_path):
    if not config_path:
        raise click.BadParameter("Config path must be supplied")

    log.info('Reading config from %s', config_path)
    config = configparser.ConfigParser()
    with open(config_path) as config_file:
        config.read_file(config_file)

    hue_bridge = connect_to_hue_bridge(config['hue_bridge'])
    influxdb_client = connect_to_influxdb(config['influxdb'])

    rooms = hue_bridge.get_group()
    light_id_to_room_id = {}
    for room_id in rooms:
        for light_id in rooms[room_id]['lights']:
            light_id_to_room_id[light_id] = room_id

    lights = hue_bridge.get_light()
<commit_msg>Switch is_on and is_reachable from field to tag<commit_after>import configparser
import datetime
import logging

import click
import influxdb
import phue

import akari.log_config  # noqa: F401


log = logging.getLogger(__name__)


def connect_to_hue_bridge(hue_config: configparser.SectionProxy) -> phue.Bridge:
    return phue.Bridge(
        ip=hue_config['ip_address'],
        username=hue_config['username'],
    )


def connect_to_influxdb(influxdb_config: configparser.SectionProxy) -> influxdb.InfluxDBClient:
    return influxdb.InfluxDBClient(
        host=influxdb_config['host'],
        port=int(influxdb_config['port']),
        database=influxdb_config['database'],
        username=influxdb_config['username'],
        password=influxdb_config['password'],
        ssl=influxdb_config.getboolean('use_ssl'),
    )


class InfluxdbPointBuilder:
    def __init__(self, measurement: str, utc_timestamp: datetime.datetime):
        self.measurement = measurement
        self.formatted_utc_timestamp = '{0}Z'.format(utc_timestamp.isoformat())

        # `InfluxDBClient.write_points`-friendly stored data
        self.points = []

    def add_point(
            self,
            *,
            light_name: str,
            light_unique_id: str,
            room_name: str,
            room_class: str,
            configured_brightness: int,
            effective_brightness: int,
            is_on: bool,
            is_reachable: bool) -> None:
        """
        Note that the brightness of a bulb is in the range [1, 254].
        However, if the bulb is off, then the effective brightness is 0.

        :param light_name: the human-provided name for this bulb
        :param light_unique_id: a long string like '00:17:88:01:10:5c:8a:5d-0b'
        :param room_name: if in a room, the human-provided name of the room
        :param room_class: if in a room, the human-specified class of the room
        :param configured_brightness: how bright the bulb is set to be, if on
        :param effective_brightness: how bright the bulb actually is
        :param is_on: whether the bulb is on
        :param is_reachable: whether the bulb is connected to the bridge
        """
        self.points.append({
            'timestamp': self.formatted_utc_timestamp,
            'measurement': self.measurement,
            'tags': {
                'light_name': light_name,
                'light_unique_id': light_unique_id,
                'room_name': room_name,
                'room_class': room_class,
                'is_on': 'true' if is_on else 'false',
                'is_reachable': 'true' if is_reachable else 'false',
            },
            'fields': {
                'configured_brightness': configured_brightness,
                'effective_brightness': effective_brightness,
            }
        })


@click.command()
@click.option('--config', 'config_path',
              type=click.Path(exists=True, readable=True, resolve_path=True))
def main(config_path):
    if not config_path:
        raise click.BadParameter("Config path must be supplied")

    log.info('Reading config from %s', config_path)
    config = configparser.ConfigParser()
    with open(config_path) as config_file:
        config.read_file(config_file)

    hue_bridge = connect_to_hue_bridge(config['hue_bridge'])
    influxdb_client = connect_to_influxdb(config['influxdb'])

    rooms = hue_bridge.get_group()
    light_id_to_room_id = {}
    for room_id in rooms:
        for light_id in rooms[room_id]['lights']:
            light_id_to_room_id[light_id] = room_id

    lights = hue_bridge.get_light()

{'fraction_non_alphanumeric': 0.09297115653878466, 'fraction_numerical': 0.006160739288714646, 'mean_word_length': 2.6258883248730966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4355414', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2085, 'n_words': 507}