github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Tools/EventBus/index.vue (about)

     1  <script setup lang="ts">
     2  import {useI18n} from '@/hooks/web/useI18n'
     3  import {Table} from '@/components/Table'
     4  import {onMounted, onUnmounted, reactive, ref, watch} from 'vue'
     5  import {Pagination, TableColumn} from '@/types/table'
     6  import api from "@/api/api";
     7  import {ApiBusStateItem} from "@/api/stub";
     8  import {ContentWrap} from "@/components/ContentWrap";
     9  import {EventTriggerCompleted} from "@/api/types";
    10  import {UUID} from "uuid-generator-ts";
    11  
    12  const {t} = useI18n()
    13  
    14  interface TableObject {
    15    tableList: ApiBusStateItem[]
    16    params?: any
    17    loading: boolean
    18    sort?: string
    19  }
    20  
    21  interface Params {
    22    page?: number;
    23    limit?: number;
    24    sort?: string;
    25  }
    26  
    27  const tableObject = reactive<TableObject>(
    28      {
    29        tableList: [],
    30        loading: false,
    31      }
    32  );
    33  
    34  const currentID = ref('')
    35  
    36  const onEventTriggerActivated = (event: EventTriggerCompleted) => {
    37  
    38  }
    39  
    40  onMounted(() => {
    41    const uuid = new UUID()
    42    currentID.value = uuid.getDashFreeUUID()
    43  
    44    // setTimeout(() => {
    45    //   stream.subscribe('event_trigger_loaded', currentID.value, onStateChanged);
    46    //   stream.subscribe('event_trigger_unloaded', currentID.value, onStateChanged);
    47    //   stream.subscribe('event_trigger_completed', currentID.value, onEventTriggerActivated);
    48    // }, 1000)
    49  })
    50  
    51  onUnmounted(() => {
    52    // stream.unsubscribe('event_trigger_loaded', currentID.value);
    53    // stream.unsubscribe('event_trigger_unloaded', currentID.value);
    54    // stream.unsubscribe('event_trigger_completed', currentID.value);
    55  })
    56  
    57  const columns: TableColumn[] = [
    58    {
    59      field: 'topic',
    60      label: t('tools.eventBus.topic'),
    61    },
    62    {
    63      field: 'min',
    64      label: t('tools.eventBus.min'),
    65      width: "80px"
    66    },
    67    {
    68      field: 'avg',
    69      label: t('tools.eventBus.avg'),
    70      width: "80px"
    71    },
    72    {
    73      field: 'max',
    74      label: t('tools.eventBus.max'),
    75      width: "80px"
    76    },
    77    {
    78      field: 'rps',
    79      label: t('tools.eventBus.rps'),
    80      width: "80px"
    81    },
    82    {
    83      field: 'subscribers',
    84      label: t('tools.eventBus.subscribers'),
    85      width: "120px"
    86    },
    87  ]
    88  const paginationObj = ref<Pagination>({
    89    currentPage: 1,
    90    pageSize: 50,
    91    total: 0,
    92    pageSizes: [50, 100, 150, 250],
    93  })
    94  
    95  const getList = async () => {
    96    tableObject.loading = true
    97  
    98    let params: Params = {
    99      page: paginationObj.value.currentPage,
   100      limit: paginationObj.value.pageSize,
   101      sort: tableObject.sort,
   102    }
   103  
   104    const res = await api.v1.developerToolsServiceGetEventBusStateList(params)
   105        .catch(() => {
   106        })
   107        .finally(() => {
   108          tableObject.loading = false
   109        })
   110    if (res) {
   111      const {items, meta} = res.data;
   112      tableObject.tableList = items;
   113      paginationObj.value.currentPage = meta.pagination.page;
   114      paginationObj.value.total = meta.pagination.total;
   115    } else {
   116      tableObject.tableList = [];
   117    }
   118  }
   119  
   120  watch(
   121      () => paginationObj.value.currentPage,
   122      () => {
   123        getList()
   124      }
   125  )
   126  
   127  watch(
   128      () => paginationObj.value.pageSize,
   129      () => {
   130        getList()
   131      }
   132  )
   133  
   134  const sortChange = (data) => {
   135    const {column, prop, order} = data;
   136    const pref: string = order === 'ascending' ? '+' : '-'
   137    tableObject.sort = pref + prop
   138    getList()
   139  }
   140  
   141  const myInterval = ref()
   142  onMounted(() => {
   143    myInterval.value = setInterval(() => {
   144      getList()
   145    }, 2000)
   146  })
   147  
   148  onUnmounted(() => {
   149    clearInterval(myInterval.value);
   150  })
   151  
   152  const tableRowClassName = (data) => {
   153    const {row, rowIndex} = data
   154    let style = ''
   155    if (row.completed) {
   156      style = 'completed'
   157    }
   158    return style
   159  }
   160  
   161  </script>
   162  
   163  <template>
   164    <ContentWrap>
   165  
   166      <Table
   167          :selection="false"
   168          v-model:pageSize="paginationObj.pageSize"
   169          v-model:currentPage="paginationObj.currentPage"
   170          :columns="columns"
   171          :data="tableObject.tableList"
   172          :loading="tableObject.loading"
   173          :pagination="paginationObj"
   174          @sort-change="sortChange"
   175          :row-class-name="tableRowClassName"
   176          style="width: 100%"
   177          :showUpPagination="20"
   178      />
   179  
   180  
   181    </ContentWrap>
   182  
   183  </template>
   184  
   185  <style lang="less">
   186  
   187  .light {
   188    .el-table__row {
   189      &.completed {
   190        --el-table-tr-bg-color: var(--el-color-primary-light-7);
   191        -webkit-transition: background-color 200ms linear;
   192        -ms-transition: background-color 200ms linear;
   193        transition: background-color 200ms linear;
   194      }
   195    }
   196  }
   197  
   198  .dark {
   199    .el-table__row {
   200      &.completed {
   201        --el-table-tr-bg-color: var(--el-color-primary-dark-2);
   202        -webkit-transition: background-color 200ms linear;
   203        -ms-transition: background-color 200ms linear;
   204        transition: background-color 200ms linear;
   205      }
   206    }
   207  }
   208  
   209  </style>