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""" | |
49 json_str = blob_info.open().read() | |
50 json_obj = json.loads(json_str) | |
51 | |
52 # Check the uniqueness of template content and store new one. | |
53 for tag, content in json_obj['templates'].iteritems(): | |
54 content_str = json.dumps(content) | |
55 tmpl_key = ndb.Key('Template', content_str) | |
56 if tag == json_obj['default_template']: | |
57 default_key = tmpl_key | |
58 if not tmpl_key.get(): | |
59 # Template of the same content does not exist. | |
60 template = Template(id=content_str, content=content) | |
61 template.put() | |
62 | |
63 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.
| |
64 | |
65 | |
66 def GetTemplate(tmpl_id): | |
67 """Get Template entity of given tmpl_id generated by ndb.Key.""" | |
68 # Get entity key. | |
69 template = ndb.Key(urlsafe=tmpl_id).get() | |
70 return json.dumps(template.content) | |
OLD | NEW |