def testGenerateTrendingInstallsCache(self): """Tests _GenerateTrendingInstallsCache.""" package1_name = 'package1' package2_name = 'package2' package3_name = 'package3' package4_name = 'package4' expected_trending = { 'success': { 'packages': [ [package1_name, 10, 66.666666666666657], [package4_name, 5, 33.333333333333329], ], 'total': 15, }, 'failure': { 'packages': [ [package2_name, 10, 40.0], [package1_name, 10, 40.0], [package3_name, 5, 20.0], ], 'total': 25, }, } for i in range(10): models.InstallLog(package=package1_name, status='0').put() models.InstallLog(package=package1_name, status='1').put() models.InstallLog(package=package2_name, status='-1').put() if i % 2: models.InstallLog(package=package3_name, status='-1').put() models.InstallLog(package=package4_name, status='0').put() reports_cache._GenerateTrendingInstallsCache(1) taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME) tasks = taskqueue_stub.get_filtered_tasks() self.assertEqual(1, len(tasks)) deferred.run(tasks[0].payload) self.assertEqual(1, len(taskqueue_stub.get_filtered_tasks())) self.assertEqual( expected_trending, reports_cache.models.ReportsCache.GetTrendingInstalls(1)[0])
except ValueError, e: logging.warning('Ignoring invalid install_datetime; %s', str(e)) install_datetime = datetime.datetime.utcnow() except util.EpochExtremeFutureValueError, e: logging.warning('Ignoring future install_datetime; %s', str(e)) install_datetime = datetime.datetime.utcnow() except util.EpochValueError, e: install_datetime = datetime.datetime.utcnow() pkg = '%s-%s' % (name, version) entity = models.InstallLog(uuid=computer.uuid, computer=computer, package=pkg, status=status, on_corp=on_corp, applesus=applesus, unattended=unattended, duration_seconds=duration_seconds, mtime=install_datetime, dl_kbytes_per_sec=dl_kbytes_per_sec) entity.success = entity.IsSuccess() to_put.append(entity) gae_util.BatchDatastoreOp(models.db.put, to_put) def post(self): """Reports get handler. Returns: A webapp.Response() response. """
def _LogInstalls(self, installs, computer): """Logs a batch of installs for a given computer. Args: installs: list, of str install data from a preflight/postflight report. computer: models.Computer entity. """ if not installs: return on_corp = self.request.get('on_corp') if on_corp == '1': on_corp = True elif on_corp == '0': on_corp = False else: on_corp = None to_put = [] for install in installs: if install.startswith('Install of'): d = { 'applesus': 'false', 'duration_seconds': None, 'download_kbytes_per_sec': None, 'name': install, 'status': 'UNKNOWN', 'version': '', 'unattended': 'false', } # support for old 'Install of FooPkg-1.0: SUCCESSFUL' style strings. try: m = LEGACY_INSTALL_RESULTS_STRING_REGEX.search(install) if not m: raise ValueError elif m.group(3) == INSTALL_RESULT_SUCCESSFUL: d['status'] = 0 else: d['status'] = m.group(4) d['name'] = m.group(1) d['version'] = m.group(2) except (IndexError, AttributeError, ValueError): logging.warning('Unknown install string format: %s', install) else: # support for new 'name=pkg|version=foo|...' style strings. d = common.KeyValueStringToDict(install) name = d.get('display_name', '') or d.get('name', '') version = d.get('version', '') status = str(d.get('status', '')) applesus = common.GetBoolValueFromString(d.get('applesus', '0')) unattended = common.GetBoolValueFromString(d.get( 'unattended', '0')) try: duration_seconds = int(d.get('duration_seconds', None)) except (TypeError, ValueError): duration_seconds = None try: dl_kbytes_per_sec = int(d.get('download_kbytes_per_sec', None)) # Ignore zero KB/s download speeds, as that's how Munki reports # unknown speed. if dl_kbytes_per_sec == 0: dl_kbytes_per_sec = None except (TypeError, ValueError): dl_kbytes_per_sec = None try: install_datetime = util.Datetime.utcfromtimestamp( d.get('time', None)) except ValueError as e: logging.info('Ignoring invalid install_datetime: %s', str(e)) install_datetime = datetime.datetime.utcnow() except util.EpochExtremeFutureValueError as e: logging.info('Ignoring extreme future install_datetime: %s', str(e)) install_datetime = datetime.datetime.utcnow() except util.EpochFutureValueError: install_datetime = datetime.datetime.utcnow() pkg = '%s-%s' % (name, version) entity = models.InstallLog(uuid=computer.uuid, computer=computer, package=pkg, status=status, on_corp=on_corp, applesus=applesus, unattended=unattended, duration_seconds=duration_seconds, mtime=install_datetime, dl_kbytes_per_sec=dl_kbytes_per_sec) entity.success = entity.IsSuccess() to_put.append(entity) gae_util.BatchDatastoreOp(models.db.put, to_put)