import { Injectable, EventEmitter } from '@angular/core'; import { DataSourceService } from './data-source.service'; import { Report } from '../model/reports'; export class TreeNode { data: T; children?: TreeNode[]; expanded?: boolean; } export class TreeDataNode { columnOne: string; columnTwo: number | string; createtimestamp?: string; modifytimestamp?: string; kind: string; items?: number | string; forEach(callback: (k: string, v: any) => void) { callback('name', this.columnOne); callback('size', this.columnTwo); callback('kind', this.kind); callback('createtimestamp', this.createtimestamp); callback('modifytimestamp', this.modifytimestamp); callback('items', this.items); } } @Injectable({ providedIn: 'root', }) export class ReportsService { refreshData = new EventEmitter(); data: TreeNode[] = []; constructor( private dataSource: DataSourceService, ) { } getAllReports(): Promise { return new Promise((resolve, reject) => { this.dataSource.getAllReports().subscribe((reports) => { // 遍历reports生成树 reports.forEach(async (report) => { // 生成当前report节点 const nodeTree = this.createReportNode(report); // 获取详细report this.data.push(nodeTree); this.refreshData.emit(this.data); }); resolve(); }); }); } private createReportNode(report: Report) { const nodeTree = new TreeNode(); const data = new TreeDataNode(); nodeTree.data = data; data.columnOne = report.filename; data.columnTwo = report.size; data.createtimestamp = report.createtimestamp; data.modifytimestamp = report.modifytimestamp; data.kind = 'doc'; return nodeTree; } getAllDetails() { this.data.forEach(reportNode => { this.getReportDetailsOf(reportNode.data.columnOne); }); } getReportDetailsOf(filename: string) { this.dataSource.getReportDetailsOf(filename).subscribe(details => { const [target] = this.data.filter((node: TreeNode) => { return node.data.columnOne === filename; }); const reportChildren = this.createNodeChildren(details.detail, { name: 'case', config: ['description', 'result', 'starttimestamp', 'completetimestamp', 'dir'], children: { name: 'case-result', config: ['result', 'total', 'starttimestamp', 'completetimestamp', 'dir'], children: { name: 'result-screenshot', config: ['description', 'screenshot', 'starttimestamp', 'completetimestamp', 'doc'], }, }, }); target.children = reportChildren; target.data.kind = 'dir'; this.data = [...this.data]; this.refreshData.emit(this.data); }, ); } createNodeChildren(detail, options: any) { const itemNames = options.config; const res = []; detail.forEach((resultItem: any) => { const nodeTree = new TreeNode(); const data = new TreeDataNode(); nodeTree.data = data; data.columnOne = resultItem[itemNames[0]]; data.columnTwo = resultItem[itemNames[1]]; data.createtimestamp = resultItem[itemNames[2]]; data.modifytimestamp = resultItem[itemNames[3]]; data.kind = itemNames[4]; if (options.children && resultItem.detail) { nodeTree.children = this.createNodeChildren(resultItem.detail, options.children); } res.push(nodeTree); }); return res; } getData() { if (this.data.length > 0) { this.refreshData.emit(this.data); } else { this.getAllReports().then(() => { this.getAllDetails(); }); } } }