Esempio n. 1
0
    def ListMeasurements(self, **unused_args):
        """Handles /measurements REST request."""

        # This is very limited and only supports time range and limit
        # queries. You might want to extend this to filter by other
        # properties, but for the purpose of measurement archiving
        # this is all we need.

        start_time = self.request.get('start_time')
        end_time = self.request.get('end_time')
        limit = self.request.get('limit')

        query = model.Measurement.all()

        if start_time:
            dt = util.MicrosecondsSinceEpochToTime(int(start_time))
            query.filter('timestamp >=', dt)
        if end_time:
            dt = util.MicrosecondsSinceEpochToTime(int(end_time))
            query.filter('timestamp <', dt)
        query.order('timestamp')
        if limit:
            results = query.fetch(int(limit))
        else:
            results = query

        output = []
        for measurement in results:
            # Need to catch case where device has been deleted
            try:
                unused_device_info = measurement.device_properties.device_info
            except db.ReferencePropertyResolveError:
                logging.exception('Device deleted for measurement %s',
                                  measurement.key().id())
                # Skip this measurement
                continue

            # Need to catch case where task has been deleted
            try:
                unused_task = measurement.task
            except db.ReferencePropertyResolveError:
                measurement.task = None
                measurement.put()

            mdict = util.ConvertToDict(measurement,
                                       timestamps_in_microseconds=True)

            # Fill in additional fields
            mdict['id'] = str(measurement.key().id())
            mdict['parameters'] = measurement.Params()
            mdict['values'] = measurement.Values()

            if 'task' in mdict and mdict['task'] is not None:
                mdict['task']['id'] = str(measurement.GetTaskID())
                mdict['task']['parameters'] = measurement.task.Params()

            output.append(mdict)
        self.response.out.write(json.dumps(output))
Esempio n. 2
0
def EncodeScheduleAsJson(schedule):
  """Given a list of Tasks, return a JSON string encoding the schedule."""
  output = []
  for task in schedule:
    # Don't send the user, tag, or filter fields with the schedule
    output_task = util.ConvertToDict(
        task, exclude_fields=['user', 'tag', 'filter','contrib'])
    # Need to add the parameters and key fields
    output_task['parameters'] = task.Params()
    output_task['key'] = str(task.key().id_or_name())
    output.append(output_task)

  return json.dumps(output)