summaryrefslogtreecommitdiffstats
path: root/tools/deep_memory_profiler/visualizer/services.py
blob: 8c8bc659fb254a0e27bbfaf1ccdba4443acfe6b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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.

# This file is expected to be used under another directory to use,
# so we disable checking import path of GAE tools from this directory.
# pylint: disable=F0401,E0611,W0232

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 'default_template' not in json_obj:
    return None
  # Return None when no default template found in templates.
  if json_obj['default_template'] not 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 CreateTemplate(content):
  """Create Template entity for user to share."""
  content_str = json.dumps(content)
  tmpl_key = ndb.Key('Template', content_str)
  if not tmpl_key.get():
    # Template of the same content does not exist.
    template = Template(id=content_str, content=content)
    template.put()

  return tmpl_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)