Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

3.9 KB
Raw Blame
import type { NacosInstanceInfo, NacosInstancePatch, NacosInstanceRef, NacosServiceDetail, NacosServiceInfo, NacosServiceUpsert } from "@/types/nacos";

export type NacosServiceInstanceHealth = "allHealthy" | "partiallyHealthy" | "noHealthyInstances" | "noInstances";

export interface NacosServiceInstanceHealthSummary {
  status: NacosServiceInstanceHealth;
  healthy: number;
  total: number;
}

export function nacosServiceInstanceHealthSummary(service: Pick<NacosServiceInfo, "ipCount" | "healthyInstanceCount">): NacosServiceInstanceHealthSummary | null {
  const total = service.ipCount;
  const healthy = service.healthyInstanceCount;
  if (total == null || healthy == null || !Number.isInteger(total) || !Number.isInteger(healthy) || total < 0 || healthy < 0 || healthy > total) return null;
  if (total === 0) return { status: "noInstances", healthy, total };
  if (healthy === total) return { status: "allHealthy", healthy, total };
  if (healthy === 0) return { status: "noHealthyInstances", healthy, total };
  return { status: "partiallyHealthy", healthy, total };
}

function canonicalJson(value: unknown): unknown {
  if (Array.isArray(value)) return value.map(canonicalJson);
  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.entries(value as Record<string, unknown>)
        .sort(([left], [right]) => left.localeCompare(right))
        .map(([key, nested]) => [key, canonicalJson(nested)]),
    );
  }
  return value;
}

export function nacosJsonObjectMatches(left: unknown, right: unknown) {
  return JSON.stringify(canonicalJson(left ?? {})) === JSON.stringify(canonicalJson(right ?? {}));
}

export function nacosIpAddressIsValid(value: string) {
  const input = value.trim();
  if (!input) return false;
  if (input.includes(":")) {
    try {
      const parsed = new URL(`http://[${input}]/`);
      return parsed.hostname.startsWith("[") && parsed.hostname.endsWith("]");
    } catch {
      return false;
    }
  }
  const octets = input.split(".");
  return octets.length === 4 && octets.every((octet) => /^\d{1,3}$/.test(octet) && (octet === "0" || !octet.startsWith("0")) && Number(octet) <= 255);
}

export function nacosInstanceRefIdentity(ref: NacosInstanceRef) {
  const lifetime = ref.ephemeral === true ? "ephemeral" : ref.ephemeral === false ? "persistent" : "unknown";
  return [ref.namespace || "public", ref.groupName || "DEFAULT_GROUP", ref.serviceName, ref.ip, ref.port, ref.clusterName || "DEFAULT", lifetime].join("\u0000");
}

export function nacosInstanceMatchesPatch(instance: NacosInstanceInfo, patch: NacosInstancePatch) {
  const weightMatches = patch.weight == null || (instance.weight != null && Math.abs(instance.weight - patch.weight) <= 1e-6);
  const metadataMatches = patch.metadata == null || nacosJsonObjectMatches(instance.metadata, patch.metadata);
  return (patch.enabled == null || instance.enabled === patch.enabled) && (patch.healthy == null || instance.healthy === patch.healthy) && weightMatches && metadataMatches;
}

export function normalizeNacosSelector(value: unknown): unknown {
  if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length === 0) return null;
  const selector = value as Record<string, unknown>;
  const type = String(selector.type ?? "").toLowerCase();
  const contextType = String(selector.contextType ?? "").toUpperCase();
  if (type === "none" || type === "noneselector" || contextType === "NONE") return null;
  return canonicalJson(selector);
}

export function nacosServiceDetailMatches(detail: NacosServiceDetail, expected: NacosServiceUpsert) {
  const thresholdMatches = expected.protectThreshold == null || (detail.protectThreshold != null && Math.abs(detail.protectThreshold - expected.protectThreshold) <= 1e-6);
  return nacosJsonObjectMatches(detail.metadata, expected.metadata) && thresholdMatches && JSON.stringify(normalizeNacosSelector(detail.selector)) === JSON.stringify(normalizeNacosSelector(expected.selector));
}