Commit c935fb91 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos/collect: Use decorator to open/close database

  This is just to clean the methods a bit
parent 280c4a2e
...@@ -55,12 +55,21 @@ class Dumper(object): ...@@ -55,12 +55,21 @@ class Dumper(object):
def writeFile(self, **kw): def writeFile(self, **kw):
raise NotImplemented("Implemented on Subclass") raise NotImplemented("Implemented on Subclass")
def withDB(function, commit=1):
def wrap_db_open_close(self, *args, **kwargs):
self.db.connect()
function(self, *args, **kwargs)
if commit:
self.db.commit()
self.db.close()
return wrap_db_open_close
class SystemReporter(Dumper): class SystemReporter(Dumper):
@withDB
def dump(self, folder): def dump(self, folder):
""" Dump data """ """ Dump data """
_date = time.strftime("%Y-%m-%d") _date = time.strftime("%Y-%m-%d")
self.db.connect()
for item, collected_item_list in six.iteritems(self.db.exportSystemAsDict(_date)): for item, collected_item_list in six.iteritems(self.db.exportSystemAsDict(_date)):
self.writeFile(item, folder, collected_item_list) self.writeFile(item, folder, collected_item_list)
...@@ -69,7 +78,6 @@ class SystemReporter(Dumper): ...@@ -69,7 +78,6 @@ class SystemReporter(Dumper):
item = "memory_%s" % partition.split("-")[-1] item = "memory_%s" % partition.split("-")[-1]
self.writeFile("disk_%s_%s" % (item, partition_id), folder, collected_item_list) self.writeFile("disk_%s_%s" % (item, partition_id), folder, collected_item_list)
self.db.close()
class SystemJSONReporterDumper(SystemReporter): class SystemJSONReporterDumper(SystemReporter):
...@@ -93,9 +101,9 @@ class SystemCSVReporterDumper(SystemReporter): ...@@ -93,9 +101,9 @@ class SystemCSVReporterDumper(SystemReporter):
class RawDumper(Dumper): class RawDumper(Dumper):
""" Dump raw data in a certain format """ Dump raw data in a certain format
""" """
@withDB(commit=1)
def dump(self, folder): def dump(self, folder):
date = time.strftime("%Y-%m-%d") date = time.strftime("%Y-%m-%d")
self.db.connect()
table_list = self.db.getTableList() table_list = self.db.getTableList()
for date_scope, amount in self.db.getDateScopeList(ignore_date=date): for date_scope, amount in self.db.getDateScopeList(ignore_date=date):
for table in table_list: for table in table_list:
...@@ -104,9 +112,6 @@ class RawDumper(Dumper): ...@@ -104,9 +112,6 @@ class RawDumper(Dumper):
self.db.markDayAsReported(date_scope, self.db.markDayAsReported(date_scope,
table_list=table_list) table_list=table_list)
self.db.commit()
self.db.close()
class RawCSVDumper(RawDumper): class RawCSVDumper(RawDumper):
def writeFile(self, name, folder, date_scope, rows): def writeFile(self, name, folder, date_scope, rows):
...@@ -134,8 +139,8 @@ class ConsumptionReportBase(object): ...@@ -134,8 +139,8 @@ class ConsumptionReportBase(object):
def __init__(self, db): def __init__(self, db):
self.db = db self.db = db
@withDB
def getPartitionCPULoadAverage(self, partition_id, date_scope): def getPartitionCPULoadAverage(self, partition_id, date_scope):
self.db.connect()
(cpu_percent_sum,), = self.db.select("user", date_scope, (cpu_percent_sum,), = self.db.select("user", date_scope,
columns="SUM(cpu_percent)", columns="SUM(cpu_percent)",
where="partition = '%s'" % partition_id) where="partition = '%s'" % partition_id)
...@@ -147,12 +152,10 @@ class ConsumptionReportBase(object): ...@@ -147,12 +152,10 @@ class ConsumptionReportBase(object):
columns="COUNT(DISTINCT time)", columns="COUNT(DISTINCT time)",
where="partition = '%s'" % partition_id) where="partition = '%s'" % partition_id)
self.db.close()
return cpu_percent_sum/sample_amount return cpu_percent_sum/sample_amount
@withDB
def getPartitionUsedMemoryAverage(self, partition_id, date_scope): def getPartitionUsedMemoryAverage(self, partition_id, date_scope):
self.db.connect()
(memory_sum,), = self.db.select("user", date_scope, (memory_sum,), = self.db.select("user", date_scope,
columns="SUM(memory_rss)", columns="SUM(memory_rss)",
where="partition = '%s'" % partition_id) where="partition = '%s'" % partition_id)
...@@ -164,12 +167,10 @@ class ConsumptionReportBase(object): ...@@ -164,12 +167,10 @@ class ConsumptionReportBase(object):
columns="COUNT(DISTINCT time)", columns="COUNT(DISTINCT time)",
where="partition = '%s'" % partition_id) where="partition = '%s'" % partition_id)
self.db.close()
return memory_sum/sample_amount return memory_sum/sample_amount
@withDB
def getPartitionDiskUsedAverage(self, partition_id, date_scope): def getPartitionDiskUsedAverage(self, partition_id, date_scope):
self.db.connect()
(disk_used_sum,), = self.db.select("folder", date_scope, (disk_used_sum,), = self.db.select("folder", date_scope,
columns="SUM(disk_used)", columns="SUM(disk_used)",
where="partition = '%s'" % partition_id) where="partition = '%s'" % partition_id)
...@@ -180,16 +181,14 @@ class ConsumptionReportBase(object): ...@@ -180,16 +181,14 @@ class ConsumptionReportBase(object):
columns="COUNT(DISTINCT time)", columns="COUNT(DISTINCT time)",
where="partition = '%s'" % partition_id) where="partition = '%s'" % partition_id)
self.db.close()
return disk_used_sum/collect_amount return disk_used_sum/collect_amount
@withDB
def getPartitionProcessConsumptionList(self, partition_id, where="", date_scope=None, def getPartitionProcessConsumptionList(self, partition_id, where="", date_scope=None,
min_time=None, max_time=None): min_time=None, max_time=None):
""" """
Query collector db to get consumed resource for last minute Query collector db to get consumed resource for last minute
""" """
self.db.connect()
consumption_list = [] consumption_list = []
if where != "": if where != "":
where = "and %s" % where where = "and %s" % where
...@@ -232,12 +231,11 @@ class ConsumptionReportBase(object): ...@@ -232,12 +231,11 @@ class ConsumptionReportBase(object):
resource_dict['user'] = pprocess.username() resource_dict['user'] = pprocess.username()
resource_dict['date'] = datetime.fromtimestamp(pprocess.create_time()).strftime("%Y-%m-%d %H:%M:%S") resource_dict['date'] = datetime.fromtimestamp(pprocess.create_time()).strftime("%Y-%m-%d %H:%M:%S")
consumption_list.append(resource_dict) consumption_list.append(resource_dict)
self.db.close()
return consumption_list return consumption_list
@withDB
def getPartitionConsumptionStatusList(self, partition_id, where="", def getPartitionConsumptionStatusList(self, partition_id, where="",
date_scope=None, min_time=None, max_time=None): date_scope=None, min_time=None, max_time=None):
self.db.connect()
if where != "": if where != "":
where = " and %s" % where where = " and %s" % where
if not date_scope: if not date_scope:
...@@ -282,7 +280,6 @@ class ConsumptionReportBase(object): ...@@ -282,7 +280,6 @@ class ConsumptionReportBase(object):
disk_used_sum, = disk_result_cursor.fetchone() disk_used_sum, = disk_result_cursor.fetchone()
if disk_used_sum is not None: if disk_used_sum is not None:
io_dict['disk_used'] = round(disk_used_sum/1024, 2) io_dict['disk_used'] = round(disk_used_sum/1024, 2)
self.db.close()
return (process_dict, memory_dict, io_dict) return (process_dict, memory_dict, io_dict)
...@@ -390,27 +387,22 @@ class ConsumptionReport(ConsumptionReportBase): ...@@ -390,27 +387,22 @@ class ConsumptionReport(ConsumptionReportBase):
def _getAverageFromList(self, data_list): def _getAverageFromList(self, data_list):
return sum(data_list)/len(data_list) return sum(data_list)/len(data_list)
@withDB
def _getCpuLoadAverageConsumption(self, date_scope): def _getCpuLoadAverageConsumption(self, date_scope):
self.db.connect()
(cpu_load_percent_list,), = self.db.select("system", date_scope, (cpu_load_percent_list,), = self.db.select("system", date_scope,
columns="SUM(cpu_percent)/COUNT(cpu_percent)") columns="SUM(cpu_percent)/COUNT(cpu_percent)")
self.db.close()
return cpu_load_percent_list return cpu_load_percent_list
@withDB
def _getMemoryAverageConsumption(self, date_scope): def _getMemoryAverageConsumption(self, date_scope):
self.db.connect()
(memory_used_list,), = self.db.select("system", date_scope, (memory_used_list,), = self.db.select("system", date_scope,
columns="SUM(memory_used)/COUNT(memory_used)") columns="SUM(memory_used)/COUNT(memory_used)")
self.db.close()
return memory_used_list return memory_used_list
@withDB
def _getZeroEmissionContribution(self): def _getZeroEmissionContribution(self):
self.db.connect() return self.db.getLastZeroEmissionRatio()
zer = self.db.getLastZeroEmissionRatio()
self.db.close()
return zer
class Journal(object): class Journal(object):
......
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