"""Custom functions for the skim_test."""
 
import os
import time
import math

import matplotlib
matplotlib.use('Agg')
import pandas as pd
import matplotlib.pyplot as plt

from ..web import AutoCMSWebpage
from ..stats import load_stats
from ..plot import (
    create_run_and_waittime_plot,
    convert_timestamp
)


def create_cmsrun_runtime_plot(joblist, filepath):
    """Create a scatterplot of runtimes with colors based on node occupancy.

    Plot of start times versus runtimes for listed jobs
    with the color of each point given by the number of
    cmsRun processes the node."""
    # make sure all job records have the cmsrun_proc_count attr
    records = [job for job in joblist if hasattr(job,'cmsrun_proc_count')]
    data = [(convert_timestamp(job.start_time), job.run_time(),
             float(job.cmsrun_proc_count)) for job in records]
    df = pd.DataFrame(data, columns=['start', 'run', 'nproc'])
    ax = df.plot(kind='scatter', figsize=(10, 4), x='start', y='run',
                 c=df['nproc'], s=75, cmap=matplotlib.cm.jet,
                 vmin=0, vmax=12)
    ax.set_xlabel('Job Start Time')
    ax.set_ylabel('Wall Clock Time [seconds]')
    ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval=4))
    ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%a\n%H:%M'))
    # pad the y-axis max a bit, force y-axis min to zero (1 if log scaled)
    x1, x2, y1, y2 = ax.axis()