Redmind commited on
Commit
571af76
·
verified ·
1 Parent(s): f127b6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +316 -12
app.py CHANGED
@@ -2,6 +2,7 @@ import json
2
  import os
3
  import logging
4
  import shutil
 
5
  from fastapi import FastAPI, File, Query,Form, Request, HTTPException, UploadFile
6
  from fastapi.responses import JSONResponse, RedirectResponse
7
  from fastapi.staticfiles import StaticFiles
@@ -10,6 +11,10 @@ from fastapi.middleware.cors import CORSMiddleware
10
  from dotenv import load_dotenv
11
  import mysql.connector
12
  from typing import List
 
 
 
 
13
 
14
  # Load environment variables
15
  load_dotenv()
@@ -52,8 +57,63 @@ DB_USER = 'u852023448_redmindgpt'
52
  DB_PASSWORD = 'redmindGpt@123'
53
  DB_HOST = '217.21.88.10'
54
  DB_NAME = 'u852023448_redmindgpt'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Function to create a new database connection
 
 
 
57
  def get_db_connection():
58
  try:
59
  cnx = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
@@ -207,6 +267,115 @@ async def get_companies():
207
  logging.error(f"Database error: {err}")
208
  raise HTTPException(status_code=500, detail="Internal Server Error")
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  @app.get("/knowledgebase")
211
  async def knowledgebase(request: Request):
212
  return templates.TemplateResponse("knowledgebase.html", {"request": request,"title":"KnowledgeBase"})
