From 2bf346426cd144a6a28e87e28b77a1a8b88030fe Mon Sep 17 00:00:00 2001 From: Lakshya Singh Kushwah Date: Tue, 19 May 2026 11:13:46 +0530 Subject: [PATCH 1/2] feat: implement redis caching and modular microservice architecture --- .../weather_microservices/app.py | 68 +++++++++++++++---- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/python-backend-api/weather_microservices/app.py b/python-backend-api/weather_microservices/app.py index 4bef6d8..19f642b 100644 --- a/python-backend-api/weather_microservices/app.py +++ b/python-backend-api/weather_microservices/app.py @@ -1,13 +1,14 @@ from fastapi import FastAPI -from worker import data_ingestion_pipeline -from database import db +from database import Sessionlocal as SessionLocal,redis_client from fastapi import Depends from sqlalchemy.orm import Session -from sqlalchemy import func -app = FastAPI(title="Weather Microservice") +from sqlalchemy import func from models import Weather +import json +app = FastAPI(title="Weather Microservice") + def get_db(): - session = db + session = SessionLocal() try: yield session finally: @@ -15,14 +16,51 @@ def get_db(): @app.get("/analytics") def get_analytics(session : Session = Depends(get_db)): - max_temp,min_temp,avg_temp = session.query(func.max(Weather.temperature),func.min(Weather.temperature),func.avg(Weather.temperature)).first() - if max_temp is None: - return {"status": "pending", "message": "No data points gathered by worker engine yet."} - return { - "status": "success", - "metrics": { - "maximum_temperature": round(max_temp), - "minimum_temperature": round(min_temp), - "average_temperature": round(avg_temp)} - } + try: + cache_key = "weather_stats" + + # 1. Check Redis first + cached_data = redis_client.get(cache_key) + if cached_data is not None: + print("🚀 CACHE HIT: Serving directly from Redis memory!") + return json.loads(cached_data) # Turn string back into JSON dictionary + + print("🐢 CACHE MISS: Fetching calculations from MySQL...") + + # 2. Run the heavy MySQL aggregation query + stats = session.query( + func.max(Weather.temperature), + func.min(Weather.temperature), + func.avg(Weather.temperature) + ).first() + + max_temp, min_temp, avg_temp = stats + if max_temp is None: + return {"status": "pending", "message": "Database empty."} + + # 3. Format the final output response + response_payload = { + "status": "success", + "metrics": { + "maximum_temperature": round(max_temp, 2), + "minimum_temperature": round(min_temp, 2), + "average_temperature": round(avg_temp, 2) + } + } + + # 4. Save a copy in Redis with a Time-To-Live (TTL) of 60 seconds + redis_client.setex( + name=cache_key, + time=60, # Expires automatically in 60 seconds + value=json.dumps(response_payload) # Convert dict to string + ) + + return response_payload + except Exception as e: + return {"status": "error", "message": str(e)} + +if __name__ == "__main__": + import uvicorn + + \ No newline at end of file From c6b8d7c8c1767339c0bee217deaf90cf48d86f57 Mon Sep 17 00:00:00 2001 From: Lakshya Singh Kushwah Date: Tue, 19 May 2026 11:22:19 +0530 Subject: [PATCH 2/2] feat: Added CI for Java ,PYTHON and C++ --- .github/workflows/c-cpp.yml | 46 +++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 6a9c312..03a0e5f 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -1,4 +1,4 @@ -name: C/C++ CI +name: Multi-Language Portfolio CI on: push: @@ -7,17 +7,39 @@ on: branches: [ "main" ] jobs: - build: - + validate-project: runs-on: ubuntu-latest - steps: - uses: actions/checkout@v4 - - name: configure - run: ./configure - - name: make - run: make - - name: make check - run: make check - - name: make distcheck - run: make distcheck + + # 1. Setup Python & Check for Syntax Errors + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + - name: Python Lint + run: | + python -m pip install --upgrade pip + pip install flake8 + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + + # 2. Setup Java & Build Check + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + - name: Build Java + run: | + # This will look for any .java files and try to compile them to ensure no syntax errors + find . -name "*.java" > java_files.txt + if [ -s java_files.txt ]; then xargs javac < java_files.txt; fi + + # 3. Setup C++ & Compile Check + - name: Build C++ + run: | + # This checks for .cpp files and compiles them with g++ + find . -name "*.cpp" > cpp_files.txt + if [ -s cpp_files.txt ]; then g++ -std=c++17 $(cat cpp_files.txt) -o check_output || echo "No main function found"; fi + if [ -s check_output ]; then ./check_output; fi + \ No newline at end of file