|
@@ -0,0 +1,288 @@
|
|
|
|
|
+/**
|
|
|
|
|
+ * 显示地理桶前端领域层(票 07)。
|
|
|
|
|
+ * 只负责:后端三类桶 -> 前端组;屏幕相邻同类桶合并;选中剔除;ready/fallback 判定;请求 epoch。
|
|
|
|
|
+ * 不碰 Vue/Cesium/DOM,可脱浏览器验证。
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+export const DISPLAY_BUCKET_PHASE = {
|
|
|
|
|
+ DISABLED: 'disabled',
|
|
|
|
|
+ LOADING: 'loading',
|
|
|
|
|
+ READY: 'ready',
|
|
|
|
|
+ FALLBACK: 'fallback',
|
|
|
|
|
+ EXPIRED: 'expired',
|
|
|
|
|
+ INCOMPATIBLE: 'incompatible',
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export const DISPLAY_BUCKET_CONTRACT = 'display-buckets-v1'
|
|
|
|
|
+export const DISPLAY_MEMBER_CONTRACT = 'display-members-v1'
|
|
|
|
|
+export const BACKEND_BUCKET_KIND = {
|
|
|
|
|
+ UNASSOCIATED_EQUIPMENT: 'unassociated-equipment',
|
|
|
|
|
+ MONITORING_POINT: 'monitoring-point',
|
|
|
|
|
+ FACILITY_POINT: 'facility-point',
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const DEVICE_PRIORITY = { ALARM: 1, WORK_ORDER: 2, WARNING: 3, NORMAL: 4, OFFLINE: 5, UNKNOWN: 6 }
|
|
|
|
|
+const POINT_PRIORITY = { ALARM: 1, WARNING: 2, WORK_ORDER: 3, NORMAL: 4, OFFLINE: 5, EMPTY: 6, UNKNOWN: 7 }
|
|
|
|
|
+
|
|
|
|
|
+function priorityOf(kind, status) {
|
|
|
|
|
+ return (kind === 'monitoring-point' ? POINT_PRIORITY : DEVICE_PRIORITY)[status] || 99
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function labelOf(kind, status) {
|
|
|
|
|
+ if (kind === 'monitoring-point' && status === 'EMPTY') return '无设备'
|
|
|
|
|
+ return {
|
|
|
|
|
+ ALARM: '报警', WARNING: '预警', WORK_ORDER: '工单处置',
|
|
|
|
|
+ NORMAL: '正常', OFFLINE: '离线', UNKNOWN: '状态未发布',
|
|
|
|
|
+ }[status] || '状态未发布'
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function summarizeCounts(kind, counts) {
|
|
|
|
|
+ const normalized = new Map()
|
|
|
|
|
+ for (const [status, count] of counts || []) if (count > 0) normalized.set(status, count)
|
|
|
|
|
+ const rows = [...normalized.entries()]
|
|
|
|
|
+ .map(([status, count]) => ({ status, label: labelOf(kind, status), count }))
|
|
|
|
|
+ .sort((a, b) => priorityOf(kind, a.status) - priorityOf(kind, b.status))
|
|
|
|
|
+ if (!rows.length) {
|
|
|
|
|
+ return { primaryStatus: 'UNKNOWN', primaryLabel: '状态未发布', primaryCount: 0, abnormalCount: 0, otherAbnormalCount: 0, counts: [] }
|
|
|
|
|
+ }
|
|
|
|
|
+ const primary = rows[0]
|
|
|
|
|
+ let abnormal = 0
|
|
|
|
|
+ for (const row of rows) if (priorityOf(kind, row.status) <= 3) abnormal += row.count
|
|
|
|
|
+ const primaryAbnormal = priorityOf(kind, primary.status) <= 3 ? primary.count : 0
|
|
|
|
|
+ return {
|
|
|
|
|
+ primaryStatus: primary.status,
|
|
|
|
|
+ primaryLabel: primary.label,
|
|
|
|
|
+ primaryCount: primary.count,
|
|
|
|
|
+ abnormalCount: abnormal,
|
|
|
|
|
+ otherAbnormalCount: Math.max(0, abnormal - primaryAbnormal),
|
|
|
|
|
+ counts: rows,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function bucketBadge(memberKind, count, statusSummary) {
|
|
|
|
|
+ const primary = statusSummary.counts[0] || null
|
|
|
|
|
+ const abnormal = primary && ['ALARM', 'WARNING', 'WORK_ORDER'].includes(primary.status)
|
|
|
|
|
+ ? { status: primary.status, label: primary.label, count: statusSummary.abnormalCount, tone: 'lv1' }
|
|
|
|
|
+ : null
|
|
|
|
|
+ const category = memberKind === 'monitoring-point' ? '监测点' : '独立设备'
|
|
|
|
|
+ const noun = memberKind === 'monitoring-point' ? '个监测点' : '台独立设备'
|
|
|
|
|
+ const typeChar = memberKind === 'monitoring-point' ? '点' : '设'
|
|
|
|
|
+ return {
|
|
|
|
|
+ kind: memberKind,
|
|
|
|
|
+ code: statusSummary.groupKey || '',
|
|
|
|
|
+ groupKey: statusSummary.groupKey || '',
|
|
|
|
|
+ count,
|
|
|
|
|
+ category,
|
|
|
|
|
+ noun,
|
|
|
|
|
+ label: count + ' ' + noun,
|
|
|
|
|
+ tone: abnormal ? 'lv1' : 'lv4',
|
|
|
|
|
+ primaryStatus: statusSummary.primaryStatus,
|
|
|
|
|
+ primaryLabel: statusSummary.primaryLabel,
|
|
|
|
|
+ abnormal,
|
|
|
|
|
+ otherAbnormalCount: statusSummary.otherAbnormalCount,
|
|
|
|
|
+ effectStatus: abnormal ? abnormal.status : '',
|
|
|
|
|
+ effect: '',
|
|
|
|
|
+ mixedIndustry: false,
|
|
|
|
|
+ typeLabel: category,
|
|
|
|
|
+ typeChar,
|
|
|
|
|
+ typeColor: '#aab7c4',
|
|
|
|
|
+ industryKey: '',
|
|
|
|
|
+ types: [],
|
|
|
|
|
+ statuses: statusSummary.counts,
|
|
|
|
|
+ tooltip: count + ' ' + noun + '\n状态:' + statusSummary.counts.map((row) => row.label + ' ' + row.count).join(' / '),
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function normalizeStatusSummary(kind, summary) {
|
|
|
|
|
+ const counts = new Map()
|
|
|
|
|
+ const rows = summary && Array.isArray(summary.counts) ? summary.counts : []
|
|
|
|
|
+ for (const row of rows) if (row && row.status) counts.set(row.status, (counts.get(row.status) || 0) + (Number(row.count) || 0))
|
|
|
|
|
+ return summarizeCounts(kind, counts)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function mergeBounds(groups) {
|
|
|
|
|
+ let west = Infinity, south = Infinity, east = -Infinity, north = -Infinity
|
|
|
|
|
+ for (const group of groups) {
|
|
|
|
|
+ const bounds = group.bounds || {}
|
|
|
|
|
+ west = Math.min(west, Number(bounds.west))
|
|
|
|
|
+ south = Math.min(south, Number(bounds.south))
|
|
|
|
|
+ east = Math.max(east, Number(bounds.east))
|
|
|
|
|
+ north = Math.max(north, Number(bounds.north))
|
|
|
|
|
+ }
|
|
|
|
|
+ return Number.isFinite(west) ? { west, south, east, north } : null
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function mergeStatus(kind, groups) {
|
|
|
|
|
+ const counts = new Map()
|
|
|
|
|
+ for (const group of groups) {
|
|
|
|
|
+ for (const row of (group.statusSummary && group.statusSummary.counts) || []) {
|
|
|
|
|
+ counts.set(row.status, (counts.get(row.status) || 0) + row.count)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return summarizeCounts(kind, counts)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function bucketClassesToGroups(classes, { project } = {}) {
|
|
|
|
|
+ const groups = []
|
|
|
|
|
+ for (const bucketClass of Array.isArray(classes) ? classes : []) {
|
|
|
|
|
+ const memberKind = BACKEND_BUCKET_KIND[bucketClass && bucketClass.kind]
|
|
|
|
|
+ if (!memberKind || !Array.isArray(bucketClass.buckets)) continue
|
|
|
|
|
+ for (const bucket of bucketClass.buckets) {
|
|
|
|
|
+ if (!bucket || !bucket.groupKey || !bucket.anchor) continue
|
|
|
|
|
+ const longitude = Number(bucket.anchor.longitude)
|
|
|
|
|
+ const latitude = Number(bucket.anchor.latitude)
|
|
|
|
|
+ if (!Number.isFinite(longitude) || !Number.isFinite(latitude)) continue
|
|
|
|
|
+ const screen = typeof project === 'function' ? project(longitude, latitude) : null
|
|
|
|
|
+ if (!screen || !Number.isFinite(screen.x) || !Number.isFinite(screen.y)) continue
|
|
|
|
|
+ const count = Number(bucket.memberCount) || 0
|
|
|
|
|
+ const statusSummary = normalizeStatusSummary(memberKind, bucket.statusSummary)
|
|
|
|
|
+ const category = memberKind === 'monitoring-point' ? '监测点' : '独立设备'
|
|
|
|
|
+ const noun = memberKind === 'monitoring-point' ? '个监测点' : '台独立设备'
|
|
|
|
|
+ const typeChar = memberKind === 'monitoring-point' ? '点' : '设'
|
|
|
|
|
+ const badge = bucketBadge(memberKind, count, statusSummary)
|
|
|
|
|
+ groups.push({
|
|
|
|
|
+ groupKey: bucket.groupKey,
|
|
|
|
|
+ kind: memberKind,
|
|
|
|
|
+ memberKind,
|
|
|
|
|
+ count,
|
|
|
|
|
+ screen: { x: screen.x, y: screen.y },
|
|
|
|
|
+ anchor: { longitude, latitude },
|
|
|
|
|
+ bounds: bucket.bounds || null,
|
|
|
|
|
+ memberCursors: bucket.memberCursor ? [bucket.memberCursor] : [],
|
|
|
|
|
+ statusSummary: {
|
|
|
|
|
+ ...statusSummary,
|
|
|
|
|
+ statuses: statusSummary.counts,
|
|
|
|
|
+ primary: statusSummary.counts[0] || null,
|
|
|
|
|
+ },
|
|
|
|
|
+ boundsWorld: bucket.bounds || null,
|
|
|
|
|
+ spreadPx: 100,
|
|
|
|
|
+ statusProfile: memberKind,
|
|
|
|
|
+ category,
|
|
|
|
|
+ noun,
|
|
|
|
|
+ typeChar,
|
|
|
|
|
+ typeLabel: category,
|
|
|
|
|
+ typeColor: '#aab7c4',
|
|
|
|
|
+ badge: { ...badge, code: bucket.groupKey, groupKey: bucket.groupKey },
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return groups
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function mergeAdjacentBuckets(groups, { thresholdPx = 22 } = {}) {
|
|
|
|
|
+ const source = Array.isArray(groups) ? groups.filter((group) => group && group.count > 0) : []
|
|
|
|
|
+ const clusters = []
|
|
|
|
|
+ for (const group of source) {
|
|
|
|
|
+ let target = null
|
|
|
|
|
+ for (const cluster of clusters) {
|
|
|
|
|
+ if (cluster.kind !== group.kind) continue
|
|
|
|
|
+ const screen = group.screen
|
|
|
|
|
+ if (cluster.groups.some((item) => Math.hypot(screen.x - item.screen.x, screen.y - item.screen.y) <= thresholdPx)) {
|
|
|
|
|
+ target = cluster
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!target) {
|
|
|
|
|
+ target = { kind: group.kind, groups: [] }
|
|
|
|
|
+ clusters.push(target)
|
|
|
|
|
+ }
|
|
|
|
|
+ target.groups.push(group)
|
|
|
|
|
+ }
|
|
|
|
|
+ return clusters.map((cluster) => {
|
|
|
|
|
+ const first = cluster.groups[0]
|
|
|
|
|
+ const count = cluster.groups.reduce((sum, item) => sum + item.count, 0)
|
|
|
|
|
+ const weighted = cluster.groups.reduce((sum, item) => ({
|
|
|
|
|
+ x: sum.x + item.screen.x * item.count,
|
|
|
|
|
+ y: sum.y + item.screen.y * item.count,
|
|
|
|
|
+ }), { x: 0, y: 0 })
|
|
|
|
|
+ const statusSummary = mergeStatus(cluster.kind, cluster.groups)
|
|
|
|
|
+ const groupKey = cluster.groups.map((item) => item.groupKey).join('+')
|
|
|
|
|
+ return {
|
|
|
|
|
+ groupKey,
|
|
|
|
|
+ kind: cluster.kind,
|
|
|
|
|
+ memberKind: first.memberKind,
|
|
|
|
|
+ count,
|
|
|
|
|
+ screen: { x: weighted.x / count, y: weighted.y / count },
|
|
|
|
|
+ anchor: first.anchor,
|
|
|
|
|
+ bounds: mergeBounds(cluster.groups),
|
|
|
|
|
+ boundsWorld: mergeBounds(cluster.groups),
|
|
|
|
|
+ spreadPx: 100,
|
|
|
|
|
+ memberCursors: cluster.groups.flatMap((item) => item.memberCursors || []),
|
|
|
|
|
+ statusSummary: { ...statusSummary, statuses: statusSummary.counts, primary: statusSummary.counts[0] || null },
|
|
|
|
|
+ badge: bucketBadge(cluster.kind, count, statusSummary),
|
|
|
|
|
+ sourceGroupKeys: cluster.groups.map((item) => item.groupKey),
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function subtractSelectedMember(group, selected) {
|
|
|
|
|
+ if (!group || !selected || selected.kind !== group.kind || !group.bounds) return group
|
|
|
|
|
+ const longitude = Number(selected.longitude)
|
|
|
|
|
+ const latitude = Number(selected.latitude)
|
|
|
|
|
+ if (!Number.isFinite(longitude) || !Number.isFinite(latitude)) return group
|
|
|
|
|
+ const bounds = group.bounds
|
|
|
|
|
+ if (longitude < Number(bounds.west) || longitude > Number(bounds.east)
|
|
|
|
|
+ || latitude < Number(bounds.south) || latitude > Number(bounds.north)) return group
|
|
|
|
|
+ const counts = new Map(group.statusSummary.counts.map((row) => [row.status, row.count]))
|
|
|
|
|
+ const status = selected.status || 'UNKNOWN'
|
|
|
|
|
+ counts.set(status, Math.max(0, (counts.get(status) || 0) - 1))
|
|
|
|
|
+ const count = Math.max(0, group.count - 1)
|
|
|
|
|
+ const statusSummary = summarizeCounts(group.kind, counts)
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...group,
|
|
|
|
|
+ count,
|
|
|
|
|
+ statusSummary: { ...statusSummary, statuses: statusSummary.counts, primary: statusSummary.counts[0] || null },
|
|
|
|
|
+ badge: bucketBadge(group.kind, count, statusSummary),
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function planDisplayBucketFallback({ enabled, result, expectedContract = DISPLAY_BUCKET_CONTRACT, expectedSnapshotAt = '' } = {}) {
|
|
|
|
|
+ if (!enabled) return { phase: DISPLAY_BUCKET_PHASE.DISABLED, reason: 'disabled', fallback: true }
|
|
|
|
|
+ if (!result || result.state !== 'success') {
|
|
|
|
|
+ const reason = (result && result.reason) || 'unavailable'
|
|
|
|
|
+ if (reason === 'snapshot-expired') return { phase: DISPLAY_BUCKET_PHASE.EXPIRED, reason, fallback: true }
|
|
|
|
|
+ return { phase: DISPLAY_BUCKET_PHASE.FALLBACK, reason, fallback: true }
|
|
|
|
|
+ }
|
|
|
|
|
+ const data = result.data || {}
|
|
|
|
|
+ if (data.contract !== expectedContract) {
|
|
|
|
|
+ return { phase: DISPLAY_BUCKET_PHASE.INCOMPATIBLE, reason: 'contract-incompatible', fallback: true }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (expectedSnapshotAt && data.snapshotAt && data.snapshotAt !== expectedSnapshotAt) {
|
|
|
|
|
+ return { phase: DISPLAY_BUCKET_PHASE.FALLBACK, reason: 'snapshot-mismatch', fallback: true }
|
|
|
|
|
+ }
|
|
|
|
|
+ return { phase: DISPLAY_BUCKET_PHASE.READY, reason: '', fallback: false }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function createDisplayBucketRequester({ fetchBuckets, onResult } = {}) {
|
|
|
|
|
+ if (typeof fetchBuckets !== 'function') throw new Error('fetchBuckets required')
|
|
|
|
|
+ let epoch = 0
|
|
|
|
|
+ let controller = null
|
|
|
|
|
+ return {
|
|
|
|
|
+ request(query) {
|
|
|
|
|
+ epoch += 1
|
|
|
|
|
+ const mine = epoch
|
|
|
|
|
+ if (controller && typeof controller.abort === 'function') controller.abort()
|
|
|
|
|
+ controller = typeof AbortController !== 'undefined' ? new AbortController() : null
|
|
|
|
|
+ const signal = controller ? controller.signal : undefined
|
|
|
|
|
+ const promise = Promise.resolve()
|
|
|
|
|
+ .then(() => fetchBuckets({ ...query, signal }))
|
|
|
|
|
+ .then((result) => {
|
|
|
|
|
+ if (mine !== epoch) return { stale: true, result }
|
|
|
|
|
+ if (typeof onResult === 'function') onResult(result, { epoch: mine })
|
|
|
|
|
+ return { stale: false, result }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((error) => {
|
|
|
|
|
+ if (mine !== epoch) return { stale: true, error }
|
|
|
|
|
+ const result = { state: 'unavailable', reason: 'request-failed', error }
|
|
|
|
|
+ if (typeof onResult === 'function') onResult(result, { epoch: mine })
|
|
|
|
|
+ return { stale: false, result }
|
|
|
|
|
+ })
|
|
|
|
|
+ return { epoch: mine, promise, signal }
|
|
|
|
|
+ },
|
|
|
|
|
+ cancel() {
|
|
|
|
|
+ epoch += 1
|
|
|
|
|
+ if (controller && typeof controller.abort === 'function') controller.abort()
|
|
|
|
|
+ },
|
|
|
|
|
+ get epoch() { return epoch },
|
|
|
|
|
+ }
|
|
|
|
|
+}
|