# Buggy code
def workflow_resources(workflow_id):
    workflow_details = Workflow.query.filter_by(run_id=workflow_id).first()
    if workflow_details is None:
        return render_template(
            "error.html", message="Workflow %s could not be found" % workflow_id
        )

    df_resources = pd.read_sql_query(
        "SELECT * FROM resource WHERE run_id='%s'" % (workflow_id), db.engine
    )
    df_task = pd.read_sql_query(
        "SELECT * FROM task WHERE run_id='%s'" % (workflow_id), db.engine
    )

    df_task_resources = pd.read_sql_query(
        """
                                          SELECT task_id, timestamp, resource_monitoring_interval,
                                          psutil_process_cpu_percent, psutil_process_time_user,
                                          psutil_process_memory_percent, psutil_process_memory_resident
                                          from resource
                                          where run_id = '%s'
                                          """
        % (workflow_id),
        db.engine,
    )

    return render_template(
        "resource_usage.html",
        workflow_details=workflow_details,
        user_time_distribution_avg_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_time_user",
            label="CPU Time Distribution",
            option="avg",
        ),
        user_time_distribution_max_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_time_user",
            label="CPU Time Distribution",
            option="max",
        ),
        memory_usage_distribution_avg_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_memory_resident",
            label="Memory Distribution",
            option="avg",
        ),
        memory_usage_distribution_max_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_memory_resident",
            label="Memory Distribution",
            option="max",
        ),
        user_time_time_series=resource_time_series(
            df_task_resources, type="psutil_process_time_user", label="CPU User Time"
        ),
        cpu_percent_time_series=resource_time_series(
            df_task_resources,
            type="psutil_process_cpu_percent",
            label="CPU Utilization",
        ),
        memory_percent_time_series=resource_time_series(
            df_task_resources,
            type="psutil_process_memory_percent",
            label="Memory Utilization",
        ),
        memory_resident_time_series=resource_time_series(
            df_task_resources,
            type="psutil_process_memory_resident",
            label="Memory Usage",
        ),
    )


# 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 workflow_resources(workflow_id):
    workflow_details = Workflow.query.filter_by(run_id=workflow_id).first()
    if workflow_details is None:
        return render_template(
            "error.html", message="Workflow %s could not be found" % workflow_id
        )

    df_resources = pd.read_sql_query(
        "SELECT * FROM resource WHERE run_id='%s'" % (workflow_id), db.engine
    )
    if df_resources.empty:
        return render_template(
            "error.html",
            message="Workflow %s does not have any resource usage records."
            % workflow_id,
        )

    df_task = pd.read_sql_query(
        "SELECT * FROM task WHERE run_id='%s'" % (workflow_id), db.engine
    )

    df_task_resources = pd.read_sql_query(
        """
                                          SELECT task_id, timestamp, resource_monitoring_interval,
                                          psutil_process_cpu_percent, psutil_process_time_user,
                                          psutil_process_memory_percent, psutil_process_memory_resident
                                          from resource
                                          where run_id = '%s'
                                          """
        % (workflow_id),
        db.engine,
    )

    return render_template(
        "resource_usage.html",
        workflow_details=workflow_details,
        user_time_distribution_avg_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_time_user",
            label="CPU Time Distribution",
            option="avg",
        ),
        user_time_distribution_max_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_time_user",
            label="CPU Time Distribution",
            option="max",
        ),
        memory_usage_distribution_avg_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_memory_resident",
            label="Memory Distribution",
            option="avg",
        ),
        memory_usage_distribution_max_plot=resource_distribution_plot(
            df_resources,
            df_task,
            type="psutil_process_memory_resident",
            label="Memory Distribution",
            option="max",
        ),
        user_time_time_series=resource_time_series(
            df_task_resources, type="psutil_process_time_user", label="CPU User Time"
        ),
        cpu_percent_time_series=resource_time_series(
            df_task_resources,
            type="psutil_process_cpu_percent",
            label="CPU Utilization",
        ),
        memory_percent_time_series=resource_time_series(
            df_task_resources,
            type="psutil_process_memory_percent",
            label="Memory Utilization",
        ),
        memory_resident_time_series=resource_time_series(
            df_task_resources,
            type="psutil_process_memory_resident",
            label="Memory Usage",
        ),
    )