Пример #1
0
def download_data(filename, merge, java_application):
    """Download appstats data from memcache."""
    oldrecords = []
    oldfile = None

    if merge:
        try:
            oldfile = open(filename, 'rb')
        except IOError:
            logging.info('No file to merge. Creating new file %s', filename)
    if oldfile:
        logging.info('Merging with existing file %s', filename)
        oldrecords = loader.UnpickleFromFile(oldfile)
        oldfile.close()
    if oldrecords:

        last_timestamp = oldrecords[0].start_timestamp_milliseconds()
        records = loader.FromMemcache(filter_timestamp=last_timestamp,
                                      java_application=java_application)
    else:
        records = loader.FromMemcache(java_application=java_application)

    merged_records = records + oldrecords
    try:
        outfile = open(filename, 'wb')
    except IOError:
        logging.error('Cannot open %s', filename)
        return
    loader.PickleToFile(merged_records, outfile)
    outfile.close()
Пример #2
0
    def ReadData(self, source):
        """Parses source option and reads appropriate data source.

    Args:
      source: Source of Appstats data. Either filename if being read from
        a file or MEMCACHE if being read from memcache.
    Returns:
      errormessage: An error message to display to the user if an error occured
        while reading data, None if no error occured.
      recordlist: A list of Appstats records in RequestStatProto protobuf format
        in reverse chronological order (i.e. most recent first).
    """
        errormessage = None
        recordlist = None
        if source == 'MEMCACHE':

            recordlist = loader.FromMemcache()
        else:
            rootdir = self.GetRoot()
            if rootdir is None:
                errormessage = 'No parent directory has app.yaml!'
                return errormessage, recordlist
            source_root = os.path.join(rootdir, source)
            try:
                outfile = mockable_open(source_root, 'rb')
            except IOError:
                logging.error('Cannot open %s', source)
                errormessage = 'Unable to open file!'
                return errormessage, recordlist
            mtime = os.path.getmtime(source_root)
            if cache.IsCached(source, mtime):
                logging.info('Using cached records from %s', source)
                recordlist = cache.recordlist
            else:
                logging.info('Reading fresh records from %s', source)
                recordlist = loader.UnpickleFromFile(outfile)
                cache.Insert(source, mtime, recordlist)
        return errormessage, recordlist