# Buggy code
def resource_time_series(tasks, type="psutil_process_time_user", label="CPU user time"):
    tasks["epoch_time"] = (
        pd.to_datetime(tasks["timestamp"]) - pd.Timestamp("1970-01-01")
    ) // pd.Timedelta("1s")
    step = int(tasks["resource_monitoring_interval"][0])
    start = tasks["epoch_time"].min()
    end = tasks["epoch_time"].max()
    tasks["relative_time"] = tasks["epoch_time"] - start
    bins = pd.cut(
        tasks["relative_time"], range(0, end - start + 1, step), include_lowest=True
    )

    df = tasks.groupby(bins, as_index=False)[type].mean()
    df["time"] = step * df.index
    fig = go.Figure(
        data=[
            go.Scatter(
                x=df["time"],
                y=df[type],
            )
        ],
        layout=go.Layout(
            xaxis=dict(autorange=True, title="Time (seconds)"),
            yaxis=dict(title=label),
            title=label,
        ),
    )
    return plot(fig, show_link=False, output_type="div", include_plotlyjs=False)


# Traceback (ZeroDivisionError)
[2019-05-03 18:18:01,362] ERROR in app: Exception on /workflow/9294c39d-a64d-4ad5-9ff8-4b8d757c1af1/resource_usage [GET]
Traceback (most recent call last):
File "/Users/awoodard/software/miniconda3/envs/parsl/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/awoodard/software/miniconda3/envs/parsl/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/awoodard/software/miniconda3/envs/parsl/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/awoodard/software/miniconda3/envs/parsl/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/awoodard/software/miniconda3/envs/parsl/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/awoodard/software/miniconda3/envs/parsl/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/awoodard/ci/viz_server/viz_server/views.py", line 145, in workflow_resources
df_resources, df_task, type='psutil_process_time_user', label='CPU Time Distribution', option='avg'),
File "/Users/awoodard/ci/viz_server/viz_server/plots/default/workflow_resource_plots.py", line 15, in resource_distribution_plot
for i in np.arange(min_range, max_range + time_step, time_step):
ZeroDivisionError: float division by zero

# Fixed code
def resource_time_series(tasks, type="psutil_process_time_user", label="CPU user time"):
    tasks["epoch_time"] = (
        pd.to_datetime(tasks["timestamp"]) - pd.Timestamp("1970-01-01")
    ) // pd.Timedelta("1s")
    step = int(tasks["resource_monitoring_interval"][0])
    start = tasks["epoch_time"].min()
    end = tasks["epoch_time"].max()
    tasks["relative_time"] = tasks["epoch_time"] - start
    if end != start:
        bins = pd.cut(
            tasks["relative_time"], range(0, end - start + 1, step), include_lowest=True
        )
        df = tasks.groupby(bins, as_index=False)[type].mean()
        df["time"] = step * df.index
        fig = go.Figure(
            data=[
                go.Scatter(
                    x=df["time"],
                    y=df[type],
                )
            ],
            layout=go.Layout(
                xaxis=dict(autorange=True, title="Time (seconds)"),
                yaxis=dict(title=label),
                title=label,
            ),
        )
    else:
        fig = go.Figure(
            data=[
                go.Scatter(
                    x=[0],
                    y=[tasks[type].mean()],
                )
            ],
            layout=go.Layout(
                xaxis=dict(autorange=True, title="Time (seconds)"),
                yaxis=dict(title=label),
                title=label,
            ),
        )
    return plot(fig, show_link=False, output_type="div", include_plotlyjs=False)