Commit 3ff0b9e6 authored by Jondy Zhao's avatar Jondy Zhao

Use list as report queue instead of Queue.

parent b63f7492
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
# #
# main process # main process
# #
# read data from json file # read data from data file (pickle)
# #
# check start state, there are 3 cases # check start state, there are 3 cases
# #
...@@ -90,7 +90,16 @@ ...@@ -90,7 +90,16 @@
# #
# Save unsending queue, report id and state to pickle file. # Save unsending queue, report id and state to pickle file.
# #
#
# Interface of computer:
#
# reportNetDriveUsage will post a list of record:
#
# (computer_id, sequence_no, timestamp, duration,
# domain, user, usage, remark)
#
# getReportSequenceNo?computer_id=XXXX
#
import argparse import argparse
from datetime import datetime from datetime import datetime
import logger import logger
...@@ -128,7 +137,7 @@ def parseArgumentTuple(): ...@@ -128,7 +137,7 @@ def parseArgumentTuple():
default="net_drive_usage_report.data") default="net_drive_usage_report.data")
parser.add_argument("--server-name", parser.add_argument("--server-name",
help="Interval in seconds to send report to master.", help="Interval in seconds to send report to master.",
default=None) default="")
option = parser.parse_args() option = parser.parse_args()
# Build option_dict # Build option_dict
...@@ -150,8 +159,10 @@ class NetDriveUsageReporter(object): ...@@ -150,8 +159,10 @@ class NetDriveUsageReporter(object):
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap_computer = None self.slap_computer = None
self.report_sequence_no = 0 self.report_sequence_no = 0
self._queue = Queue.queue(self.queue_size) self._queue = None
self._state_file = self.data_file + ".state" self._state_file = self.data_file + ".state"
self._domain_name = None
self._domain_account = None
def initializeConnection(self): def initializeConnection(self):
connection_dict = {} connection_dict = {}
...@@ -161,22 +172,32 @@ class NetDriveUsageReporter(object): ...@@ -161,22 +172,32 @@ class NetDriveUsageReporter(object):
**connection_dict) **connection_dict)
self.slap_computer = self.slap.registerComputer(self.computer_id) self.slap_computer = self.slap.registerComputer(self.computer_id)
def _getUserInfo(self):
user_info = netuser.userInfo()
self._domain_name = user_info[1]
self._domain_account = user_info[0]
def run(self): def run(self):
self._getUserInfo()
self.initializeConnection() self.initializeConnection()
self._loadReportState() self._loadReportState()
self._sendReportInQueue() self._sendReportInQueue()
self.report_state = REPORT_STATE_RUNNING self.report_state = REPORT_STATE_RUNNING
pickle.dump(self.report_state, self._state_file) pickle.dump(self.report_state, self._state_file)
current_timestamp = datetime.now() current_timestamp = datetime.now()
while True: try:
last_timestamp = datetime.now() while True:
d = last_timestamp - current_timestamp last_timestamp = datetime.now()
if d.seconds < self.report_interval: d = last_timestamp - current_timestamp
sleep(self.report_interval) if d.seconds < self.report_interval:
continue sleep(self.report_interval)
current_timestamp = last_timestamp continue
r = self.getUsageReport() r = self.getUsageReport(d.seconds, current_timestamp)
self.sendUsageReport(r) current_timestamp = last_timestamp
if not self.sendUsageReport(r):
break
except KeyboardInterrupt:
pass
self._saveReportState() self._saveReportState()
self.report_state = REPORT_STATE_STOP self.report_state = REPORT_STATE_STOP
pickle.dump(self.report_state, self._state_file) pickle.dump(self.report_state, self._state_file)
...@@ -199,34 +220,35 @@ class NetDriveUsageReporter(object): ...@@ -199,34 +220,35 @@ class NetDriveUsageReporter(object):
pass # get sequence no from master sever pass # get sequence no from master sever
self.report_sequence_no = s["sequence-no"] self.report_sequence_no = s["sequence-no"]
for r in s["queue"]: self._queue = s["queue"]
self._queue.put(r)
def _saveReportState(self): def _saveReportState(self):
q = []
try:
while not self._queue.empty():
q.append(self._queue.get_nowait())
except Queue.Empty:
pass
s = { s = {
"computer-id" : self.computer_id, "computer-id" : self.computer_id,
"sequence-no" : self.report_sequence_no, "sequence-no" : self.report_sequence_no,
"queue" : q, "queue" : self._queue,
} }
pickle.dump(s, self.data_file) pickle.dump(s, self.data_file)
def getUsageReport(self): def getUsageReport(self, duration, timestamp=None):
if timestamp is None:
timestamp = datetime.now()
r = [self.computer_id, self.report_sequence_no, timestamp, duration,
self._domain_name, self._domain_account, ]
self.report_sequence_no += 1 self.report_sequence_no += 1
return netuse.usagereport() remark = []
total = 0
def sendUsageReport(self, report): for x in netuse.usageReport(self.server_name):
if self._sendReportInQueue(): total += x[2]
if not self._postData(report): remark.append(" ".join(map(str, x[0:3])))
self._queue.put(report) r.append(total)
else: r.append("\n".join(remark))
self._queue.put(report) return r
def sendUsageReport(self, r):
self._queue[0:0] = [r]
return self._sendReportInQueue()
def _postData(self, r): def _postData(self, r):
"""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.
...@@ -236,10 +258,11 @@ class NetDriveUsageReporter(object): ...@@ -236,10 +258,11 @@ class NetDriveUsageReporter(object):
def _sendReportInQueue(self): def _sendReportInQueue(self):
try: try:
while True: while True:
r = self._queue.get_nowait() r = self._queue[-1]
if not self._postData(r): if not self._postData(r):
return False return False
except Queue.Empty: self._queue.pop()
except IndexError:
pass pass
return True return True
......
...@@ -264,7 +264,7 @@ static PyMethodDef NetUseMethods[] = { ...@@ -264,7 +264,7 @@ static PyMethodDef NetUseMethods[] = {
netuse_user_info, netuse_user_info,
METH_VARARGS, METH_VARARGS,
( (
"userinfo()\n\n" "userInfo()\n\n"
"Get the logon user information, return a tuple:\n" "Get the logon user information, return a tuple:\n"
"(user, domain, server).\n" "(user, domain, server).\n"
) )
...@@ -274,12 +274,12 @@ static PyMethodDef NetUseMethods[] = { ...@@ -274,12 +274,12 @@ static PyMethodDef NetUseMethods[] = {
netuse_map_drive, netuse_map_drive,
METH_VARARGS, METH_VARARGS,
( (
"mapdrive()\n\n" "mapDrive()\n\n"
"Create mapped drive from server shared folder\n" "Create mapped drive from server shared folder\n"
) )
}, },
{ {
"usagereport", "usageReport",
netuse_usage_report, netuse_usage_report,
METH_VARARGS, METH_VARARGS,
( (
......
...@@ -23,17 +23,22 @@ class BaseTestCase(unittest.TestCase): ...@@ -23,17 +23,22 @@ class BaseTestCase(unittest.TestCase):
class NetUsageTests(BaseTestCase): class NetUsageTests(BaseTestCase):
def test_user_info(self): def test_user_info(self):
u = netuse.userinfo() u = netuse.userInfo()
self.assertEquals(len(u), 3) self.assertEquals(len(u), 3)
self.assertEquals(u, []) self.assertEquals(u, [])
def test_usage_report(self): def test_usage_report(self):
r = netuse.usagereport() r = netuse.usageReport()
self.assertEquals(len(r), 0) self.assertEquals(len(r), 0)
self.assertEquals(r, []) self.assertEquals(r, [])
def test_usage_report_server(self): def test_usage_report_server(self):
r = netuse.usagereport('myserver') r = netuse.usageReport('myserver')
self.assertEquals(len(r), 0)
self.assertEquals(r, [])
def test_usage_report_server_is_none(self):
r = netuse.usageReport(None)
self.assertEquals(len(r), 0) self.assertEquals(len(r), 0)
self.assertEquals(r, []) self.assertEquals(r, [])
......
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