Commit 77f57fec authored by Rafael Monnerat's avatar Rafael Monnerat

slapos/collect: Preserve entries at the database for 15 days

  This may case more memory usage and more 'live' data to handle,
  for this reason, I'm making it configurable
parent 04bdaacb
...@@ -90,6 +90,7 @@ def do_collect(conf): ...@@ -90,6 +90,7 @@ def do_collect(conf):
except (KeyboardInterrupt, SystemExit, NoSuchProcess): except (KeyboardInterrupt, SystemExit, NoSuchProcess):
raise raise
days_to_preserve = conf.getint("slapos", "collect_cache", 15)
log_directory = "%s/var/data-log" % conf.get("slapos", "instance_root") log_directory = "%s/var/data-log" % conf.get("slapos", "instance_root")
mkdir_p(log_directory, 0o755) mkdir_p(log_directory, 0o755)
...@@ -161,7 +162,7 @@ def do_collect(conf): ...@@ -161,7 +162,7 @@ def do_collect(conf):
compressLogFolder(log_directory) compressLogFolder(log_directory)
# Drop older entries already reported # Drop older entries already reported
database.garbageCollect() database.garbageCollect(int(days_to_preserve))
except AccessDenied: except AccessDenied:
print("You HAVE TO execute this script with root permission.") print("You HAVE TO execute this script with root permission.")
......
...@@ -259,7 +259,7 @@ class Database: ...@@ -259,7 +259,7 @@ class Database:
return [i[0] for i in self._execute( return [i[0] for i in self._execute(
"SELECT name FROM sqlite_master WHERE type='table'")] "SELECT name FROM sqlite_master WHERE type='table'")]
def _getGarbageCollectionDateList(self, days_to_preserve=3): def _getGarbageCollectionDateList(self, days_to_preserve):
""" Return the list of dates to Preserve when data collect """ Return the list of dates to Preserve when data collect
""" """
base = datetime.datetime.utcnow().date() base = datetime.datetime.utcnow().date()
...@@ -268,11 +268,11 @@ class Database: ...@@ -268,11 +268,11 @@ class Database:
date_list.append((base - datetime.timedelta(days=x)).strftime("%Y-%m-%d")) date_list.append((base - datetime.timedelta(days=x)).strftime("%Y-%m-%d"))
return date_list return date_list
def garbageCollect(self): def garbageCollect(self, days_to_preserve=3):
""" Garbase collect the database, by removing older records already """ Garbase collect the database, by removing older records already
reported. reported.
""" """
date_list = self._getGarbageCollectionDateList() date_list = self._getGarbageCollectionDateList(days_to_preserve)
where_clause = "reported = 1" where_clause = "reported = 1"
for _date in date_list: for _date in date_list:
where_clause += " AND date != '%s' " % _date where_clause += " AND date != '%s' " % _date
......
...@@ -214,7 +214,7 @@ class TestCollectDatabase(unittest.TestCase): ...@@ -214,7 +214,7 @@ class TestCollectDatabase(unittest.TestCase):
def test_garbage_collection_date_list(self): def test_garbage_collection_date_list(self):
database = db.Database(self.instance_root) database = db.Database(self.instance_root)
self.assertEqual(len(database._getGarbageCollectionDateList()), 3) self.assertEqual(len(database._getGarbageCollectionDateList(3)), 3)
self.assertEqual(len(database._getGarbageCollectionDateList(1)), 1) self.assertEqual(len(database._getGarbageCollectionDateList(1)), 1)
self.assertEqual(len(database._getGarbageCollectionDateList(0)), 0) self.assertEqual(len(database._getGarbageCollectionDateList(0)), 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment