Example #1
0
def test_width():
    fig = Figure()

    assert fig.width > 0

    fig.width = 40
    assert fig.width == 40

    with pytest.raises(ValueError):
        fig.width = 0

    with pytest.raises(ValueError):
        fig.width = -10

    with pytest.raises(ValueError):
        fig.width = '40'

    res = fig.show().split('\n')
    assert all(len(r) > 40 and len(r) < 55 for r in res[2:-2])
def create_chart(scriptpath, args):
    timerange = str(args.dayrange)
    interval = ""
    filename = scriptpath + timerange + "range_" + interval + "interval.json"
    urllib3.disable_warnings()
    if not path.isfile(filename) or (
            time() - path.getmtime(filename)) / 60 > 15 or args.force:
        url = "https://bitcoincharts.com/charts/chart.json?m=bitstampUSD&SubmitButton=Draw&r={0}&i={1}"\
            .format(timerange, interval)
        with open(filename, "w") as file:
            dump(requests.get(url, verify=False).json(), file)
    with open(filename, "r") as file:
        response = load(file)

    zipped = list(zip(*response))
    timestamps = zipped[0]
    weighted = zipped[7]
    openprice = weighted[0]
    closeprice = weighted[-1]
    change = (closeprice - openprice) / openprice
    if change > 0:
        color = custom_text_color((38, 200, 0))
    else:
        color = custom_text_color((255, 43, 0))
    print(f'Current price: ${closeprice:.2f}, {color}{change:.2%}{ENDC}')
    if args.log:
        weighted = [log(i) for i in weighted]

    fig = Figure()
    fig.width = int(args.width)
    fig.height = int(args.height)
    fig.x_axis_round = 2
    fig.set_x_limits(min_=min(timestamps), max_=max(timestamps))
    fig.set_y_limits(min_=min(weighted), max_=max(weighted))
    fig.y_label = "USD/BTC"
    fig.color_mode = 'byte'
    fig.y_axis_transform = lambda x: "${:,.2f}".format(x)
    if args.log:
        fig.y_axis_transform = lambda x: "${:,.2f}".format(exp(x))
    fig.x_axis_transform = lambda x: '{:%m-%d-%y}'.format(
        datetime.fromtimestamp(x))
    fig.plot(timestamps, weighted, lc=2, label="Bitcoin price")

    print(fig.show(legend=True))
    print('Data provided by bitcoincharts [{0}]'.format(
        "http://bitcoincharts.com/"))
    print('Last Updated {:.2} minutes ago'.format(
        (time() - path.getmtime(filename)) / 60))
Example #3
0
    def plot_ram(self, show_legend=False) -> str:
        """
        Creates a str with a histogram that can be printed to the command line.

        :param show_legend: Shows a legend
        :return: str
        """
        ram = [
            ram.size_update / 1000  # convert to MB
            for ram in self.image_size_events
        ]
        dates = [ram.time_stamp for ram in self.image_size_events]
        if len(ram) == 0:
            return ""  # No memory usage detected

        if len(ram) == 1:
            return str(f"Single memory update found:\n"
                       f"Memory usage on the {dates[0]} "
                       f"was updated to {ram[0]} MB\n")

        # else
        fig = Figure()
        fig.y_ticks_fkt = lambda x, y: self.mean_y_value(ram, x, y)
        fig.width = 55
        fig.height = 15
        fig.set_x_limits(min_=min(dates))
        min_ram = int(min(ram))  # raises error if not casted
        fig.set_y_limits(min_=min_ram)
        fig.y_label = "Usage [MB]"
        fig.x_label = "Time"

        # this will use the self written function _
        # num_formatter, to convert the y-label to int values
        fig.register_label_formatter(float, _int_formatter)
        fig.plot(dates, ram, lc='green', label="Continuous Graph")
        fig.scatter(dates, ram, lc='red', label="Single Values")

        return fig.show(legend=show_legend)
Example #4
0
def test_float_converter():
    fig = Figure()
    fig.with_colors = False
    fig.width = 30
    fig.height = 15

    def _converter(v):
        return float(v) - 1

    fig.register_float_converter(float, _converter)
    fig.register_float_converter(int, _converter)

    fig.plot(list(range(100)), [i % 20 for i in range(100)])

    # axis are at the wrong position
    expected = """\
   (Y)     ^
20.9000000 |
19.3800000 | ⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
17.8600000 | ⠀⠀⠀⡇⠀⠀⠀⣼⠀⠀⠀⠀⣼⠀⠀⠀⠀⣼⠀⠀⠀⠀⣼⠀⠀⠀⠀⡜⠀⠀
16.3400000 | ⠀⠀⠀⡇⠀⠀⢀⢿⠀⠀⠀⢀⢿⠀⠀⠀⢀⢿⠀⠀⠀⢀⢿⠀⠀⠀⢀⠇⠀⠀
14.8200000 | ⠀⠀⠀⡇⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⠀⠀⠀
13.3000000 | ⠀⠀⠀⡇⠀⠀⡇⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⠀⠀⠀
11.7800000 | ⠀⠀⠀⡇⠀⢰⠁⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⠀⠀⠀
10.2600000 | ⠀⠀⠀⡇⠀⡸⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⠀⠀⠀
8.74000000 | ⠀⠀⠀⡇⠀⡇⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⠀⠀⠀
7.22000000 | ⠀⠀⠀⡇⢸⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⠀⠀⠀
5.70000000 | ⠀⠀⠀⡇⡎⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⠀⠀⠀
4.18000000 | ⠀⠀⠀⣧⠃⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⠀⠀⠀
2.66000000 | ⠀⠀⠀⣿⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⠀⠀⠀
1.14000000 | ⣀⣀⣀⣇⣀⣀⣀⣸⣇⣀⣀⣀⣸⣇⣀⣀⣀⣸⣇⣀⣀⣀⣸⣇⣀⣀⣀⣀⣀⣀
-0.3800000 | ⠀⠀⠰⡇⠀⠀⠀⠸⠁⠀⠀⠀⠸⠁⠀⠀⠀⠸⠁⠀⠀⠀⠸⠁⠀⠀⠀⠀⠀⠀
-1.9000000 | ⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
-----------|-|---------|---------|---------|-> (X)
           | -9.900000 29.700000 69.300000 108.90000"""
    print(fig.show())
    assert expected == fig.show()
