OLD | NEW |
(Empty) | |
| 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 import json |
| 6 |
| 7 from google.appengine.ext import blobstore |
| 8 from google.appengine.ext import ndb |
| 9 |
| 10 |
| 11 class Profiler(ndb.Model): |
| 12 """Profiler entity to store json data. Use run_id as its key. |
| 13 Json data will be stored at blobstore, but can be referred by BlobKey.""" |
| 14 blob_key = ndb.BlobKeyProperty() |
| 15 |
| 16 |
| 17 class Template(ndb.Model): |
| 18 """Template to breakdown profiler with multiple tags. |
| 19 Use content as its key.""" |
| 20 content = ndb.JsonProperty() |
| 21 |
| 22 |
| 23 def CreateProfiler(blob_info): |
| 24 """Create Profiler entity in database of uploaded file. Return run_id.""" |
| 25 json_str = blob_info.open().read() |
| 26 json_obj = json.loads(json_str) |
| 27 |
| 28 # Check the uniqueness of data run_id and store new one. |
| 29 run_id = json_obj['run_id'] |
| 30 prof_key = ndb.Key('Profiler', run_id) |
| 31 if not prof_key.get(): |
| 32 # Profile for this run_id does not exist |
| 33 profiler = Profiler(id=run_id, blob_key=blob_info.key()) |
| 34 profiler.put() |
| 35 |
| 36 return run_id |
| 37 |
| 38 |
| 39 def GetProfiler(run_id): |
| 40 """Get Profiler entity from database of given run_id.""" |
| 41 # Get entity key. |
| 42 profiler = ndb.Key('Profiler', run_id).get() |
| 43 return blobstore.BlobReader(profiler.blob_key).read() |
| 44 |
| 45 |
| 46 def CreateTemplates(blob_info): |
| 47 """Create Template entities for all templates of uploaded file. Return ndb.Key |
| 48 of default template or None if not indicated or found in templates.""" |
| 49 json_str = blob_info.open().read() |
| 50 json_obj = json.loads(json_str) |
| 51 |
| 52 # Return None when no default template indicated. |
| 53 if not 'default_template' in json_obj: |
| 54 return None |
| 55 # Return None when no default template found in templates. |
| 56 if not json_obj['default_template'] in json_obj['templates']: |
| 57 return None |
| 58 |
| 59 # Check the uniqueness of template content and store new one. |
| 60 for tag, content in json_obj['templates'].iteritems(): |
| 61 content_str = json.dumps(content) |
| 62 tmpl_key = ndb.Key('Template', content_str) |
| 63 if tag == json_obj['default_template']: |
| 64 default_key = tmpl_key |
| 65 if not tmpl_key.get(): |
| 66 # Template of the same content does not exist. |
| 67 template = Template(id=content_str, content=content) |
| 68 template.put() |
| 69 |
| 70 return default_key |
| 71 |
| 72 |
| 73 def GetTemplate(tmpl_id): |
| 74 """Get Template entity of given tmpl_id generated by ndb.Key.""" |
| 75 # Get entity key. |
| 76 template = ndb.Key(urlsafe=tmpl_id).get() |
| 77 return json.dumps(template.content) |
OLD | NEW |