lab info save added
diff --git a/persistance/keystone.py b/persistance/keystone.py
new file mode 100644
index 0000000..e6308b5
--- /dev/null
+++ b/persistance/keystone.py
@@ -0,0 +1,89 @@
+from functools import partial
+import json
+import urllib2
+from keystoneclient.v2_0 import Client as keystoneclient
+from keystoneclient import exceptions
+
+
+class Urllib2HTTP(object):
+    """
+    class for making HTTP requests
+    """
+
+    allowed_methods = ('get', 'put', 'post', 'delete', 'patch', 'head')
+
+    def __init__(self, root_url, headers=None, echo=False):
+        """
+        """
+        if root_url.endswith('/'):
+            self.root_url = root_url[:-1]
+        else:
+            self.root_url = root_url
+
+        self.headers = headers if headers is not None else {}
+        self.echo = echo
+
+    def do(self, method, path, params=None):
+        if path.startswith('/'):
+            url = self.root_url + path
+        else:
+            url = self.root_url + '/' + path
+
+        if method == 'get':
+            assert params == {} or params is None
+            data_json = None
+        else:
+            data_json = json.dumps(params)
+
+        request = urllib2.Request(url,
+                                  data=data_json,
+                                  headers=self.headers)
+        if data_json is not None:
+            request.add_header('Content-Type', 'application/json')
+
+        request.get_method = lambda: method.upper()
+        response = urllib2.urlopen(request)
+
+        if response.code < 200 or response.code > 209:
+            raise IndexError(url)
+
+        content = response.read()
+
+        if '' == content:
+            return None
+
+        return json.loads(content)
+
+    def __getattr__(self, name):
+        if name in self.allowed_methods:
+            return partial(self.do, name)
+        raise AttributeError(name)
+
+
+class KeystoneAuth(Urllib2HTTP):
+    def __init__(self, root_url, creds, headers=None, echo=False,
+                 admin_node_ip=None):
+        super(KeystoneAuth, self).__init__(root_url, headers, echo)
+        self.keystone_url = "http://{0}:5000/v2.0".format(admin_node_ip)
+        self.keystone = keystoneclient(
+            auth_url=self.keystone_url, **creds)
+        self.refresh_token()
+
+    def refresh_token(self):
+        """Get new token from keystone and update headers"""
+        try:
+            self.keystone.authenticate()
+            self.headers['X-Auth-Token'] = self.keystone.auth_token
+        except exceptions.AuthorizationFailure:
+            print 'asdfsf'
+
+    def do(self, method, path, params=None):
+        """Do request. If gets 401 refresh token"""
+        try:
+            return super(KeystoneAuth, self).do(method, path, params)
+        except urllib2.HTTPError as e:
+            if e.code == 401:
+                self.refresh_token()
+                return super(KeystoneAuth, self).do(method, path, params)
+            else:
+                raise
diff --git a/persistance/models.py b/persistance/models.py
index d09ed95..f2b7c35 100644
--- a/persistance/models.py
+++ b/persistance/models.py
@@ -32,14 +32,19 @@
 
 class Lab(db.Model):
     id = db.Column(db.Integer, primary_key=True)
-    name = db.Column(db.String(64))
+    name = db.Column(db.String(64), unique=True)
+    url = db.Column(db.String(256))
     type = db.Column(db.String(4096))
