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..aa7575e4b4fe6a50f9621f983f5c5bc8b376838d |
--- /dev/null |
+++ b/tools/deep_memory_profiler/visualizer/services.py |
@@ -0,0 +1,70 @@ |
+# 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""" |
+ json_str = blob_info.open().read() |
+ json_obj = json.loads(json_str) |
+ |
+ # 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 |
sullivan
2013/09/20 13:59:51
default_key doesn't get initialized, so won't you
junjianx
2013/09/24 05:53:53
Done.
|
+ |
+ |
+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) |