Commit f55f0ffe authored by Jondy Zhao's avatar Jondy Zhao

Use TioXML format for netdrive usage report

parent c62f29f3
...@@ -3,10 +3,7 @@ ...@@ -3,10 +3,7 @@
import sys import sys
import os import os
try: from setuptools import setup, Extension
from setuptools import setup, Extension
except ImportError:
from distutils.core import setup, Extension
def get_description(): def get_description():
README = os.path.abspath(os.path.join(os.path.dirname(__file__), 'README')) README = os.path.abspath(os.path.join(os.path.dirname(__file__), 'README'))
...@@ -16,7 +13,7 @@ def get_description(): ...@@ -16,7 +13,7 @@ def get_description():
finally: finally:
f.close() f.close()
VERSION = "0.1.1" VERSION = "0.1.2"
if sys.platform.startswith("cygwin"): if sys.platform.startswith("cygwin"):
...@@ -41,25 +38,26 @@ else: ...@@ -41,25 +38,26 @@ else:
def main(): def main():
setup_args = dict( setup_args = dict(
name='netdrive.report', name='netdrive',
version=VERSION, version=VERSION,
download_url='http://',
description='A tool used to report the usage of net drive in the Windows', description='A tool used to report the usage of net drive in the Windows',
long_description=get_description(), long_description=get_description(),
keywords=['netdrive',], keywords=['netdrive',],
scripts=['src/netreport.py'], py_modules=['src/netreport'],
author='Nexedi', author='Nexedi',
author_email='jondy.zhao@nexedi.com', author_email='jondy.zhao@nexedi.com',
maintainer='Jondy Zhao', maintainer='Jondy Zhao',
maintainer_email='jondy.zhao@nexedi.com', maintainer_email='jondy.zhao@nexedi.com',
url='http://',
license='GPLv3', license='GPLv3',
zip_safe=False, zip_safe=False,
install_requires=[ install_requires=[
'lxml', 'lxml',
'slapos.core', 'slapos.core',
'setuptools',
'zc.buildout', # plays with buildout
'zc.recipe.egg', # for scripts generation
], ],
entry_points = { entry_points={
'console_scripts': [ 'console_scripts': [
'netdrive-reporter = netreport:main', 'netdrive-reporter = netreport:main',
], ],
......
...@@ -52,7 +52,10 @@ def parseArgumentTuple(): ...@@ -52,7 +52,10 @@ def parseArgumentTuple():
default="/etc/slapos/ssl/computer.key") default="/etc/slapos/ssl/computer.key")
parser.add_argument("--report-interval", parser.add_argument("--report-interval",
help="Interval in seconds to send report to master.", help="Interval in seconds to send report to master.",
default=300) default=300.0)
parser.add_argument("--report-path",
help="Where to save TioXML report.",
default=300.0)
parser.add_argument("--data-file", parser.add_argument("--data-file",
help="File used to save report data.", help="File used to save report data.",
default="net_drive_usage_report.data") default="net_drive_usage_report.data")
...@@ -80,6 +83,7 @@ class NetDriveUsageReporter(object): ...@@ -80,6 +83,7 @@ class NetDriveUsageReporter(object):
self._domain_account = None self._domain_account = None
self._config_id = None self._config_id = None
self._report_date = None self._report_date = None
self.report_interval = float(self.report_interval)
self.initializeDatabase(self.data_file) self.initializeDatabase(self.data_file)
def initializeConnection(self): def initializeConnection(self):
...@@ -163,12 +167,16 @@ class NetDriveUsageReporter(object): ...@@ -163,12 +167,16 @@ class NetDriveUsageReporter(object):
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))
def _postData(self, xml_data): def _postData(self, report):
"""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.
""" """
if xml_data is not None: if report is not None:
self._slap_computer.reportNetDriveUsage(xml_data) name = "netdrive-report-%s.xml" % datetime.now().isoformat()
etree.ElementTree(report).write(
os.path.join(self.report_path, name),
xml_declaration=True
)
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)
...@@ -198,32 +206,45 @@ class NetDriveUsageReporter(object): ...@@ -198,32 +206,45 @@ class NetDriveUsageReporter(object):
def generateDailyReport(self, config_id, computer_id, domain_account, def generateDailyReport(self, config_id, computer_id, domain_account,
report_date, remove=True): report_date, remove=True):
q = self._db.execute q = self._db.execute
root = etree.Element("report") report = etree.Element("consumption")
element = etree.Element("computer") for r in q("SELECT remote_folder, duration, usage_bytes FROM net_drive_usage "
element.text = computer_id "WHERE config_id=? AND strftime('%Y-%m-%d', start)=?",
root.append(element) (config_id, report_date)):
movement = etree.Element('movement')
element = etree.Element("resource")
element.text = r[0]
movement.append(element)
element = etree.Element("account") element = etree.Element("title")
element.text = 'NetDrive Usage %s' % report_date
movement.append(element)
element = etree.Element("reference")
element.text = domain_account element.text = domain_account
root.append(element) movement.append(element)
element = etree.Element("date") element = etree.Element("reference")
element.text = report_date element.text = report_date
root.append(element) movement.append(element)
element = etree.Element("usage") element = etree.Element("quantity")
root.append(element) element.text = str(r[1] * r[2])
usage = element movement.append(element)
element = etree.Element("details") element = etree.Element("price")
root.append(element) element.text = '0.0'
movement.append(element)
element = etree.Element("VAT")
movement.append(element)
element = etree.Element("category")
element.text = "NetDrive"
movement.append(element)
report.append(movement)
total = 0
for r in q("SELECT duration, usage_bytes FROM net_drive_usage "
"WHERE config_id=? AND strftime('%Y-%m-%d', start)=?",
(config_id, report_date)):
total += r[0] * r[1]
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 "
...@@ -232,8 +253,7 @@ class NetDriveUsageReporter(object): ...@@ -232,8 +253,7 @@ class NetDriveUsageReporter(object):
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)=?",
(config_id, report_date)) (config_id, report_date))
if total: return report
return etree.tostring(root, encoding='utf-8', 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