Readability improvements (or so PEP 8 style guide claims...)

This commit is contained in:
sparky8512 2021-01-06 10:12:56 -08:00
parent a5036db9e0
commit d165791559
4 changed files with 74 additions and 60 deletions

View file

@ -12,7 +12,7 @@ import spacex.api.device.device_pb2_grpc
# Note that if you remove the 'with' clause here, you need to separately # Note that if you remove the 'with' clause here, you need to separately
# call channel.close() when you're done with the gRPC connection. # call channel.close() when you're done with the gRPC connection.
with grpc.insecure_channel('192.168.100.1:9200') as channel: with grpc.insecure_channel("192.168.100.1:9200") as channel:
stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel) stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={}))

View file

@ -6,14 +6,14 @@
# This script pulls the current status once and prints to stdout. # This script pulls the current status once and prints to stdout.
# #
###################################################################### ######################################################################
import datetime
import grpc import grpc
import spacex.api.device.device_pb2 import spacex.api.device.device_pb2
import spacex.api.device.device_pb2_grpc import spacex.api.device.device_pb2_grpc
import datetime with grpc.insecure_channel("192.168.100.1:9200") as channel:
with grpc.insecure_channel('192.168.100.1:9200') as channel:
stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel) stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={}))
@ -27,11 +27,25 @@ alert_bits = 0
for alert in status.alerts.ListFields(): for alert in status.alerts.ListFields():
alert_bits |= (1 if alert[1] else 0) << (alert[0].number - 1) alert_bits |= (1 if alert[1] else 0) << (alert[0].number - 1)
print(",".join([timestamp.replace(microsecond=0).isoformat(), status.device_info.id, csv_data = [
status.device_info.hardware_version, status.device_info.software_version, timestamp.replace(microsecond=0).isoformat(),
spacex.api.device.dish_pb2.DishState.Name(status.state)]) + "," + status.device_info.id,
",".join(str(x) for x in [status.device_state.uptime_s, status.snr, status.seconds_to_first_nonempty_slot, status.device_info.hardware_version,
status.pop_ping_drop_rate, status.downlink_throughput_bps, status.uplink_throughput_bps, status.device_info.software_version,
status.pop_ping_latency_ms, alert_bits, status.obstruction_stats.fraction_obstructed, spacex.api.device.dish_pb2.DishState.Name(status.state)
status.obstruction_stats.currently_obstructed, status.obstruction_stats.last_24h_obstructed_s]) + "," + ]
",".join(str(x) for x in status.obstruction_stats.wedge_abs_fraction_obstructed)) csv_data.extend(str(x) for x in [
status.device_state.uptime_s,
status.snr,
status.seconds_to_first_nonempty_slot,
status.pop_ping_drop_rate,
status.downlink_throughput_bps,
status.uplink_throughput_bps,
status.pop_ping_latency_ms,
alert_bits,
status.obstruction_stats.fraction_obstructed,
status.obstruction_stats.currently_obstructed,
status.obstruction_stats.last_24h_obstructed_s
])
csv_data.extend(str(x) for x in status.obstruction_stats.wedge_abs_fraction_obstructed)
print(",".join(csv_data))

View file

