# Buggy code
def ionosphere_metric_data(requested_timestamp, data_for_metric, context):
    """
    Get a list of all training data folders and metrics
    """
    base_name = data_for_metric.replace(settings.FULL_NAMESPACE, "", 1)
    if context == "training_data":
        log_context = "training data"
    if context == "features_profiles":
        log_context = "features profile data"
    logger.info(
        "%s requested for %s at %s"
        % (context, str(base_name), str(requested_timestamp))
    )
    metric_paths = []
    images = []
    timeseries_dir = base_name.replace(".", "/")
    if context == "training_data":
        metric_data_dir = "%s/%s/%s" % (
            settings.IONOSPHERE_DATA_FOLDER,
            str(requested_timestamp),
            timeseries_dir,
        )
    if context == "features_profiles":
        metric_data_dir = "%s/%s/%s" % (
            settings.IONOSPHERE_PROFILES_FOLDER,
            timeseries_dir,
            str(requested_timestamp),
        )
    human_date = time.strftime(
        "%Y-%m-%d %H:%M:%S %Z (%A)", time.localtime(int(requested_timestamp))
    )
    metric_var_filename = "%s.txt" % str(base_name)
    metric_vars_file = False
    ts_json_filename = "%s.json" % str(base_name)
    ts_json_file = "none"
    td_files = listdir(metric_data_dir)
    for i_file in td_files:
        metric_file = path.join(metric_data_dir, i_file)
        metric_paths.append([i_file, metric_file])
        if i_file.endswith(".png"):
            images.append(str(metric_file))
        if i_file == metric_var_filename:
            metric_vars_file = str(metric_file)
        if i_file == ts_json_filename:
            ts_json_file = str(metric_file)

    metric_vars_ok = False
    metric_vars = ["error: could not read metrics vars file", metric_vars_file]
    if path.isfile(metric_vars_file):
        try:
            metric_vars = []
            with open(metric_vars_file) as f:
                for line in f:
                    add_line = line.replace("\n", "")
                    metric_vars.append(add_line)

            metric_vars_ok = True
        except:
            metric_vars_ok = False

    ts_json_ok = False
    ts_json = ["error: could not timeseries json file", ts_json_file]
    if path.isfile(ts_json_file):
        try:
            ts_json = []
            with open(ts_json_file) as f:
                for line in f:
                    ts_json.append(line)

            ts_json_ok = True
        except:
            ts_json_ok = False

    data_to_process = False
    if metric_vars_ok and ts_json_ok:
        data_to_process = True
    panorama_anomaly_id = False
    url = (
        "%s/panorama?metric=%s&from_timestamp=%s&until_timestamp=%s&panorama_anomaly_id=true"
        % (
            settings.SKYLINE_URL,
            str(base_name),
            str(requested_timestamp),
            str(requested_timestamp),
        )
    )
    panorama_resp = None
    logger.info("getting anomaly id from panorama: %s" % str(url))
    if settings.WEBAPP_AUTH_ENABLED:
        user = str(settings.WEBAPP_AUTH_USER)
        password = str(settings.WEBAPP_AUTH_USER_PASSWORD)
    try:
        if settings.WEBAPP_AUTH_ENABLED:
            r = requests.get(url, timeout=2, auth=(user, password))
        else:
            r = requests.get(url, timeout=2)
        panorama_resp = True
    except:
        logger.error(traceback.format_exc())
        logger.error("error :: failed to get anomaly id from panorama: %s" % str(url))

    if panorama_resp:
        try:
            data = literal_eval(r.text)
            if str(data) == "[]":
                panorama_anomaly_id = None
                logger.debug("debug :: panorama anomlay data: %s" % str(data))
            else:
                panorama_anomaly_id = int(data[0][0])
                logger.debug("debug :: panorama anomlay data: %s" % str(data))
        except:
            logger.error(traceback.format_exc())
            logger.error(
                "error :: failed to get anomaly id from panorama response: %s"
                % str(r.text)
            )

    return (
        metric_paths,
        images,
        human_date,
        metric_vars,
        ts_json,
        data_to_process,
        panorama_anomaly_id,
    )


