File size: 1,060 Bytes
4e00df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from datetime import datetime, timedelta

# -- Create a table called "stats"
# create table
#   stats (
#     -- A column called "time" with data type "timestamp"
#     time timestamp,
#     -- A column called "details" with data type "text"
#     chat boolean,
#     embedding boolean,
#     details text,
#     metadata jsonb,
#     -- An "integer" primary key column called "id" that is generated always as identity
#     id integer primary key generated always as identity
#   );


def get_usage_today(supabase):
    # Returns the number of rows in the stats table for the last 24 hours
    response = supabase.table("stats").select("id", count="exact").gte("time", datetime.now() - timedelta(hours=24)).execute()
    return response.count

def add_usage(supabase, type, details, metadata):
    # Adds a row to the stats table
    supabase.table("stats").insert({
        "time": datetime.now().isoformat(),
        "chat": type == "chat",
        "embedding": type == "embedding",
        "details": details,
        "metadata": metadata
    }).execute()