Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change param to unicode string

  • Loading branch information...
commit f81f0e44a85a34093492ca6674c647a4aaab8fcf 1 parent 2438d3f
@hokerffb hokerffb authored
View
18 kanyun/server/api_client.py → kanyun/client/api_client.py
@@ -18,7 +18,7 @@
[u'S', u'instance-00000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
"""
-param_tmpl = ['S', 'instance-00000001@pyw.novalocal', 'cpu', 'total', 0, 5, 1332897600, 0]
+param_tmpl = ['S', u'instance-00000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
def invoke(socket, param):
# print "Sending request ", param
@@ -53,11 +53,11 @@ def invoke_getInstacesList(socket, cf_str):
def invoke_getbykey(socket, row_id):
ret = list()
cmd = list()
- cmd.append([u'K', row_id, "vmnetwork"])
- cmd.append([u'K', row_id, "mem"])
- cmd.append([u'K', row_id, "nic"])
- cmd.append([u'K', row_id, "blk"])
- cmd.append([u'G', row_id, "cpu", "total"])
+ cmd.append([u'K', row_id, u"vmnetwork"])
+ cmd.append([u'K', row_id, u"mem"])
+ cmd.append([u'K', row_id, u"nic"])
+ cmd.append([u'K', row_id, u"blk"])
+ cmd.append([u'G', row_id, u"cpu", u"total"])
for i in cmd:
cf_str = i[2]
@@ -109,13 +109,13 @@ def invoke_statistics(api_client, row_id, cf_str, scf_str, statistic, period=5,
class ApiClient():
def __init__(self, api_host = '127.0.0.1', api_port = '5556'):
# default value
- self.cf_str = ''
- self.scf_str = ''
+ self.cf_str = u''
+ self.scf_str = u''
self.statistic = 0
self.period = 5
self.time_from = 0
self.time_to = 0
- self.key = ''
+ self.key = u''
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
View
18 kanyun/monitoring.egg-info/PKG-INFO
@@ -1,18 +0,0 @@
-Metadata-Version: 1.0
-Name: monitoring
-Version: 0.1
-Summary: OpenStack Monitoring System
-Home-page: https://git.sws.sina.com.cn/pyw_code/pyw_code
-Author: Sina Corp.
-Author-email: pengyuwei@gmail.com
-License: Apache
-Description: OpenStack Monitoring System
-Keywords: hello world example examples
-Platform: UNKNOWN
-Classifier: Development Status :: 4 - Beta
-Classifier: Environment :: Console
-Classifier: Intended Audience :: Developers
-Classifier: Intended Audience :: Information Technology
-Classifier: License :: OSI Approved :: Apache Software License
-Classifier: Operating System :: OS Independent
-Classifier: Programming Language :: Python
View
14 kanyun/monitoring.egg-info/SOURCES.txt
@@ -1,14 +0,0 @@
-setup.py
-monitoring/monitoring.egg-info/PKG-INFO
-monitoring/monitoring.egg-info/SOURCES.txt
-monitoring/monitoring.egg-info/dependency_links.txt
-monitoring/monitoring.egg-info/requires.txt
-monitoring/monitoring.egg-info/top_level.txt
-monitoring/server/__init__.py
-monitoring/server/api_client.py
-monitoring/server/api_server.py
-monitoring/server/db.py
-monitoring/server/plugin_agent_srv.py
-monitoring/server/server.py
-monitoring/server/test_api_client.py
-monitoring/server/test_api_server.py
View
1  kanyun/monitoring.egg-info/dependency_links.txt
@@ -1 +0,0 @@
-
View
1  kanyun/monitoring.egg-info/requires.txt
@@ -1 +0,0 @@
-pyzmq
View
1  kanyun/monitoring.egg-info/top_level.txt
@@ -1 +0,0 @@
-monitoring
View
21 kanyun/server/demux.conf
@@ -1,21 +0,0 @@
-[MySQL]
-host: 127.0.0.1
-passwd: nova
-user: root
-db: nova
-
-[Demux]
-handler_host: *
-handler_port: 5557
-broadcast_host: *
-broadcast_port: 5558
-feedback_host: *
-feedback_port: 5559
-db_host: 127.0.0.1
-api_host: *
-api_port: 5556
-worker_timeout: 60
-
-[Client]
-api_host: 10.210.228.23
-api_port: 5556
View
80 kanyun/server/test_api_client.py
@@ -1,80 +0,0 @@
-import sys
-import ConfigParser
-import json
-import zmq
-
-"""
-protocol:
-[CMD, row_id, cf_str, scf_str, statistic, period, time_from, time_to]
-example:
-[u'S', u'instance-00000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
-"""
-
-param_tmpl = ['S', 'instance-00000001@pyw.novalocal', 'cpu', 'total', 0, 5, 1332897600, 0]
-
-def invoke(socket, param):
- print "Sending request ", param
- socket.send (json.dumps(param))
-
- # Get the reply.
- message = socket.recv()
- print "Received reply ", "[", message, "]"
-
- return json.loads(message)
-
-def main():
-# if len(sys.argv) <> 2:
-# logging.debug( "usage: python %s [1-3]" % sys.argv[0]
-# sys.exit(0)
-
-# worker_id = sys.argv[1]
-# if worker_id not in ['1', '2', '3']:
-# logging.debug( "usage: python %s [1-3]" % sys.argv[0]
-# sys.exit(0)
- if len(sys.argv) == 2:
- print "usage:\ntest_api_client\ntest_api_client <id> <cf> <scf>"
- return
-
- config = ConfigParser.ConfigParser()
- config.read("demux.conf")
- cfg = dict(config.items('Client'))
-
- context = zmq.Context()
-
- # Socket to talk to server
- print "Connecting to hello world server..."
- api_client = context.socket(zmq.REQ)
- api_client.connect("tcp://%(api_host)s:%(api_port)s" % cfg)
-
- if len(sys.argv) == 4:
- row_id, cf_str, scf_str = sys.argv[1], sys.argv[2], sys.argv[3]
- param = [u'G', row_id, cf_str, scf_str]
- r = invoke(api_client, param)
- return
-
-
- # Do 10 requests, waiting each time for a response
- for request in range (1,2):
- # cmd, row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to
- param = param_tmpl
-
- param[4] = 0
- param[5] = 2
- r = invoke(api_client, param)
- print 'SUM=', '(no result)' if r is None else r.values()[0]
-
- param[4] = 1
- r = invoke(api_client, param)
- print 'MAX=', '(no result)' if r is None else r.values()[0]
-
- param[4] = 2
- r = invoke(api_client, param)
- print 'MIX=', '(no result)' if r is None else r.values()[0]
-
- param[4] = 3
- r = invoke(api_client, param)
- print 'AVERAGE=', '(no result)' if r is None else r.values()[0]
-
-
-if __name__ == '__main__':
- main()
View
121 kanyun/server/test_api_server.py
@@ -1,121 +0,0 @@
-import unittest
-import time
-import sys
-import random
-import mox
-import pycassa
-
-from kanyun.server.api_server import *
-
-class StatisticsTest(unittest.TestCase):
- def setUp(self):
- self.mox = mox.Mox()
-
- def tearDown(self):
- self.mox.UnsetStubs()
-
- def testStatistics(self):
- s = api_server.Statistics()
- isum = 0.0
- val = 1
- for i in range(1,11):
- isum += i
- s.update(i)
- print "%02d. value=%02d, sum=%02d, max=%02d, min=%02d, diff=%02d, agerage=%00.f" \
- % (i, i, s.get_sum(), s.get_max(), s.get_min(), s.get_diff(), s.get_agerage())
- assert s.get_sum() == isum
- assert s.get_max() == i
- assert s.get_min() == 1
- assert s.get_agerage() == isum / i
- print "Statistics test \t[\033[1;33mOK\033[0m]"
-
-def InitApiMox():
- pass
-
-class ColumnFamilyMox():
- def __init__(self, a='', b=''):
- pass
- pass
-# return "ColumnFamilyMox"
-
-class TestApiServer(unittest.TestCase):
- def setUp(self):
- self.mox = mox.Mox()
-
- def tearDown(self):
- self.mox.UnsetStubs()
-
- def testGetCf(self):
- m = self.mox
- self.mox.StubOutWithMock(api_server, 'init_api')
- api_server.init_api().AndReturn(InitApiMox())
-
- self.mox.StubOutWithMock(pycassa, 'ColumnFamily')
- pycassa.ColumnFamily().AndReturn(ColumnFamilyMox())
- pycassa.ColumnFamily('db', 'cpu').AndReturn(ColumnFamilyMox())
-
- self.mox.ReplayAll()
- api_server.data_db = 'db'
- api_server.init_api()
- api_server.get_cf("cpu")
- api_server.get_cf2("testcf")
- self.mox.VerifyAll()
-
-#
- def test_api_1(self):
- time.clock()
- row_id = 'instance-00000001@pyw.novalocal'
- statistic = api_server.STATISTIC.AVERAGE
- period = 5
- time_from = 1332815400
- time_to = 0
- scf_str = 'total'
- cf_str = 'cpu'
- rs, count, _ = api_server.api_getdata(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
- print "%d results, spend %f seconds" % (count, time.clock())
- print '-' * 60
-
- def test_api_statistic(self):
- time.clock()
- row_id = 'instance-00000001@pyw.novalocal'
- period = 5
- time_from = 1332833464
- time_to = 0
- scf_str = 'total'
- cf_str = 'cpu'
-
- statistic = api_server.STATISTIC.SUM
- rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
- print "%d SUM results, spend %f seconds" % (count, time.clock())
- print '-' * 60
-
- statistic = api_server.STATISTIC.MAXIMUM
- rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
- print "%d MAXIMUM results, spend %f seconds" % (count, time.clock())
- print '-' * 60
-
- statistic = api_server.STATISTIC.MINIMUM
- rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
- print "%d MINIMUM results, spend %f seconds" % (count, time.clock())
- print '-' * 60
-
- statistic = api_server.STATISTIC.AVERAGE
- rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
- print "%d AVERAGE results, spend %f seconds" % (count, time.clock())
- print '-' * 60
- def test_demo(self):
- self.assertTrue(True)
-
- def test_demo2(self):
- self.assertEqual(['vda', 'vdb'], ['vda', 'vdb'])
-
-if __name__ == '__main__':
- time.clock()
- ApiTestSuite = unittest.TestSuite()
- ApiTestSuite.addTest(StatisticsTest("testStatistics"))
- ApiTestSuite.addTest(TestApiServer("testGetCf"))
-# ApiTestSuite.addTest(ApiServerTest("testIsTimeToWorkFirst"))
-# ApiTestSuite.addTest(ApiServerTest("testInfoPush"))
-
- runner = unittest.TextTestRunner()
- runner.run(ApiTestSuite)
View
81 tests/test_api_server.py
@@ -13,9 +13,7 @@
import pycassa
from collections import OrderedDict
-
from kanyun.server import api_server
-#from kanyun.server.api_server import *
class StatisticsTest(unittest.TestCase):
def setUp(self):
@@ -65,7 +63,6 @@ class ColumnFamilyMox():
def __init__(self, a='', b=''):
pass
pass
-# return "ColumnFamilyMox"
class TestApiServer(unittest.TestCase):
def setUp(self):
@@ -98,26 +95,30 @@ def testParamTypeCheck(self):
def testApiGetdata(self):
time.clock()
row_id = 'instance-00000001@pyw.novalocal'
- statistic = api_server.STATISTIC.AVERAGE
- period = 5
time_from = 1332815400
time_to = 0
scf_str = 'total'
cf_str = 'cpu'
- rs, count, _ = api_server.api_getdata(row_id, cf_str, scf_str, time_from=time_from, time_to=time_to)
-# print rs
+
+ self.mox.StubOutWithMock(api_server, 'api_getdata')
+ api_server.api_getdata(row_id, cf_str, scf_str, \
+ time_from, time_to).AndReturn(ApiGetDataMox())
+ self.mox.ReplayAll()
+
+ rs, count, _ = api_server.api_getdata(row_id, cf_str, scf_str, time_from, time_to)
+ self.mox.VerifyAll()
print "%d results, spend %f seconds" % (count, time.clock())
print "ApiGetdata test \t\t\t[\033[1;33mOK\033[0m]"
print '-' * 60
def testApiStatisticSUM(self):
time.clock()
- row_id = 'instance-00000001@pyw.novalocal'
+ row_id = u'instance-00000001@pyw.novalocal'
period = 5
time_from = 1332833464
time_to = 0
- cf_str = 'cpu'
- scf_str = 'total'
+ cf_str = u'cpu'
+ scf_str = u'total'
statistic = api_server.STATISTIC.SUM
m = self.mox
@@ -139,12 +140,12 @@ def testApiStatisticSUM(self):
def testApiStatisticMAX(self):
time.clock()
- row_id = 'instance-00000001@pyw.novalocal'
+ row_id = u'instance-00000001@pyw.novalocal'
period = 5
time_from = 1332833464
time_to = 0
- cf_str = 'cpu'
- scf_str = 'total'
+ cf_str = u'cpu'
+ scf_str = u'total'
statistic = api_server.STATISTIC.MAXIMUM
m = self.mox
@@ -166,12 +167,12 @@ def testApiStatisticMAX(self):
def testApiStatisticMIN(self):
time.clock()
- row_id = 'instance-00000001@pyw.novalocal'
+ row_id = u'instance-00000001@pyw.novalocal'
period = 5
time_from = 1332833464
time_to = 0
- cf_str = 'cpu'
- scf_str = 'total'
+ cf_str = u'cpu'
+ scf_str = u'total'
statistic = api_server.STATISTIC.MINIMUM
m = self.mox
@@ -193,12 +194,12 @@ def testApiStatisticMIN(self):
def testApiStatisticAVG(self):
time.clock()
- row_id = 'instance-00000001@pyw.novalocal'
+ row_id = u'instance-00000001@pyw.novalocal'
period = 5
time_from = 1332833464
time_to = 0
- cf_str = 'cpu'
- scf_str = 'total'
+ cf_str = u'cpu'
+ scf_str = u'total'
statistic = api_server.STATISTIC.AVERAGE
m = self.mox
@@ -217,46 +218,6 @@ def testApiStatisticAVG(self):
print "api_statistics AVERAGE test \t\t\t[\033[1;33mOK\033[0m]"
print '-' * 60
-# def testApiStatistic(self):
-# time.clock()
-# row_id = 'instance-00000001@pyw.novalocal'
-# period = 5
-# time_from = 1332833464
-# time_to = 0
-# cf_str = 'cpu'
-# scf_str = 'total'
-# statistic = api_server.STATISTIC.SUM
-#
-# m = self.mox
-# m.StubOutWithMock(api_server, 'api_getdata')
-# api_server.api_getdata(row_id, cf_str, scf_str, \
-# time_from, time_to).AndReturn(ApiGetDataMox())
-# self.mox.ReplayAll()
-#
-# statistic = api_server.STATISTIC.MINIMUM
-# rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, \
-# statistic, period=period,
-# time_from=time_from, time_to=time_to)
-# print "%d MINIMUM results, spend %f seconds" % (count, time.clock())
-# print '-' * 60
-#
-# statistic = api_server.STATISTIC.AVERAGE
-# rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, \
-# statistic, period=period,
-# time_from=time_from, time_to=time_to)
-# print "%d AVERAGE results, spend %f seconds" % (count, time.clock())
-# print '-' * 60
-#
-# self.mox.VerifyAll()
-#
-# print "api_statistics test \t\t\t[\033[1;33mOK\033[0m]"
-#
-# def test_demo(self):
-# self.assertTrue(True)
-
-# def test_demo2(self):
-# self.assertEqual(['vda', 'vdb'], ['vda', 'vdb'])
-
if __name__ == '__main__':
time.clock()
ApiTestSuite = unittest.TestSuite()
@@ -268,7 +229,5 @@ def testApiStatisticAVG(self):
ApiTestSuite.addTest(TestApiServer("testApiStatisticMIN"))
ApiTestSuite.addTest(TestApiServer("testApiStatisticAVG"))
-# ApiTestSuite.addTest(TestApiServer("testApiStatistic"))
-
runner = unittest.TextTestRunner()
runner.run(ApiTestSuite)
View
6 tests/test_client.py
@@ -20,8 +20,8 @@
client = ApiClient()
client.period = 1
- client.cf_str = "cpu"
- client.scf_str = "total"
+ client.cf_str = u"cpu"
+ client.scf_str = u"total"
client.time_from = time.time() - 30*24*60*60
rs = client.getlist(client.cf_str)
print rs
@@ -33,7 +33,7 @@
# if not r is None: print len(r)
for i1 in r:
client.key = i
- client.scf_str = '10.0.0.2'
+ client.scf_str = u'10.0.0.2'
client.time_to = time.time()
print "sum = ", client.get_sum()
print "max = ", client.get_max()
Please sign in to comment.
Something went wrong with that request. Please try again.