Skip to content

Commit

Permalink
update paho
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed May 28, 2024
1 parent 85384eb commit ac72f1e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 53 deletions.
24 changes: 24 additions & 0 deletions .run/server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="server" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
<module name="heimdallr" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/heimdallr/entry_points" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/heimdallr/entry_points/server.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>
12 changes: 6 additions & 6 deletions heimdallr/configuration/heimdallr_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
CALENDAR_INTERVAL_MS = 1000 * 60 * 60

BUILD_STATUS_MAPPING = NOD(
#streamserver="https://travis-ci.org/aivclab/streamserver.svg?branch=master",
#dlcourse="https://github.com/aivclab/dlcourse.svg?branch=master",
#poserecorder="https://github.com/aivclab/pose-recorder.svg",
#vision="https://travis-ci.com/aivclab/vision.svg?branch=master",
#RayKarsten="https://github.com/aivclab/RayKarstenWebAsm.svg",
#dmr="https://github.com/aivclab/dmr.svg",
# streamserver="https://travis-ci.org/aivclab/streamserver.svg?branch=master",
# dlcourse="https://github.com/aivclab/dlcourse.svg?branch=master",
# poserecorder="https://github.com/aivclab/pose-recorder.svg",
# vision="https://travis-ci.com/aivclab/vision.svg?branch=master",
# RayKarsten="https://github.com/aivclab/RayKarstenWebAsm.svg",
# dmr="https://github.com/aivclab/dmr.svg",
) # .as_dict()
BUILD_STATUS_INTERVAL = "build-status-interval"

Expand Down
96 changes: 72 additions & 24 deletions heimdallr/entry_points/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from flask import Response
from paho import mqtt
from paho.mqtt.client import Client, MQTTv5
from paho.mqtt.enums import CallbackAPIVersion
from pandas import DataFrame
from waitress import serve
from warg import NOD, default_datetime_repr, ensure_existence
Expand Down Expand Up @@ -66,13 +67,13 @@
# external CSS stylesheets
external_stylesheets = [
"https://codepen.io/chriddyp/pen/bWLwgP.css",
#{
# {
# "href": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css",
# "rel": "stylesheet",
# "integrity": "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO",
# "crossorigin": "anonymous",
#},
dash_bootstrap_components.themes.DARKLY
# },
dash_bootstrap_components.themes.DARKLY,
]

GPU_STATS = NOD()
Expand All @@ -85,6 +86,7 @@
MQTT_CLIENT = Client(
client_id=CLIENT_ID,
protocol=MQTTv5,
callback_api_version=CallbackAPIVersion.VERSION2
# clean_session=True
)
MQTT_CLIENT.tls_set(tls_version=paho.mqtt.client.ssl.PROTOCOL_TLS)
Expand All @@ -96,7 +98,7 @@

DEVELOPMENT = False
DASH_APP.layout = get_root_layout(DEVELOPMENT)
#LOG_WRITER: Writer = MockWriter()
# LOG_WRITER: Writer = MockWriter()


