aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/services/Portainer.vue
blob: c7e996270829fad7bcff39fd18cded8f047e81bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<template>
  <Generic :item="item">
    <template #indicator>
      <div class="notifs">
        <strong v-if="running > 0" class="notif running" title="Running">
          {{ running }}
        </strong>
        <strong v-if="dead > 0" class="notif dead" title="Dead">
          {{ dead }}
        </strong>
        <strong
          v-if="misc > 0"
          class="notif misc"
          title="Other (creating, paused, exited, etc.)"
        >
          {{ misc }}
        </strong>
      </div>
    </template>
  </Generic>
</template>

<script>
import service from "@/mixins/service.js";
import Generic from "./Generic.vue";

export default {
  name: "Portainer",
  mixins: [service],
  props: {
    item: Object,
  },
  components: {
    Generic,
  },
  data: () => ({
    endpoints: null,
    containers: null,
  }),
  computed: {
    running: function () {
      if (!this.containers) {
        return "";
      }
      return this.containers.filter((container) => {
        return container.State.toLowerCase() === "running";
      }).length;
    },
    dead: function () {
      if (!this.containers) {
        return "";
      }
      return this.containers.filter((container) => {
        return container.State.toLowerCase() === "dead";
      }).length;
    },
    misc: function () {
      if (!this.containers) {
        return "";
      }
      return this.containers.filter((container) => {
        return (
          container.State.toLowerCase() !== "running" &&
          container.State.toLowerCase() !== "dead"
        );
      }).length;
    },
  },
  created() {
    this.fetchStatus();
  },
  methods: {
    fetchStatus: async function () {
      const headers = {
        "X-Api-Key": this.item.apikey,
      };

      this.endpoints = await this.fetch("/api/endpoints", { headers }).catch(
        (e) => {
          console.error(e);
        }
      );

      let containers = [];
      for (let endpoint of this.endpoints) {
        const uri = `/api/endpoints/${endpoint.Id}/docker/containers/json?all=1`;
        const endpointContainers = await this.fetch(uri, { headers }).catch(
          (e) => {
            console.error(e);
          }
        );

        containers = containers.concat(endpointContainers);
      }

      this.containers = containers;
    },
  },
};
</script>

<style scoped lang="scss">
.notifs {
  position: absolute;
  color: white;
  font-family: sans-serif;
  top: 0.3em;
  right: 0.5em;

  .notif {
    display: inline-block;
    padding: 0.2em 0.35em;
    border-radius: 0.25em;
    position: relative;
    margin-left: 0.3em;
    font-size: 0.8em;

    &.running {
      background-color: #4fd671;
    }

    &.dead {
      background-color: #e51111;
    }

    &.misc {
      background-color: #2ed0c8;
    }
  }
}
</style>