Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit 52474f3

Browse files
author
sowerstl
committed
Endpoint to get CodeGov JSON file info; (DOECODE-636)
1 parent dfe7a59 commit 52474f3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

src/main/java/gov/osti/services/CodeGov.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
99
import com.fasterxml.jackson.databind.node.ObjectNode;
10+
import com.fasterxml.jackson.databind.node.ArrayNode;
1011
import com.fasterxml.jackson.databind.ser.FilterProvider;
1112
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
1213
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
@@ -19,8 +20,16 @@
1920
import java.io.IOException;
2021
import java.nio.file.Files;
2122
import java.nio.file.Paths;
23+
import java.nio.file.attribute.FileTime;
24+
import java.time.ZoneId;
25+
import java.time.format.DateTimeFormatter;
26+
import java.time.format.FormatStyle;
2227
import java.util.ArrayList;
2328
import java.util.List;
29+
import java.util.Locale;
30+
import java.util.Map;
31+
import java.util.TreeMap;
32+
import java.nio.file.attribute.BasicFileAttributes;
2433

2534
import javax.persistence.EntityManager;
2635
import javax.persistence.TypedQuery;
@@ -58,6 +67,7 @@ public class CodeGov {
5867

5968
// logger instance
6069
private static final Logger log = LoggerFactory.getLogger(CodeGov.class);
70+
6171
// absolute filesystem location to store uploaded files, if any
6272
private static final String FILE_UPLOADS = DoeServletContextListener.getConfigurationProperty("file.uploads");
6373

@@ -221,6 +231,89 @@ public Response listCodeGovData()
221231
}
222232
}
223233

234+
/**
235+
* Acquire info about the latest CodeGov file.
236+
*
237+
* @return extra Code.gov JSON information
238+
* @throws JsonProcessingException
239+
*/
240+
@GET
241+
@Path("/info")
242+
@Produces(MediaType.APPLICATION_JSON)
243+
public Response getCodeGovInfo() throws JsonProcessingException {
244+
try {
245+
java.nio.file.Path codegovFile = Paths.get(FILE_UPLOADS, "codegov", "code.json");
246+
247+
// if no file was found, fail
248+
if (!Files.exists(codegovFile))
249+
return ErrorResponse.status(Response.Status.NOT_FOUND, "Code.gov JSON file not found!").build();
250+
251+
// read file info
252+
BasicFileAttributes attr;
253+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/dd/yyyy h:mm:ss a").withLocale(Locale.US).withZone(ZoneId.systemDefault());
254+
255+
try {
256+
attr = Files.readAttributes(codegovFile, BasicFileAttributes.class);
257+
} catch (Exception e) {
258+
log.warn("Cannot get the Code Gov JSON file attributes - " + e);
259+
return ErrorResponse.status(Response.Status.NOT_FOUND, "Unable to get file attributes from Code.gov JSON file!").build();
260+
}
261+
262+
// read file
263+
JsonNode json = JSON_MAPPER.readTree(new FileInputStream(codegovFile.toString()));
264+
265+
// get releases
266+
JsonNode releases = json.get("releases");
267+
268+
// iterate, counting usage types
269+
Map<String, Integer> usageMap = new TreeMap();
270+
if (releases.isArray()) {
271+
ArrayNode releasesArray = (ArrayNode) releases;
272+
273+
for (int i = 0; i < releasesArray.size(); i++) {
274+
JsonNode projectNode = releasesArray.get(i);
275+
276+
String permission = "Unknown";
277+
try {
278+
permission = projectNode.get("permissions").get("usageType").asText();
279+
} catch (Exception e) {
280+
permission = "Unknown";
281+
}
282+
283+
Integer count = usageMap.get(permission);
284+
count = (count == null) ? 1 : count + 1;
285+
286+
usageMap.put(permission, count);
287+
288+
}
289+
}
290+
291+
// generate return JSON
292+
ObjectNode info = JSON_MAPPER.createObjectNode();
293+
ObjectNode records = JSON_MAPPER.createObjectNode();
294+
ObjectNode usage = JSON_MAPPER.createObjectNode();
295+
296+
for (String key : usageMap.keySet()) {
297+
int count = usageMap.get(key);
298+
usage.put(key, count);
299+
}
300+
301+
records.put("total", (releases != null ? releases.size() : 0));
302+
records.put("usage", usage);
303+
304+
info.put("records", records);
305+
info.put("last_accessed", formatter.format(attr.lastAccessTime().toInstant()));
306+
info.put("last_modified", formatter.format(attr.lastModifiedTime().toInstant()));
307+
308+
return Response.status(Response.Status.OK)
309+
.entity(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(info)).build();
310+
311+
} catch (IOException e) { // IO
312+
log.warn("JSON conversion error: " + e.getMessage());
313+
return ErrorResponse.status(Response.Status.INTERNAL_SERVER_ERROR, "JSON conversion error.").build();
314+
}
315+
}
316+
224317
/**
225318
* Acquire a listing of all Approved records.
226319
*

0 commit comments

Comments
 (0)