Commit f55f0ffe authored by Jondy Zhao's avatar Jondy Zhao

Use TioXML format for netdrive usage report

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