index.vue 4.19 KB
Newer Older
Johannes Edmeier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!--
  - Copyright 2014-2018 the original author or authors.
  -
  - 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.
  -->

<template>
18 19 20 21 22 23 24 25 26 27
  <div class="section">
    <div class="container">
      <h1 class="title">Event Journal</h1>
      <div v-if="error" class="message is-warning">
        <div class="message-body">
          <strong>
            <font-awesome-icon class="has-text-warning" icon="exclamation-triangle"/>
            Server connection failed.
          </strong>
          <p v-text="error.message"/>
Johannes Edmeier committed
28
        </div>
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
      </div>
      <table class="table is-fullwidth">
        <thead>
          <tr>
            <th>Application</th>
            <th>Instance</th>
            <th>Time</th>
            <th>Event</th>
          </tr>
        </thead>
        <tbody>
          <template v-for="event in events">
            <tr class="is-selectable" :key="event.key"
                @click="showPayload[event.key] ? $delete(showPayload, event.key) : $set(showPayload, event.key, true)">
              <td v-text="instanceNames[event.instance] || '?'"/>
              <td v-text="event.instance"/>
              <td v-text="event.timestamp.format('L HH:mm:ss.SSS')"/>
              <td>
                <span v-text="event.type"/> <span v-if="event.type === 'STATUS_CHANGED'"
                                                  v-text="`(${event.payload.statusInfo.status})`"/>
              </td>
            </tr>
            <tr :key="`${event.key}-detail`" v-if="showPayload[event.key]">
              <td colspan="4">
                <pre v-text="toJson(event.payload)"/>
              </td>
            </tr>
          </template>
        </tbody>
      </table>
Johannes Edmeier committed
59
    </div>
60
  </div>
Johannes Edmeier committed
61 62 63
</template>

<script>
64
  import subscribing from '@/mixins/subscribing';
Johannes Edmeier committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  import Instance from '@/services/instance';
  import _ from 'lodash';
  import moment from 'moment';

  class Event {
    constructor(event) {
      this.instance = event.instance;
      this.version = event.version;
      this.type = event.type;
      this.timestamp = moment(event.timestamp);
      this.payload = _.omit(event, ['instance', 'version', 'timestamp', 'type']);
    }

    get key() {
      return `${this.instance}-${this.version}`;
    }
  }

Johannes Edmeier committed
83
  const component = {
84
    mixins: [subscribing],
Johannes Edmeier committed
85
    data: () => ({
86
      events: [],
Johannes Edmeier committed
87 88
      showPayload: {},
      instanceNames: {},
89
      error: null
Johannes Edmeier committed
90 91 92 93 94 95 96
    }),
    methods: {
      toJson(obj) {
        return JSON.stringify(obj, null, 4);
      },
      addEvents(data) {
        const converted = data.map(event => new Event(event));
97 98
        converted.reverse();
        this.events = converted.concat(this.events);
Johannes Edmeier committed
99 100 101 102 103 104

        const newInstanceNames = converted.filter(event => event.type === 'REGISTERED').reduce((names, event) => ({
          ...names,
          [event.instance]: event.payload.registration.name
        }), {});
        _.assign(this.instanceNames, newInstanceNames);
105
      },
106 107
      createSubscription() {
        return Instance.getEventStream().subscribe({
108
          next: message => {
109
            this.error = null;
110 111
            this.addEvents([message.data])
          },
112 113
          error: error => {
            console.warn('Listening for events failed:', error);
114
            this.error = error;
115
          }
116
        });
Johannes Edmeier committed
117 118 119 120 121
      }
    },
    async created() {
      try {
        this.addEvents((await Instance.fetchEvents()).data);
122
        this.error = null;
123
        this.events.sort((a, b) => b.timestamp - a.timestamp)
124 125
      } catch (error) {
        console.warn('Fetching events failed:', error);
126
        this.error = error;
Johannes Edmeier committed
127
      }
128
    }
Johannes Edmeier committed
129 130 131 132 133 134 135 136 137 138
  };

  export default component;
  export const view = {
    path: '/journal',
    name: 'journal',
    handle: 'Journal',
    order: 100,
    component: component
  };
139
</script>