Skip to content

Commit a04eda4

Browse files
authored
Merge pull request #143 from mcanoy/migrate-dates
more data cleansing
2 parents fa94299 + 66d0306 commit a04eda4

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/main/java/com/redhat/labs/lodestar/service/MigrationService.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.LocalDateTime;
55
import java.time.ZoneOffset;
66
import java.time.format.DateTimeFormatter;
7+
import java.time.format.DateTimeParseException;
78
import java.util.*;
89

910
import javax.enterprise.context.ApplicationScoped;
@@ -164,12 +165,22 @@ private String migrateEngagement(Engagement engagement) {
164165
try {
165166
use.setCreated(convertDateTime(use.getCreated()));
166167
use.setUpdated(convertDateTime(use.getUpdated()));
167-
} catch(Exception ex) {
168+
} catch(RuntimeException ex) {
168169
LOGGER.error ("dtf exception {}", use.getCreated(), ex);
169170
}
170171
}));
171172
}
172173

174+
if(copy.getLaunch() != null) {
175+
try {
176+
Instant.parse(copy.getLaunch().getLaunchedDateTime());
177+
} catch (DateTimeParseException ex) {
178+
LOGGER.error("No standard launch date {}", copy.getLaunch().getLaunchedDateTime());
179+
copy.getLaunch().setLaunchedDateTime(convertDateTime(copy.getLaunch().getLaunchedDateTime()));
180+
}
181+
}
182+
183+
173184
copy.setHostingEnvironments(null);
174185
copy.setEngagementUsers(null);
175186
copy.setCommits(null);
@@ -183,8 +194,31 @@ private <T> T clone(T toClone, Class<T> clazz) {
183194
}
184195

185196
private String convertDateTime(String oldDateTime) {
197+
String[] patterns = {"yyyy-MM-dd'T'HH:mm:ss.SSSSSS", "yyyy-MM-dd'T'HH:mm:ss.SSS"};
198+
199+
String date = null;
200+
201+
for(int i=0; i<patterns.length; i++) {
202+
try {
203+
date = convertDateTime(oldDateTime, patterns[i]);
204+
break;
205+
} catch (DateTimeParseException ex) {
206+
LOGGER.error("No standard date {}, pattern {}", oldDateTime, patterns[i]);
207+
}
208+
}
209+
210+
if(date == null) {
211+
throw new RuntimeException("Unable to parse date " + oldDateTime);
212+
}
213+
214+
LOGGER.trace("date converted={}", date);
215+
return date;
216+
}
217+
218+
private String convertDateTime(String oldDateTime, String pattern) {
186219
LOGGER.trace("Date In {}", oldDateTime);
187-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
220+
221+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
188222
LocalDateTime expected = LocalDateTime.parse(oldDateTime, formatter);
189223
Instant instant = expected.toInstant(ZoneOffset.UTC);
190224
LOGGER.trace("Date Out {}", instant);

src/test/java/com/redhat/labs/lodestar/service/MigrationServiceTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redhat.labs.lodestar.service;
22

3+
import java.time.Instant;
34
import java.util.ArrayList;
45
import java.util.Collections;
56
import java.util.List;
@@ -59,12 +60,15 @@ public static void setup() {
5960
List<UseCase> uses = new ArrayList<>();
6061
uses.add(UseCase.builder().title("use case").created("2021-08-26T20:42:46.050483")
6162
.updated("2021-08-26T20:42:46.050483").build());
62-
uses.add(UseCase.builder().title("use case2").build());
63+
uses.add(UseCase.builder().title("use case2").created("2021-08-26T20:42:46.050483").build());
6364

64-
Engagement e = Engagement.builder().uuid("a1").categories(cats).useCases(uses).projectId(1).build();
65+
Launch launch = Launch.builder().launchedBy("Alfredo").launchedByEmail("[email protected]").launchedDateTime("2021-08-26T20:42:46.050").build();
66+
67+
Engagement e = Engagement.builder().uuid("a1").categories(cats).useCases(uses).projectId(1).launch(launch).build();
6568
allEngagements.add(e);
6669

67-
e = Engagement.builder().uuid("c3").projectId(3).engagementUsers(engagementUsers).build();
70+
launch = Launch.builder().launchedBy("Alfredo").launchedByEmail("[email protected]").launchedDateTime(Instant.now().toString()).build();
71+
e = Engagement.builder().uuid("c3").projectId(3).engagementUsers(engagementUsers).launch(launch).build();
6872
allEngagements.add(e);
6973

7074
e = Engagement.builder().uuid("d4").projectId(4).engagementUsers(engagementUsers).artifacts(artifacts).build();

0 commit comments

Comments
 (0)