Esempio n. 1
0
 def test_get_oldest_health_data_for_releases(self):
     data = get_oldest_health_data_for_releases([(self.project.id,
                                                  self.session_release)])
     assert data == {
         (self.project.id, self.session_release):
         format_timestamp(self.session_started // 3600 * 3600)
     }
Esempio n. 2
0
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)
Esempio n. 3
0
def upsert_missing_release(project, version):
    """This adds a release to postgres if it should exist but does not do yet."""
    try:
        return ReleaseProject.objects.get(project=project, release__version=version).release
    except ReleaseProject.DoesNotExist:
        rows = get_oldest_health_data_for_releases([(project.id, version)])
        if rows:
            oldest = next(rows.values())
            release = Release.get_or_create(project=project, version=version, date_added=oldest)
            release.add_project(project)
            return release