<commit_before><!--
  - 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>
  <sba-panel title="Process" v-if="hasLoaded">
    <div>
      <div v-if="error" class="message is-danger">
        <div class="message-body">
          <strong>
            <font-awesome-icon class="has-text-danger" icon="exclamation-triangle"/>
            Fetching process metrics failed.
          </strong>
          <p v-text="error.message"/>
        </div>
      </div>
      <div class="level">
        <div class="level-item has-text-centered" v-if="pid">
          <div>
            <p class="heading">PID</p>
            <p v-text="pid"/>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="uptime">
          <div>
            <p class="heading">Uptime</p>
            <p>
              <process-uptime :value="uptime"/>
            </p>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="cpuCount">
          <div>
            <p class="heading">CPUs</p>
            <p v-text="cpuCount"/>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="systemLoad">
          <div>
            <p class="heading">System Load (last 1m)</p>
            <p v-text="systemLoad.toFixed(2)"/>
          </div>
        </div>
      </div>
    </div>
  </sba-panel>
</template>

<script>
  import Instance from '@/services/instance';
  import processUptime from './process-uptime';

  export default {
    props: {
      instance: {
        type: Instance,
        required: true
      }
    },
    components: {processUptime},
    data: () => ({
      hasLoaded: false,
      error: null,
      pid: null,
      uptime: null,
      systemLoad: null,
      cpuCount: null
    }),
    created() {
      this.fetchUptime();
      this.fetchCpuCount();
      this.fetchSystemLoad();
      this.fetchPid();
    },
    methods: {
      async fetchUptime() {
        try {
          this.uptime = await this.fetchMetric('process.uptime');
        } catch (error) {
          this.error = error;
          console.warn('Fetching Uptime failed:', error);
        }
        this.hasLoaded = true;
      },
      async fetchSystemLoad() {
        try {
          this.systemLoad = await this.fetchMetric('system.load.average.1m');
        } catch (error) {
          console.warn('Fetching System Load failed:', error);
        }
        this.hasLoaded = true;
      },
      async fetchCpuCount() {
        try {
          this.cpuCount = await this.fetchMetric('system.cpu.count');
        } catch (error) {
          console.warn('Fetching Cpu Count failed:', error);
        }
        this.hasLoaded = true;
      },
      async fetchMetric(name) {
        const response = await this.instance.fetchMetric(name);
        return response.data.measurements[0].value;
      },
      async fetchPid() {
        if (this.instance.hasEndpoint('env')) {
          try {
            const response = await this.instance.fetchEnv('PID');
            this.pid = response.data.property.value;
          } catch (error) {
            console.warn('Fetching PID failed:', error);
          }
          this.hasLoaded = true;
        }
      }
    }
  }
</script><commit_msg>Show cpu usage for process and system<commit_after><!--
  - 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>
  <sba-panel title="Process" v-if="hasLoaded">
    <div>
      <div v-if="error" class="message is-danger">
        <div class="message-body">
          <strong>
            <font-awesome-icon class="has-text-danger" icon="exclamation-triangle"/>
            Fetching process metrics failed.
          </strong>
          <p v-text="error.message"/>
        </div>
      </div>
      <div class="level">
        <div class="level-item has-text-centered" v-if="pid">
          <div>
            <p class="heading">PID</p>
            <p v-text="pid"/>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="uptime">
          <div>
            <p class="heading">Uptime</p>
            <p>
              <process-uptime :value="uptime"/>
            </p>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="processCpuLoad">
          <div>
            <p class="heading">Process CPU Usage</p>
            <p v-text="processCpuLoad.toFixed(2)"/>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="systemCpuLoad">
          <div>
            <p class="heading">System CPU Usage</p>
            <p v-text="systemCpuLoad.toFixed(2)"/>
          </div>
        </div>
        <div class="level-item has-text-centered" v-if="systemCpuCount">
          <div>
            <p class="heading">CPUs</p>
            <p v-text="systemCpuCount"/>
          </div>
        </div>
      </div>
    </div>
  </sba-panel>
</template>

<script>
  import subscribing from '@/mixins/subscribing';
  import Instance from '@/services/instance';
  import {Observable} from '@/utils/rxjs';
  import processUptime from './process-uptime';

  export default {
    props: {
      instance: {
        type: Instance,
        required: true
      }
    },
    mixins: [subscribing],
    components: {processUptime},
    data: () => ({
      hasLoaded: false,
      error: null,
      pid: null,
      uptime: null,
      systemCpuLoad: null,
      processCpuLoad: null,
      systemCpuCount: null
    }),
    created() {
      this.fetchPid();
      this.fetchUptime();
      this.fetchCpuCount();
    },
    methods: {
      async fetchUptime() {
        try {
          this.uptime = await this.fetchMetric('process.uptime');
        } catch (error) {
          this.error = error;
          console.warn('Fetching Uptime failed:', error);
        }
        this.hasLoaded = true;
      },
      async fetchPid() {
        if (this.instance.hasEndpoint('env')) {
          try {
            const response = await this.instance.fetchEnv('PID');
            this.pid = response.data.property.value;
          } catch (error) {
            console.warn('Fetching PID failed:', error);
          }
          this.hasLoaded = true;
        }
      },
      async fetchCpuCount() {
        try {
          this.systemCpuCount = await this.fetchMetric('system.cpu.count');
        } catch (error) {
          console.warn('Fetching Cpu Count failed:', error);
        }
        this.hasLoaded = true;
      },
      createSubscription() {
        const vm = this;
        return Observable.timer(0, 2500)
          .concatMap(this.fetchCpuLoadMetrics)
          .subscribe({
            next: data => {
              vm.processCpuLoad = data.processCpuLoad;
              vm.systemCpuLoad = data.systemCpuLoad;
            },
            error: error => {
              vm.hasLoaded = true;
              console.warn('Fetching CPU Usage metrics failed:', error);
              vm.error = error;
            }
          });
      },
      async fetchCpuLoadMetrics() {
        const fetchProcessCpuLoad = this.fetchMetric('process.cpu.usage');
        const fetchSystemCpuLoad = this.fetchMetric('system.cpu.usage');
        let processCpuLoad;
        let systemCpuLoad;
        try {
          processCpuLoad = await fetchProcessCpuLoad
        } catch (error) {
          console.warn('Fetching Process CPU Load failed:', error);
        }
        try {
          systemCpuLoad = await fetchSystemCpuLoad
        } catch (error) {
          console.warn('Fetching Sytem CPU Load failed:', error);
        }
        return {
          processCpuLoad,
          systemCpuLoad
        };
      },
      async fetchMetric(name) {
        const response = await this.instance.fetchMetric(name);
        return response.data.measurements[0].value;
      }
    }
  }
</script>
{'fraction_non_alphanumeric': 0.1309391032790542, 'fraction_numerical': 0.004015168414008476, 'mean_word_length': 1.819811320754717, 'pattern_counts': {'":': 0, '<': 108, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6893763', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2621, 'n_words': 633}