# Traceback (AttributeError)
2016-08-22 16:42:05 :: 7874 :: Traceback (most recent call last):
File "/opt/skyline/github/skyline/skyline/panorama/panorama.py", line 297, in spin_process
metric_vars.from_timestamp
AttributeError: 'module' object has no attribute 'from_timestamp'

2016-08-22 16:42:05 :: 7874 :: error :: failed to read from_timestamp variable from check file - /opt/skyline/panaroma/check/1471884121.stats.statsd.graphiteStats.flush_length.txt

# Fixed code
def ionosphere_metric_data(requested_timestamp, data_for_metric, context):
    """
    Get a list of all training data folders and metrics
    """

    # @added 20170104 - Feature #1842: Ionosphere - Graphite now graphs
    #                   Feature #1830: Ionosphere alerts
    # Use the new_load_metric_vars method
    def new_load_metric_vars(metric_vars_file):
        """
        Load the metric variables for a check from a metric check variables file

        :param metric_vars_file: the path and filename to the metric variables files
        :type metric_vars_file: str
        :return: the metric_vars module object or ``False``
        :rtype: list

        """
        if path.isfile(metric_vars_file):
            logger.info(
                "loading metric variables from metric_check_file - %s"
                % (str(metric_vars_file))
            )
        else:
            logger.error(
                "error :: loading metric variables from metric_check_file - file not found - %s"
                % (str(metric_vars_file))
            )
            return False

        metric_vars = []
        with open(metric_vars_file) as f:
            for line in f:
                no_new_line = line.replace("\n", "")
                no_equal_line = no_new_line.replace(" = ", ",")
                array = str(no_equal_line.split(",", 1))
                add_line = literal_eval(array)
                metric_vars.append(add_line)

        string_keys = ["metric", "anomaly_dir", "added_by", "app", "source"]
        float_keys = ["value"]
        int_keys = ["from_timestamp", "metric_timestamp", "added_at", "full_duration"]
        array_keys = ["algorithms", "triggered_algorithms"]
        boolean_keys = ["graphite_metric", "run_crucible_tests"]

        metric_vars_array = []
        for var_array in metric_vars:
            key = None
            value = None
            if var_array[0] in string_keys:
                key = var_array[0]
                value_str = str(var_array[1]).replace("'", "")
                value = str(value_str)
                if var_array[0] == "metric":
                    metric = value
            if var_array[0] in float_keys:
                key = var_array[0]
                value_str = str(var_array[1]).replace("'", "")
                value = float(value_str)
            if var_array[0] in int_keys:
                key = var_array[0]
                value_str = str(var_array[1]).replace("'", "")
                value = int(value_str)
            if var_array[0] in array_keys:
                key = var_array[0]
                value = literal_eval(str(var_array[1]))
            if var_array[0] in boolean_keys:
                key = var_array[0]
                if str(var_array[1]) == "True":
                    value = True
                else:
                    value = False
            if key:
                metric_vars_array.append([key, value])

            if len(metric_vars_array) == 0:
                logger.error(
                    "error :: loading metric variables - none found"
                    % (str(metric_vars_file))
                )
                return False

            if settings.ENABLE_DEBUG:
                logger.info(
                    "debug :: metric_vars determined - metric variable - metric - %s"
                    % str(metric_vars.metric)
                )

        logger.info("debug :: metric_vars for %s" % str(metric))
        logger.info("debug :: %s" % str(metric_vars_array))

        return metric_vars_array

    base_name = data_for_metric.replace(settings.FULL_NAMESPACE, "", 1)
    if context == "training_data":
        log_context = "training data"
    if context == "features_profiles":
        log_context = "features profile data"
    logger.info(
        "%s requested for %s at %s"
        % (context, str(base_name), str(requested_timestamp))
    )
    metric_paths = []
    images = []
    timeseries_dir = base_name.replace(".", "/")
    if context == "training_data":
        metric_data_dir = "%s/%s/%s" % (
            settings.IONOSPHERE_DATA_FOLDER,
            str(requested_timestamp),
            timeseries_dir,
        )
    if context == "features_profiles":
        metric_data_dir = "%s/%s/%s" % (
            settings.IONOSPHERE_PROFILES_FOLDER,
            timeseries_dir,
            str(requested_timestamp),
        )
    human_date = time.strftime(
        "%Y-%m-%d %H:%M:%S %Z (%A)", time.localtime(int(requested_timestamp))
    )
    metric_var_filename = "%s.txt" % str(base_name)
    metric_vars_file = False
    ts_json_filename = "%s.json" % str(base_name)
    ts_json_file = "none"
    td_files = listdir(metric_data_dir)
    for i_file in td_files:
        metric_file = path.join(metric_data_dir, i_file)
        metric_paths.append([i_file, metric_file])
        if i_file.endswith(".png"):
            images.append(str(metric_file))
        if i_file == metric_var_filename:
            metric_vars_file = str(metric_file)
        if i_file == ts_json_filename:
            ts_json_file = str(metric_file)

    metric_vars_ok = False
    metric_vars = ["error: could not read metrics vars file", metric_vars_file]
    if path.isfile(metric_vars_file):
        try:
            # @modified 20170104 - Feature #1842: Ionosphere - Graphite now graphs
            #                      Feature #1830: Ionosphere alerts
            # Use the new_load_metric_vars method
            # metric_vars = []
            # with open(metric_vars_file) as f:
            #     for line in f:
            #         add_line = line.replace('\n', '')
            #         metric_vars.append(add_line)
            metric_vars = new_load_metric_vars(metric_vars_file)
            metric_vars_ok = True
        except:
            metric_vars_ok = False
            logger.error(traceback.format_exc())
            logger.error(
                "error :: failed to load metric_vars from: %s" % str(metric_vars_file)
            )

    ts_json_ok = False
    ts_json = ["error: could not timeseries json file", ts_json_file]
    if path.isfile(ts_json_file):
        try:
            ts_json = []
            with open(ts_json_file) as f:
                for line in f:
                    ts_json.append(line)

            ts_json_ok = True
        except:
            ts_json_ok = False

    data_to_process = False
    if metric_vars_ok and ts_json_ok:
        data_to_process = True
    panorama_anomaly_id = False
    url = (
        "%s/panorama?metric=%s&from_timestamp=%s&until_timestamp=%s&panorama_anomaly_id=true"
        % (
            settings.SKYLINE_URL,
            str(base_name),
            str(requested_timestamp),
            str(requested_timestamp),
        )
    )
    panorama_resp = None
    logger.info("getting anomaly id from panorama: %s" % str(url))
    if settings.WEBAPP_AUTH_ENABLED:
        user = str(settings.WEBAPP_AUTH_USER)
        password = str(settings.WEBAPP_AUTH_USER_PASSWORD)
    try:
        if settings.WEBAPP_AUTH_ENABLED:
            r = requests.get(url, timeout=2, auth=(user, password))
        else:
            r = requests.get(url, timeout=2)
        panorama_resp = True
    except:
        logger.error(traceback.format_exc())
        logger.error("error :: failed to get anomaly id from panorama: %s" % str(url))

    if panorama_resp:
        try:
            data = literal_eval(r.text)
            if str(data) == "[]":
                panorama_anomaly_id = None
                logger.debug("debug :: panorama anomlay data: %s" % str(data))
            else:
                panorama_anomaly_id = int(data[0][0])
                logger.debug("debug :: panorama anomlay data: %s" % str(data))
        except:
            logger.error(traceback.format_exc())
            logger.error(
                "error :: failed to get anomaly id from panorama response: %s"
                % str(r.text)
            )

    return (
        metric_paths,
        images,
        human_date,
        metric_vars,
        ts_json,
        data_to_process,
        panorama_anomaly_id,
    )