Last active
June 8, 2023 21:58
-
-
Save danpaldev/345f99c5f08a4e4ba5246ce606632fdf to your computer and use it in GitHub Desktop.
Fetching Metrics on the backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Django Endpoint | |
from .redis_client import redis_client | |
#... | |
#... | |
#POST ${HOST}/metrics | |
#REQUEST BODY: {models: [], timestamps: {start: 1111, end: 2222}} | |
class MetricsView(): | |
def post(self, request): | |
request_body = json.loads(request.body) | |
epoch_time = int(time.time()) | |
modified_epoch_time = epoch_time - 43200 # 12 hours = 43200 secs | |
models = request_body.get( | |
'models', ['gpt-4', 'gpt-3.5-turbo', 'claude-v1']) | |
timestamps = request_body.get( | |
'timestamps', {"start": modified_epoch_time, "end": epoch_time}) | |
response = {'metrics': [], 'models': []} | |
for model in models: | |
try: | |
members = redis_client.zrange( | |
model, timestamps['start'], timestamps['end'], byscore=True) | |
response['models'].append(model) | |
except json.JSONDecodeError as e: | |
print(f"Failed to decode JSON string: {e}") | |
pass | |
for member in members: | |
try: | |
response['metrics'].append( | |
json.loads(member.replace("'", '"'))) | |
except json.JSONDecodeError as e: | |
print(f"Failed to decode JSON string: {e}") | |
pass | |
return HttpResponse(json.dumps(response)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis | |
from django.conf import settings | |
pool = redis.ConnectionPool(host='10.0.0.1', | |
port=6379, decode_responses=True) | |
redis_client = redis.Redis(connection_pool=pool) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis==4.5.5 | |
hiredis==2.2.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment