Commit f444ce92 authored by Jondy Zhao's avatar Jondy Zhao

Check all config_id when net-drive-reporter start.

parent 716c8a75
...@@ -110,6 +110,7 @@ class NetDriveUsageReporter(object): ...@@ -110,6 +110,7 @@ class NetDriveUsageReporter(object):
def run(self): def run(self):
self.initializeConfigData() self.initializeConfigData()
self.sendAllReport()
self.initializeConnection() self.initializeConnection()
last_timestamp = datetime.now() last_timestamp = datetime.now()
try: try:
...@@ -134,6 +135,17 @@ class NetDriveUsageReporter(object): ...@@ -134,6 +135,17 @@ class NetDriveUsageReporter(object):
" VALUES (?, ?, ?, ?, ?, ?)", " VALUES (?, ?, ?, ?, ?, ?)",
(self._config_id, r[0], r[1], start, duration, r[3] - r[2])) (self._config_id, r[0], r[1], start, duration, r[3] - r[2]))
def sendAllReport(self):
"""Called at startup of this application, send all report
in the config table."""
q = self._db.execute
for r in q("SELECT _rowid, domain_account, computer_id, report_date "
"FROM config "
"WHERE report_date < date('now')"):
self._postData(self.generateDailyReport(*r))
q("UPDATE config SET report_date = date('now') "
"WHERE report_date < date('now')")
def sendReport(self): def sendReport(self):
# If report_date is not today, then # If report_date is not today, then
# Generate xml data from local table by report_date # Generate xml data from local table by report_date
...@@ -142,7 +154,10 @@ class NetDriveUsageReporter(object): ...@@ -142,7 +154,10 @@ class NetDriveUsageReporter(object):
# (Optional) Move all the reported data to histroy table # (Optional) Move all the reported data to histroy table
today = date.today().isoformat() today = date.today().isoformat()
if self._report_date < today: if self._report_date < today:
self._postData(self.generateDailyReport()) self._postData(self.generateDailyReport(self._config_id,
self.computer_id,
self._domain_account,
self._report_date))
self._db.execute("UPDATE config SET report_date=? where _rowid=?", self._db.execute("UPDATE config SET report_date=? where _rowid=?",
(today, self._config_id)) (today, self._config_id))
...@@ -150,7 +165,8 @@ class NetDriveUsageReporter(object): ...@@ -150,7 +165,8 @@ class NetDriveUsageReporter(object):
"""Send a marshalled dictionary of the net drive usage record """Send a marshalled dictionary of the net drive usage record
serialized via_getDict. serialized via_getDict.
""" """
self.slap_computer.reportNetDriveUsage(xml_data) if xml_data is not None:
self.slap_computer.reportNetDriveUsage(xml_data)
def initializeDatabase(self, db_path): def initializeDatabase(self, db_path):
self._db = sqlite3.connect(db_path, isolation_level=None) self._db = sqlite3.connect(db_path, isolation_level=None)
...@@ -177,35 +193,35 @@ class NetDriveUsageReporter(object): ...@@ -177,35 +193,35 @@ class NetDriveUsageReporter(object):
usage_bytes INTEGER, usage_bytes INTEGER,
remark TEXT)""") remark TEXT)""")
def generateDailyReport(self, report_date=None, remove=False): def generateDailyReport(self, config_id, computer_id, domain_account,
if report_date is None: report_date, remove=True):
report_date = self._report_date
q = self._db.execute q = self._db.execute
root = etree.Element("report") root = etree.Element("report")
computer = etree.Element("computer") computer = etree.Element("computer")
computer.text = self.computer_id computer.text = computer_id
account = etree.Element("account") account = etree.Element("account")
account.text = self._domain_account account.text = domain_account
report_date = etree.Element("date") report_date = etree.Element("date")
report_date.text = self._report_date report_date.text = report_date
usage = etree.Element("usage") usage = etree.Element("usage")
details = etree.Element("details") details = etree.Element("details")
root.append(computer, account, report_date, usage, details) root.append(computer, account, report_date, usage, details)
total = 0 total = 0
for r in q("SELECT duration, usage_bytes FROM net_drive_usage " for r in q("SELECT duration, usage_bytes FROM net_drive_usage "
"WHERE config_id=? AND strftime('%Y-%m-%d', start)=?", "WHERE config_id=? AND strftime('%Y-%m-%d', start)=?",
(self._config_id, report_date)): (_config_id, report_date)):
total += r[0] * r[1] total += r[0] * r[1]
usage.text = str(total) usage.text = str(total)
if remove: if remove:
q("INSERT INTO net_drive_usage_history " q("INSERT INTO net_drive_usage_history "
"SELECT * FROM net_drive_usage " "SELECT * FROM net_drive_usage "
"WHERE config_id=? AND strftime('%Y-%m-%d', start)=?", "WHERE config_id=? AND strftime('%Y-%m-%d', start)=?",
(self._config_id, report_date)) (_config_id, report_date))
q("DELETE FROM net_drive_usage " q("DELETE FROM net_drive_usage "
"WHERE config_id=? AND strftime('%Y-%m-%d', start)=?", "WHERE config_id=? AND strftime('%Y-%m-%d', start)=?",
(self._config_id, report_date)) (_config_id, report_date))
return etree.tostring(root, xml_declaration=True) if total:
return etree.tostring(root, xml_declaration=True)
def main(): def main():
reporter = NetDriveUsageReporter(parseArgumentTuple()) reporter = NetDriveUsageReporter(parseArgumentTuple())
......
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