Skip to content

Commit ccbde2b

Browse files
authored
Merge pull request #145 from mcanoy/category-data
add category data. fix use case dates
2 parents ccee418 + 9c16f0a commit ccbde2b

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

src/main/java/com/redhat/labs/lodestar/models/Category.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public class Category extends EngagementAttribute {
1515

1616
private String name;
1717
private Integer count;
18+
private String region;
1819

1920
}

src/main/java/com/redhat/labs/lodestar/models/Engagement.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.redhat.labs.lodestar.models;
22

3-
import java.util.List;
3+
import java.util.*;
44

55
import javax.json.bind.annotation.JsonbProperty;
66

@@ -59,4 +59,12 @@ public class Engagement {
5959
@JsonbProperty("billing_codes")
6060
private List<BillingCodes> billingCodes;
6161

62+
@JsonbProperty("region")
63+
private String mapRegion;
64+
@JsonbProperty("type")
65+
private String mapType;
66+
@JsonbProperty("categories")
67+
private Set<String> mapCategories;
68+
private String name;
69+
6270
}

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

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

3+
import java.security.*;
34
import java.time.Instant;
45
import java.time.LocalDateTime;
56
import java.time.ZoneOffset;
@@ -27,6 +28,7 @@ public class MigrationService {
2728
private static final String ARTIFACT_JSON = ENGAGEMENT_DIR + "artifacts.json";
2829
private static final String HOSTING_JSON = ENGAGEMENT_DIR + "hosting.json";
2930
private static final String ENGAGEMENT_JSON = ENGAGEMENT_DIR + "engagement.json";
31+
private static final String CATEGORY_JSON = ENGAGEMENT_DIR + "category.json";
3032

3133
@Inject
3234
EngagementService engagementService;
@@ -53,7 +55,7 @@ public class MigrationService {
5355
String commitBranch;
5456

5557
private final Map<Integer, Engagement> allEngagements = new HashMap<>();
56-
58+
5759
/**
5860
* The migration is idempotent so no harm in rerunning. It will only update
5961
* engagements that haven't been migrated.
@@ -65,6 +67,10 @@ public void migrate(boolean migrateUuids, boolean migrateParticipants, boolean m
6567

6668
getAllEngagements(); //hydrate before stream
6769

70+
if(LOGGER.isDebugEnabled()) {
71+
getAllEngagements().values().forEach(e -> LOGGER.debug("To Migrate {}", e.getUuid()));
72+
}
73+
6874
if(migrateUuids) {
6975
LOGGER.info("Start Migrate uuids");
7076
migrateUuids();
@@ -80,6 +86,7 @@ public void migrate(boolean migrateUuids, boolean migrateParticipants, boolean m
8086
private void migrateAll(boolean migrateParticipants, boolean migrateArtifacts, boolean migrateHosting,
8187
boolean migrateEngagements, boolean overwrite, List<String> uuids ) {
8288
int counter = 0;
89+
LOGGER.debug("Migration engagement count {}", getAllEngagements().size());
8390
for(Engagement e : getAllEngagements().values()) {
8491

8592
if(uuids.isEmpty() || uuids.contains(e.getUuid())) {
@@ -91,6 +98,8 @@ private void migrateAll(boolean migrateParticipants, boolean migrateArtifacts, b
9198
if(migrateEngagements) {
9299
content = migrateEngagement(e);
93100
actions.add(createAction(content, ENGAGEMENT_JSON, overwrite, e.getProjectId()));
101+
content = migrateCategories(e);
102+
actions.add(createAction(content, CATEGORY_JSON, overwrite, e.getProjectId()));
94103
}
95104

96105
if(migrateParticipants) {
@@ -109,7 +118,7 @@ private void migrateAll(boolean migrateParticipants, boolean migrateArtifacts, b
109118
}
110119

111120
if(!actions.isEmpty()) {
112-
String commitMessage = "Migrating to v2";
121+
String commitMessage = String.format("Migrating to v2 %s", getEmoji());
113122

114123
CommitMultiple commit = CommitMultiple.builder().id(e.getProjectId()).branch(commitBranch).commitMessage(commitMessage).actions(actions)
115124
.authorName(commitAuthor).authorEmail(commitEmail).build();
@@ -142,35 +151,39 @@ private Action createAction(String content, String filePath, boolean overwrite,
142151
*/
143152
private void migrateUuids() {
144153
List<Project> allProjects = projectService.getProjectsByGroup(engagementRepositoryId, true);
145-
146154
allProjects.parallelStream().forEach(this::updateProjectWithUuid);
147155
}
148156

149-
private String migrateEngagement(Engagement engagement) {
157+
private String migrateCategories(Engagement engagement) {
150158
Engagement copy = clone(engagement, Engagement.class);
151159

152-
if(copy.getCategories() != null) {
160+
if(copy.getCategories() == null) {
161+
copy.setCategories(new ArrayList<>());
162+
} else {
153163
copy.getCategories().forEach(cat -> {
154164
try {
155165
cat.setCreated(convertDateTime(cat.getCreated()));
156-
cat.setUpdated(convertDateTime(cat.getUpdated()));
166+
cat.setEngagementUuid(engagement.getUuid());
167+
cat.setRegion(engagement.getRegion());
157168
} catch(Exception ex) {
158169
LOGGER.error ("dtf exception {}", cat.getCreated(), ex);
159170
}
160171
});
161172
}
162173

163-
if(copy.getUseCases() != null) {
164-
copy.getUseCases().forEach((use -> {
165-
try {
166-
use.setCreated(convertDateTime(use.getCreated()));
167-
use.setUpdated(convertDateTime(use.getUpdated()));
168-
} catch(RuntimeException ex) {
169-
LOGGER.error ("dtf exception {}", use.getCreated(), ex);
170-
}
171-
}));
174+
return json.toJson(copy.getCategories());
175+
}
176+
177+
private String migrateEngagement(Engagement engagement) {
178+
Engagement copy = clone(engagement, Engagement.class);
179+
Set<String> cats = new TreeSet<>();
180+
181+
if(copy.getCategories() != null) {
182+
copy.getCategories().forEach(cat -> cats.add(cat.getName()));
172183
}
173184

185+
copy.setMapCategories(cats);
186+
174187
if(copy.getLaunch() != null) {
175188
try {
176189
Instant.parse(copy.getLaunch().getLaunchedDateTime());
@@ -180,6 +193,31 @@ private String migrateEngagement(Engagement engagement) {
180193
}
181194
}
182195

196+
if(copy.getUseCases() != null) {
197+
for(UseCase use : copy.getUseCases()) {
198+
try {
199+
Instant.parse(use.getCreated());
200+
} catch (DateTimeParseException ex) {
201+
LOGGER.error("No standard use case create date {}", use.getCreated());
202+
use.setCreated(convertDateTime(use.getCreated()));
203+
}
204+
205+
try {
206+
Instant.parse(use.getUpdated());
207+
} catch (DateTimeParseException ex) {
208+
LOGGER.error("No standard use case update date {}", use.getUpdated());
209+
use.setUpdated(convertDateTime(use.getUpdated()));
210+
}
211+
}
212+
}
213+
214+
copy.setMapRegion(copy.getRegion());
215+
copy.setMapType(copy.getType());
216+
copy.setName(copy.getProjectName());
217+
copy.setCategories(null);
218+
copy.setRegion(null);
219+
copy.setType(null);
220+
copy.setProjectName(null);
183221

184222
copy.setHostingEnvironments(null);
185223
copy.setEngagementUsers(null);
@@ -198,12 +236,12 @@ private String convertDateTime(String oldDateTime) {
198236

199237
String date = null;
200238

201-
for(int i=0; i<patterns.length; i++) {
239+
for (String pattern : patterns) {
202240
try {
203-
date = convertDateTime(oldDateTime, patterns[i]);
241+
date = convertDateTime(oldDateTime, pattern);
204242
break;
205243
} catch (DateTimeParseException ex) {
206-
LOGGER.error("No standard date {}, pattern {}", oldDateTime, patterns[i]);
244+
LOGGER.error("No standard date {}, pattern {}", oldDateTime, pattern);
207245
}
208246
}
209247

@@ -302,4 +340,14 @@ private String migrateHostingToGitlab(Engagement engagement) {
302340
return json.toJson(hosting);
303341
}
304342

343+
private String getEmoji() {
344+
String bear = "\ud83d\udc3b";
345+
346+
int bearCodePoint = bear.codePointAt(bear.offsetByCodePoints(0, 0));
347+
int mysteryAnimalCodePoint = bearCodePoint + new SecureRandom().nextInt(144);
348+
char[] mysteryEmoji = { Character.highSurrogate(mysteryAnimalCodePoint),
349+
Character.lowSurrogate(mysteryAnimalCodePoint) };
350+
351+
return String.valueOf(mysteryEmoji);
352+
}
305353
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public static void setup() {
6060
List<UseCase> uses = new ArrayList<>();
6161
uses.add(UseCase.builder().title("use case").created("2021-08-26T20:42:46.050483")
6262
.updated("2021-08-26T20:42:46.050483").build());
63-
uses.add(UseCase.builder().title("use case2").created("2021-08-26T20:42:46.050483").build());
63+
uses.add(UseCase.builder().title("use case2").created("2021-08-26T20:42:46.050483")
64+
.updated("2021-08-26T20:42:46.050483").build());
6465

6566
Launch launch = Launch.builder().launchedBy("Alfredo").launchedByEmail("[email protected]").launchedDateTime("2021-08-26T20:42:46.050").build();
6667

0 commit comments

Comments
 (0)