refactor(services): 重构服务文件结构,将服务按功能分类到不同目录
- 将服务文件按功能分类到core、ai、analytics、security等目录 - 修复logger导入路径问题,统一使用相对路径 - 更新相关文件的导入路径引用 - 添加新的批量操作组件导出文件 - 修复dashboard页面中的类型错误 - 添加dotenv依赖到package.json
This commit is contained in:
80
dashboard/src/services/monitoringDataSource.ts
Normal file
80
dashboard/src/services/monitoringDataSource.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { http } from './http';
|
||||
|
||||
export interface MonitoringItem {
|
||||
id: string;
|
||||
name: string;
|
||||
status: string;
|
||||
createdAt: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface MonitoringDataSource {
|
||||
list(): Promise<MonitoringItem[]>;
|
||||
create(data: Partial<MonitoringItem>): Promise<MonitoringItem>;
|
||||
update(id: string, data: Partial<MonitoringItem>): Promise<MonitoringItem>;
|
||||
delete(id: string): Promise<void>;
|
||||
}
|
||||
|
||||
class MonitoringApiDataSource implements MonitoringDataSource {
|
||||
async list() {
|
||||
const response = await http.get('/api/monitoring/list');
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
async create(data: Partial<MonitoringItem>) {
|
||||
const response = await http.post('/api/monitoring', data);
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
async update(id: string, data: Partial<MonitoringItem>) {
|
||||
const response = await http.put(`/api/monitoring/${id}`, data);
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await http.delete(`/api/monitoring/${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
class MonitoringMockDataSource implements MonitoringDataSource {
|
||||
private items: MonitoringItem[] = [
|
||||
{ id: '1', name: '示例项目 1', status: 'ACTIVE', createdAt: new Date().toISOString() },
|
||||
{ id: '2', name: '示例项目 2', status: 'INACTIVE', createdAt: new Date().toISOString() },
|
||||
];
|
||||
|
||||
async list() {
|
||||
return this.items;
|
||||
}
|
||||
|
||||
async create(data: Partial<MonitoringItem>) {
|
||||
const item: MonitoringItem = {
|
||||
id: String(this.items.length + 1),
|
||||
name: data.name || '',
|
||||
status: data.status || 'ACTIVE',
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
this.items.push(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
async update(id: string, data: Partial<MonitoringItem>) {
|
||||
const index = this.items.findIndex(item => item.id === id);
|
||||
if (index >= 0) {
|
||||
this.items[index] = { ...this.items[index], ...data };
|
||||
return this.items[index];
|
||||
}
|
||||
throw new Error('Item not found');
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
const index = this.items.findIndex(item => item.id === id);
|
||||
if (index >= 0) {
|
||||
this.items.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const useMock = process.env.NODE_ENV === 'development' || process.env.REACT_APP_USE_MOCK === 'true';
|
||||
export const MonitoringDataSource: MonitoringDataSource = useMock
|
||||
? new MonitoringMockDataSource()
|
||||
: new MonitoringApiDataSource();
|
||||
Reference in New Issue
Block a user