<commit_before><template>
  <div id="main" class="panel panel-default">
    <div class="panel-heading">
      <i class="bi bi-card-list"></i> Fuzzing Pools
    </div>
    <table class="table table-condensed table-hover table-bordered table-db">
      <thead>
        <tr>
          <th
            v-on:click.exact="sortBy('pool_id')"
            v-on:click.ctrl.exact="addSort('pool_id')"
            :class="{
              active:
                sortKeys.includes('pool_id') || sortKeys.includes('-pool_id'),
            }"
            width="25px"
          >
            ID
          </th>
          <th
            v-on:click.exact="sortBy('pool_name')"
            v-on:click.ctrl.exact="addSort('pool_name')"
            :class="{
              active:
                sortKeys.includes('pool_name') ||
                sortKeys.includes('-pool_name'),
            }"
            width="100px"
          >
            Name
          </th>
          <th
            v-on:click.exact="sortBy('running')"
            v-on:click.ctrl.exact="addSort('running')"
            :class="{
              active:
                sortKeys.includes('running') || sortKeys.includes('-running'),
            }"
            width="75px"
          >
            # of Tasks (Running/Requested)
          </th>
          <th
            v-on:click.exact="sortBy('status')"
            v-on:click.ctrl.exact="addSort('status')"
            :class="{
              active:
                sortKeys.includes('status') || sortKeys.includes('-status'),
            }"
            width="150px"
          >
            Status
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="pool in ordered_pools" :key="pool.id">
          <td>
            <a :href="pool.view_url">{{ pool.pool_id }}</a>
          </td>
          <td>{{ pool.pool_name }}</td>
          <td>{{ pool.running }}/{{ pool.size }}</td>
          <td>
            <span
              class="label"
              :class="{
                'label-warning':
                  pool.status !== 'idle' && pool.status !== 'healthy',
                'label-success': pool.status === 'healthy',
                'label-danger': pool.status === 'idle',
              }"
              >{{ pool.status }}</span
            >
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import _throttle from "lodash/throttle";
import _orderBy from "lodash/orderBy";
import swal from "sweetalert";
import { E_SERVER_ERROR } from "../../helpers";
import * as api from "../../api";

const defaultSortKey = "pool_name";

export default {
  data: function () {
    return {
      loading: true,
      pools: null,
      sortKeys: [defaultSortKey],
    };
  },
  created: function () {
    this.fetch();
  },
  computed: {
    ordered_pools: function () {
      return _orderBy(
        this.pools,
        this.sortKeys.map((key) =>
          key.startsWith("-") ? key.substring(1) : key
        ),
        this.sortKeys.map((key) => (key.startsWith("-") ? "desc" : "asc"))
      );
    },
  },
  methods: {
    buildParams() {
      return {
        vue: "1",
      };
    },
    fetch: _throttle(
      async function () {
        this.loading = true;
        try {
          const data = await api.listPools(this.buildParams());
          this.pools = data.results;
        } catch (err) {
          if (
            err.response &&
            err.response.status === 400 &&
            err.response.data
          ) {
            // eslint-disable-next-line no-console
            console.debug(err.response.data);
            swal("Oops", E_SERVER_ERROR, "error");
            this.loading = false;
          }
        }
        this.loading = false;
      },
      500,
      { trailing: true }
    ),
    addSort: function (sortKey) {
      /*
       * add sort by sortKey to existing sort keys
       * if already sorting, by sortKey,
       *   reverse the sort order without changing the priority of sort keys
       * if not sorting by sortKey yet,
       *   sort first by this sortKey and then by existing sort keys<commit_msg>[taskmanager] Use case-insensitive sort for pool names.<commit_after><template>
  <div id="main" class="panel panel-default">
    <div class="panel-heading">
      <i class="bi bi-card-list"></i> Fuzzing Pools
    </div>
    <table class="table table-condensed table-hover table-bordered table-db">
      <thead>
        <tr>
          <th
            v-on:click.exact="sortBy('pool_id')"
            v-on:click.ctrl.exact="addSort('pool_id')"
            :class="{
              active:
                sortKeys.includes('pool_id') || sortKeys.includes('-pool_id'),
            }"
            width="25px"
          >
            ID
          </th>
          <th
            v-on:click.exact="sortBy('pool_name_isort')"
            v-on:click.ctrl.exact="addSort('pool_name_isort')"
            :class="{
              active:
                sortKeys.includes('pool_name_isort') ||
                sortKeys.includes('-pool_name_isort'),
            }"
            width="100px"
          >
            Name
          </th>
          <th
            v-on:click.exact="sortBy('running')"
            v-on:click.ctrl.exact="addSort('running')"
            :class="{
              active:
                sortKeys.includes('running') || sortKeys.includes('-running'),
            }"
            width="75px"
          >
            # of Tasks (Running/Requested)
          </th>
          <th
            v-on:click.exact="sortBy('status')"
            v-on:click.ctrl.exact="addSort('status')"
            :class="{
              active:
                sortKeys.includes('status') || sortKeys.includes('-status'),
            }"
            width="150px"
          >
            Status
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="pool in ordered_pools" :key="pool.id">
          <td>
            <a :href="pool.view_url">{{ pool.pool_id }}</a>
          </td>
          <td>{{ pool.pool_name }}</td>
          <td>{{ pool.running }}/{{ pool.size }}</td>
          <td>
            <span
              class="label"
              :class="{
                'label-warning':
                  pool.status !== 'idle' && pool.status !== 'healthy',
                'label-success': pool.status === 'healthy',
                'label-danger': pool.status === 'idle',
              }"
              >{{ pool.status }}</span
            >
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import _throttle from "lodash/throttle";
import _orderBy from "lodash/orderBy";
import swal from "sweetalert";
import { E_SERVER_ERROR } from "../../helpers";
import * as api from "../../api";

const defaultSortKey = "pool_name_isort";

export default {
  data: function () {
    return {
      loading: true,
      pools: null,
      sortKeys: [defaultSortKey],
    };
  },
  created: function () {
    this.fetch();
  },
  computed: {
    ordered_pools: function () {
      return _orderBy(
        this.pools,
        this.sortKeys.map((key) =>
          key.startsWith("-") ? key.substring(1) : key
        ),
        this.sortKeys.map((key) => (key.startsWith("-") ? "desc" : "asc"))
      );
    },
  },
  methods: {
    buildParams() {
      return {
        vue: "1",
      };
    },
    fetch: _throttle(
      async function () {
        this.loading = true;
        try {
          const data = await api.listPools(this.buildParams());
          this.pools = data.results.map((pool) => {
            pool.pool_name_isort = pool.pool_name.toLowerCase();
            return pool;
          });
        } catch (err) {
          if (
            err.response &&
            err.response.status === 400 &&
            err.response.data
          ) {
            // eslint-disable-next-line no-console
            console.debug(err.response.data);
            swal("Oops", E_SERVER_ERROR, "error");
            this.loading = false;
          }
        }
        this.loading = false;
      },
      500,
      { trailing: true }
    ),
    addSort: function (sortKey) {
      /*
       * add sort by sortKey to existing sort keys
       * if already sorting, by sortKey,
       *   reverse the sort order without changing the priority of sort keys
       * if not sorting by sortKey yet,
       *   sort first by this sortKey and then by existing sort keys
{'fraction_non_alphanumeric': 0.15013627207015048, 'fraction_numerical': 0.004265908282971916, 'mean_word_length': 1.4671148786904413, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4546133', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2477, 'n_words': 493}