Serve seconds

This commit is contained in:
Eden Kirin
2023-03-20 23:25:53 +01:00
parent 1876158d9d
commit 3776bdca24
21 changed files with 1373 additions and 44 deletions

View File

@ -1,8 +1,18 @@
import logging
import time
from typing import Optional
import grpc
from models import GetCurrentTimeResponse
from stubs import serve_hours_pb2
from stubs import serve_hours_pb2_grpc
from stubs import (
serve_hours_pb2,
serve_hours_pb2_grpc,
serve_minutes_pb2,
serve_minutes_pb2_grpc,
serve_seconds_pb2,
serve_seconds_pb2_grpc,
serve_milliseconds_pb2,
serve_milliseconds_pb2_grpc,
)
from stubs import serve_currenttime_pb2
from stubs import serve_currenttime_pb2_grpc
@ -20,12 +30,60 @@ SERVE_MILLISECONDS_PORT = 50004
TIMEZONE = "Europe/Zagreb"
def get_hours() -> int:
with grpc.insecure_channel(f"{SERVE_HOURS_HOST}:{SERVE_HOURS_PORT}") as channel:
stub = serve_hours_pb2_grpc.ServeHoursStub(channel)
response = stub.GetHours(serve_hours_pb2.GetHoursRequest(timezone=TIMEZONE))
print("GetHours() response: ", response)
return response.hours
def get_hours() -> Optional[int]:
try:
with grpc.insecure_channel(f"{SERVE_HOURS_HOST}:{SERVE_HOURS_PORT}") as channel:
stub = serve_hours_pb2_grpc.ServeHoursStub(channel)
response = stub.GetHours(serve_hours_pb2.GetHoursRequest(timezone=TIMEZONE))
return response.hours
except Exception as ex:
logging.error(ex)
return None
def get_minutes() -> Optional[int]:
try:
with grpc.insecure_channel(
f"{SERVE_MINUTES_HOST}:{SERVE_MINUTES_PORT}"
) as channel:
stub = serve_minutes_pb2_grpc.ServeMinutesStub(channel)
response = stub.GetMinutes(
serve_minutes_pb2.GetMinutesRequest(timezone=TIMEZONE)
)
return response.minutes
except Exception as ex:
logging.error(ex)
return None
def get_seconds() -> Optional[int]:
try:
with grpc.insecure_channel(
f"{SERVE_SECONDS_HOST}:{SERVE_SECONDS_PORT}"
) as channel:
stub = serve_seconds_pb2_grpc.ServeSecondsStub(channel)
response = stub.GetSeconds(
serve_seconds_pb2.GetSecondsRequest(timezone=TIMEZONE)
)
return response.seconds
except Exception as ex:
logging.error(ex)
return None
def get_milliseconds() -> Optional[int]:
try:
with grpc.insecure_channel(
f"{SERVE_MILLISECONDS_HOST}:{SERVE_MILLISECONDS_PORT}"
) as channel:
stub = serve_milliseconds_pb2_grpc.ServeMillisecondsStub(channel)
response = stub.GetMilliseconds(
serve_milliseconds_pb2.GetMillisecondsRequest(timezone=TIMEZONE)
)
return response.milliseconds
except Exception as ex:
logging.error(ex)
return None
def get_currenttime() -> GetCurrentTimeResponse:
@ -40,11 +98,17 @@ def get_currenttime() -> GetCurrentTimeResponse:
def run():
hours = get_hours()
print(">>>>>>>>>>>>> Hours:", hours)
t = time.perf_counter()
currenttime = get_currenttime()
print(">>>>>>>>>>>>> CurrentTime:", currenttime)
hours = get_hours()
minutes = get_minutes()
seconds = get_seconds()
milliseconds = get_milliseconds()
t = time.perf_counter() - t
# print(f"RESULT: {hours}:{minutes}:{seconds}:{milliseconds}")
print(f"T: {t}")
if __name__ == "__main__":