+    fuel_version = db.Column(db.String(64))
+    ceph_version = db.Column(db.String(64))
+    lab_general_info = db.Column(db.Text)
+    lab_meta = db.Column(db.Text)
 
 
 class Result(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     build_id = db.Column(db.Integer, ForeignKey('build.id'))
-    lab_id = db.Column(db.Integer)
+    lab_id = db.Column(db.Integer, ForeignKey('lab.id'))
     date = db.Column(db.DateTime)
     param_combination_id = db.Column(db.Integer, ForeignKey('param_combination.id'))
     bandwith = db.Column(db.Float)
diff --git a/persistance/storage_api.py b/persistance/storage_api.py
index f899084..a1503d4 100644
--- a/persistance/storage_api.py
+++ b/persistance/storage_api.py
@@ -1,7 +1,9 @@
 import datetime
 import math
+from urlparse import urlparse
 
 from flask import json
+from persistance.keystone import KeystoneAuth
 from sqlalchemy import sql
 from persistance.models import *
 
@@ -34,6 +36,84 @@
     return math.sqrt(sum(map(lambda x: (x - m) ** 2, l)))
 
 
+
+def total_lab_info(data):
+    # <koder>: give 'd' meaningful name
+    d = {}
+    d['nodes_count'] = len(data['nodes'])
+    d['total_memory'] = 0
+    d['total_disk'] = 0
+    d['processor_count'] = 0
+
+    for node in data['nodes']:
+        d['total_memory'] += node['memory']['total']
+        d['processor_count'] += len(node['processors'])
+
+        for disk in node['disks']:
+            d['total_disk'] += disk['size']
+
+    to_gb = lambda x: x / (1024 ** 3)
+    d['total_memory'] = format(to_gb(d['total_memory']), ',d')
+    d['total_disk'] = format(to_gb(d['total_disk']), ',d')
+    return d
+
+
+def collect_lab_data(url, cred):
+    u = urlparse(url)
+    keystone = KeystoneAuth(root_url=url, creds=cred, admin_node_ip=u.hostname)
+    lab_info = keystone.do(method='get', path="/api/nodes")
+    fuel_version = keystone.do(method='get', path="/api/version/")
+
+    nodes = []
+    result = {}
+
+    for node in lab_info:
+        # <koder>: give p,i,d,... vars meaningful names
+        d = {}
+        d['name'] = node['name']
+        p = []
+        i = []
+        disks = []
+        devices = []
+
+        for processor in node['meta']['cpu']['spec']:
+             p.append(processor)
+
+        for iface in node['meta']['interfaces']:
+            i.append(iface)
+
+        m = node['meta']['memory'].copy()
+
+        for disk in node['meta']['disks']:
+            disks.append(disk)
+
+        d['memory'] = m
+        d['disks'] = disks
+        d['devices'] = devices
+        d['interfaces'] = i
+        d['processors'] = p
+
+        nodes.append(d)
+
+    result['nodes'] = nodes
+    # result['name'] = 'Perf-1 Env'
+    result['fuel_version'] = fuel_version['release']
+
+    return result
+
+
+def get_build_info(build_name):
+    session = db.session()
+    result = session.query(Result, Build).join(Build).filter(Build.name == build_name).first()
+    lab = session.query(Lab).filter(Lab.id == result[0].lab_id).first()
+    return eval(lab.lab_general_info)
+
+
+def get_build_detailed_info(build_name):
+    data = get_build_info(build_name)
+    return total_lab_info(data)
+
+
 def process_build_data(build):
     for item in build.items():
         if type(item[1]) is list:
@@ -107,9 +187,17 @@
         return rs[0][0]
 
 
-def add_lab(lab_name):
-    pass
+def add_lab(session, lab_url, lab_name, ceph_version, fuel_version, data, info):
+    result = session.query(Lab).filter(Lab.name == lab_name).all()
 
+    if len(result) != 0:
+        return result[0].id
+    else:
+        lab = Lab(name=lab_name, url=lab_url, ceph_version=ceph_version,
+                  fuel_version=fuel_version, lab_general_info=str(data), lab_meta=str(info))
+        session.add(lab)
+        session.commit()
+        return lab.id
 
 #function store list of builds in database
 def add_data(data):
@@ -124,12 +212,26 @@
                              build_data.pop("type"),
                              build_data.pop("iso_md5"),
                              )
+        creds = {"username": build_data.pop("username"),
+                 "password": build_data.pop("password"),
+                 "tenant_name": build_data.pop("tenant_name")
+        }
+        lab_url = build_data.pop("lab_url")
+        lab_name = build_data.pop("lab_name")
+        ceph_version = build_data.pop("ceph_version")
+        data = collect_lab_data(lab_url, creds)
+        data['name'] = lab_name
+        info = total_lab_info(data)
+        lab_id = add_lab(session, lab_url=lab_name, lab_name=lab_url,
+                ceph_version=ceph_version, fuel_version=data['fuel_version'], data=data, info=info)
+
         date = build_data.pop("date")
         date = datetime.datetime.strptime(date, "%a %b %d %H:%M:%S %Y")
 
         for params, [bw, dev] in build_data.items():
             param_comb_id = add_param_comb(session, *params.split(" "))
-            result = Result(param_combination_id=param_comb_id, build_id=build_id, bandwith=bw, date=date)
+            result = Result(param_combination_id=param_comb_id, build_id=build_id,
+                            bandwith=bw, date=date, lab_id=lab_id)
             session.add(result)
             session.commit()
 
@@ -317,7 +419,14 @@
 
 if __name__ == '__main__':
     # add_build("Some build", "GA", "bla bla")
+    cred = {"username": "admin", "password": "admin", "tenant_name": "admin"}
     json_data = '[{\
+        "username": "admin",\
+        "password": "admin", \
+        "tenant_name": "admin",\
+        "lab_url": "http://172.16.52.112:8000",\
+        "lab_name": "Perf-1-Env",\
+        "ceph_version": "v0.80 Firefly",\
         "randwrite a 256k": [16885, 1869],\
         "randwrite s 4k": [79, 2],\
         "read a 64k": [74398, 11618],\
@@ -349,6 +458,12 @@
         "iso_md5": "bla bla"\
     },\
     {\
+        "username": "admin",\
+        "password": "admin", \
+        "tenant_name": "admin",\
+        "lab_url": "http://172.16.52.112:8000",\
+        "ceph_version": "v0.80 Firefly",\
+        "lab_name": "Perf-1-Env",\
         "randwrite a 256k": [20212, 5690],\
         "randwrite s 4k": [83, 6],\
         "read a 64k": [89394, 3912],\
@@ -380,6 +495,12 @@
         "iso_md5": "bla bla"\
     },\
     {\
+        "username": "admin",\
+        "password": "admin", \
+        "tenant_name": "admin",\
+        "lab_url": "http://172.16.52.112:8000",\
+        "ceph_version": "v0.80 Firefly",\
+        "lab_name": "Perf-1-Env",\
         "randwrite a 256k": [16885, 1869],\
         "randwrite s 4k": [79, 2],\
         "read a 64k": [74398, 11618],\