from elasticsearch import Elasticsearch
from datetime import datetime
from dateutil.relativedelta import relativedelta

# Connect to your Elasticsearch instance
CLOUD_ID = "SCIC_ElasticSearch:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjQ0MyRmYWQyOTUwYTllNDE0MTE2YjIwN2QzYTE0MzQyMzcyMSRjZDI5OTY4MTQ1ODA0MDU4OWU4ZmVmMWQ2NDViYzNjMw=="

ELASTIC_PASSWORD = "8oKIqy312EBsAPzWT64NUzji"

es = Elasticsearch(cloud_id=CLOUD_ID, basic_auth=("elastic", ELASTIC_PASSWORD))
# Define the index name
index_name = "upload1"

# Specify the model name
# model_name = "gpt-4-0125-preview"

# Function to get records for each month of a specified year
def get_monthly_records(year,model_name):
    # Initialize a dictionary to hold results for each month
    monthly_results = {}

    # Loop through each month of the specified year
    for month in range(1, 13):
        # Calculate the start and end dates for the month
        start_date = datetime(year, month, 1)
        end_date = (start_date + relativedelta(months=1)).replace(day=1)

        # Convert dates to epoch milliseconds
        start_epoch = int(start_date.timestamp() * 1000)
        end_epoch = int(end_date.timestamp() * 1000)

        # Elasticsearch query for the specific month
        query = {
            "query": {
                "bool": {
                    "must": [
                        {"term": {"modelname.keyword": model_name}},  # Exact match using 'term' query
                        # {"match": {"modelname": model_name}},
                        {
                            "range": {
                                "created_at": {
                                    "gte": start_epoch,
                                    "lt": end_epoch  # use 'lt' for exclusive end date
                                }
                            }
                        }
                    ]
                }
            }
        }

        # Execute the search
        response = es.search(index=index_name, body=query)

        # Store results in the dictionary
        monthly_results[start_date.strftime("%B %Y")] = response['hits']['hits']

    return monthly_results

# Example usage
input_year = 2024  # Replace with the year you want to query
results = get_monthly_records(input_year,"gpt-4-0125-preview")

mon_gpt4prev=[]
# Print the results for each month
for month, records in results.items():
    print(f"Records for {month}:")
    tom=0
    for record in records:
        # print(record['_source'])
        if 'human token' in record['_source']:
            tom=tom+record['_source']['human token']
        elif 'assistant token' in record['_source']:
            tom=tom+record['_source']['assistant token']
    mon_gpt4prev.append(tom)
    print("\n")
    
    
input_year = 2024  # Replace with the year you want to query
results = get_monthly_records(input_year,"gpt-4o")

mon_gpt4=[]
# Print the results for each month
for month, records in results.items():
    print(f"Records for {month}:")
    tom=0
    for record in records:
        # print(record['_source'])
        if 'human token' in record['_source']:
            tom=tom+record['_source']['human token']
        elif 'assistant token' in record['_source']:
            tom=tom+record['_source']['assistant token']
    mon_gpt4.append(tom)
    print("\n")