def debounce_update_release_health_data(organization, project_ids): """This causes a flush of snuba health data to the postgres tables once per minute for the given projects. """ # Figure out which projects need to get updates from the snuba. should_update = {} cache_keys = ["debounce-health:%d" % id for id in project_ids] cache_data = cache.get_many(cache_keys) for project_id, cache_key in izip(project_ids, cache_keys): if cache_data.get(cache_key) is None: should_update[project_id] = cache_key if not should_update: return projects = {p.id: p for p in Project.objects.get_many_from_cache(should_update.keys())} # This gives us updates for all release-projects which have seen new # health data over the last days. It will miss releases where the last # date is longer than what `get_changed_project_release_model_adoptions` # considers recent. project_releases = get_changed_project_release_model_adoptions(should_update.keys()) # Check which we already have rows for. existing = set( ReleaseProject.objects.filter( project_id__in=[x[0] for x in project_releases], release__version__in=[x[1] for x in project_releases], ).values_list("project_id", "release__version") ) to_upsert = [] for key in project_releases: if key not in existing: to_upsert.append(key) if to_upsert: dates = get_oldest_health_data_for_releases(to_upsert) for project_id, version in to_upsert: project = projects.get(project_id) if project is None: # should not happen continue # We might have never observed the release. This for instance can # happen if the release only had health data so far. For these cases # we want to create the release the first time we observed it on the # health side. release = Release.get_or_create( project=project, version=version, date_added=dates.get((project_id, version)) ) # Make sure that the release knows about this project. Like we had before # the project might not have been associated with this release yet. release.add_project(project) # Debounce updates for a minute cache.set_many(dict(izip(should_update.values(), [True] * len(should_update))), 60)
def debounce_update_release_health_data(organization, project_ids): """This causes a flush of snuba health data to the postgres tables once per minute for the given projects. """ # Figure out which projects need to get updates from the snuba. should_update = {} cache_keys = ["debounce-health:%d" % id for id in project_ids] cache_data = cache.get_many(cache_keys) for project_id, cache_key in izip(project_ids, cache_keys): if cache_data.get(cache_key) is None: should_update[project_id] = cache_key if not should_update: return projects = { p.id: p for p in Project.objects.get_many_from_cache(should_update.keys()) } # This gives us updates for all release-projects which have seen new # health data over the last 24 hours. It will miss releases where the last # date is <24h ago. We need to aggregate the data for the totals per release # manually here now. This does not take environments into account. for project_id, version in get_changed_project_release_model_adoptions( should_update.keys()): project = projects.get(project_id) if project is None: # should not happen continue # We might have never observed the release. This for instance can # happen if the release only had health data so far. For these cases # we want to create the release the first time we observed it on the # health side. release = Release.get_or_create(project=project, version=version) # Make sure that the release knows about this project. Like we had before # the project might not have been associated with this release yet. release.add_project(project) # Debounce updates for a minute cache.set_many( dict(izip(should_update.values(), [True] * len(should_update))), 60)