View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.core.job.progress;
19  
20  import lombok.Getter;
21  import lombok.NoArgsConstructor;
22  import lombok.Setter;
23  import org.apache.shardingsphere.data.pipeline.core.context.TransmissionJobItemContext;
24  import org.apache.shardingsphere.data.pipeline.core.job.JobStatus;
25  import org.apache.shardingsphere.data.pipeline.core.task.progress.IncrementalTaskProgress;
26  import org.apache.shardingsphere.data.pipeline.core.task.progress.InventoryTaskProgress;
27  import org.apache.shardingsphere.data.pipeline.core.task.PipelineTask;
28  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
29  
30  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  /**
35   * Transmission job item progress.
36   */
37  @NoArgsConstructor
38  @Getter
39  @Setter
40  public final class TransmissionJobItemProgress implements PipelineJobItemProgress {
41      
42      private DatabaseType sourceDatabaseType;
43      
44      private String dataSourceName;
45      
46      private JobItemInventoryTasksProgress inventory;
47      
48      private JobItemIncrementalTasksProgress incremental;
49      
50      private long inventoryRecordsCount;
51      
52      private long processedRecordsCount;
53      
54      private boolean active;
55      
56      private JobStatus status = JobStatus.RUNNING;
57      
58      public TransmissionJobItemProgress(final TransmissionJobItemContext context) {
59          sourceDatabaseType = context.getJobConfig().getSourceDatabaseType();
60          dataSourceName = context.getDataSourceName();
61          inventory = getInventoryTasksProgress(context.getInventoryTasks());
62          incremental = getIncrementalTasksProgress(context.getIncrementalTasks());
63          inventoryRecordsCount = context.getInventoryRecordsCount();
64          processedRecordsCount = context.getProcessedRecordsCount();
65          status = context.getStatus();
66      }
67      
68      private JobItemIncrementalTasksProgress getIncrementalTasksProgress(final Collection<PipelineTask> incrementalTasks) {
69          return new JobItemIncrementalTasksProgress(incrementalTasks.isEmpty() ? null : (IncrementalTaskProgress) incrementalTasks.iterator().next().getTaskProgress());
70      }
71      
72      private JobItemInventoryTasksProgress getInventoryTasksProgress(final Collection<PipelineTask> inventoryTasks) {
73          Map<String, InventoryTaskProgress> inventoryTaskProgressMap = new HashMap<>(inventoryTasks.size(), 1F);
74          for (PipelineTask each : inventoryTasks) {
75              inventoryTaskProgressMap.put(each.getTaskId(), (InventoryTaskProgress) each.getTaskProgress());
76          }
77          return new JobItemInventoryTasksProgress(inventoryTaskProgressMap);
78      }
79  }