def _post_get_hook(cls, key, future):  # pylint: disable=unused-argument
     """Throws an exception when external users try to get() internal data."""
     entity = future.get_result()
     if entity is None:
         return
     # Internal-only objects should never be accessed by non-internal accounts!
     if (getattr(entity, 'internal_only', False)
             and not datastore_hooks.IsUnalteredQueryPermitted()):
         # Keep info about the fact that we're doing an access check out of the
         # callstack in case app engine shows it to the user.
         assert False
Example #2
0
def _UpdateRevisionMap(revision_map,
                       parent_test,
                       rev,
                       num_points,
                       start_rev=None,
                       end_rev=None):
    """Updates a dict of revisions to data point information for one test.

  Depending on which arguments are given, there are several ways that
  this function can update the dict of revisions:
    1. If start_rev and end_rev are given, then revisions in this range
       are used. The num_points argument is ignored.
    2. Otherwise, if rev is given, then revisions before and after the
       specified revision are used.
    3. Otherwise, the latest revisions are used.

  Args:
    revision_map: A dict mapping revision numbers to dicts of point info.
        Each point info dict contains information from a Row entity.
    parent_test: A TestMetadata entity with Row children.
    rev: The middle revision in the revision map (could be None).
    num_points: The number of points to include in the revision map.
    start_rev: Start revision number (optional).
    end_rev: End revision number (optional).
  """
    anomaly_annotation_map = _GetAnomalyAnnotationMap(parent_test.key)
    assert (datastore_hooks.IsUnalteredQueryPermitted()
            or not parent_test.internal_only)

    if start_rev and end_rev:
        rows = graph_data.GetRowsForTestInRange(parent_test.key, start_rev,
                                                end_rev, True)
    elif rev:
        assert num_points
        rows = graph_data.GetRowsForTestAroundRev(parent_test.key, rev,
                                                  num_points, True)
    else:
        assert num_points
        rows = graph_data.GetLatestRowsForTest(parent_test.key,
                                               num_points,
                                               privileged=True)

    parent_test_key = parent_test.key.urlsafe()
    for row in rows:
        if row.revision not in revision_map:
            revision_map[row.revision] = {}
        revision_map[row.revision][parent_test_key] = _PointInfoDict(
            row, anomaly_annotation_map)
Example #3
0
def SetCache(test_path, rows):
    """Sets the saved graph revisions data for a test.

  Args:
    test_path: A test path string.
    rows: A list of [revision, value, timestamp] triplets.
  """
    # This first set generally only sets the internal-only cache.
    namespaced_stored_object.Set(_CACHE_KEY % test_path, rows)

    # If this is an internal_only query for externally available data,
    # set the cache for that too.
    if datastore_hooks.IsUnalteredQueryPermitted():
        test = utils.TestKey(test_path).get()
        if test and not test.internal_only:
            namespaced_stored_object.SetExternal(_CACHE_KEY % test_path, rows)
Example #4
0
    def get(self):
        """Gets CSV from data store and outputs it.

    Request parameters:
      test_path: Full test path of one trace.
      rev: End revision number; if not given, latest revision is used.
      num_points: Number of Rows to get data for.
      attr: Comma-separated list of attributes (columns) to return.

    Outputs:
      CSV file contents.
    """
        test_path = self.request.get('test_path')
        rev = self.request.get('rev')
        num_points = int(self.request.get('num_points', 500))
        attributes = self.request.get('attr', 'revision,value').split(',')

        if not test_path:
            self.ReportError('No test path given.', status=400)
            return

        logging.info('Got request to /graph_csv for test: "%s".', test_path)

        test_key = utils.TestKey(test_path)
        test = test_key.get()
        assert (datastore_hooks.IsUnalteredQueryPermitted()
                or not test.internal_only)
        datastore_hooks.SetSinglePrivilegedRequest()
        q = graph_data.Row.query()
        q = q.filter(
            graph_data.Row.parent_test == utils.OldStyleTestKey(test_key))
        if rev:
            q = q.filter(graph_data.Row.revision <= int(rev))
        q = q.order(-graph_data.Row.revision)
        points = reversed(q.fetch(limit=num_points))

        rows = self._GenerateRows(points, attributes)

        output = StringIO.StringIO()
        csv.writer(output).writerows(rows)
        self.response.headers['Content-Type'] = 'text/csv'
        self.response.headers['Content-Disposition'] = (
            'attachment; filename=%s.csv' % test.test_name)
        self.response.out.write(output.getvalue())