Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed Oct 10, 2023
1 parent 2e9878f commit 18b4921
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions examples/metrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" Server metrics upload.
"""
# -*- coding: utf-8 -*-

import asyncio
import os
import platform
import time
Expand All @@ -12,9 +11,11 @@
from aleph_message.status import MessageStatus

from aleph.sdk.chains.ethereum import get_fallback_account
from aleph.sdk.client import AuthenticatedAlephClientSync, AuthenticatedAlephHttpClient
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.conf import settings

# -*- coding: utf-8 -*-


def get_sysinfo():
uptime = int(time.time() - psutil.boot_time())
Expand Down Expand Up @@ -53,10 +54,12 @@ def get_cpu_cores():
return [c._asdict() for c in psutil.cpu_times_percent(0, percpu=True)]


def send_metrics(
session: AuthenticatedAlephClientSync, metrics
async def send_metrics(
session: AuthenticatedAlephHttpClient, metrics
) -> Tuple[AlephMessage, MessageStatus]:
return session.create_aggregate(key="metrics", content=metrics, channel="SYSINFO")
return await session.create_aggregate(
key="metrics", content=metrics, channel="SYSINFO"
)


def collect_metrics():
Expand All @@ -68,17 +71,17 @@ def collect_metrics():
}


def main():
async def main():
account = get_fallback_account()
with AuthenticatedAlephHttpClient(
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as session:
while True:
metrics = collect_metrics()
message, status = send_metrics(session, metrics)
message, status = await send_metrics(session, metrics)
print("sent", message.item_hash)
time.sleep(10)


if __name__ == "__main__":
main()
asyncio.run(main())
4 changes: 2 additions & 2 deletions examples/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def get_input_data(value):
return value.decode("utf-8")


def send_metrics(account, metrics):
with AuthenticatedAlephHttpClient(
async def send_metrics(account, metrics):
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as session:
return session.create_aggregate(
Expand Down

0 comments on commit 18b4921

Please sign in to comment.