1 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 # This file is expected to be used under another directory to use, 6 # so we disable checking import path of GAE tools from this directory. 7 # pylint: disable=F0401,E0611,W0232 8 9 import json 10 11 from google.appengine.ext import blobstore 12 from google.appengine.ext import ndb 13 14 15 class Profiler(ndb.Model): 16 """Profiler entity to store json data. Use run_id as its key. 17 Json data will be stored at blobstore, but can be referred by BlobKey.""" 18 blob_key = ndb.BlobKeyProperty() 19 20 21 class Template(ndb.Model): 22 """Template to breakdown profiler with multiple tags. 23 Use content as its key.""" 24 content = ndb.JsonProperty() 25 26 27 def CreateProfiler(blob_info): 28 """Create Profiler entity in database of uploaded file. Return run_id.""" 29 json_str = blob_info.open().read() 30 json_obj = json.loads(json_str) 31 32 # Check the uniqueness of data run_id and store new one. 33 run_id = json_obj['run_id'] 34 prof_key = ndb.Key('Profiler', run_id) 35 if not prof_key.get(): 36 # Profile for this run_id does not exist 37 profiler = Profiler(id=run_id, blob_key=blob_info.key()) 38 profiler.put() 39 40 return run_id 41 42 43 def GetProfiler(run_id): 44 """Get Profiler entity from database of given run_id.""" 45 # Get entity key. 46 profiler = ndb.Key('Profiler', run_id).get() 47 return blobstore.BlobReader(profiler.blob_key).read() 48 49 50 def CreateTemplates(blob_info): 51 """Create Template entities for all templates of uploaded file. Return ndb.Key 52 of default template or None if not indicated or found in templates.""" 53 json_str = blob_info.open().read() 54 json_obj = json.loads(json_str) 55 56 # Return None when no default template indicated. 57 if 'default_template' not in json_obj: 58 return None 59 # Return None when no default template found in templates. 60 if json_obj['default_template'] not in json_obj['templates']: 61 return None 62 63 # Check the uniqueness of template content and store new one. 64 for tag, content in json_obj['templates'].iteritems(): 65 content_str = json.dumps(content) 66 tmpl_key = ndb.Key('Template', content_str) 67 if tag == json_obj['default_template']: 68 default_key = tmpl_key 69 if not tmpl_key.get(): 70 # Template of the same content does not exist. 71 template = Template(id=content_str, content=content) 72 template.put() 73 74 return default_key 75 76 77 def CreateTemplate(content): 78 """Create Template entity for user to share.""" 79 content_str = json.dumps(content) 80 tmpl_key = ndb.Key('Template', content_str) 81 if not tmpl_key.get(): 82 # Template of the same content does not exist. 83 template = Template(id=content_str, content=content) 84 template.put() 85 86 return tmpl_key 87 88 89 def GetTemplate(tmpl_id): 90 """Get Template entity of given tmpl_id generated by ndb.Key.""" 91 # Get entity key. 92 template = ndb.Key(urlsafe=tmpl_id).get() 93 return json.dumps(template.content) 94