def test_scan_for_tilt_data_parse_influxdb_with_mac(mock_influxdb, ): config = MockConfigParserMac('influxdb')[0] emitter = influxdb.InfluxDB(config=config) tilty.emit(emitters=[emitter], tilt_data={ 'color': 'black', 'gravity': 1, 'temp': 32, 'mac': '00:0a:95:9d:68:16', 'timestamp': 155558888 }) assert mock_influxdb.mock_calls == [ mock.call( config={ 'url': 'http://www.google.com', 'database': 'foo', 'gravity_payload_template': 'gravity,mac={{ mac }} color={{ color }} value={{ gravity }} {{timestamp}}', # noqa 'temperature_payload_template': 'temperature,scale=fahrenheit,mac={{ mac }} color={{ color }} value={{ temp }} {{timestamp}}' # noqa }), mock.call().emit( tilt_data={ 'color': 'black', 'gravity': 1, 'temp': 32, 'mac': '00:0a:95:9d:68:16', 'timestamp': 155558888 }) ]
def test_influxdb(mock_influx_client, ): config = { 'url': 'http://www.google.com', 'database': 'foo', 'gravity_payload': '{"measurement": "gravity", "tags": {"color": "Black"}, "fields": {"value": 1.054}}', # noqa 'temperature_payload': '{"measurement": "temperature", "tags": {"color": "Black", "scale": "fahrenheight"}, "fields": {"value": 32}}', # noqa } influxdb.InfluxDB(config=config).emit() assert mock_influx_client.mock_calls == [ mock.call('http://www.google.com', 80, None, None, 'foo'), mock.call().write_points([{ 'measurement': 'temperature', 'tags': { 'color': 'Black', 'scale': 'fahrenheight' }, 'fields': { 'value': 32 } }]), mock.call().write_points([{ 'measurement': 'gravity', 'tags': { 'color': 'Black' }, 'fields': { 'value': 1.054 } }]) ]
def test_influxdb(mock_influx_client, ): config = { 'url': 'http://www.google.com', 'database': 'foo', 'gravity_payload_template': '{"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}}', # noqa 'temperature_payload_template': '{"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}}', # noqa } influxdb.InfluxDB(config=config).emit({ 'temp': 80, 'color': 'black', 'gravity': 1.054, 'timestamp': 155558888, 'mac': 'foo', }) assert mock_influx_client.mock_calls == [ mock.call('http://www.google.com', 80, None, None, 'foo'), mock.call().write_points([{ 'measurement': 'temperature', 'tags': { 'color': 'black' }, 'fields': { 'value': 80 } }]), mock.call().write_points([{ 'measurement': 'gravity', 'tags': { 'color': 'black' }, 'fields': { 'value': 1.054 } }]) ]
def test_influxdb( mock_influx_client, ): config = { 'url': 'http://www.google.com', 'org': 'foo', 'bucket': 'wat', 'token': 'somelongtoken', 'gravity_payload_template': '{"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}}', # noqa 'temperature_payload_template': '{"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}}', # noqa } influxdb.InfluxDB(config=config).emit({ 'temp': 80, 'color': 'black', 'gravity': 1.054, 'timestamp': 155558888, 'mac': 'foo', }) assert mock_influx_client.mock_calls == [ mock.call( org='foo', token='somelongtoken', url='http://www.google.com', verify_ssl=False ), mock.call().write_api(write_options=mock.ANY), mock.call().write_api().write( bucket='wat', org='foo', record='{"measurement": "temperature", "tags": {"color": "black"}, "fields": {"value": 80}}' # noqa ), mock.call().write_api().write( bucket='wat', org='foo', record='{"measurement": "gravity", "tags": {"color": "black"}, "fields": {"value": 1.054}}' # noqa ), ]
def emit(config, tilt_data): """ Find and call emitters from config config (dict): configuration file loaded from disk tilt_data (dict): data returned from valid tilt device scan """ if tilt_data is None: return # <start config sample> # [webhook] # url = http://www.foo.com # self.headers = {"Content-Type": "application/json"} # payload_template = {"color": "{{ color }}", "gravity"... # method = GET if config.has_section('webhook'): _template = Template(config['webhook']['payload_template']) _config = { 'url': config['webhook']['url'], 'headers': json.loads(config['webhook'].get('headers')), 'method': config['webhook']['method'], 'payload': json.loads( _template.render( color=tilt_data['color'], gravity=tilt_data['gravity'], temp=tilt_data['temp'], timestamp=tilt_data['timestamp'], )), } _webhook = webhook.Webhook(config=_config) _webhook.emit() # <start config sample> # [influxdb] # url = www.foo.com # port = 80 # database = tilty # gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}} # noqa # pylint: disable=line-too-long # temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}} # noqa # pylint: disable=line-too-long if config.has_section('influxdb'): _gravity_template = Template( config['influxdb']['gravity_payload_template']) # noqa _temperature_template = Template( config['influxdb']['temperature_payload_template']) # noqa _config = { 'url': config['influxdb']['url'], 'database': config['influxdb']['database'], 'temperature_payload': _temperature_template.render( color=tilt_data['color'], gravity=tilt_data['gravity'], temp=tilt_data['temp'], timestamp=tilt_data['timestamp'], ), 'gravity_payload': _gravity_template.render( color=tilt_data['color'], gravity=tilt_data['gravity'], temp=tilt_data['temp'], timestamp=tilt_data['timestamp'], ), } _influxdb = influxdb.InfluxDB(config=_config) _influxdb.emit() # <start config sample> # [datadog] # host = 'host' # port = 'port' if config.has_section('datadog'): _config = { 'host': config['datadog']['host'], 'port': config['datadog']['port'], 'gravity': tilt_data['gravity'], 'temperature': tilt_data['temp'], 'color': tilt_data['color'], } _datadog = datadog.Datadog(config=_config) _datadog.emit()