Soham commited on
Commit
7bb1ee2
·
1 Parent(s): 86d3912

changed download logs

Browse files
.gitignore CHANGED
@@ -1,3 +1,4 @@
1
  .venv
2
  .env
3
  __pycache__/
 
 
1
  .venv
2
  .env
3
  __pycache__/
4
+ audit_logs.json
.vscode/launch.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Python Debugger: FastAPI",
9
+ "type": "debugpy",
10
+ "request": "launch",
11
+ "module": "uvicorn",
12
+ "args": [
13
+ "main:app",
14
+ "--reload"
15
+ ],
16
+ "jinja": true
17
+ }
18
+ ]
19
+ }
ai_model/callAiModel.py CHANGED
@@ -4,7 +4,7 @@ from langchain.prompts import PromptTemplate
4
  from langchain_huggingface import HuggingFaceEndpoint
5
 
6
  # HuggingFace Endpoint Initialization
7
- repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
8
  llmModel = HuggingFaceEndpoint(
9
  repo_id=repo_id,
10
  max_new_tokens=512,
 
4
  from langchain_huggingface import HuggingFaceEndpoint
5
 
6
  # HuggingFace Endpoint Initialization
7
+ repo_id = ""
8
  llmModel = HuggingFaceEndpoint(
9
  repo_id=repo_id,
10
  max_new_tokens=512,
routes/route.py CHANGED
@@ -160,7 +160,6 @@ async def add_or_update_chat(
160
  return {"message": "Chat added successfully."}
161
 
162
 
163
-
164
  @router.post(
165
  "/api/model/getAllTitleByUserId",
166
  tags=["model"],
@@ -183,6 +182,10 @@ async def get_all_title(
183
  return chatList
184
 
185
 
 
 
 
 
186
 
187
  @router.post(
188
  "/api/model/getAllLogsByDateRange",
@@ -196,24 +199,27 @@ async def get_logs_by_daterange(
196
  verify_token(token.credentials)
197
  current_date = datetime.now(timezone.utc) # Ensure timezone awareness
198
 
199
- # Determine start_date based on duration format
200
- if " - " in data.duration: # Handle custom date range
201
- try:
202
  start_date_str, end_date_str = map(str.strip, data.duration.split(" - "))
203
  start_date = datetime.fromisoformat(start_date_str)
204
  end_date = datetime.fromisoformat(end_date_str)
205
- except ValueError:
206
- raise ValueError("Invalid date format. Use ISO 8601 format: YYYY-MM-DDTHH:MM:SS.ssssss+00:00 - YYYY-MM-DDTHH:MM:SS.ssssss+00:00")
207
- else: # Handle predefined durations
208
- if data.duration == "1 Day":
209
- start_date = current_date - timedelta(days=1)
210
- elif data.duration == "Current Week":
211
- start_date = current_date - timedelta(weeks=1)
212
- elif data.duration == "Last 2 Weeks":
213
- start_date = current_date - timedelta(weeks=2)
214
- else:
215
- raise ValueError("Invalid duration selected")
216
- end_date = current_date
 
 
 
217
 
218
  # Ensure timestamps are in UTC
219
  start_date = start_date.astimezone(timezone.utc)
@@ -226,7 +232,14 @@ async def get_logs_by_daterange(
226
  encrypted_data = encrypted_record["encrypted_data"]
227
  decrypted_data = cipher.decrypt(encrypted_data).decode()
228
  log = json.loads(decrypted_data)
229
- log_timestamp = datetime.fromisoformat(log["Timestamp"]).astimezone(timezone.utc)
 
 
 
 
 
 
 
230
 
231
  if start_date <= log_timestamp <= end_date:
232
  logs_list.append(log)
@@ -252,7 +265,7 @@ async def delete_chat_history(
252
 
253
  # Attempt to delete the document
254
  delete_result = chat_collection.delete_one({"titleUId": titleUId, "userID": userID})
255
-
256
  if delete_result.deleted_count == 0:
257
  raise HTTPException(status_code=404, detail="No chat history found to delete")
258
 
 
160
  return {"message": "Chat added successfully."}
161
 
162
 
 
163
  @router.post(
164
  "/api/model/getAllTitleByUserId",
165
  tags=["model"],
 
182
  return chatList
183
 
184
 
185
+ from fastapi import APIRouter, Depends, HTTPException
186
+ from datetime import datetime, timedelta, timezone
187
+ import json
188
+
189
 
190
  @router.post(
191
  "/api/model/getAllLogsByDateRange",
 
199
  verify_token(token.credentials)
200
  current_date = datetime.now(timezone.utc) # Ensure timezone awareness
201
 
202
+ # Determine start_date and end_date
203
+ try:
204
+ if " - " in data.duration: # Custom date range
205
  start_date_str, end_date_str = map(str.strip, data.duration.split(" - "))
206
  start_date = datetime.fromisoformat(start_date_str)
207
  end_date = datetime.fromisoformat(end_date_str)
208
+ else: # Predefined durations
209
+ if data.duration == "1 Day":
210
+ start_date = current_date - timedelta(days=1)
211
+ elif data.duration == "Current Week":
212
+ start_date = current_date - timedelta(weeks=1)
213
+ elif data.duration == "Last 2 Weeks":
214
+ start_date = current_date - timedelta(weeks=2)
215
+ else:
216
+ raise HTTPException(status_code=400, detail="Invalid duration selected")
217
+ end_date = current_date
218
+ except ValueError:
219
+ raise HTTPException(
220
+ status_code=400,
221
+ detail="Invalid date format. Use ISO 8601 format: YYYY-MM-DDTHH:MM:SS.ssssss+00:00 - YYYY-MM-DDTHH:MM:SS.ssssss+00:00",
222
+ )
223
 
224
  # Ensure timestamps are in UTC
225
  start_date = start_date.astimezone(timezone.utc)
 
232
  encrypted_data = encrypted_record["encrypted_data"]
233
  decrypted_data = cipher.decrypt(encrypted_data).decode()
234
  log = json.loads(decrypted_data)
235
+
236
+ # Ensure timestamp is parsed correctly
237
+ try:
238
+ log_timestamp = datetime.fromisoformat(log["timestamp"]).astimezone(
239
+ timezone.utc
240
+ )
241
+ except (ValueError, KeyError):
242
+ continue # Skip logs with invalid timestamps
243
 
244
  if start_date <= log_timestamp <= end_date:
245
  logs_list.append(log)
 
265
 
266
  # Attempt to delete the document
267
  delete_result = chat_collection.delete_one({"titleUId": titleUId, "userID": userID})
268
+
269
  if delete_result.deleted_count == 0:
270
  raise HTTPException(status_code=404, detail="No chat history found to delete")
271
 
test.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pymongo import MongoClient
2
+
3
+ uri = "mongodb+srv://soham:Soham2001@potholedetection.gt3rb.mongodb.net/?retryWrites=true&w=majority&appName=potholeDetection"
4
+
5
+ try:
6
+ client = MongoClient(uri)
7
+ db = client.admin # Use the "admin" database for the ping command
8
+ result = db.command("ping")
9
+ print("Ping successful:", result)
10
+ except Exception as e:
11
+ print("Ping failed:", e)
utils/download_mongodb_logs.py DELETED
@@ -1,51 +0,0 @@
1
- from pymongo import MongoClient
2
- from cryptography.fernet import Fernet
3
- import json
4
- from datetime import datetime, timedelta
5
-
6
- # MongoDB connection string
7
- connection_string = "mongodb+srv://soham2000:soham2000@cluster0.lbu4i.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
8
-
9
- # Encryption key (generate this once and store securely)
10
- encryption_key = b"kFRvFTtpl6WDr0eceOteS5IAnv3ps0YtCavmUwllO0k="
11
- cipher = Fernet(encryption_key)
12
-
13
- client = MongoClient(connection_string)
14
- db = client["nodetest"]
15
- collection = db["logs"]
16
- logs_list = []
17
- # Calculate the date range based on the selected duration
18
- def download_logs(duration):
19
- # Get the current date
20
- current_date = datetime.now()
21
-
22
- # Calculate the start and end date based on the selected duration
23
- if duration == "1 day":
24
- start_date = current_date - timedelta(days=1)
25
- end_date = current_date
26
- elif duration == "1 week":
27
- start_date = current_date - timedelta(weeks=1)
28
- end_date = current_date
29
- elif duration == "2 weeks":
30
- start_date = current_date - timedelta(weeks=2)
31
- end_date = current_date
32
- else:
33
- raise ValueError("Invalid duration selected")
34
-
35
- # Decrypt and filter logs based on the calculated date range
36
- for encrypted_record in collection.find():
37
- encrypted_data = encrypted_record["encrypted_data"]
38
- decrypted_data = cipher.decrypt(encrypted_data).decode()
39
- log = json.loads(decrypted_data)
40
-
41
- # Parse the Timestamp field into a datetime object
42
- log_timestamp = datetime.fromisoformat(log["Timestamp"])
43
-
44
- # Check if the log falls within the specified date range
45
- if start_date <= log_timestamp <= end_date:
46
- logs_list.append(log)
47
-
48
- return logs_list
49
-
50
-
51
- download_logs("1 week")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils/email_validator.py DELETED
@@ -1,37 +0,0 @@
1
- from email.mime.text import MIMEText
2
- from email.mime.multipart import MIMEMultipart
3
- import smtplib
4
-
5
- # Function to send activation email with HTML content
6
- def send_activation_email(email: str, activation_otp: int):
7
- sender_email = "chandratresoham@gmail.com" # Update with your email address
8
- sender_password = "wbuc okcv hzzn iwyx" # Update with your email password
9
- # Update with your website URL
10
-
11
- # HTML content for the email body
12
- email_body = f"""
13
- <html>
14
- <body>
15
- <p>
16
- Hello,<br><br>
17
- You have successfully registered to the system.<br><br>
18
- Please enter below otp to verify your accout<br><br>
19
- <p style="font-size:17px; font-weight:bold">{activation_otp}</p><br><br>
20
- Thank you!<br>
21
- </p>
22
- </body>
23
- </html>
24
- """
25
-
26
- # Create MIMEText object with HTML content
27
- message = MIMEMultipart("alternative")
28
- message['From'] = sender_email
29
- message['To'] = email
30
- message['Subject'] = "Activate your account"
31
- message.attach(MIMEText(email_body, 'html'))
32
-
33
- # Connect to SMTP server and send email
34
- with smtplib.SMTP('smtp.gmail.com', 587) as server: # Update with your SMTP server details
35
- server.starttls()
36
- server.login(sender_email, sender_password)
37
- server.sendmail(sender_email, email, message.as_string())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils/logs_generator.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from datetime import datetime, timedelta
3
+ import random
4
+ from faker import Faker
5
+
6
+ # Initialize Faker instance
7
+ fake = Faker()
8
+
9
+ # Sample data
10
+ actions = ["LOGIN", "LOGOUT", "SELECT", "UPDATE", "DELETE"]
11
+ objects_accessed = [
12
+ "System Configurations",
13
+ "Employee Records",
14
+ "Order Details",
15
+ "User Profiles",
16
+ "Transaction Logs",
17
+ "Server Logs",
18
+ "Product Catalog",
19
+ "Payment Information",
20
+ "Customer Feedback",
21
+ "Internal Reports",
22
+ "Audit Trails",
23
+ "Access Logs",
24
+ "Security Policies",
25
+ "Employee Attendance",
26
+ "Network Configurations",
27
+ "HR Records",
28
+ "Financial Statements",
29
+ "Inventory Data",
30
+ ]
31
+ status_options = ["Success", "Failure"]
32
+
33
+ # Function to generate a random IP address
34
+ def generate_ip():
35
+ return f"{random.randint(1, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}"
36
+
37
+ # Function to generate a random timestamp within a specific day
38
+ def generate_random_timestamp(date):
39
+ random_time = timedelta(seconds=random.randint(0, 86400)) # Random seconds in a day
40
+ return (date + random_time).isoformat()
41
+
42
+ # Function to generate log entry
43
+ def generate_log_entry(date):
44
+ timestamp = generate_random_timestamp(date)
45
+ user = fake.name()
46
+ action = random.choice(actions)
47
+ object_accessed = random.choice(objects_accessed)
48
+ ip_address = generate_ip()
49
+ status = random.choice(status_options)
50
+
51
+ return {
52
+ "timestamp": timestamp,
53
+ "user": user,
54
+ "action": action,
55
+ "objectAccessed": object_accessed,
56
+ "ipAddress": ip_address,
57
+ "status": status,
58
+ }
59
+
60
+ # Define your start and end date
61
+ start_date = datetime(2025, 1, 1)
62
+ end_date = datetime(2025, 8, 2)
63
+
64
+ # Generate logs per day
65
+ log_entries = []
66
+ current_date = start_date
67
+ while current_date <= end_date:
68
+ logs_per_day = random.randint(15, 20) # Generate 15 to 20 logs per day
69
+ for _ in range(logs_per_day):
70
+ log_entries.append(generate_log_entry(current_date))
71
+ current_date += timedelta(days=1) # Move to the next day
72
+
73
+ # Define the path to the JSON file
74
+ file_path = "audit_logs.json"
75
+
76
+ # Write the generated logs to the file
77
+ with open(file_path, "w") as json_file:
78
+ json.dump(log_entries, json_file, indent=4)
79
+
80
+ print(f"Logs have been written to {file_path}")
utils/logstomongodb.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pymongo import MongoClient
2
+ from cryptography.fernet import Fernet
3
+ import json
4
+
5
+ # MongoDB connection string
6
+ connection_string = "mongodb+srv://soham2000:soham2000@cluster0.lbu4i.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
7
+
8
+ # Encryption key (generate this once and store securely)
9
+ # encryption_key = Fernet.generate_key()
10
+ cipher = Fernet(b"kFRvFTtpl6WDr0eceOteS5IAnv3ps0YtCavmUwllO0k=")
11
+
12
+ # Replace <db_password> with your actual password in the connection string
13
+ client = MongoClient(connection_string)
14
+ db = client['nodetest'] # Database name
15
+ collection = db['logs'] # Collection name
16
+
17
+ file_path = "audit_logs.json" # Path to your logs file
18
+ with open(file_path, 'r') as file:
19
+ logs = json.load(file)
20
+
21
+ # Encrypt and insert logs
22
+ for log in logs:
23
+ log_json = json.dumps(log) # Convert the log object to JSON string
24
+ encrypted_log = cipher.encrypt(log_json.encode()) # Encrypt the JSON string
25
+ collection.insert_one({"encrypted_data": encrypted_log}) # Insert encrypted log
26
+
27
+ print("Logs have been encrypted and inserted into the database.")
28
+
29
+ # Decrypt a log (for testing)
30
+ for encrypted_record in collection.find():
31
+ encrypted_data = encrypted_record['encrypted_data']
32
+ decrypted_data = cipher.decrypt(encrypted_data).decode()
33
+ print("Decrypted Log:", json.loads(decrypted_data))