Skip to content

Commit

Permalink
Merge pull request #245 from binbat/feat-node-strategy
Browse files Browse the repository at this point in the history
feat(liveman): change api node strategy
  • Loading branch information
a-wing authored Oct 25, 2024
2 parents 7b881f9 + 3d5743c commit ae5e2e7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
32 changes: 18 additions & 14 deletions liveman/src/route/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};

use api::strategy::Strategy;

use crate::{result::Result, AppState};

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -14,30 +16,32 @@ pub enum NodeState {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Node {
pub alias: String,
pub url: String,
pub pub_max: u16,
pub sub_max: u16,
pub status: NodeState,
alias: String,
url: String,
status: NodeState,
strategy: Strategy,
duration: String,
}

pub async fn index(State(mut state): State<AppState>) -> Result<Json<Vec<Node>>> {
let map_info = state.storage.info_raw_all().await.unwrap();

state.storage.nodes().await;
Ok(Json(
state
.storage
.get_cluster()
.get_map_nodes()
.into_iter()
.map(|x| Node {
alias: x.alias.clone(),
url: x.url,
pub_max: x.pub_max,
sub_max: x.sub_max,
status: match map_info.get(&x.alias) {
.map(|(alias, node)| Node {
alias,
url: node.url,
status: match node.strategy {
Some(_) => NodeState::Running,
None => NodeState::Stopped,
},
strategy: node.strategy.unwrap_or_default(),
duration: match node.duration {
Some(s) => format!("{}", s.as_millis()),
None => Default::default(),
},
})
.collect(),
))
Expand Down
4 changes: 2 additions & 2 deletions liveman/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct Node {
pub url: String,

streams: Vec<Stream>,
strategy: Option<Strategy>,
duration: Option<Duration>,
pub strategy: Option<Strategy>,
pub duration: Option<Duration>,
}

impl Node {
Expand Down
4 changes: 2 additions & 2 deletions web/liveman/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function login(username: string, password: string) {
export interface Node {
alias: string;
url: string;
pub_max: number;
sub_max: number;
duration: string;
strategy: Record<string, string | number | boolean>,
status: 'running' | 'stopped';
}

Expand Down
31 changes: 26 additions & 5 deletions web/liveman/components/nodes-table.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRefreshTimer } from '../../shared/hooks/use-refresh-timer';
import { StyledCheckbox } from '../../shared/components/styled-checkbox';

import { getNodes } from '../api';
import { type Node, getNodes } from '../api';

async function getNodesSorted() {
const nodes = await getNodes();
Expand All @@ -24,8 +24,8 @@ export function NodesTable() {
<tr>
<th class="min-w-24">Alias</th>
<th class="min-w-24">Status</th>
<th>Max Publish Cnt.</th>
<th>Max subscribe Cnt.</th>
<th>Delay</th>
<th class="min-w-72">Strategy</th>
<th class="min-w-72">API URL</th>
</tr>
</thead>
Expand All @@ -34,8 +34,10 @@ export function NodesTable() {
<tr>
<td class="text-center">{n.alias}</td>
<td class="text-center">{n.status}</td>
<td class="text-center">{n.pub_max}</td>
<td class="text-center">{n.sub_max}</td>
<td class="text-center">{n.duration}ms</td>
<td class="text-center">
<NodeStrategyTable strategy={n.strategy} />
</td>
<td class="text-center"><a href={n.url} target="_blank">{n.url}</a></td>
</tr>
))}
Expand All @@ -45,3 +47,22 @@ export function NodesTable() {
</>
);
}

type NodeStrategyTableProps = Pick<Node, 'strategy'>;

function NodeStrategyTable({ strategy }: NodeStrategyTableProps) {
return (
<div class="h-[1lh] overflow-hidden relative group hover:overflow-visible">
<table class="mx-auto px-1 bg-white @dark:bg-neutral-800 rounded group-hover:absolute group-hover:inset-x-0 group-hover:z-1 group-hover:outline group-hover:outline-indigo-500">
<tbody>
{Object.entries(strategy).map(([k, v]) => (
<tr>
<th class="text-left">{k}</th>
<td>{`${v}`}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}

0 comments on commit ae5e2e7

Please sign in to comment.