Exemplo n.º 1
0
    def render(self, request):
        conformity_name = [i.name for i in Conformity.objects.all()]
        default_columns = [
            {"text": "Minion id", "value": "minion_id"},
            {"text": "Last Highstate", "value": "last_highstate"},
            {"text": "Highstate Conformity", "value": "conformity"},
            {"text": "Succeeded", "value": "succeeded"},
            {"text": "Unchanged", "value": "unchanged"},
            {"text": "Failed", "value": "failed"},
        ]
        for conformity in conformity_name:
            default_columns.append({"text": conformity, "value": conformity})

        # Get conformity data.
        _, _, rendered_conformity = render_conformity()
        # Compute number of succeeded, unchanged and failed states.
        conformity_data = []
        minions = Minions.objects.all()
        for minion in minions:
            succeeded, unchanged, failed = 0, 0, 0
            last_highstate = minion.last_highstate()
            if last_highstate:
                last_highstate_date = last_highstate.alter_time
                last_highstate = last_highstate.loaded_ret()["return"]
                # Sls error
                if isinstance(last_highstate, list):
                    succeeded, unchanged, failed = None, None, 1
                else:
                    for state in last_highstate:
                        if last_highstate[state]["result"] is True:
                            succeeded += 1
                        elif last_highstate[state]["result"] is None:
                            unchanged += 1
                        else:
                            failed += 1
            else:
                last_highstate_date, succeeded, unchanged, failed = (
                    None,
                    None,
                    None,
                    None,
                )

            default_conformity = {
                "minion_id": minion.minion_id,
                "last_highstate": last_highstate_date,
                "conformity": minion.conformity(),
                "succeeded": succeeded,
                "unchanged": unchanged,
                "failed": failed,
            }
            # Add custom conformity values to datatable data.
            if rendered_conformity:
                for conformity in rendered_conformity[minion.minion_id]:
                    default_conformity.update(conformity)
            conformity_data.append(default_conformity)
        return Response({"name": default_columns, "data": conformity_data})
Exemplo n.º 2
0
    def conformity(self, request):
        highstate_conformity = {"conform": 0, "conflict": 0, "unknown": 0}
        for minion in Minions.objects.all():
            if minion.conformity() is True:
                highstate_conformity["conform"] += 1
            elif minion.conformity() is False:
                highstate_conformity["conflict"] += 1
            else:
                highstate_conformity["unknown"] += 1

        conformity_name, conformity_data, _ = render_conformity()
        conformity_name.insert(0, "HIGHSTATE")
        conformity_data.insert(0, highstate_conformity)
        return Response({"name": conformity_name, "data": conformity_data})
Exemplo n.º 3
0
    def conformity_detail(self, request, minion_id):
        minion = self.get_object()

        # Get conformity data.
        _, _, custom_conformity = render_conformity(minion.minion_id)
        custom_conformity = (
            custom_conformity[minion.minion_id] if custom_conformity else None
        )
        minion_conformity = minion.conformity()

        # Convert states to html.
        conv = Ansi2HTMLConverter(inline=False, scheme="xterm")
        last_highstate = minion.last_highstate()
        succeeded, unchanged, failed = {}, {}, {}

        if last_highstate:
            last_highstate = last_highstate.loaded_ret()["return"]
            # Sls error
            if isinstance(last_highstate, list):
                failed = {"error": last_highstate[0]}
            else:
                for state in last_highstate:
                    state_name = state.split("_|-")[1]
                    formatted = highstate_output.output(
                        {minion.minion_id: {state: last_highstate[state]}},
                        summary=False,
                    )
                    if last_highstate[state]["result"] is True:
                        succeeded[state_name] = conv.convert(
                            formatted, ensure_trailing_newline=True
                        )
                    elif last_highstate[state]["result"] is None:
                        unchanged[state_name] = conv.convert(
                            formatted, ensure_trailing_newline=True
                        )
                    else:
                        failed[state_name] = conv.convert(
                            formatted, ensure_trailing_newline=True
                        )
        return Response(
            {
                "custom_conformity": custom_conformity,
                "conformity": minion_conformity,
                "succeeded": succeeded,
                "unchanged": unchanged,
                "failed": failed,
            }
        )
Exemplo n.º 4
0
def test_render_conformity(minion_master, foo_conformity, dummy_state):
    conformity_names, ret, details = render_conformity()
    assert conformity_names == ["FOO"]
    assert ret == [{"noice": 1}]
    assert details == {"master": [{"foo": "noice"}]}