@ -7,6 +7,8 @@
# the specified InfluxDB database in a loop. # the specified InfluxDB database in a loop.
# #
###################################################################### ######################################################################
import time
from influxdb import InfluxDBClient from influxdb import InfluxDBClient
from influxdb import SeriesHelper from influxdb import SeriesHelper
@ -15,10 +17,8 @@ import grpc
import spacex.api.device.device_pb2 import spacex.api.device.device_pb2
import spacex.api.device.device_pb2_grpc import spacex.api.device.device_pb2_grpc
import time verbose = True
sleep_time = 30
fVerbose = True
sleepTime = 30
class DeviceStatusSeries(SeriesHelper): class DeviceStatusSeries(SeriesHelper):
class Meta: class Meta:
@ -41,20 +41,20 @@ class DeviceStatusSeries(SeriesHelper):
"fraction_obstructed"] "fraction_obstructed"]
tags = ["id"] tags = ["id"]
influxClient = InfluxDBClient(host="localhost", port=8086, username="script-user", password="password", database="dishstats", ssl=False, retries=1, timeout=15) influx_client = InfluxDBClient(host="localhost", port=8086, username="script-user", password="password", database="dishstats", ssl=False, retries=1, timeout=15)
try: try:
dishChannel = None dish_channel = None
lastId = None last_id = None
fLastFailed = False last_failed = False
pending = 0 pending = 0
count = 0 count = 0
while True: while True:
try: try:
if dishChannel is None: if dish_channel is None:
dishChannel = grpc.insecure_channel("192.168.100.1:9200") dish_channel = grpc.insecure_channel("192.168.100.1:9200")
stub = spacex.api.device.device_pb2_grpc.DeviceStub(dishChannel) stub = spacex.api.device.device_pb2_grpc.DeviceStub(dish_channel)
response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={}))
status = response.dish_get_status status = response.dish_get_status
DeviceStatusSeries( DeviceStatusSeries(
@ -74,45 +74,45 @@ try:
pop_ping_latency_ms=status.pop_ping_latency_ms, pop_ping_latency_ms=status.pop_ping_latency_ms,
currently_obstructed=status.obstruction_stats.currently_obstructed, currently_obstructed=status.obstruction_stats.currently_obstructed,
fraction_obstructed=status.obstruction_stats.fraction_obstructed) fraction_obstructed=status.obstruction_stats.fraction_obstructed)
lastId = status.device_info.id last_id = status.device_info.id
fLastFailed = False last_failed = False
except Exception as e: except grpc.RpcError:
if not dishChannel is None: if dish_channel is not None:
dishChannel.close() dish_channel.close()
dishChannel = None dish_channel = None
if fLastFailed: if last_failed:
if not lastId is None: if last_id is not None:
DeviceStatusSeries(id=lastId, state="DISH_UNREACHABLE") DeviceStatusSeries(id=last_id, state="DISH_UNREACHABLE")
else: else:
# Retry once, because the connection may have been lost while # Retry once, because the connection may have been lost while
# we were sleeping # we were sleeping
fLastFailed = True last_failed = True
continue continue
pending = pending + 1 pending = pending + 1
if fVerbose: if verbose:
print("Samples: " + str(pending)) print("Samples: " + str(pending))
count = count + 1 count = count + 1
if count > 5: if count > 5:
try: try:
DeviceStatusSeries.commit(influxClient) DeviceStatusSeries.commit(influx_client)
if fVerbose: if verbose:
print("Wrote " + str(pending)) print("Wrote " + str(pending))
pending = 0 pending = 0
except Exception as e: except Exception as e:
print("Failed to write: " + str(e)) print("Failed to write: " + str(e))
count = 0 count = 0
if sleepTime > 0: if sleep_time > 0:
time.sleep(sleepTime) time.sleep(sleep_time)
else: else:
break break
finally: finally:
# Flush on error/exit # Flush on error/exit
try: try:
DeviceStatusSeries.commit(influxClient) DeviceStatusSeries.commit(influx_client)
if fVerbose: if verbose:
print("Wrote " + str(pending)) print("Wrote " + str(pending))
except Exception as e: except Exception as e:
print("Failed to write: " + str(e)) print("Failed to write: " + str(e))
influxClient.close() influx_client.close()
if not dishChannel is None: if dish_channel is not None:
dishChannel.close() dish_channel.close()

View file

