def num_tests(request): context = {} start = (TimeUtils.get_local_time() - datetime.timedelta(days=180)).replace(hour=0, minute=0) bins = get_bins(start, datetime.timedelta(days=7)) set_passed(start, "week", "Passed tests in last 6 months, by week", context, "month_chart", "%m/%d", bins) start = (TimeUtils.get_local_time() - datetime.timedelta(days=7)).replace(hour=0, minute=0) bins = get_bins(start, datetime.timedelta(days=1)) set_passed(start, "day", "Passed tests in last week, by day", context, "week_chart", "%m/%d", bins) return render(request, 'ci/num_tests.html', context)
def handle(self, *args, **options): dryrun = options["dryrun"] days = options["days"] hours = options["hours"] allowed_fail = options["allowed_fail"] client_runner_user = options["client_runner_user"] if days: d = TimeUtils.get_local_time() - timedelta(days=days) elif hours: d = TimeUtils.get_local_time() - timedelta(hours=hours) jobs = models.Job.objects.filter(active=True, ready=True, status=models.JobStatus.NOT_STARTED, created__lt=d) if client_runner_user: if ":" not in client_runner_user: raise CommandError("Invalid format for username: %s" % client_runner_user) host, username = client_runner_user.split(":") git_server = models.GitServer.objects.get(name=host) git_user = models.GitUser.objects.get(name=username, server=git_server) jobs = jobs.filter( (Q(recipe__client_runner_user=None) & Q(recipe__build_user__build_key=git_user.build_key)) | Q(recipe__client_runner_user__build_key=git_user.build_key)) count = jobs.count() prefix = "" if dryrun: prefix = "DRY RUN: " if allowed_fail: err_msg = "Set to allowed to fail due to civet client not running this job in too long a time" status = models.JobStatus.FAILED_OK msg = "Job allowed to fail" else: err_msg = "Canceled due to civet client not running this job in too long a time" status = models.JobStatus.CANCELED msg = "Job canceled" for job in jobs.all(): self.stdout.write("%s%s: %s: %s: %s" % (prefix, msg, job.pk, job, job.created)) if not dryrun: views.set_job_canceled(job, err_msg, status) UpdateRemoteStatus.job_complete(job) job.event.set_complete_if_done() if count == 0: self.stdout.write("No jobs to cancel")
def test_set_passed(self): result = utils.create_step_result() result.save() context = {} start = (TimeUtils.get_local_time() - datetime.timedelta(days=1)).replace(hour=0, minute=0) bins = Stats.get_bins(start, datetime.timedelta(days=1)) p = Stats.set_passed(start, "day", "Passed tests in last 6 months, by day", context, "month_chart", "%m/%d", bins) # no models.JobTestStatistics records for j in p[1:]: self.assertEqual(j[1], 0) self.assertIn("month_chart", context) context = {} models.JobTestStatistics.objects.create(job=result.job, passed=20, skipped=30, failed=40) p = Stats.set_passed(start, "day", "Passed tests in last 6 months, by day", context, "month_chart", "%m/%d", bins) self.assertNotEqual(context, {}) self.assertEqual(len(p), 3) self.assertEqual(p[2][1], 20) self.assertIn("month_chart", context)
def get_bins(start_date, step): bins = [start_date] now = TimeUtils.get_local_time().replace(hour=23, minute=59) prev = start_date while True: new = prev + step if new < now: bins.append(new) prev = new else: break return bins
def num_prs_by_repo(request): context = {} repos_q = models.Repository.objects.filter(active=True).order_by("name").values("id", "name").all() repo_map = { v.get("id"): v.get("name") for v in repos_q } start = (TimeUtils.get_local_time() - datetime.timedelta(days=180)).replace(hour=0, minute=0) bins = get_bins(start, datetime.timedelta(days=7)) set_all_repo_prs(repos_q, start, "week", "Number of new PRs in last 6 months, by week", context, "%m/%d", bins) start = (TimeUtils.get_local_time() - datetime.timedelta(days=7)).replace(hour=0, minute=0) bins = get_bins(start, datetime.timedelta(days=1)) set_all_repo_prs(repos_q, start, "day", "Number of new PRs in last week, by day", context, "%m/%d", bins) sorted_repos_by_name = sorted(list(repo_map.keys()), key=lambda v: repo_map[v].lower()) repo_data = [] for key in sorted_repos_by_name: repo_graphs = context.get("repo_graphs", {}).get(key, []) if repo_graphs: repo_data.append({"id": key, "name": repo_map[key], "graphs": repo_graphs}) context["repos"] = repo_data return render(request, 'ci/num_prs.html', context)
def test_cancel_old_jobs(self, mock_post, mock_get): out = StringIO() with self.assertRaises(CommandError): management.call_command("cancel_old_jobs", stdout=out) out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--dryrun", "--days", "1", stdout=out) self.compare_counts() self.assertIn("No jobs to cancel", out.getvalue()) j = utils.create_job() created = TimeUtils.get_local_time() - timedelta(days=2) utils.update_job(j, ready=True, active=True, status=models.JobStatus.NOT_STARTED, created=created, complete=False) # Make sure dryrun doesn't change anything out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--dryrun", "--days", "1", stdout=out) self.compare_counts() self.assertIn(str(j), out.getvalue()) j.refresh_from_db() self.assertEqual(j.status, models.JobStatus.NOT_STARTED) # Should update the job and event status out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--days", "1", stdout=out) self.compare_counts(active_branches=1, canceled=1, events_canceled=1, num_changelog=1, num_events_completed=1, num_jobs_completed=1) self.assertIn(str(j), out.getvalue()) j.refresh_from_db() j.event.refresh_from_db() self.assertTrue(j.complete) self.assertEqual(j.status, models.JobStatus.CANCELED) self.assertEqual(j.event.status, models.JobStatus.CANCELED) self.assertTrue(j.event.complete) # Should not change anything since it isn't old enough utils.update_job(j, status=models.JobStatus.NOT_STARTED, complete=False) out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--days", "3", stdout=out) self.compare_counts() self.assertIn("No jobs to cancel", out.getvalue()) self.assertNotIn(str(j), out.getvalue()) j.refresh_from_db() self.assertEqual(j.status, models.JobStatus.NOT_STARTED) # Should update the job and event status created = TimeUtils.get_local_time() - timedelta(hours=2) utils.update_job(j, status=models.JobStatus.NOT_STARTED, complete=False, created=created) out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--hours", "1", stdout=out) self.compare_counts(canceled=1, num_changelog=1, num_jobs_completed=1) self.assertIn(str(j), out.getvalue()) j.refresh_from_db() self.assertEqual(j.status, models.JobStatus.CANCELED) # Should not change anything since it isn't old enough utils.update_job(j, status=models.JobStatus.NOT_STARTED, complete=False, created=created) out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--hours", "3", stdout=out) self.compare_counts() self.assertIn("No jobs to cancel", out.getvalue()) self.assertNotIn(str(j), out.getvalue()) j.refresh_from_db() self.assertEqual(j.status, models.JobStatus.NOT_STARTED) # Make sure setting allowed to fail works utils.update_job(j, status=models.JobStatus.NOT_STARTED, complete=False, created=created) out = StringIO() self.set_counts() management.call_command("cancel_old_jobs", "--hours", "1", "--allowed-fail", stdout=out) self.compare_counts(events_canceled=-1, num_changelog=1, num_jobs_completed=1) self.assertIn(str(j), out.getvalue()) j.refresh_from_db() self.assertEqual(j.status, models.JobStatus.FAILED_OK) # Check the --client-runner-user option only accepts <host>:<user> syntax utils.update_job(j, status=models.JobStatus.NOT_STARTED, complete=False, created=created) out = StringIO() self.set_counts() with self.assertRaises(CommandError): management.call_command("cancel_old_jobs", "--hours", "1", '--client-runner-user', 'foo', stdout=out) self.compare_counts() # Valid --client-runner-user self.set_counts() management.call_command( "cancel_old_jobs", "--hours", "1", '--client-runner-user', "%s:%s" % (j.recipe.build_user.server.name, j.recipe.build_user.name), stdout=out) self.compare_counts(canceled=1, num_changelog=1, num_jobs_completed=1, events_canceled=1) # --client-runner-user with no jobs utils.update_job(j, status=models.JobStatus.NOT_STARTED, complete=False, created=created) other_user = utils.create_user(name="other_user") self.set_counts() management.call_command("cancel_old_jobs", "--hours", "1", '--client-runner-user', "%s:%s" % (other_user.server.name, other_user.name), stdout=out) self.compare_counts()