@@ -262,9 +431,9 @@ async def get_document(company_id: str = Query(...), company_name: str = Query(.
262
  cnx = get_db_connection()
263
  cursor = cnx.cursor()
264
  query = """
265
- SELECT kb.company_id, kb.file_path, kb.document_name, kb.document_desc,kb.department,kb.version,kb.vectorDBflag,kb.last_updated
266
- FROM u852023448_redmindgpt.knowledge_base kb
267
- JOIN u852023448_redmindgpt.company_detail cd ON kb.company_id = cd.company_id
268
  WHERE kb.company_id = %s and cd.company_name=%s
269
  """
270
  logging.info(f"Executing query: {query} with company_id: {company_id}")
@@ -279,15 +448,17 @@ async def get_document(company_id: str = Query(...), company_name: str = Query(.
279
  cnx.close()
280
  companies=[]
281
  for row in result:
 
282
  companies.append({
283
- "company_id": row[0],
284
- # "file_path":row[1],
285
- "document_name": row[2],
286
- "document_desc": row[3],
287
- "department": row[4],
288
- "version": row[5],
289
- "vectorDBflag":row[6],
290
- "last_updated": row[7]
 
291
  })
292
  if companies:
293
  return companies
@@ -298,6 +469,139 @@ async def get_document(company_id: str = Query(...), company_name: str = Query(.
298
  logging.error(f"Database error: {err}")
299
  raise HTTPException(status_code=500, detail="Internal Server Error")
300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  @app.get("/data_connectors")
302
  async def data_connectors(request: Request):
303
  return templates.TemplateResponse("data_connectors.html", {"request": request, "title": "Data Connectors"})
 
2
  import os
3
  import logging
4
  import shutil
5
+ import asyncpg
6
  from fastapi import FastAPI, File, Query,Form, Request, HTTPException, UploadFile
7
  from fastapi.responses import JSONResponse, RedirectResponse
8
  from fastapi.staticfiles import StaticFiles
 
11
  from dotenv import load_dotenv
12
  import mysql.connector
13
  from typing import List
14
+ from pydantic import BaseModel
15
+ import psycopg2
16
+
17
+
18
 
19
  # Load environment variables
20
  load_dotenv()
 
57
  DB_PASSWORD = 'redmindGpt@123'
58
  DB_HOST = '217.21.88.10'
59
  DB_NAME = 'u852023448_redmindgpt'
60
+ from pydantic import BaseModel
61
+ class DatabaseConnection(BaseModel):
62
+ database_type: str
63
+ server: str
64
+ port: str
65
+ databaseName: str
66
+ username: str
67
+ password: str
68
+ @app.post("/api/connect")
69
+ async def connect_to_database(connection: DatabaseConnection):
70
+ try:
71
+ if connection.database_type == "postgres":
72
+ conn = psycopg2.connect(
73
+ host=connection.server,
74
+ port=connection.port,
75
+ database=connection.databaseName,
76
+ user=connection.username,
77
+ password=connection.password
78
+ )
79
+ query_schemas = "SELECT schema_name FROM information_schema.schemata"
80
+ query_tables = "SELECT table_name FROM information_schema.tables WHERE table_schema = %s"
81
+ elif connection.database_type == "mysql":
82
+ print(f"inside mysql",connection.server,connection.port,connection.databaseName,connection.username,connection.password)
83
+ conn = mysql.connector.connect(
84
+ host=connection.server,
85
+ port=connection.port,
86
+ database=connection.databaseName,
87
+ user=connection.username,
88
+ password=connection.password
89
+ )
90
+ query_schemas = "SELECT schema_name FROM information_schema.schemata"
91
+ query_tables = "SELECT table_name FROM information_schema.tables WHERE table_schema = %s"
92
+ else:
93
+ raise HTTPException(status_code=400, detail="Unsupported database type")
94
+
95
+ cursor = conn.cursor()
96
+
97
+ # Fetch all schemas
98
+ cursor.execute(query_schemas)
99
+ schemas = cursor.fetchall()
100
+
101
+ # Fetch all tables within each schema
102
+ schema_tables = {}
103
+ for schema in schemas:
104
+ cursor.execute(query_tables, (schema[0],))
105
+ tables = cursor.fetchall()
106
+ schema_tables[schema[0]] = [table[0] for table in tables]
107
+
108
+ cursor.close()
109
+ conn.close()
110
+
111
+ return {"schemas": [schema[0] for schema in schemas], "schema_tables": schema_tables, "success": True}
112
 
113
+ except Exception as e:
114
+ raise HTTPException(status_code=500, detail=str(e))
115
+
116
+ # Function to create a new database connection for MySQL (Example)
117
  def get_db_connection():
118
  try:
119
  cnx = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
 
267
  logging.error(f"Database error: {err}")
268
  raise HTTPException(status_code=500, detail="Internal Server Error")
269
 
270
+ #to view the details
271
+ @app.get("/api/getcompanydetails/{company_id}")
272
+ async def get_company_details(company_id: int):
273
+ company = await get_company_from_db(company_id)
274
+ if not company:
275
+ raise HTTPException(status_code=404, detail="Company not found")
276
+ return company
277
+
278
+ async def get_company_from_db(company_id: int):
279
+ try:
280
+ # Establish a connection to the database
281
+ cnx = get_db_connection()
282
+ if cnx is None:
283
+ raise HTTPException(status_code=500, detail="Failed to connect to the database")
284
+ cursor = cnx.cursor(dictionary=True)
285
+ query = "SELECT * FROM company_detail WHERE company_id = %s"
286
+ cursor.execute(query, (company_id,))
287
+ company = cursor.fetchone()
288
+ cursor.close()
289
+ cnx.close()
290
+ return company
291
+ except mysql.connector.Error as err:
292
+ logging.error(f"Error fetching company: {err}")
293
+ raise HTTPException(status_code=500, detail="Failed to fetch company")
294
+ # to edit the details
295
+ @app.put("/api/putcompanydetails/{company_id}")
296
+ async def update_company_details(company_id: int,
297
+ company_name: str = Form(...),
298
+ company_code: str = Form(...),
299
+ domain: str = Form(...),
300
+ llm_tools: List[str] = Form(...)):
301
+ print(f"Received company_id",company_id) # Debug statement
302
+ logging.info(f"Received request for company data")
303
+ company_data = {
304
+ 'company_name': company_name,
305
+ 'company_code': company_code,
306
+ 'domain': domain,
307
+ 'llm_tools': ','.join(llm_tools)
308
+ }
309
+ updated_company = await update_company_in_db(company_id, company_data)
310
+ if not updated_company:
311
+ raise HTTPException(status_code=500, detail="Failed to update company")
312
+ return updated_company
313
+
314
+ async def update_company_in_db(company_id: int, company_data: dict):
315
+ try:
316
+ print(f"Received company_nid inside function",company_id) # Debug statement
317
+ logging.info(f"Received request for company name")
318
+ cnx = get_db_connection()
319
+ if cnx is None:
320
+ raise HTTPException(status_code=500, detail="Failed to connect to the database")
321
+ cursor = cnx.cursor()
322
+ update_query = """
323
+ UPDATE redmind_gpt.company_detail cd
324
+ SET cd.company_name = %s, cd.company_code = %s, cd.domain = %s, cd.llm_tools = %s
325
+ WHERE cd.company_id = %s;
326
+ """
327
+ logging.info(f"Executing query: {update_query} with company_id: {company_id}")
328
+ params = (company_id,company_data)
329
+ logging.info(f"Query parameters: {params}")
330
+ print(f"Query parameters: {params}")
331
+
332
+
333
+ cursor.execute(update_query, (
334
+ company_data['company_name'],
335
+ company_data['company_code'],
336
+ company_data['domain'],
337
+ company_data['llm_tools'],
338
+ company_id
339
+ ))
340
+
341
+ cnx.commit()
342
+ success = cursor.rowcount > 0
343
+ cursor.close()
344
+ cnx.close()
345
+ if not success:
346
+ return None
347
+ return company_data
348
+ except mysql.connector.Error as err:
349
+ logging.error(f"Error updating company: {err}")
350
+ raise HTTPException(status_code=500, detail="Failed to update company")
351
+
352
+
353
+ def delete_company_from_db(company_id: int) -> bool:
354
+ print(f"Received company_name: {company_id}") # Debug statement
355
+ logging.info(f"Received request for company name: {company_id}")
356
+ try:
357
+ # Establish a connection to the database
358
+ cnx = get_db_connection()
359
+ if cnx is None:
360
+ raise HTTPException(status_code=500, detail="Failed to connect to the database")
361
+ cursor = cnx.cursor()
362
+ delete_query = "DELETE FROM company_detail WHERE company_id = %s"
363
+ cursor.execute(delete_query, (company_id,))
364
+ cnx.commit()
365
+ success = cursor.rowcount > 0
366
+ cursor.close()
367
+ cnx.close()
368
+ return success
369
+ except mysql.connector.Error as err:
370
+ logging.error(f"Error deleting company: {err}")
371
+ raise HTTPException(status_code=500, detail="Failed to delete company")
372
+ @app.delete("/api/delcompanydetails/{company_id}")
373
+ async def delete_company(company_id: int):
374
+ deletion_success = delete_company_from_db(company_id)
375
+ if not deletion_success:
376
+ raise HTTPException(status_code=404, detail="Company not found or failed to delete")
377
+ return {"message": "Company deleted successfully"}
378
+
379
  @app.get("/knowledgebase")
380
  async def knowledgebase(request: Request):
381
  return templates.TemplateResponse("knowledgebase.html", {"request": request,"title":"KnowledgeBase"})
 
431
  cnx = get_db_connection()
432
  cursor = cnx.cursor()
433
  query = """
434
+ SELECT kb.kid,kb.company_id, kb.file_path, kb.document_name, kb.document_desc,kb.department,kb.version,kb.vectorDBflag,kb.last_updated
435
+ FROM redmind_gpt.knowledge_base kb
436
+ JOIN redmind_gpt.company_detail cd ON kb.company_id = cd.company_id
437
  WHERE kb.company_id = %s and cd.company_name=%s
438
  """
439
  logging.info(f"Executing query: {query} with company_id: {company_id}")
 
448
  cnx.close()
449
  companies=[]
450
  for row in result:
451
+
452
  companies.append({
453
+ "row_id":row[0],
454
+ "company_id": row[1],
455
+ #"file_path":row[2],
456
+ "document_name": row[3],
457
+ "document_desc": row[4],
458
+ "department": row[5],
459
+ "version": row[6],
460
+ "vectorDBflag":row[7],
461
+ "last_updated": row[8]
462
  })
463
  if companies:
464
  return companies
 
469
  logging.error(f"Database error: {err}")
470
  raise HTTPException(status_code=500, detail="Internal Server Error")
471
 
472
+ #to get data for view in knowledgebase
473
+ @app.get("/api/getknowledgebase/{company_id}")
474
+ async def get_company_details(company_id: int):
475
+ company = await get_knowledge_from_db(company_id)
476
+ if not company:
477
+ raise HTTPException(status_code=404, detail="Company not found")
478
+ return company
479
+
480
+ async def get_knowledge_from_db(company_id: int):
481
+ try:
482
+ # Establish a connection to the database
483
+ cnx = get_db_connection()
484
+ if cnx is None:
485
+ raise HTTPException(status_code=500, detail="Failed to connect to the database")
486
+ cursor = cnx.cursor(dictionary=True)
487
+ query = "SELECT * FROM knowledge_base WHERE kid = %s"
488
+ cursor.execute(query, (company_id,))
489
+ company = cursor.fetchone()
490
+ cursor.close()
491
+ cnx.close()
492
+ if company:
493
+ logging.debug(f"Extracted filename")
494
+ if company.get('file_path'):
495
+ company['file_path'] = os.path.basename(company['file_path'])
496
+ logging.debug(f"Extracted filename: {company['file_path']}")
497
+ return company
498
+ else:
499
+ raise HTTPException(status_code=404, detail="Company not found or file not found for the company")
500
+ except mysql.connector.Error as err:
501
+ logging.error(f"Error fetching company: {err}")
502
+ raise HTTPException(status_code=500, detail="Failed to fetch company")
503
+
504
+ # to edit the knowledgebase details
505
+ @app.put("/api/putknowledgebase/{kid}")
506
+ async def update_company_details(
507
+ kid: int,
508
+ company_id: str = Form(...),
509
+ # file_name: str = Form(...),
510
+ documentName: str = Form(...),
511
+ documentDescription: str = Form(...),
512
+ department: str = Form(...),
513
+ version: str = Form(...),
514
+ vectorDBFlag: str = Form(...),
515
+ lastUpdated: str = Form(...)):
516
+ logging.info(f"Received request for company data with ID: {kid}")
517
+
518
+ # upload_folder = "uploads/"
519
+ # os.makedirs(upload_folder, exist_ok=True)
520
+ # file_path = os.path.join(upload_folder, uploadFile.filename)
521
+
522
+ # with open(file_path, "wb") as buffer:
523
+ # shutil.copyfileobj(uploadFile.file, buffer)
524
+
525
+ company_data = {
526
+ 'company_id': company_id,
527
+ #'file_path': file_name,
528
+ 'document_name': documentName,
529
+ 'document_desc': documentDescription,
530
+ 'department': department,
531
+ 'version': version,
532
+ 'vectorDBflag': vectorDBFlag,
533
+ 'last_updated': lastUpdated
534
+ }
535
+
536
+ updated_company = await update_knowledge_in_db(kid, company_data)
537
+ if not updated_company:
538
+ raise HTTPException(status_code=500, detail="Failed to update company")
539
+ return updated_company
540
+ async def update_knowledge_in_db(kid: int, company_data: dict):
541
+ try:
542
+ logging.info(f"Updating knowledge base for ID: {kid}")
543
+ cnx = get_db_connection()
544
+ if cnx is None:
545
+ raise HTTPException(status_code=500, detail="Failed to connect to the database")
546
+
547
+ cursor = cnx.cursor()
548
+ update_query = """
549
+ UPDATE redmind_gpt.knowledge_base kb
550
+ SET kb.company_id,kb.document_name=%s, kb.document_desc=%s, kb.department=%s, kb.version=%s, kb.vectorDBflag=%s, kb.last_updated=%s
551
+ WHERE kb.kid = %s;
552
+ """
553
+ cursor.execute(update_query, (
554
+ company_data['company_id'],
555
+ # company_data['file_path'],
556
+ company_data['document_name'],
557
+ company_data['document_desc'],
558
+ company_data['department'],
559
+ company_data['version'],
560
+ company_data['vectorDBflag'],
561
+ company_data['last_updated'],
562
+ kid
563
+ ))
564
+
565
+ cnx.commit()
566
+ success = cursor.rowcount > 0
567
+ cursor.close()
568
+ cnx.close()
569
+ if not success:
570
+ return None
571
+ return company_data
572
+ except mysql.connector.Error as err:
573
+ logging.error(f"Error updating company: {err}")
574
+ raise HTTPException(status_code=500, detail="Failed to update company")
575
+
576
+
577
+ def delete_knowledge_from_db(company_id: int) -> bool:
578
+ print(f"Received knowledge base company_id: {company_id}") # Debug statement
579
+ logging.info(f"Received request for knowledgebase company id: {company_id}")
580
+ try:
581
+ # Establish a connection to the database
582
+ cnx = get_db_connection()
583
+ if cnx is None:
584
+ raise HTTPException(status_code=500, detail="Failed to connect to the database")
585
+ cursor = cnx.cursor()
586
+ delete_query = "DELETE FROM knowledge_base WHERE kid = %s"
587
+ cursor.execute(delete_query, (company_id,))
588
+ cnx.commit()
589
+ success = cursor.rowcount > 0
590
+ cursor.close()
591
+ cnx.close()
592
+ return success
593
+ except mysql.connector.Error as err:
594
+ logging.error(f"Error deleting company: {err}")
595
+ raise HTTPException(status_code=500, detail="Failed to delete company")
596
+
597
+ @app.delete("/api/delknowledgebase/{company_id}")
598
+ async def delete_company(company_id: int):
599
+ deletion_success = delete_knowledge_from_db(company_id)
600
+ if not deletion_success:
601
+ raise HTTPException(status_code=404, detail="Company not found or failed to delete")
602
+ return {"message": "Company deleted successfully"}
603
+
604
+
605
  @app.get("/data_connectors")
606
  async def data_connectors(request: Request):
607
  return templates.TemplateResponse("data_connectors.html", {"request": request, "title": "Data Connectors"})