def test_get_percentage_works_as_expected(self): float_nums = [ (0.123, "12.30%"), (3.1243453, "312.43%"), (213.12312, "21,312.31%"), ] int_nums = [(0.14, "14%"), (1.300, "130%")] for num, expected in float_nums: self.assertEqual( to_percentage(num, rounding=2, precision=2, use_comma=True), expected) for num, expected in int_nums: self.assertEqual( to_percentage(num, rounding=2, precision=2, use_comma=True), expected)
def humanize_attrs(cls, obj): humanized_attrs = {} for attr in cls.DATETIME_ATTRIBUTES: humanized_attrs[attr] = humanize_timesince(getattr(obj, attr)) for attr in cls.PERCENT_ATTRIBUTES: humanized_attrs[attr] = to_percentage(getattr(obj, attr), cls.ROUNDING) for attr in cls.MEM_SIZE_ATTRIBUTES: humanized_attrs[attr] = to_unit_memory(getattr(obj, attr)) return humanized_attrs
def humanize_attrs(key, value, rounding=2): if key in [ "created_at", "updated_at", "started_at", "finished_at", "schedule_at", "last_update_time", "last_transition_time", ]: return humanize_timesince(value) if key in ["cpu_percentage"]: return to_percentage(value, rounding) if key in ["memory_free", "memory_used", "memory_total"]: return to_unit_memory(value) return value
def test_works_as_expected_for_valid_values(self): test_data = [ (0, "0%"), (0.25, "25%"), (-0.25, "-25%"), (12, "1200%"), (0.123, "12.3%"), (0.12345, "12.35%"), (0.12001, "12%"), (0.12101, "12.1%"), ("0", "0%"), ("0.25", "25%"), (3.1243453, "312.43%"), (213.12312, "21312.31%"), (0.14, "14%"), (1.300, "130%"), ] for value, expected in test_data: result = to_percentage(value) self.assertEqual(result, expected)
def resources(cls, jobs_resources): jobs_resources = to_list(jobs_resources) click.clear() data = [["Job", "Mem Usage / Total", "CPU% - CPUs"]] for job_resources in jobs_resources: job_resources = ContainerResourcesConfig.from_dict(job_resources) line = [ job_resources.job_name, "{} / {}".format( to_unit_memory(job_resources.memory_used), to_unit_memory(job_resources.memory_limit), ), "{} - {}".format( to_percentage(job_resources.cpu_percentage / 100), job_resources.n_cpus, ), ] data.append(line) click.echo(tabulate(data, headers="firstrow")) sys.stdout.flush()
def gpu_resources(cls, jobs_resources): jobs_resources = to_list(jobs_resources) click.clear() data = [[ "job_name", "name", "GPU Usage", "GPU Mem Usage / Total", "GPU Temperature", "Power Draw / Limit", ]] non_gpu_jobs = 0 for job_resources in jobs_resources: job_resources = ContainerResourcesConfig.from_dict(job_resources) line = [] if not job_resources.gpu_resources: non_gpu_jobs += 1 continue for gpu_resources in job_resources.gpu_resources: line += [ job_resources.job_name, gpu_resources.name, to_percentage(gpu_resources.utilization_gpu / 100), "{} / {}".format( to_unit_memory(gpu_resources.memory_used), to_unit_memory(gpu_resources.memory_total), ), gpu_resources.temperature_gpu, "{} / {}".format(gpu_resources.power_draw, gpu_resources.power_limit), ] data.append(line) if non_gpu_jobs == len(jobs_resources): Printer.print_error( "No GPU job was found, please run `resources` command without `-g | --gpu` option." ) exit(1) click.echo(tabulate(data, headers="firstrow")) sys.stdout.flush()
def test_works_as_expected_for_precision(self): test_data = [ (0, "0%"), (0.25, "25%"), (-0.25, "-25%"), (12, "1200%"), (0.123, "12.300%"), (0.12345, "12.345%"), (0.12001, "12.001%"), (0.12101, "12.101%"), ("0", "0%"), ("0.25", "25%"), (3.1243453, "312.435%"), (213.12312, "21,312.312%"), (0.14, "14%"), (1.300, "130%"), ] for value, expected in test_data: result = to_percentage(value, rounding=3, precision=3, use_comma=True) self.assertEqual(result, expected)
def test_raises_value_error_for_invalid_types(self): with self.assertRaises(ValueError): to_percentage("foo")