Skip to content

Instantly share code, notes, and snippets.

@danpaldev
Last active June 8, 2023 21:58
Show Gist options
  • Save danpaldev/345f99c5f08a4e4ba5246ce606632fdf to your computer and use it in GitHub Desktop.
Save danpaldev/345f99c5f08a4e4ba5246ce606632fdf to your computer and use it in GitHub Desktop.
Fetching Metrics on the backend
#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))
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)
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