| Index: tools/deep_memory_profiler/visualizer/services.py
|
| diff --git a/tools/deep_memory_profiler/visualizer/services.py b/tools/deep_memory_profiler/visualizer/services.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ac146fbca6f72dd9639dc124b87488540f4e3a09
|
| --- /dev/null
|
| +++ b/tools/deep_memory_profiler/visualizer/services.py
|
| @@ -0,0 +1,77 @@
|
| +# Copyright 2013 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import json
|
| +
|
| +from google.appengine.ext import blobstore
|
| +from google.appengine.ext import ndb
|
| +
|
| +
|
| +class Profiler(ndb.Model):
|
| + """Profiler entity to store json data. Use run_id as its key.
|
| + Json data will be stored at blobstore, but can be referred by BlobKey."""
|
| + blob_key = ndb.BlobKeyProperty()
|
| +
|
| +
|
| +class Template(ndb.Model):
|
| + """Template to breakdown profiler with multiple tags.
|
| + Use content as its key."""
|
| + content = ndb.JsonProperty()
|
| +
|
| +
|
| +def CreateProfiler(blob_info):
|
| + """Create Profiler entity in database of uploaded file. Return run_id."""
|
| + json_str = blob_info.open().read()
|
| + json_obj = json.loads(json_str)
|
| +
|
| + # Check the uniqueness of data run_id and store new one.
|
| + run_id = json_obj['run_id']
|
| + prof_key = ndb.Key('Profiler', run_id)
|
| + if not prof_key.get():
|
| + # Profile for this run_id does not exist
|
| + profiler = Profiler(id=run_id, blob_key=blob_info.key())
|
| + profiler.put()
|
| +
|
| + return run_id
|
| +
|
| +
|
| +def GetProfiler(run_id):
|
| + """Get Profiler entity from database of given run_id."""
|
| + # Get entity key.
|
| + profiler = ndb.Key('Profiler', run_id).get()
|
| + return blobstore.BlobReader(profiler.blob_key).read()
|
| +
|
| +
|
| +def CreateTemplates(blob_info):
|
| + """Create Template entities for all templates of uploaded file. Return ndb.Key
|
| + of default template or None if not indicated or found in templates."""
|
| + json_str = blob_info.open().read()
|
| + json_obj = json.loads(json_str)
|
| +
|
| + # Return None when no default template indicated.
|
| + if not 'default_template' in json_obj:
|
| + return None
|
| + # Return None when no default template found in templates.
|
| + if not json_obj['default_template'] in json_obj['templates']:
|
| + return None
|
| +
|
| + # Check the uniqueness of template content and store new one.
|
| + for tag, content in json_obj['templates'].iteritems():
|
| + content_str = json.dumps(content)
|
| + tmpl_key = ndb.Key('Template', content_str)
|
| + if tag == json_obj['default_template']:
|
| + default_key = tmpl_key
|
| + if not tmpl_key.get():
|
| + # Template of the same content does not exist.
|
| + template = Template(id=content_str, content=content)
|
| + template.put()
|
| +
|
| + return default_key
|
| +
|
| +
|
| +def GetTemplate(tmpl_id):
|
| + """Get Template entity of given tmpl_id generated by ndb.Key."""
|
| + # Get entity key.
|
| + template = ndb.Key(urlsafe=tmpl_id).get()
|
| + return json.dumps(template.content)
|
|
|