lalithadevi commited on
Commit
f6a4997
1 Parent(s): 4e10a9e

Update db_operations/db_write.py

Browse files
Files changed (1) hide show
  1. db_operations/db_write.py +14 -8
db_operations/db_write.py CHANGED
@@ -1,6 +1,6 @@
1
  import pymongo
2
  import os
3
- from config import DATABASE, COLLECTION, PREDICTION_DATABASE, PREDICTION_COLLECTION
4
  from logger import get_logger
5
 
6
  logger = get_logger()
@@ -9,10 +9,15 @@ class DBWrite:
9
  """
10
  Inserts processed news into MongoDB
11
  """
12
- def __init__(self):
13
  self.url = os.getenv('DB_URL')
14
- self.database = DATABASE
15
- self.collection = COLLECTION
 
 
 
 
 
16
  self.__client = None
17
  self.__error = 0
18
 
@@ -31,7 +36,8 @@ class DBWrite:
31
 
32
  db = self.__client[self.database]
33
  coll = db[self.collection]
34
- coll.drop()
 
35
  coll.insert_many(documents=documents)
36
  except Exception as insert_err:
37
  self.__error = 1
@@ -44,14 +50,14 @@ class DBWrite:
44
  self.__client = None
45
 
46
  def insert_news_into_db(self, documents: list):
47
- logger.warning('Entering insert_news_into_db()')
48
  if self.url is not None:
49
  if self.__error == 0:
50
  self.__connect()
51
  if self.__error == 0:
52
  self.__insert(documents=documents)
53
  if self.__error == 0:
54
- print("Insertion Successful")
55
  if self.__client is not None:
56
  self.__close_connection()
57
- logger.warning('Exiting insert_news_into_db()')
 
1
  import pymongo
2
  import os
3
+ from config import DATABASE, COLLECTION, PREDICTION_DATABASE, PREDICTION_COLLECTION, COLLECT_PREDICTION_DATA
4
  from logger import get_logger
5
 
6
  logger = get_logger()
 
9
  """
10
  Inserts processed news into MongoDB
11
  """
12
+ def __init__(self, db_type: str="production"):
13
  self.url = os.getenv('DB_URL')
14
+ self.db_type = db_type
15
+ self.database = ARCHIVE_DATABASE
16
+ self.collection = ARCHIVE_COLLECTION
17
+ if self.db_type == "production":
18
+ self.database = DATABASE
19
+ self.collection = COLLECTION
20
+
21
  self.__client = None
22
  self.__error = 0
23
 
 
36
 
37
  db = self.__client[self.database]
38
  coll = db[self.collection]
39
+ if (self.db_type == "production") or (COLLECT_PREDICTION_DATA==0):
40
+ coll.drop()
41
  coll.insert_many(documents=documents)
42
  except Exception as insert_err:
43
  self.__error = 1
 
50
  self.__client = None
51
 
52
  def insert_news_into_db(self, documents: list):
53
+ logger.warning(f'Entering insert_news_into_db() : {self.db_type}')
54
  if self.url is not None:
55
  if self.__error == 0:
56
  self.__connect()
57
  if self.__error == 0:
58
  self.__insert(documents=documents)
59
  if self.__error == 0:
60
+ logger.warning(f"Insertion Successful: {self.db_type}. {len(documents)} documents inserted.")
61
  if self.__client is not None:
62
  self.__close_connection()
63
+ logger.warning(f'Exiting insert_news_into_db(): {self.db_type}')