raylu 11 лет назад
Родитель
Сommit
b74f5e5891
2 измененных файлов с 55 добавлено и 50 удалено
  1. 51 0
      api/fileio.py
  2. 4 50
      api/server.py

+ 51 - 0
api/fileio.py

@@ -0,0 +1,51 @@
+import struct
+
+formats = {
+	'cpu': 'f',
+	'mem': 'q',
+	'net': 'q',
+	'disk': 'q',
+}
+
+def read_stats(f):
+	buf = f.read()
+	stats = {}
+	index = 0
+	while index < len(buf):
+		# read the key
+		key_size = ord(buf[index])
+		fmt = '%dp' % (key_size + 1)
+		data = struct.unpack(fmt, buf[index:index+key_size+1])
+		key = data[0]
+
+		# get the format_code
+		split = key.split('.')
+		stat_group = split[0]
+		format_code = formats[stat_group]
+
+		# read the data
+		fmt = '%dp 1440%s' % (key_size + 1, format_code)
+		size = struct.calcsize(fmt)
+		data = struct.unpack(fmt, buf[index:index+size])
+		dict_insert(stats, split, data[1:])
+		index += size
+	return stats
+
+def dict_insert(d, split, value):
+	if len(split) > 1:
+		d.setdefault(split[0], {})
+		dict_insert(d[split[0]], split[1:], value)
+	else:
+		d[split[0]] = value
+
+def write_datum(f, data):
+	for stat_group, stats in data.items():
+		if stat_group == 'client_id':
+			continue
+		format_code = formats[stat_group]
+		for stat, datum in stats.items():
+			key = '%s.%s' % (stat_group, stat)
+			fmt = '%dp 1440%s' % (len(key) + 1, format_code)
+			array = [-1] * 1440
+			array[0] = datum
+			f.write(struct.pack(fmt, key.encode('utf-8'), *array))

+ 4 - 50
api/server.py

@@ -4,12 +4,12 @@ import errno
 import json
 import os
 from os import path
-import struct
 import warnings
 
 warnings.filterwarnings('ignore', 'libevent')
 import gevent.pywsgi
 
+import fileio
 import reloader
 
 DATA_DIR = path.expanduser('~/sysvitals_data')
@@ -37,47 +37,13 @@ def application(environ, start_response):
 	else:
 		print 'split was', split
 
-formats = {
-	'cpu': 'f',
-	'mem': 'q',
-	'net': 'q',
-	'disk': 'q',
-}
-
 def get_data(split, environ):
 	group = int(split[1])
 	client_id = int(split[3])
 	data_path = path.join(DATA_DIR, str(group), str(client_id))
 	with open(data_path, 'r') as f:
-		buf = f.read()
-	rval = {}
-	index = 0
-	while index < len(buf):
-		# read the key
-		key_size = ord(buf[index])
-		fmt = '%dp' % (key_size + 1)
-		data = struct.unpack(fmt, buf[index:index+key_size+1])
-		key = data[0]
-
-		# get the format_code
-		split = key.split('.')
-		stat_group = split[0]
-		format_code = formats[stat_group]
-
-		# read the data
-		fmt = '%dp 1440%s' % (key_size + 1, format_code)
-		size = struct.calcsize(fmt)
-		data = struct.unpack(fmt, buf[index:index+size])
-		dict_insert(rval, split, data[1:])
-		index += size
-	return json.dumps(rval)
-
-def dict_insert(d, split, value):
-	if len(split) > 1:
-		d.setdefault(split[0], {})
-		dict_insert(d[split[0]], split[1:], value)
-	else:
-		d[split[0]] = value
+		stats = fileio.read_stats(f)
+	return json.dumps(stats)
 
 def post_datum(split, environ):
 	group = int(split[1])
@@ -90,19 +56,7 @@ def post_datum(split, environ):
 		if e.errno != errno.EEXIST:
 			raise
 	with open(path.join(group_dir, str(client_id)), 'w') as f:
-		for stat_group, stats in body.items():
-			if stat_group == 'client_id':
-				continue
-			format_code = formats[stat_group]
-			for stat, data in stats.items():
-				key = '%s.%s' % (stat_group, stat)
-				write_stat(f, key.encode('utf-8'), format_code, data)
+		fileio.write_datum(f, body)
 	return '{"status": "ok"}'
 
-def write_stat(f, key, format_code, data):
-	fmt = '%dp 1440%s' % (len(key) + 1, format_code)
-	array = [-1] * 1440
-	array[0] = data
-	f.write(struct.pack(fmt, key, *array))
-
 main()