File size: 6,561 Bytes
9f94679
 
2365bbf
9f94679
2365bbf
 
9f94679
 
 
2365bbf
 
 
15beb90
2365bbf
 
15beb90
2365bbf
 
 
 
 
 
 
15beb90
d651ab3
2365bbf
 
 
 
 
d651ab3
15beb90
 
2365bbf
 
 
57d8b65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2365bbf
 
 
 
 
 
 
 
 
 
 
57d8b65
470e35c
2365bbf
 
57d8b65
 
 
 
 
2365bbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f94679
2365bbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf44152
2365bbf
 
 
 
 
 
 
 
 
 
9f94679
 
 
 
 
 
2365bbf
 
9f94679
2365bbf
9f94679
2365bbf
9f94679
2365bbf
9f94679
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from fastapi import FastAPI
from pydantic import BaseModel
from google.oauth2.service_account import Credentials
import gspread
from google.auth.exceptions import GoogleAuthError
import os
import uvicorn

app = FastAPI()

# Define chapter categories
physics_chapters = {
    "physics and measurement", "kinematics", "laws of motion", "work, energy, and power", "rotational motion",
    "gravitation", "properties of solids and liquids", "thermodynamics", "kinetic theory of gases",
    "oscillations and waves", "electrostatics", "current electricity",
    "magnetic effects of current and magnetism", "electromagnetic induction and alternating currents",
    "electromagnetic waves", "optics", "dual nature of matter and radiation", "atoms and nuclei",
    "electronic devices"
}

chemistry_chapters = {
    "some basic concepts in chemistry", "atomic structure", "chemical bonding and molecular structure",
    "chemical thermodynamics", "solutions", "equilibrium", "redox reactions and electrochemistry",
    "chemical kinetics", "classifications of elements and periodicity in properties", "p- block elements",
    "d - and f - block elements", "co-ordination compounds", "purification and characterisation of organic compounds",
    "some basic principles of organic chemistry", "hydrocarbons", "organic compounds containing halogens",
    "organic compounds containing oxygen", "organic compounds containing nitrogen", "biomolecules"
}

maths_chapters = {
    "sets, relation and functions", "complex numbers and quadratic equations", "matrices and determinants",
    "permutations and combinations", "binomial theorem", "sequence and series", "limits, continuity, and diffrentiability",
    "integral calculus", "diffrential equations", "co-ordinate geometry", "straight lines", "circle and conic sections",
    "three dimensional geometry", "vector algebra", "statistics and probability", "trigonometry"
}

# class Chapter:
#     def __init__(self):
#         self.data = {}

#     def add_marks(self, chapter_name, marks):
#         if chapter_name not in self.data:
#             self.data[chapter_name] = {"sum_marks": 0, "count": 0}
#         self.data[chapter_name]["sum_marks"] += marks
#         self.data[chapter_name]["count"] += 1

#     def print_averages(self):
#         for chapter, stats in self.data.items():
#             if stats["count"] > 0:
#                 avg = stats["sum_marks"] / stats["count"]
#         return f"{chapter}: Average = {round(avg, 1)}"
class Chapter:
    def __init__(self):
        self.data = {}

    def add_marks(self, chapter_name, marks):
        if chapter_name not in self.data:
            self.data[chapter_name] = {"sum_marks": 0, "count": 0}
        self.data[chapter_name]["sum_marks"] += marks
        self.data[chapter_name]["count"] += 1

    def print_averages(self):
        averages = {}
        for chapter, stats in self.data.items():
            if stats["count"] > 0:
                avg = stats["sum_marks"] / stats["count"]
                averages[chapter] = round(avg, 1)
            else:
                averages[chapter] = 0
        return averages


class PhysicsChapter(Chapter):
    pass

class ChemistryChapter(Chapter):
    pass

class MathsChapter(Chapter):
    pass

def get_credentials_from_env():
    service_account_info = {
        "type": os.getenv("SERVICE_ACCOUNT_TYPE"),
        "project_id": os.getenv("PROJECT_ID"),
        "private_key_id": os.getenv("PRIVATE_KEY_ID"),
        "private_key": os.getenv("PRIVATE_KEY").replace('\\n', '\n'),
        "client_email": os.getenv("CLIENT_EMAIL"),
        "client_id": os.getenv("CLIENT_ID"),
        "auth_uri": os.getenv("AUTH_URI"),
        "token_uri": os.getenv("TOKEN_URI"),
        "auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
        "client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
        "universe_domain": os.getenv("UNIVERSE_DOMAIN")
    }

    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
    return creds

@app.get("/analyze_chapter_averages")
def analyze_chapter_averages():
    try:
        # Set up credentials
        creds = get_credentials_from_env()
        client = gspread.authorize(creds)

        # Open the Google Sheet (replace with your sheet URL)
        sheet = client.open_by_url('https://docs.google.com/spreadsheets/d/13PUHySUXWtKBusjugoe7Dbsm39PwBUfG4tGLipspIx4/edit?gid=0#gid=0').worksheet('Sheet1')

        # Get all values from the sheet
        data = sheet.get_all_values()

        # Initialize objects for each subject
        physics_tracker = PhysicsChapter()
        chemistry_tracker = ChemistryChapter()
        maths_tracker = MathsChapter()

        # Iterate over each row in the data
        for row in data[1:]:  # Assuming the first row is headers
            user_id = row[0]  # First column is user_id
            i = 1
            while i < len(row):
                chapter_name = row[i]

                # Ensure chapter_name is a string and marks are valid numbers
                if isinstance(chapter_name, str):
                    chapter_name = chapter_name.strip().lower()
                    marks = row[i + 1]

                    if isinstance(marks, (int, float)):
                        # Check if the chapter belongs to Physics, Chemistry, or Maths
                        if chapter_name in physics_chapters:
                            physics_tracker.add_marks(chapter_name, marks)
                        elif chapter_name in chemistry_chapters:
                            chemistry_tracker.add_marks(chapter_name, marks)
                        elif chapter_name in maths_chapters:
                            maths_tracker.add_marks(chapter_name, marks)

                i += 2  # Move to the next chapter and marks

        # Return average marks per chapter for each subject
        return {
            "Physics Chapters": physics_tracker.print_averages(),
            "Chemistry Chapters": chemistry_tracker.print_averages(),
            "Maths Chapters": maths_tracker.print_averages()
        }

    except GoogleAuthError as e:
        return {"error": f"Authentication error: {e}"}
    except gspread.exceptions.SpreadsheetNotFound:
        return {"error": "Spreadsheet not found. Please check the URL."}
    except Exception as e:
        return {"error": f"An error occurred: {e}"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)