@ -14,7 +14,7 @@ import grpc
import spacex.api.device.device_pb2 import spacex.api.device.device_pb2
import spacex.api.device.device_pb2_grpc import spacex.api.device.device_pb2_grpc
with grpc.insecure_channel('192.168.100.1:9200') as channel: with grpc.insecure_channel("192.168.100.1:9200") as channel:
stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel) stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={}))
@ -26,25 +26,25 @@ alert_bits = 0
for alert in status.alerts.ListFields(): for alert in status.alerts.ListFields():
alert_bits |= (1 if alert[1] else 0) << (alert[0].number - 1) alert_bits |= (1 if alert[1] else 0) << (alert[0].number - 1)
topicPrefix = "starlink/dish_status/" + status.device_info.id + "/" topic_prefix = "starlink/dish_status/" + status.device_info.id + "/"
msgs = [(topicPrefix + "hardware_version", status.device_info.hardware_version, 0, False), msgs = [(topic_prefix + "hardware_version", status.device_info.hardware_version, 0, False),
(topicPrefix + "software_version", status.device_info.software_version, 0, False), (topic_prefix + "software_version", status.device_info.software_version, 0, False),
(topicPrefix + "state", spacex.api.device.dish_pb2.DishState.Name(status.state), 0, False), (topic_prefix + "state", spacex.api.device.dish_pb2.DishState.Name(status.state), 0, False),
(topicPrefix + "uptime", status.device_state.uptime_s, 0, False), (topic_prefix + "uptime", status.device_state.uptime_s, 0, False),
(topicPrefix + "snr", status.snr, 0, False), (topic_prefix + "snr", status.snr, 0, False),
(topicPrefix + "seconds_to_first_nonempty_slot", status.seconds_to_first_nonempty_slot, 0, False), (topic_prefix + "seconds_to_first_nonempty_slot", status.seconds_to_first_nonempty_slot, 0, False),
(topicPrefix + "pop_ping_drop_rate", status.pop_ping_drop_rate, 0, False), (topic_prefix + "pop_ping_drop_rate", status.pop_ping_drop_rate, 0, False),
(topicPrefix + "downlink_throughput_bps", status.downlink_throughput_bps, 0, False), (topic_prefix + "downlink_throughput_bps", status.downlink_throughput_bps, 0, False),
(topicPrefix + "uplink_throughput_bps", status.uplink_throughput_bps, 0, False), (topic_prefix + "uplink_throughput_bps", status.uplink_throughput_bps, 0, False),
(topicPrefix + "pop_ping_latency_ms", status.pop_ping_latency_ms, 0, False), (topic_prefix + "pop_ping_latency_ms", status.pop_ping_latency_ms, 0, False),
(topicPrefix + "alerts", alert_bits, 0, False), (topic_prefix + "alerts", alert_bits, 0, False),
(topicPrefix + "fraction_obstructed", status.obstruction_stats.fraction_obstructed, 0, False), (topic_prefix + "fraction_obstructed", status.obstruction_stats.fraction_obstructed, 0, False),
(topicPrefix + "currently_obstructed", status.obstruction_stats.currently_obstructed, 0, False), (topic_prefix + "currently_obstructed", status.obstruction_stats.currently_obstructed, 0, False),
# While the field name for this one implies it covers 24 hours, the # While the field name for this one implies it covers 24 hours, the
# empirical evidence suggests it only covers 12 hours. It also resets # empirical evidence suggests it only covers 12 hours. It also resets
# on dish reboot, so may not cover that whole period. Rather than try # on dish reboot, so may not cover that whole period. Rather than try
# to convey that complexity in the topic label, just be a bit vague: # to convey that complexity in the topic label, just be a bit vague:
(topicPrefix + "seconds_obstructed", status.obstruction_stats.last_24h_obstructed_s, 0, False), (topic_prefix + "seconds_obstructed", status.obstruction_stats.last_24h_obstructed_s, 0, False),
(topicPrefix + "wedges_fraction_obstructed", ",".join(str(x) for x in status.obstruction_stats.wedge_abs_fraction_obstructed), 0, False)] (topic_prefix + "wedges_fraction_obstructed", ",".join(str(x) for x in status.obstruction_stats.wedge_abs_fraction_obstructed), 0, False)]
paho.mqtt.publish.multiple(msgs, hostname="localhost", client_id=status.device_info.id) paho.mqtt.publish.multiple(msgs, hostname="localhost", client_id=status.device_info.id)