wrwWakeMapper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import {
  2. fetchRemoteJson,
  3. isRemoteImageUrl,
  4. resolveResourceUrl,
  5. } from "./wrwRemoteData";
  6. import { formatWakeMetric, formatWakePercent } from "./wrwTableUtils";
  7. /** 接口 *Path 字段 → 前端图表数据 key */
  8. const CHART_PATH_MAP = [
  9. { chartKey: "windSpeedDistribution", pathKey: "windSpeedDistributionChart" },
  10. { chartKey: "wakeRose", pathKey: "wakeRoseDiagramPath" },
  11. { chartKey: "wakeDistribute", pathKey: "wakeDistributePath" },
  12. { chartKey: "wakeLossConfident", pathKey: "wakeLossConfidentPath" },
  13. { chartKey: "velocityDeficitBar", pathKey: "velocityDeficitPath" },
  14. ];
  15. const CHART_PAYLOAD_ALIASES = {
  16. windSpeedDistribution: ["windSpeedDistribution", "windSpeedDistributionChart"],
  17. wakeRose: ["wakeRose", "windRose"],
  18. wakeDistribute: ["wakeDistribute", "layoutLossScatter"],
  19. wakeLossConfident: ["wakeLossConfident", "lossConfidenceScatter"],
  20. velocityDeficitBar: ["velocityDeficitBar", "velocityDeficit", "lossDelBar"],
  21. };
  22. function emptyPageData() {
  23. return {
  24. charts: {},
  25. tableData: [],
  26. turbineNames: [],
  27. wakeDescription: "",
  28. meta: {},
  29. };
  30. }
  31. function unwrapWindRosePayload(payload) {
  32. if (!payload || typeof payload !== "object") return null;
  33. let current = payload;
  34. for (let depth = 0; depth < 4; depth += 1) {
  35. if (current.windRose && typeof current.windRose === "object") {
  36. current = current.windRose;
  37. continue;
  38. }
  39. if (current.wakeRose && typeof current.wakeRose === "object") {
  40. current = current.wakeRose;
  41. continue;
  42. }
  43. if (current.data && typeof current.data === "object") {
  44. const nested =
  45. current.data.windRose || current.data.wakeRose || current.data;
  46. if (nested && nested !== current) {
  47. current = nested;
  48. continue;
  49. }
  50. }
  51. break;
  52. }
  53. return current;
  54. }
  55. function normalizeChartPayload(chartKey, payload) {
  56. if (!payload || typeof payload !== "object") return null;
  57. if (chartKey === "wakeRose") {
  58. return unwrapWindRosePayload(payload);
  59. }
  60. const aliases = CHART_PAYLOAD_ALIASES[chartKey] || [chartKey];
  61. for (let i = 0; i < aliases.length; i += 1) {
  62. if (payload[aliases[i]]) {
  63. return payload[aliases[i]];
  64. }
  65. }
  66. if (Array.isArray(payload.turbines) || Array.isArray(payload.points)) {
  67. return payload;
  68. }
  69. if (Array.isArray(payload.items)) {
  70. return payload;
  71. }
  72. if (Array.isArray(payload.bands)) {
  73. return payload;
  74. }
  75. return payload;
  76. }
  77. function hasChartPayload(chartKey, data) {
  78. if (!data || typeof data !== "object") return false;
  79. if (data.__imageUrl) return true;
  80. if (Array.isArray(data.points) && data.points.length) return true;
  81. if (Array.isArray(data.items) && data.items.length) return true;
  82. if (Array.isArray(data.turbines) && data.turbines.length) return true;
  83. if (chartKey === "wakeRose" && !isEmptyWakeRose(data)) return true;
  84. return false;
  85. }
  86. function normalizeChartsObject(rawCharts = {}, vo = {}) {
  87. const charts = { ...(rawCharts || {}) };
  88. Object.keys(CHART_PAYLOAD_ALIASES).forEach((chartKey) => {
  89. if (hasChartPayload(chartKey, charts[chartKey])) return;
  90. const aliases = CHART_PAYLOAD_ALIASES[chartKey] || [chartKey];
  91. for (let i = 0; i < aliases.length; i += 1) {
  92. const alias = aliases[i];
  93. const sources = [charts[alias], vo[alias]];
  94. for (let j = 0; j < sources.length; j += 1) {
  95. const source = sources[j];
  96. if (!source) continue;
  97. const normalized = normalizeChartPayload(chartKey, source);
  98. if (hasChartPayload(chartKey, normalized)) {
  99. charts[chartKey] = normalized;
  100. return;
  101. }
  102. }
  103. }
  104. });
  105. return charts;
  106. }
  107. async function loadChartFromPath(chartKey, path) {
  108. if (!path) return null;
  109. const resolved = resolveResourceUrl(path);
  110. if (isRemoteImageUrl(resolved)) {
  111. return chartKey === "wakeDistribute" ? { __imageUrl: resolved } : null;
  112. }
  113. try {
  114. const json = await fetchRemoteJson(resolved);
  115. return normalizeChartPayload(chartKey, json);
  116. } catch (error) {
  117. console.warn(`加载尾流图表资源失败 [${chartKey}]:`, path, error);
  118. return null;
  119. }
  120. }
  121. async function loadDescriptionText(path) {
  122. if (!path) return "";
  123. const resolved = resolveResourceUrl(path);
  124. if (isRemoteImageUrl(resolved)) return "";
  125. try {
  126. const response = await fetch(resolved, { method: "GET", credentials: "include" });
  127. if (!response.ok) return "";
  128. return response.text();
  129. } catch (error) {
  130. console.warn("加载尾流描述文本失败:", path, error);
  131. return "";
  132. }
  133. }
  134. function formatConfidence(confidentLevel) {
  135. return formatWakePercent(confidentLevel);
  136. }
  137. function formatNumber(value) {
  138. return formatWakeMetric(value);
  139. }
  140. export function mapWakeTurbineList(list) {
  141. if (!Array.isArray(list)) return [];
  142. return list.map((item) => ({
  143. name: item.engineId || item.engineName || item.name || "—",
  144. engineId: item.engineId || "",
  145. affected: item.isWake === 1 || item.isWake === true ? "yes" : "no",
  146. turbulence: formatNumber(item.turbulenceIntensity),
  147. loss: formatNumber(item.velocityDeficit),
  148. confidence: formatConfidence(item.confidentLevel),
  149. image: resolveResourceUrl(item.wakeTurbinePath || item.image || ""),
  150. }));
  151. }
  152. function extractTurbineNames(charts, turbineList) {
  153. const names = [];
  154. const pushName = (id) => {
  155. if (id && !names.includes(id)) names.push(id);
  156. };
  157. charts.windSpeedDistribution?.turbines?.forEach((item) => {
  158. pushName(item.displayName || item.engineId);
  159. });
  160. charts.wakeRose?.turbines?.forEach((item) => {
  161. pushName(item.engineId);
  162. });
  163. if (Array.isArray(turbineList)) {
  164. turbineList.forEach((item) => pushName(item.engineId));
  165. }
  166. return names;
  167. }
  168. function formatDataDate(dataDate) {
  169. if (!dataDate) return "";
  170. return String(dataDate).slice(0, 10);
  171. }
  172. function isEmptyWakeRose(rose) {
  173. return !rose || !Array.isArray(rose.turbines) || !rose.turbines.length;
  174. }
  175. function hasRoseBands(turbine) {
  176. return (
  177. turbine &&
  178. Array.isArray(turbine.bands) &&
  179. turbine.bands.some((band) => Array.isArray(band.values) && band.values.length)
  180. );
  181. }
  182. function normalizeTurbineRoseData(engineId, payload) {
  183. const normalized = unwrapWindRosePayload(payload);
  184. if (!normalized || typeof normalized !== "object") return null;
  185. const meta = {
  186. speedBands: normalized.speedBands,
  187. directions: normalized.directions,
  188. };
  189. if (Array.isArray(normalized.turbines) && normalized.turbines.length) {
  190. const matched =
  191. normalized.turbines.find((item) => item.engineId === engineId) ||
  192. normalized.turbines[0];
  193. if (!hasRoseBands(matched)) return null;
  194. return {
  195. engineId: matched.engineId || engineId,
  196. bands: matched.bands,
  197. speedBands: meta.speedBands,
  198. directions: meta.directions,
  199. };
  200. }
  201. if (hasRoseBands(normalized)) {
  202. return {
  203. engineId: normalized.engineId || engineId,
  204. bands: normalized.bands,
  205. speedBands: meta.speedBands,
  206. directions: meta.directions,
  207. };
  208. }
  209. return null;
  210. }
  211. function getTurbineRosePath(item) {
  212. const path =
  213. item?.wakeRoseDiagramPath ||
  214. item?.windRoseDiagramPath ||
  215. item?.wakeRosePath ||
  216. "";
  217. return typeof path === "string" ? path.trim() : "";
  218. }
  219. function getInlineTurbineRoseSource(item) {
  220. if (!item || typeof item !== "object") return null;
  221. const pathField =
  222. item.wakeRoseDiagramPath || item.windRoseDiagramPath || item.wakeRosePath;
  223. if (pathField && typeof pathField === "object") {
  224. return normalizeTurbineRoseData(item.engineId, pathField);
  225. }
  226. if (item.windRose || item.wakeRose) {
  227. return normalizeTurbineRoseData(
  228. item.engineId,
  229. item.windRose || item.wakeRose,
  230. );
  231. }
  232. return null;
  233. }
  234. async function loadTurbineRoseFromItem(item) {
  235. const engineId = item?.engineId || "";
  236. const inlineRose = getInlineTurbineRoseSource(item);
  237. if (inlineRose) return inlineRose;
  238. const rosePath = getTurbineRosePath(item);
  239. if (!rosePath || isRemoteImageUrl(resolveResourceUrl(rosePath))) {
  240. return null;
  241. }
  242. try {
  243. const json = await fetchRemoteJson(rosePath);
  244. return normalizeTurbineRoseData(engineId, json);
  245. } catch (error) {
  246. console.warn(`加载机组玫瑰图失败 [${engineId}]:`, rosePath, error);
  247. return null;
  248. }
  249. }
  250. async function loadTurbineRoseDiagrams(turbineList) {
  251. if (!Array.isArray(turbineList) || !turbineList.length) {
  252. return [];
  253. }
  254. const results = await Promise.all(
  255. turbineList.map((item) => loadTurbineRoseFromItem(item)),
  256. );
  257. return results.filter(Boolean);
  258. }
  259. function mergeWakeRoseData(farmRose, turbineList, perTurbineRoses) {
  260. const roseMap = new Map();
  261. (farmRose?.turbines || []).forEach((item) => {
  262. if (item?.engineId) roseMap.set(item.engineId, item);
  263. });
  264. perTurbineRoses.forEach((item) => {
  265. if (item?.engineId) roseMap.set(item.engineId, item);
  266. });
  267. if (!roseMap.size) return farmRose || null;
  268. const turbines = [];
  269. if (Array.isArray(turbineList)) {
  270. turbineList.forEach((item) => {
  271. const id = item?.engineId;
  272. if (id && roseMap.has(id)) {
  273. turbines.push(roseMap.get(id));
  274. roseMap.delete(id);
  275. }
  276. });
  277. }
  278. roseMap.forEach((item) => turbines.push(item));
  279. const metaSource =
  280. perTurbineRoses.find((item) => item.speedBands || item.directions) ||
  281. farmRose ||
  282. {};
  283. return {
  284. speedBands: metaSource.speedBands || farmRose?.speedBands,
  285. directions: metaSource.directions || farmRose?.directions,
  286. turbines: turbines.map(({ speedBands, directions, ...item }) => item),
  287. };
  288. }
  289. export async function loadWakePageData(vo) {
  290. if (!vo || typeof vo !== "object") {
  291. return emptyPageData();
  292. }
  293. const turbineList = Array.isArray(vo.getWakeTurbineList)
  294. ? vo.getWakeTurbineList
  295. : [];
  296. let charts = normalizeChartsObject(
  297. vo.charts && typeof vo.charts === "object" ? vo.charts : {},
  298. vo,
  299. );
  300. const [wakeDescription, perTurbineRoses] = await Promise.all([
  301. loadDescriptionText(vo.wakeDiscriptionPath),
  302. loadTurbineRoseDiagrams(turbineList),
  303. ...CHART_PATH_MAP.filter(({ chartKey }) => chartKey !== "wakeRose").map(
  304. async ({ chartKey, pathKey }) => {
  305. if (charts[chartKey]) return;
  306. const loaded = await loadChartFromPath(chartKey, vo[pathKey]);
  307. if (loaded) {
  308. charts[chartKey] = loaded;
  309. }
  310. },
  311. ),
  312. ]);
  313. const mergedWakeRose = mergeWakeRoseData(null, turbineList, perTurbineRoses);
  314. if (mergedWakeRose?.turbines?.length) {
  315. charts.wakeRose = mergedWakeRose;
  316. } else if (isEmptyWakeRose(charts.wakeRose)) {
  317. const farmRose = await loadChartFromPath("wakeRose", vo.wakeRoseDiagramPath);
  318. if (farmRose && !isEmptyWakeRose(farmRose)) {
  319. charts.wakeRose = farmRose;
  320. }
  321. }
  322. const tableData = mapWakeTurbineList(turbineList);
  323. const turbineNames = extractTurbineNames(charts, turbineList);
  324. if (
  325. !hasChartPayload("velocityDeficitBar", charts.velocityDeficitBar) &&
  326. turbineList.length
  327. ) {
  328. charts.velocityDeficitBar = {
  329. items: turbineList.map((item) => ({
  330. engineId: item.engineId,
  331. lossPercent: item.lossPercent ?? item.velocityDeficit,
  332. turbulenceIntensity: item.turbulenceIntensity,
  333. velocityDeficit: item.velocityDeficit,
  334. })),
  335. };
  336. }
  337. if (
  338. !hasChartPayload("wakeLossConfident", charts.wakeLossConfident) &&
  339. turbineList.length
  340. ) {
  341. charts.wakeLossConfident = {
  342. points: turbineList.map((item) => ({
  343. engineId: item.engineId,
  344. velocityDeficit: item.velocityDeficit,
  345. confidentLevel: item.confidentLevel,
  346. })),
  347. };
  348. }
  349. return {
  350. charts,
  351. tableData,
  352. turbineNames,
  353. wakeDescription,
  354. turbineList,
  355. meta: {
  356. fieldId: vo.fieldId || "",
  357. dataDate: formatDataDate(vo.dataDate),
  358. },
  359. };
  360. }