# -*- coding: utf-8 -*-
# stdlib
import time

# project
import base_reporter


class DeployReporter(base_reporter.BaseReporter):
    """
    上报部署数据的类
    """
    def __init__(self, total_task, org, ip, uc_server):
        """
        初始化
        :param report_uri: 如/api/v1/report
        :param total_task: task总数
        :param org: org
        :param ip: 当前ip
        :param uc_server: uc服务器ip:端口
        :return:
        """
        base_reporter.BaseReporter.__init__(self, total_task, org, ip, uc_server)
        self._report_uri = "/api/v1/deploy/report"

    def finish_task(self, start_time, pkg_name, status, msg, data=None):
        with self._lock:
            self._current_task += 1
        event = self._format_event(start_time,pkg_name, status, msg, data)
        self._report(event)

    def report_no_task(self):
        event = self._format_event(int(time.time()), None, base_reporter.BaseReporter.STATUS_OK, "No task", {})
        event["current"] = 1
        event["total"] = 1
        self._report(event)

    def _format_event(self, start_time, pkg_name, status, msg, data):
        event = {
            "org": self._org,
            "ip": self._ip,
            "start_time": int(start_time),
            "end_time": int(time.time()),
            "pkg_name": pkg_name,
            "status": status,
            "msg": msg,
            "current": self._current_task,
            "total": self._total_task
        }
        return event