Example #5
0
def test_lbl_formatter():
    fig = Figure()
    fig.with_colors = False
    fig.width = 30
    fig.height = 15

    def _num_formatter(val, chars, delta, left=False):
        align = '<' if left else ''
        return '{:{}{}d}'.format(int(val), align, chars)

    fig.register_label_formatter(float, _num_formatter)
    fig.register_label_formatter(int, _num_formatter)

    fig.plot(list(range(100)), [i % 20 for i in range(100)])

    expected = """\
   (Y)     ^
        20 |
        19 | ⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
        17 | ⠀⠀⢸⠀⠀⠀⠀⣼⠀⠀⠀⠀⣼⠀⠀⠀⠀⣼⠀⠀⠀⠀⣼⠀⠀⠀⠀⡜⠀⠀
        16 | ⠀⠀⢸⠀⠀⠀⢀⢿⠀⠀⠀⢀⢿⠀⠀⠀⢀⢿⠀⠀⠀⢀⢿⠀⠀⠀⢀⠇⠀⠀
        14 | ⠀⠀⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⠀⠀⠀
        13 | ⠀⠀⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⢸⠀⠀⠀⡇⠀⠀⠀
        11 | ⠀⠀⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⢸⠀⠀⢰⠁⠀⠀⠀
        10 | ⠀⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⢸⠀⠀⡸⠀⠀⠀⠀
         8 | ⠀⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⢸⠀⠀⡇⠀⠀⠀⠀
         7 | ⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⢸⠀⢸⠀⠀⠀⠀⠀
         5 | ⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⢸⠀⡎⠀⠀⠀⠀⠀
         4 | ⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⢸⢠⠃⠀⠀⠀⠀⠀
         2 | ⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⢸⢸⠀⠀⠀⠀⠀⠀
         1 | ⠀⠀⢸⡇⠀⠀⠀⢸⡇⠀⠀⠀⢸⡇⠀⠀⠀⢸⡇⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀
         0 | ⠤⠤⢼⠥⠤⠤⠤⠼⠥⠤⠤⠤⠼⠥⠤⠤⠤⠼⠥⠤⠤⠤⠼⠥⠤⠤⠤⠤⠤⠤
        -1 | ⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
-----------|-|---------|---------|---------|-> (X)
           | -9        29        69        108      """
    print(fig.show())
    assert expected == fig.show()
Example #6
0
    def analyze(self, log_files: list_of_logs) -> log_inf_list:
        """
        Analyze the given log files one by one.

        :param log_files: list of valid HTCondor log files
        :return: list with information of each log file
        """
        logging.info('Starting the analyze mode')

        if not log_files:
            raise_value_error("No files to analyze")

        result_list = list()

        # create progressbar, do not redirect output
        with Progress(transient=True, redirect_stdout=False,
                      redirect_stderr=False) as progress:

            task = progress.add_task("Analysing...", total=len(log_files))

            for file in log_files:
                progress.update(task, advance=1)
                result_dict = dict()

                logging.debug(f"Analysing the HTCondor log file: {file}")
                msg = f"[green]Job analysis of: {file}[/green]"
                result_dict["description"] = msg

                job_dict, res_dict, time_dict, \
                    ram_history, occurred_errors = self.log_to_dict(file)
                if job_dict:
                    result_dict["execution-details"] = job_dict

                if time_dict:
                    result_dict["times"] = time_dict

                if res_dict:
                    result_dict["all-resources"] = \
                        self.manage_thresholds(res_dict)

                # show HTCondor errors
                if occurred_errors:
                    result_dict["errors"] = occurred_errors

                # managing the ram history
                if ram_history:
                    ram = np.array(ram_history.get('Image size updates'))
                    dates = np.array(ram_history.get('Dates'))

                if ram_history and len(ram) > 1:
                    fig = Figure()
                    fig.width = 55
                    fig.height = 15
                    fig.set_x_limits(min_=min(dates))
                    min_ram = int(min(ram))  # raises error if not casted
                    fig.set_y_limits(min_=min_ram)
                    fig.y_label = "Usage"
                    fig.x_label = "Time"

                    # this will use the self written function _
                    # num_formatter, to convert the y-label to int values
                    fig.register_label_formatter(float, _int_formatter)
                    fig.plot(dates, ram, lc='green', label="Continuous Graph")
                    fig.scatter(dates, ram, lc='red', label="Single Values")

                    result_dict["ram-history"] = fig.show(
                        legend=self.show_legend)
                elif ram_history:
                    msg = f"Single memory update found:\n" \
                        f"Memory usage on the {dates[0]} " \
                        f"was updatet to {ram[0]} MB"
                    result_dict["ram-history"] = msg

                if self.show_list:
                    job_spec_id = self.get_job_spec_id(file)
                    if 'htc-err' in self.show_list:
                        result_dict['htc-err'] = self.htcondor_stderr(
                            job_spec_id + self.ext_err)
                    if 'htc-out' in self.show_list:
                        result_dict['htc-out'] = self.htcondor_stdout(
                            job_spec_id + self.ext_out)

                result_list.append(result_dict)

        return result_list