package io.trygvis.esper.testing.web.resource; import fj.data.*; import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.jenkins.*; import io.trygvis.esper.testing.jenkins.xml.*; import io.trygvis.esper.testing.util.*; import io.trygvis.esper.testing.util.sql.*; import io.trygvis.esper.testing.web.*; import org.joda.time.*; import java.net.*; import java.sql.*; import java.util.*; import java.util.List; import javax.ws.rs.*; import javax.ws.rs.core.*; @Path("/resource/jenkins") public class JenkinsResource extends AbstractResource { public JenkinsResource(DatabaseAccess da) { super(da); } @GET @Path("/server") @Produces(MediaType.APPLICATION_JSON) public List getServers() throws Exception { return da.inTransaction(new JenkinsDaosCallback>() { protected List run() throws SQLException { List list = new ArrayList<>(); for (JenkinsServerDto server : daos.jenkinsDao.selectServers(false)) { list.add(getJenkinsServerJson.apply(server)); } return list; } }); } @GET @Path("/server/{uuid}") @Produces(MediaType.APPLICATION_JSON) public JenkinsServerJson getServer(@PathParam("uuid") String s) throws Exception { final UUID uuid = parseUuid(s); return sql(new JenkinsDaosCallback>() { protected SqlOption run() throws SQLException { return daos.jenkinsDao.selectServer(uuid).map(getJenkinsServerJson); } }); } @GET @Path("/job") @Produces(MediaType.APPLICATION_JSON) public List getJobs(@MagicParam(query = "server") final UUID server, @MagicParam final PageRequest page) throws Exception { return da.inTransaction(new JenkinsDaosCallback>() { protected List run() throws SQLException { List jobs = new ArrayList<>(); for (JenkinsJobDto job : daos.jenkinsDao.selectJobsByServer(server, page)) { jobs.add(getJenkinsJobJson.apply(job)); } return jobs; } }); } @GET @Path("/job/{uuid}") @Produces(MediaType.APPLICATION_JSON) public JenkinsJobDetailJson getJob(@MagicParam final UUID uuid) throws Exception { return sql(new JenkinsDaosCallback>() { protected SqlOption run() throws SQLException { return daos.jenkinsDao.selectJob(uuid).map(getJenkinsJobJsonDetail); } }); } @GET @Path("/build") @Produces(MediaType.APPLICATION_JSON) public List getBuilds(@MagicParam(query = "job") final UUID job, @MagicParam final PageRequest page) throws Exception { return da.inTransaction(new JenkinsDaosCallback>() { protected List run() throws SQLException { List builds = new ArrayList<>(); for (JenkinsBuildDto dto : daos.jenkinsDao.selectBuildByJob(job, page)) { builds.add(getJenkinsBuildJson.apply(dto)); } return builds; } }); } @GET @Path("/build/{uuid}") @Produces(MediaType.APPLICATION_JSON) public JenkinsBuildJsonDetail getBuild(@MagicParam final UUID build) throws Exception { return sql(new JenkinsDaosCallback>() { protected SqlOption run() throws SQLException { return daos.jenkinsDao.selectBuild(build).map(getJenkinsBuildJsonDetail); } }); } @GET @Path("/user/{uuid}") @Produces(MediaType.APPLICATION_JSON) public JenkinsUserJson getUser(@MagicParam final UUID user) throws Exception { return sql(new JenkinsDaosCallback>() { protected SqlOption run() throws SQLException { return daos.jenkinsDao.selectUser(user).map(getJenkinsUserJson); } }); } public static UUID parseUuid(String s) { try { return UUID.fromString(s); } catch (IllegalArgumentException e) { throw new WebApplicationException(Response.Status.BAD_REQUEST); } } abstract class JenkinsDaosCallback implements DatabaseAccess.DaosCallback { protected Daos daos; protected abstract T run() throws SQLException; private final XmlParser xmlParser = new XmlParser(); public T run(Daos daos) throws SQLException { this.daos = daos; return run(); } protected SqlF getJenkinsServerJson = new SqlF() { public JenkinsServerJson apply(JenkinsServerDto server) throws SQLException { int count = daos.jenkinsDao.selectJobCountForServer(server.uuid); List jobs = new ArrayList<>(); for (JenkinsJobDto jobDto : daos.jenkinsDao.selectJobsByServer(server.uuid, PageRequest.FIRST_PAGE)) { jobs.add(getJenkinsJobJson.apply(jobDto)); } return new JenkinsServerJson(server.uuid, server.createdDate, server.name, server.url, server.enabled, count, jobs); } }; protected SqlF getJenkinsJobJson = new SqlF() { public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.url, job.displayName.toNull()); } }; protected SqlF getJenkinsJobJsonDetail = new SqlF() { public JenkinsJobDetailJson apply(JenkinsJobDto dto) throws SQLException { return new JenkinsJobDetailJson( getJenkinsJobJson.apply(dto), daos.jenkinsDao.selectBuildCountByJob(dto.uuid)); } }; protected SqlF getJenkinsBuildJson = new SqlF() { public JenkinsBuildJson apply(JenkinsBuildDto dto) throws SQLException { Option xmlO = daos.fileDao.load(dto.file).toFj(). bind(xmlParser.parseDocument). bind(JenkinsBuildXml.parse); if(xmlO.isNone()) { return new JenkinsBuildJson(dto.uuid, dto.createdDate, new DateTime(dto.createdDate), "unknown", 0, 0); } JenkinsBuildXml xml = xmlO.some(); return new JenkinsBuildJson(dto.uuid, dto.createdDate, new DateTime(xml.timestamp), xml.result.orSome("unknown"), xml.number, xml.duration); } }; protected SqlF getJenkinsBuildJsonDetail = new SqlF() { public JenkinsBuildJsonDetail apply(JenkinsBuildDto dto) throws SQLException { List users = new ArrayList<>(); for (UUID user : dto.users) { users.add(daos.jenkinsDao.selectUser(user).map(getJenkinsUserJson).get()); } return new JenkinsBuildJsonDetail( getJenkinsBuildJson.apply(dto), users); } }; protected SqlF getJenkinsUserJson = new SqlF() { public JenkinsUserJson apply(JenkinsUserDto dto) throws SQLException { return new JenkinsUserJson(dto.uuid, dto.createdDate, dto.absoluteUrl); } }; } } class JenkinsServerJson { public final UUID uuid; public final DateTime createdDate; public final String name; public final URI url; public final boolean enabled; public final int jobCount; public final List recentJobs; JenkinsServerJson(UUID uuid, DateTime createdDate, String name, URI url, boolean enabled, int jobCount, List recentJobs) { this.uuid = uuid; this.createdDate = createdDate; this.name = name; this.url = url; this.enabled = enabled; this.jobCount = jobCount; this.recentJobs = recentJobs; } } class JenkinsJobJson { public final UUID uuid; public final DateTime createdDate; public final UUID server; public final URI url; public final String displayName; JenkinsJobJson(UUID uuid, DateTime createdDate, UUID server, URI url, String displayName) { this.uuid = uuid; this.createdDate = createdDate; this.server = server; this.url = url; this.displayName = displayName; } } class JenkinsJobDetailJson { public final JenkinsJobJson job; public final Integer buildCount; JenkinsJobDetailJson(JenkinsJobJson job, Integer buildCount) { this.job = job; this.buildCount = buildCount; } } class JenkinsBuildJson { public final UUID uuid; public final DateTime createdDate; public final DateTime timestamp; public final String result; public final int number; public final int duration; JenkinsBuildJson(UUID uuid, DateTime createdDate, DateTime timestamp, String result, int number, int duration) { this.uuid = uuid; this.createdDate = createdDate; this.timestamp = timestamp; this.result = result; this.number = number; this.duration = duration; } } class JenkinsBuildJsonDetail { public final JenkinsBuildJson build; public final List participants; JenkinsBuildJsonDetail(JenkinsBuildJson build, List participants) { this.build = build; this.participants = participants; } } class JenkinsUserJson { public final UUID uuid; public final DateTime createdDate; public final String absoluteUrl; JenkinsUserJson(UUID uuid, DateTime createdDate, String absoluteUrl) { this.uuid = uuid; this.createdDate = createdDate; this.absoluteUrl = absoluteUrl; } }