data-center.service.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Injectable, inject } from '@angular/core';
  2. import { Observable } from 'rxjs';
  3. import { RUNTIME_CONFIG } from '../../app/core/config/runtime-config';
  4. import { CloudApiService } from '../../app/core/services/cloud-api.service';
  5. export type DataCenterJobStatus = 'pending' | 'processing' | 'completed' | 'partial' | 'failed' | 'cancelled';
  6. export type DataCenterSyncScope = 'product' | 'reviews';
  7. export type DataCenterJobEventLevel = 'info' | 'warning' | 'error';
  8. export interface DataCenterSource {
  9. id: string;
  10. workspaceId: string;
  11. platform: string;
  12. kind: string;
  13. status: string;
  14. lastCheckedAt: string | null;
  15. credentialStorage: 'environment' | 'external_secret';
  16. }
  17. export interface DataCenterImportBatch {
  18. id: string;
  19. workspaceId: string;
  20. platform: string;
  21. sourceKind: string;
  22. sourceFile: string | null;
  23. status: string;
  24. totalRows: number;
  25. successRows: number;
  26. failedRows: number;
  27. createdAt: string;
  28. completedAt: string | null;
  29. }
  30. export interface DataCenterSyncJob {
  31. id: string;
  32. workspaceId: string;
  33. platform: string;
  34. status: DataCenterJobStatus;
  35. scopes: DataCenterSyncScope[];
  36. productIds: string[];
  37. progress: number;
  38. attempts: number;
  39. maxAttempts: number;
  40. errorSummary: string | null;
  41. requestedAt: string;
  42. startedAt: string | null;
  43. completedAt: string | null;
  44. }
  45. export interface DataCenterJobEvent {
  46. id: string;
  47. level: DataCenterJobEventLevel;
  48. type: string;
  49. message: string;
  50. details: Record<string, unknown>;
  51. createdAt: string;
  52. }
  53. export interface DataCenterPage<T> {
  54. items: T[];
  55. nextCursor: string | null;
  56. }
  57. @Injectable({ providedIn: 'root' })
  58. export class DataCenterService {
  59. private readonly cloud = inject(CloudApiService);
  60. dataSources(workspaceId: string): Observable<{ items: DataCenterSource[] }> {
  61. return this.cloud.run({ action: 'data-source.list', workspaceId });
  62. }
  63. imports(workspaceId: string, limit = 50, cursor = ''): Observable<DataCenterPage<DataCenterImportBatch>> {
  64. return this.cloud.run({ action: 'import.list', workspaceId, payload: { limit, ...(cursor ? { cursor } : {}) } });
  65. }
  66. jobs(workspaceId: string, status: DataCenterJobStatus | '' = '', limit = 50, cursor = ''): Observable<DataCenterPage<DataCenterSyncJob>> {
  67. return this.cloud.run({ action: 'sync.jobs.list', workspaceId, platform: RUNTIME_CONFIG.domesticPlatform, payload: { limit, ...(status ? { status } : {}), ...(cursor ? { cursor } : {}) } });
  68. }
  69. jobEvents(workspaceId: string, jobId: string): Observable<{ items: DataCenterJobEvent[] }> {
  70. return this.cloud.run({ action: 'sync.job.events', workspaceId, platform: RUNTIME_CONFIG.domesticPlatform, payload: { jobId } });
  71. }
  72. enqueueSync(workspaceId: string, productIds: string[], scopes: DataCenterSyncScope[]): Observable<{ job: DataCenterSyncJob }> {
  73. return this.cloud.run({ action: 'sync.enqueue', workspaceId, platform: RUNTIME_CONFIG.domesticPlatform, payload: { productIds, scopes } });
  74. }
  75. retryJob(jobId: string): Observable<{ job: DataCenterSyncJob }> {
  76. return this.cloud.run({ action: 'sync.job.retry', payload: { jobId } });
  77. }
  78. cancelJob(jobId: string): Observable<{ job: DataCenterSyncJob }> {
  79. return this.cloud.run({ action: 'sync.job.cancel', payload: { jobId } });
  80. }
  81. }