# (C) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from oslo_log import log
import simplejson as json

from monasca_persister.repositories.influxdb import abstract_repository
from monasca_persister.repositories.influxdb import line_utils
from monasca_persister.repositories.utils import parse_alarm_state_hist_message

LOG = log.getLogger(__name__)


class AlarmStateHistInfluxdbRepository(
        abstract_repository.AbstractInfluxdbRepository):

    def __init__(self):

        super(AlarmStateHistInfluxdbRepository, self).__init__()

    def process_message(self, message):

        (alarm_id, metrics, new_state, old_state, link,
         lifecycle_state, state_change_reason,
         sub_alarms_json_snake_case, tenant_id,
         time_stamp) = parse_alarm_state_hist_message(
            message)

        name = 'alarm_state_history'
        fields = []
        fields.append('tenant_id=' + line_utils.escape_value(tenant_id))
        fields.append('alarm_id=' + line_utils.escape_value(alarm_id))
        fields.append('metrics=' + line_utils.escape_value(
            json.dumps(metrics, ensure_ascii=False)))
        fields.append('new_state=' + line_utils.escape_value(new_state))
        fields.append('old_state=' + line_utils.escape_value(old_state))
        fields.append('link=' + line_utils.escape_value(link))
        fields.append('lifecycle_state=' + line_utils.escape_value(
            lifecycle_state))
        fields.append('reason=' + line_utils.escape_value(
            state_change_reason))
        fields.append('reason_data=' + line_utils.escape_value("{}"))
        fields.append('sub_alarms=' + line_utils.escape_value(
            sub_alarms_json_snake_case))

        line = name + ',tenant_id=' + line_utils.escape_tag(tenant_id)
        line += ' ' + ','.join(fields)
        line += ' ' + str(int(time_stamp))

        LOG.debug(line)

        return line, tenant_id