@DASH_APP.callback(
Expand Down Expand Up @@ -199,6 +201,8 @@ def update_table(n: int) -> Div:
else:
df = DataFrame(["No data"], columns=("data",))

logger.warning(df)

compute_machines.append(
DataTable(
id="gpu-table-0",
Expand All @@ -207,10 +211,12 @@ def update_table(n: int) -> Div:
page_size=ALL_CONSTANTS.TABLE_PAGE_SIZE,
# style_as_list_view=True,
style_data_conditional=[
#{"if": {"row_index": "odd"}, "backgroundColor": "rgb(248, 248, 248)"}
{"if": {"row_index": "odd"}, "backgroundColor": "rgb(50, 50, 50)"},
{"if": {"row_index": "even"}, "backgroundColor": "rgb(60, 60, 60)"},
],
style_header={
#"backgroundColor": "rgb(230, 230, 230)", "fontWeight": "bold",
"backgroundColor": "rgb(30, 30, 30)",
"fontWeight": "bold",
},
)
)
Expand All @@ -233,6 +239,8 @@ def update_table(n: int) -> Div:
else:
df = DataFrame(["No data"], columns=("data",))

logger.warning(df)

compute_machines.append(
DataTable(
id="du-table-0",
Expand All @@ -241,10 +249,11 @@ def update_table(n: int) -> Div:
page_size=ALL_CONSTANTS.TABLE_PAGE_SIZE,
# style_as_list_view=True,
style_data_conditional=[
{"if": {"row_index": "odd"}, "backgroundColor": "rgb(248, 248, 248)"}
{"if": {"row_index": "odd"}, "backgroundColor": "rgb(50, 50, 50)"},
{"if": {"row_index": "even"}, "backgroundColor": "rgb(60, 60, 60)"},
],
style_header={
"backgroundColor": "rgb(230, 230, 230)",
"backgroundColor": "rgb(30, 30, 30)",
"fontWeight": "bold",
},
)
Expand Down Expand Up @@ -292,17 +301,25 @@ def on_message(client: Any, userdata: Any, result: mqtt.client.MQTTMessage) -> N
"""description"""
global GPU_STATS
global KEEP_ALIVE

d = json.loads(result.payload)

keys = d.keys()

logger.warning("received message")

for key in keys:

if "gpu_stats" in d[key]:
GPU_STATS[key] = d[key]["gpu_stats"]
DU_STATS[key] = d[key]["du_stats"]
else:
GPU_STATS[key] = d[key] # ["gpu_stats"]
DU_STATS[key] = {}

KEEP_ALIVE[key] = 0
logger.info(

logger.warning(
f"received payload for {keys}, retain:{result.retain}, timestamp:{result.timestamp}"
)

Expand Down Expand Up @@ -333,16 +350,48 @@ def setup_mqtt_connection(settings) -> None:
settings.mqtt_password,
)
try:

MQTT_CLIENT.user_data_set([])

MQTT_CLIENT.connect(
settings.mqtt_broker,
settings.mqtt_port,
keepalive=60,
)
MQTT_CLIENT.subscribe(ALL_CONSTANTS.MQTT_TOPIC, ALL_CONSTANTS.MQTT_QOS)

except Exception as e:
logger.error(f"MQTT connection error: {e}")
# raise e

def on_unsubscribe(client, userdata, mid, reason_code_list, properties):
# Be careful, the reason_code_list is only present in MQTTv5.
# In MQTTv3 it will always be empty
if len(reason_code_list) == 0 or not reason_code_list[0].is_failure:
print("unsubscribe succeeded (if SUBACK is received in MQTTv3 it success)")
else:
print(f"Broker replied with failure: {reason_code_list[0]}")
#client.disconnect()


def on_subscribe(client, userdata, mid, reason_code_list, properties):
# Since we subscribed only for a single channel, reason_code_list contains
# a single entry
if reason_code_list[0].is_failure:
logger.warning(f"Broker rejected you subscription: {reason_code_list[0]}")
else:
logger.warning(f"Broker granted the following QoS: {reason_code_list[0].value}")


def on_connect(client, userdata, flags, reason_code, properties):
if reason_code.is_failure:
print(f"Failed to connect: {reason_code}. loop_forever() will retry connection")
else:
# we should always subscribe from on_connect callback to be sure
# our subscribed is persisted across reconnections.

MQTT_CLIENT.subscribe(ALL_CONSTANTS.MQTT_TOPIC, qos=ALL_CONSTANTS.MQTT_QOS)



def main(
*args,
Expand All @@ -351,43 +400,42 @@ def main(
**kwargs,
) -> None:
"""description"""
global DEVELOPMENT
global DEVELOPMENT
global MQTT_CLIENT

if False:
if setting_scope == SettingScopeEnum.user:
LOG_WRITER = LogWriter(
ensure_existence(PROJECT_APP_PATH.user_log) / f"{PROJECT_NAME}_server.log"
ensure_existence(PROJECT_APP_PATH.user_log)
/ f"{PROJECT_NAME}_server.log"
)
else:
LOG_WRITER = LogWriter(
ensure_existence(PROJECT_APP_PATH.site_log) / f"{PROJECT_NAME}_server.log"
ensure_existence(PROJECT_APP_PATH.site_log)
/ f"{PROJECT_NAME}_server.log"
)

LOG_WRITER.open()

MQTT_CLIENT.on_message = on_message
# MQTT_CLIENT.on_disconnect = on_disconnect
MQTT_CLIENT.on_subscribe = on_subscribe
MQTT_CLIENT.on_unsubscribe = on_unsubscribe
MQTT_CLIENT.on_connect = on_connect

print("#0")
crystallised_heimdallr_settings = HeimdallrSettings(setting_scope)
setup_mqtt_connection(settings=crystallised_heimdallr_settings)

if True:
crystallised_heimdallr_settings = HeimdallrSettings(setting_scope)
setup_mqtt_connection(settings=crystallised_heimdallr_settings)

DASH_APP.title = ALL_CONSTANTS.HTML_TITLE
DASH_APP.update_title = ALL_CONSTANTS.HTML_TITLE
host = ALL_CONSTANTS.SERVER_ADDRESS
port = ALL_CONSTANTS.SERVER_PORT

print("#1")

DEVELOPMENT = development
DASH_APP.layout = get_root_layout(DEVELOPMENT)

print("#2")

if development:
DASH_APP.run_server(
DASH_APP.run(
host=host,
port=port,
debug=ALL_CONSTANTS.DEBUG,
Expand All @@ -397,7 +445,7 @@ def main(
# DASH_APP.run_server(host=host, port=port)
serve(DASH_APP.server, **kwargs)

#LOG_WRITER.close()
# LOG_WRITER.close()


if __name__ == "__main__":
Expand Down
42 changes: 22 additions & 20 deletions heimdallr/server/board_layout/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,28 @@ def get_header() -> html.Div:
[html.H1(HTML_TITLE)],
className="col text-left align-self-center p-1",
),
# html.Div(
# [
# html.Img(
# src="/assets/alexandra.png",
# style={"height": "110px", "object-fit": "contain"},
# ),
# ]
# + (
# [
# html.Img(
# src=f"data:image/svg+xml;base64,{encoded.decode()}",
# # className='img-responsive',
# style={"height": "110px", "object-fit": "contain"},
# )
# ]
# if encoded
# else []
# ),
# className="col text-center p-1",
# ),
html.Div(
[]
# +[
# html.Img(
# src="/assets/alexandra.png",
# style={"height": "110px", "object-fit": "contain"},
# ),
# ]
# + (
# [
# html.Img(
# src=f"data:image/svg+xml;base64,{encoded.decode()}",
# # className='img-responsive',
# style={"height": "110px", "object-fit": "contain"},
# )
# ]
# if encoded
# else []
# ),
,
className="col text-center p-1",
),
html.Div(
[
html.H1(id=TIME_ID),
Expand Down
6 changes: 3 additions & 3 deletions heimdallr/utilities/server/gpu_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from heimdallr.utilities.date_tools import timestamp_to_datetime
from heimdallr.utilities.publisher.unpacking import pull_gpu_info

MB_DIVISOR = int(1024 ** 2)
MB_DIVISOR = int(1024**2)

__all__ = [
"to_overall_gpu_process_df",
Expand All @@ -40,7 +40,7 @@


def to_overall_gpu_process_df(
gpu_stats: Mapping, sort_by_key="used_gpu_mem"
gpu_stats: Mapping, sort_by_key="used_gpu_mem"
) -> DataFrame:
"""
to overall gpu usage process df
Expand Down Expand Up @@ -87,7 +87,7 @@ def to_overall_gpu_process_df(


def per_machine_per_device_pie_charts(
gpu_stats: Mapping, keep_alive: Sequence[Number]
gpu_stats: Mapping, keep_alive: Sequence[Number]
) -> List[html.Div]:
"""
per machine per device pie charts
Expand Down

0 comments on commit ac72f1e

Please sign in to comment.