Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(liveman): change api node strategy #245

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

#[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,

Check warning on line 23 in liveman/src/route/node.rs

View check run for this annotation

Codecov / codecov/patch

liveman/src/route/node.rs#L23

Added line #L23 was not covered by tests
}

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 {

Check warning on line 33 in liveman/src/route/node.rs

View check run for this annotation

Codecov / codecov/patch

liveman/src/route/node.rs#L33

Added line #L33 was not covered by tests
alias,
url: node.url,

Check warning on line 35 in liveman/src/route/node.rs

View check run for this annotation

Codecov / codecov/patch

liveman/src/route/node.rs#L35

Added line #L35 was not covered by tests
status: match node.strategy {
Some(_) => NodeState::Running,
None => NodeState::Stopped,
},
strategy: node.strategy.unwrap_or_default(),

Check warning on line 40 in liveman/src/route/node.rs

View check run for this annotation

Codecov / codecov/patch

liveman/src/route/node.rs#L40

Added line #L40 was not covered by tests
duration: match node.duration {
Some(s) => format!("{}", s.as_millis()),
None => Default::default(),

Check warning on line 43 in liveman/src/route/node.rs

View check run for this annotation

Codecov / codecov/patch

liveman/src/route/node.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
},
})
.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 url: String,

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

Check warning on line 36 in liveman/src/store.rs

View check run for this annotation

Codecov / codecov/patch

liveman/src/store.rs#L36

Added line #L36 was not covered by tests
}

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>
);
}
Loading