Merge pull request #59 from tcivie/node-configuration-metrics
Node configuration metrics
This commit is contained in:
commit
e57f315dcb
|
@ -86,6 +86,8 @@ CREATE TABLE IF NOT EXISTS node_configurations
|
||||||
-- Configuration (MQTT)
|
-- Configuration (MQTT)
|
||||||
mqtt_encryption_enabled BOOLEAN DEFAULT FALSE,
|
mqtt_encryption_enabled BOOLEAN DEFAULT FALSE,
|
||||||
mqtt_json_enabled BOOLEAN DEFAULT FALSE,
|
mqtt_json_enabled BOOLEAN DEFAULT FALSE,
|
||||||
|
mqtt_json_message_timestamp TIMESTAMP DEFAULT NOW(),
|
||||||
|
|
||||||
mqtt_configured_root_topic TEXT DEFAULT '',
|
mqtt_configured_root_topic TEXT DEFAULT '',
|
||||||
mqtt_info_last_timestamp TIMESTAMP DEFAULT NOW(),
|
mqtt_info_last_timestamp TIMESTAMP DEFAULT NOW(),
|
||||||
|
|
||||||
|
|
|
@ -192,6 +192,23 @@ class NodeConfigurationMetrics(metaclass=Singleton):
|
||||||
return
|
return
|
||||||
|
|
||||||
def db_operation(cur, conn):
|
def db_operation(cur, conn):
|
||||||
|
# Update the last MQTT message timestamp for every message
|
||||||
|
cur.execute("""
|
||||||
|
UPDATE node_configurations
|
||||||
|
SET mqtt_info_last_timestamp = NOW()
|
||||||
|
WHERE node_id = %s
|
||||||
|
""", (node_id,))
|
||||||
|
|
||||||
|
# If it's a JSON message, update the JSON message timestamp
|
||||||
|
if mqtt_json_enabled:
|
||||||
|
cur.execute("""
|
||||||
|
UPDATE node_configurations
|
||||||
|
SET mqtt_json_message_timestamp = NOW(),
|
||||||
|
mqtt_json_enabled = TRUE
|
||||||
|
WHERE node_id = %s
|
||||||
|
""", (node_id,))
|
||||||
|
|
||||||
|
# Perform the main update
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
INSERT INTO node_configurations (
|
INSERT INTO node_configurations (
|
||||||
node_id,
|
node_id,
|
||||||
|
@ -203,9 +220,14 @@ class NodeConfigurationMetrics(metaclass=Singleton):
|
||||||
ON CONFLICT(node_id)
|
ON CONFLICT(node_id)
|
||||||
DO UPDATE SET
|
DO UPDATE SET
|
||||||
mqtt_encryption_enabled = COALESCE(EXCLUDED.mqtt_encryption_enabled, node_configurations.mqtt_encryption_enabled),
|
mqtt_encryption_enabled = COALESCE(EXCLUDED.mqtt_encryption_enabled, node_configurations.mqtt_encryption_enabled),
|
||||||
mqtt_json_enabled = COALESCE(EXCLUDED.mqtt_json_enabled, node_configurations.mqtt_json_enabled),
|
mqtt_json_enabled = CASE
|
||||||
|
WHEN (node_configurations.mqtt_info_last_timestamp - node_configurations.mqtt_json_message_timestamp) > INTERVAL '1 hour'
|
||||||
|
THEN FALSE
|
||||||
|
ELSE COALESCE(EXCLUDED.mqtt_json_enabled, node_configurations.mqtt_json_enabled)
|
||||||
|
END,
|
||||||
mqtt_configured_root_topic = COALESCE(EXCLUDED.mqtt_configured_root_topic, node_configurations.mqtt_configured_root_topic),
|
mqtt_configured_root_topic = COALESCE(EXCLUDED.mqtt_configured_root_topic, node_configurations.mqtt_configured_root_topic),
|
||||||
mqtt_info_last_timestamp = NOW()
|
mqtt_info_last_timestamp = NOW()
|
||||||
|
RETURNING mqtt_json_enabled
|
||||||
""", (node_id, mqtt_encryption_enabled, mqtt_json_enabled, mqtt_configured_root_topic))
|
""", (node_id, mqtt_encryption_enabled, mqtt_json_enabled, mqtt_configured_root_topic))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
|
@ -153,6 +153,7 @@ class MessageProcessor:
|
||||||
gateway_node_id = str(int(json_packet['sender'][1:], 16))
|
gateway_node_id = str(int(json_packet['sender'][1:], 16))
|
||||||
NodeConfigurationMetrics().process_mqtt_update(
|
NodeConfigurationMetrics().process_mqtt_update(
|
||||||
node_id=gateway_node_id,
|
node_id=gateway_node_id,
|
||||||
|
mqtt_json_enabled=True,
|
||||||
mqtt_encryption_enabled=json_packet.get('encrypted', False),
|
mqtt_encryption_enabled=json_packet.get('encrypted', False),
|
||||||
mqtt_configured_root_topic=topic
|
mqtt_configured_root_topic=topic
|
||||||
)
|
)
|
||||||
|
@ -167,6 +168,7 @@ class MessageProcessor:
|
||||||
gateway_node_id = str(int(service_envelope.gateway_id[1:], 16))
|
gateway_node_id = str(int(service_envelope.gateway_id[1:], 16))
|
||||||
NodeConfigurationMetrics().process_mqtt_update(
|
NodeConfigurationMetrics().process_mqtt_update(
|
||||||
node_id=gateway_node_id,
|
node_id=gateway_node_id,
|
||||||
|
mqtt_json_enabled=False,
|
||||||
mqtt_encryption_enabled=is_encrypted,
|
mqtt_encryption_enabled=is_encrypted,
|
||||||
mqtt_configured_root_topic=topic
|
mqtt_configured_root_topic=topic
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue