コード例 #1
0
ファイル: ops_mode.py プロジェクト: Viq111/datadog-launchpad
    def __init__(self, datadog_conf_path, colunms_per_category=8):
        "Set up a ops mode to datadog"
        if colunms_per_category not in [1 ,2, 4, 8]:
            raise RuntimeError("colunms_per_category must be 1, 2, 4 or 8")
        self.col = colunms_per_category
        self.categories = [] # List of [category, nb_monitors]
        self.button_id = {} # Assign a button <-> monitor_id

        # Initialize datadog
        with open(datadog_conf_path, "r") as f:
            conf = json.loads(f.read())
        self.datadog = FastMonitor(**conf)

        ModeController.__init__(self)
コード例 #2
0
ファイル: ops_mode.py プロジェクト: Viq111/datadog-launchpad
class OpsMode(ModeController):
    def __init__(self, datadog_conf_path, colunms_per_category=8):
        "Set up a ops mode to datadog"
        if colunms_per_category not in [1 ,2, 4, 8]:
            raise RuntimeError("colunms_per_category must be 1, 2, 4 or 8")
        self.col = colunms_per_category
        self.categories = [] # List of [category, nb_monitors]
        self.button_id = {} # Assign a button <-> monitor_id

        # Initialize datadog
        with open(datadog_conf_path, "r") as f:
            conf = json.loads(f.read())
        self.datadog = FastMonitor(**conf)

        ModeController.__init__(self)

    def _max_nb_categories(self):
        return 64 / self.col

    def _button_to_launchpad_button(self, button):
        """
        The launchpad bottom left button is 11, going one column on the left is
        + 1 and going one row up is +10
        In our design, we use 0 on top left, 7 on top right and 63 on bottom right
        """
        row = button / 8
        col = button % 8
        return (8 - row) * 10 + col + 1

    def _assign_button(self, category):
        "Return the num of the button"
        # Add the category if it doesn't exist
        if category not in [c[0] for c in self.categories]:
            self.categories.append([category, 1])
            if len(self.categories) > self._max_nb_categories():
                raise RuntimeError("You already used the maximum number of categories")
            row = len(self.categories) - 1
            col = 0
        else: # Add it to the category
            for i, c in enumerate(self.categories):
                if category == c[0]:
                    c[1] += 1
                    if c[1] > self.col:
                        raise RuntimeError("You already used the maximum number of monitor in {}".format(category))
                    row = i
                    col = c[1] - 1
                    break
        return self._button_to_launchpad_button(row * 8 + col)

    def _status_to_light(self, status):
        "Return a LightColor and LightMode"
        status = status.lower()
        if status in STATUS_TO_LIGHT:
            return STATUS_TO_LIGHT[status]
        else:
            return (LightColor.GREY, LightMode.BLINK)

    def add_monitor(self, id, category):
        "Assign a new monitor by its id to a category"
        button = self._assign_button(category)
        self.button_id[button] = id

    def notify_action(self, button):
        "Notified when you click on a button"
        id_ = self.button_id.get(button)
        if id_ is None:
            print "No monitor assigned to this button"
        else:
            url = "https://app.datadoghq.com/monitors#{}".format(id_)
            webbrowser.open(url)

    def get_status(self):
        "Get monitor status"
        result = []

        # Time datadog query
        start = time.time()
        monitors = self.datadog.query_all()
        stop = int((time.time()-start)*1000)
        print "Took {}ms to query datadog".format(stop)
        reverse_dict = {v: k for k,v in self.button_id.iteritems()}

        for monitor in monitors:
            id_ = monitor["id"]
            if id_ in reverse_dict:
                button = reverse_dict[id_]
                color_mode = self._status_to_light(monitor.get('overall_state'))
                result.append((button, color_mode))

        return result