aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-12-21 12:16:29 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2012-12-21 12:16:29 +0100
commite7b1958ce5e93ead2d7d3c74eabe00a4186a048a (patch)
treea73bfce74bc740f76f11e5376a233eed026b93f5 /src/main
parentd25d523d2a7f7e4c3446d81740e09e487ad807d0 (diff)
downloadesper-testing-e7b1958ce5e93ead2d7d3c74eabe00a4186a048a.tar.gz
esper-testing-e7b1958ce5e93ead2d7d3c74eabe00a4186a048a.tar.bz2
esper-testing-e7b1958ce5e93ead2d7d3c74eabe00a4186a048a.tar.xz
esper-testing-e7b1958ce5e93ead2d7d3c74eabe00a4186a048a.zip
o Adding a 'core' domain module.
o Adding a table scanner and a job that converts jenkins builds to builds.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/io/trygvis/esper/testing/AbstractEntity.java (renamed from src/main/java/io/trygvis/esper/testing/AbstractDto.java)8
-rw-r--r--src/main/java/io/trygvis/esper/testing/Daos.java3
-rw-r--r--src/main/java/io/trygvis/esper/testing/EntityRef.java23
-rw-r--r--src/main/java/io/trygvis/esper/testing/Util.java27
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/BuildDto.java21
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/CoreDao.java73
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java53
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/PersonDto.java15
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/TablePoller.java140
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/Unbreakable.java4
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java2
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java65
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java2
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java8
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java2
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsUserDto.java2
-rw-r--r--src/main/resources/ddl-core.sql41
-rw-r--r--src/main/resources/ddl-jenkins.sql3
18 files changed, 445 insertions, 47 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/AbstractDto.java b/src/main/java/io/trygvis/esper/testing/AbstractEntity.java
index 6c2e778..927a223 100644
--- a/src/main/java/io/trygvis/esper/testing/AbstractDto.java
+++ b/src/main/java/io/trygvis/esper/testing/AbstractEntity.java
@@ -4,20 +4,20 @@ import org.joda.time.*;
import java.util.*;
-public abstract class AbstractDto {
+public abstract class AbstractEntity {
public final UUID uuid;
public final DateTime createdDate;
- protected AbstractDto(UUID uuid, DateTime createdDate) {
+ protected AbstractEntity(UUID uuid, DateTime createdDate) {
this.uuid = uuid;
this.createdDate = createdDate;
}
public boolean equals(Object o) {
if (this == o) return true;
- if (!(o instanceof AbstractDto)) return false;
+ if (!(o instanceof AbstractEntity)) return false;
- AbstractDto that = (AbstractDto) o;
+ AbstractEntity that = (AbstractEntity) o;
return uuid.equals(that.uuid);
}
diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java
index c80f10c..5ddb75b 100644
--- a/src/main/java/io/trygvis/esper/testing/Daos.java
+++ b/src/main/java/io/trygvis/esper/testing/Daos.java
@@ -1,5 +1,6 @@
package io.trygvis.esper.testing;
+import io.trygvis.esper.testing.core.*;
import io.trygvis.esper.testing.gitorious.*;
import io.trygvis.esper.testing.jenkins.*;
@@ -28,6 +29,7 @@ public class Daos implements Closeable {
public final GitoriousProjectDao gitoriousProjectDao;
public final GitoriousRepositoryDao gitoriousRepositoryDao;
public final JenkinsDao jenkinsDao;
+ public final CoreDao coreDao;
public final int seq;
public static int counter = 1;
@@ -38,6 +40,7 @@ public class Daos implements Closeable {
gitoriousProjectDao = new GitoriousProjectDao(c);
gitoriousRepositoryDao = new GitoriousRepositoryDao(c);
jenkinsDao = new JenkinsDao(c);
+ coreDao = new CoreDao(c);
}
public void close() throws IOException {
diff --git a/src/main/java/io/trygvis/esper/testing/EntityRef.java b/src/main/java/io/trygvis/esper/testing/EntityRef.java
new file mode 100644
index 0000000..6ed4147
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/EntityRef.java
@@ -0,0 +1,23 @@
+package io.trygvis.esper.testing;
+
+import java.util.*;
+
+public abstract class EntityRef {
+ public final UUID uuid;
+ public final String type;
+
+ protected EntityRef(UUID uuid, String type) {
+ this.uuid = uuid;
+ this.type = type;
+ }
+
+ public static class JenkinsRef extends EntityRef {
+ private JenkinsRef(UUID uuid) {
+ super(uuid, "jenkins");
+ }
+ }
+
+ public static JenkinsRef jenkinsRef(UUID uuid) {
+ return new JenkinsRef(uuid);
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/Util.java b/src/main/java/io/trygvis/esper/testing/Util.java
index 5e4b2a5..4e1bf93 100644
--- a/src/main/java/io/trygvis/esper/testing/Util.java
+++ b/src/main/java/io/trygvis/esper/testing/Util.java
@@ -3,11 +3,16 @@ package io.trygvis.esper.testing;
import fj.*;
import fj.data.*;
import static fj.data.Option.*;
+
+import io.trygvis.esper.testing.sql.*;
import org.jdom2.*;
import org.joda.time.*;
import java.net.*;
import java.sql.*;
+import java.sql.Array;
+import java.util.*;
+import java.util.List;
public class Util {
public static final F<Timestamp, java.util.Date> timestampToDate = new F<Timestamp, java.util.Date>() {
@@ -75,4 +80,26 @@ public class Util {
public static Option<Element> child(Element e, String childName) {
return fromNull(e.getChild(childName));
}
+
+ public static UUID[] toUuidArray(ResultSet rs, int index) throws SQLException {
+ Array array = rs.getArray(index);
+ if(array == null) {
+ return new UUID[0];
+ }
+ String[] strings = (String[]) array.getArray();
+ UUID[] uuids = new UUID[strings.length];
+ for (int i = 0; i < strings.length; i++) {
+ uuids[i] = UUID.fromString(strings[i]);
+ }
+ return uuids;
+ }
+
+ public static <A> List<A> toList(PreparedStatement s, SqlF<ResultSet, A> f) throws SQLException {
+ List<A> list = new ArrayList<>();
+ ResultSet rs = s.executeQuery();
+ while(rs.next()) {
+ list.add(f.apply(rs));
+ }
+ return list;
+ }
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/BuildDto.java b/src/main/java/io/trygvis/esper/testing/core/BuildDto.java
new file mode 100644
index 0000000..37de1b3
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/BuildDto.java
@@ -0,0 +1,21 @@
+package io.trygvis.esper.testing.core;
+
+import io.trygvis.esper.testing.*;
+import org.joda.time.*;
+
+import java.util.*;
+
+class BuildDto extends AbstractEntity {
+ public final DateTime timestamp;
+ public final boolean success;
+ public final UUID referenceUuid;
+ public final String referenceType;
+
+ BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, UUID referenceUuid, String referenceType) {
+ super(uuid, createdDate);
+ this.timestamp = timestamp;
+ this.success = success;
+ this.referenceUuid = referenceUuid;
+ this.referenceType = referenceType;
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/core/CoreDao.java b/src/main/java/io/trygvis/esper/testing/core/CoreDao.java
new file mode 100644
index 0000000..a010dad
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/CoreDao.java
@@ -0,0 +1,73 @@
+package io.trygvis.esper.testing.core;
+
+import io.trygvis.esper.testing.*;
+import io.trygvis.esper.testing.sql.*;
+import org.joda.time.*;
+
+import java.sql.*;
+import java.util.*;
+
+import static io.trygvis.esper.testing.sql.SqlOption.*;
+import static java.lang.System.*;
+
+public class CoreDao {
+ private final Connection c;
+
+ public static final String PERSON = "uuid, created_date, name";
+
+ public static final SqlF<ResultSet, PersonDto> person = new SqlF<ResultSet, PersonDto>() {
+ public PersonDto apply(ResultSet rs) throws SQLException {
+ int i = 1;
+ return new PersonDto(
+ UUID.fromString(rs.getString(i++)),
+ new DateTime(rs.getTimestamp(i++).getTime()),
+ rs.getString(i));
+ }
+ };
+
+ public static final String BUILD = "uuid, created_date, timestamp, success, reference_type, reference_uuid";
+
+ public CoreDao(Connection c) {
+ this.c = c;
+ }
+
+ public SqlOption<PersonDto> selectPerson(String id) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE id=?")) {
+ int i = 1;
+ s.setString(i, id);
+ return fromRs(s.executeQuery()).map(person);
+ }
+ }
+
+ public SqlOption<PersonDto> selectPersonByJenkinsUuid(UUID jenkinsUser) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=(SELECT person FROM person_jenkins_user WHERE jenkins_user=?)")) {
+ int i = 1;
+ s.setString(i, jenkinsUser.toString());
+ return fromRs(s.executeQuery()).map(person);
+ }
+ }
+
+ public UUID insertBuild(DateTime timestamp, boolean success, EntityRef ref) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("INSERT INTO build(" + BUILD + ") VALUES(?, ?, ?, ?, ?, ?)")) {
+ UUID uuid = UUID.randomUUID();
+ int i = 1;
+ s.setString(i++, uuid.toString());
+ s.setTimestamp(i++, new Timestamp(currentTimeMillis()));
+ s.setTimestamp(i++, new Timestamp(timestamp.getMillis()));
+ s.setBoolean(i++, success);
+ s.setString(i++, ref.type);
+ s.setString(i, ref.uuid.toString());
+ s.executeUpdate();
+ return uuid;
+ }
+ }
+
+ public void insertBuildParticipant(UUID build, UUID person) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("INSERT INTO build_participant(build, person) VALUES(?, ?)")) {
+ int i = 1;
+ s.setString(i++, build.toString());
+ s.setString(i, person.toString());
+ s.executeUpdate();
+ }
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java b/src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java
new file mode 100644
index 0000000..cb9157c
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java
@@ -0,0 +1,53 @@
+package io.trygvis.esper.testing.core;
+
+import com.jolbox.bonecp.*;
+import io.trygvis.esper.testing.*;
+import io.trygvis.esper.testing.jenkins.*;
+import io.trygvis.esper.testing.sql.*;
+import org.slf4j.*;
+
+import java.sql.*;
+import java.util.*;
+
+import static fj.data.Option.some;
+import static io.trygvis.esper.testing.Config.*;
+import static io.trygvis.esper.testing.EntityRef.jenkinsRef;
+
+public class JenkinsBuildPoller implements TablePoller.NewRowCallback<JenkinsBuildDto> {
+ Logger logger = LoggerFactory.getLogger(getClass());
+
+ public static void main(String[] args) throws Exception {
+ String pollerName = "jenkins_build";
+ String tableName = "jenkins_build";
+ String columnNames = JenkinsDao.JENKINS_BUILD;
+ SqlF<ResultSet, JenkinsBuildDto> f = JenkinsDao.jenkinsBuild;
+ TablePoller.NewRowCallback<JenkinsBuildDto> callback = new JenkinsBuildPoller();
+
+ Config config = loadFromDisk();
+
+ BoneCPDataSource dataSource = config.createBoneCp();
+
+ new TablePoller<>(pollerName, tableName, columnNames, some("array_length(users, 1) > 0"), f, callback).work(dataSource);
+ }
+
+ public void process(Connection c, JenkinsBuildDto jenkinsBuild) throws SQLException {
+ Daos daos = new Daos(c);
+ CoreDao coreDao = daos.coreDao;
+
+ UUID uuid = coreDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid));
+ logger.info("Created build uuid={}", uuid);
+
+ for (UUID user : jenkinsBuild.users) {
+ SqlOption<PersonDto> personO = coreDao.selectPersonByJenkinsUuid(user);
+
+ // This happens if no one has claimed the user id.
+ if(personO.isNone()) {
+ continue;
+ }
+
+ UUID person = personO.get().uuid;
+ logger.info("Created build participant, person={}", person);
+ coreDao.insertBuildParticipant(uuid, person);
+ }
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/core/PersonDto.java b/src/main/java/io/trygvis/esper/testing/core/PersonDto.java
new file mode 100644
index 0000000..33355a3
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/PersonDto.java
@@ -0,0 +1,15 @@
+package io.trygvis.esper.testing.core;
+
+import io.trygvis.esper.testing.*;
+import org.joda.time.*;
+
+import java.util.*;
+
+public class PersonDto extends AbstractEntity {
+ public final String name;
+
+ public PersonDto(UUID uuid, DateTime createdDate, String name) {
+ super(uuid, createdDate);
+ this.name = name;
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java
new file mode 100644
index 0000000..58a3e75
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java
@@ -0,0 +1,140 @@
+package io.trygvis.esper.testing.core;
+
+import fj.data.*;
+import io.trygvis.esper.testing.sql.*;
+import org.joda.time.format.*;
+import org.slf4j.*;
+
+import javax.sql.*;
+import java.sql.*;
+import java.util.*;
+import java.util.List;
+
+import static fj.data.Option.*;
+import static java.lang.System.*;
+
+public class TablePoller<A> {
+
+ private static final DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
+
+ private final Logger logger = LoggerFactory.getLogger(getClass());
+ private final String pollerName;
+ private final String tableName;
+ private final String columnNames;
+ private final String filter;
+ private final SqlF<ResultSet, A> f;
+ private final NewRowCallback<A> callback;
+
+ public TablePoller(String pollerName, String tableName, String columnNames, Option<String> filter, SqlF<ResultSet, A> f, NewRowCallback<A> callback) {
+ this.pollerName = pollerName;
+ this.tableName = tableName;
+ this.columnNames = columnNames;
+ this.filter = filter.orSome("true");
+ this.f = f;
+ this.callback = callback;
+ }
+
+ public void work(DataSource dataSource) throws Exception {
+ while (true) {
+ try (Connection c = dataSource.getConnection()) {
+ long start = currentTimeMillis();
+ TablePollerDao dao = new TablePollerDao(c);
+
+ Option<Timestamp> o = dao.getLastCreatedDateForPoller();
+
+ if (o.isNone()) {
+ logger.info("First run of poller '{}'", pollerName);
+ } else {
+ logger.info("Running poller '{}', last run was {}", pollerName, formatter.print(o.some().getTime()));
+ }
+
+ Timestamp lastCreatedDate = o.orSome(new Timestamp(0));
+
+ Option<Timestamp> o2 = dao.getOldestCreatedDateAfter(lastCreatedDate);
+
+ if (o2.isSome()) {
+ Timestamp oldestCreatedDate = o2.some();
+
+ List<A> rows = dao.getRowsCreatedAt(oldestCreatedDate);
+
+ logger.info("Processing {} rows created at {}", rows.size(), formatter.print(oldestCreatedDate.getTime()));
+
+ for (A row : rows) {
+ callback.process(c, row);
+ }
+ } else {
+ logger.debug("No new rows.");
+
+ Thread.sleep(1000);
+ }
+
+ dao.insertOrUpdate(o.isNone(), o2.orSome(new Timestamp(start)), new Timestamp(start), currentTimeMillis() - start, null);
+
+ c.commit();
+ }
+ }
+ }
+
+ public static interface NewRowCallback<A> {
+ void process(Connection c, A A) throws SQLException;
+ }
+
+ private class TablePollerDao {
+ private final Connection c;
+
+ private TablePollerDao(Connection c) {
+ this.c = c;
+ }
+
+ public Option<Timestamp> getLastCreatedDateForPoller() throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT last_created_date FROM table_poller_status WHERE poller_name=?")) {
+ s.setString(1, pollerName);
+ ResultSet rs = s.executeQuery();
+ if (!rs.next()) {
+ return none();
+ }
+
+ return some(rs.getTimestamp(1));
+ }
+ }
+
+ public Option<Timestamp> getOldestCreatedDateAfter(Timestamp timestamp) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT min(created_date) FROM " + tableName + " WHERE created_date > ? AND " + filter)) {
+ s.setTimestamp(1, timestamp);
+ ResultSet rs = s.executeQuery();
+ rs.next();
+ return fromNull(rs.getTimestamp(1));
+ }
+ }
+
+ public List<A> getRowsCreatedAt(Timestamp timestamp) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + columnNames + " FROM " + tableName + " WHERE created_date = ? AND " + filter)) {
+ s.setTimestamp(1, timestamp);
+
+ ResultSet rs = s.executeQuery();
+
+ List<A> list = new ArrayList<>();
+ while (rs.next()) {
+ list.add(f.apply(rs));
+ }
+ return list;
+ }
+ }
+
+ public void insertOrUpdate(boolean insert, Timestamp lastCreatedDate, Timestamp now, long duration, String status) throws SQLException {
+ String insertSql = "INSERT INTO table_poller_status(last_created_date, last_run, duration, status, poller_name) VALUES(?, ?, ?, ?, ?)";
+
+ String updateSql = "UPDATE table_poller_status SET last_created_date=?, last_run=?, duration=?, status=? WHERE poller_name=?";
+
+ try (PreparedStatement s = c.prepareStatement(insert ? insertSql : updateSql)) {
+ int i = 1;
+ s.setTimestamp(i++, lastCreatedDate);
+ s.setTimestamp(i++, now);
+ s.setLong(i++, duration);
+ s.setString(i++, status);
+ s.setString(i, pollerName);
+ s.executeUpdate();
+ }
+ }
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java b/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java
new file mode 100644
index 0000000..1200516
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java
@@ -0,0 +1,4 @@
+package io.trygvis.esper.testing.core;
+
+public class Unbreakable {
+}
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java
index 04c21c0..41d90ae 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java
@@ -6,7 +6,7 @@ import org.joda.time.*;
import java.net.*;
import java.util.*;
-public class JenkinsBuildDto extends AbstractDto {
+public class JenkinsBuildDto extends AbstractEntity {
public final UUID job;
public final String entryId;
public final URI url;
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java
index 2cd71c0..7f52bd0 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java
@@ -6,11 +6,11 @@ import org.joda.time.*;
import java.net.*;
import java.sql.*;
-import java.sql.Array;
import java.util.*;
import java.util.List;
import static fj.data.Option.*;
+import static io.trygvis.esper.testing.Util.toUuidArray;
import static io.trygvis.esper.testing.sql.SqlOption.fromRs;
import static java.lang.System.*;
@@ -47,20 +47,22 @@ public class JenkinsDao {
return list;
}
- private JenkinsJobDto jenkinsJob(ResultSet rs) throws SQLException {
- int i = 1;
- return new JenkinsJobDto(
- UUID.fromString(rs.getString(i++)),
- new DateTime(rs.getTimestamp(i++).getTime()),
- UUID.fromString(rs.getString(i++)),
- URI.create(rs.getString(i++)),
- fromNull(rs.getString(i)));
- }
+ public static final SqlF<ResultSet, JenkinsJobDto> jenkinsJob = new SqlF<ResultSet, JenkinsJobDto>() {
+ public JenkinsJobDto apply(ResultSet rs) throws SQLException {
+ int i = 1;
+ return new JenkinsJobDto(
+ UUID.fromString(rs.getString(i++)),
+ new DateTime(rs.getTimestamp(i++).getTime()),
+ UUID.fromString(rs.getString(i++)),
+ URI.create(rs.getString(i++)),
+ fromNull(rs.getString(i)));
+ }
+ };
public List<JenkinsJobDto> toJobList(ResultSet rs) throws SQLException {
List<JenkinsJobDto> list = new ArrayList<>();
while (rs.next()) {
- list.add(jenkinsJob(rs));
+ list.add(jenkinsJob.apply(rs));
}
return list;
}
@@ -93,19 +95,6 @@ public class JenkinsDao {
}
};
- private static UUID[] toUuidArray(ResultSet rs, int index) throws SQLException {
- Array array = rs.getArray(index);
- if(array == null) {
- return new UUID[0];
- }
- String[] strings = (String[]) array.getArray();
- UUID[] uuids = new UUID[strings.length];
- for (int i = 0; i < strings.length; i++) {
- uuids[i] = UUID.fromString(strings[i]);
- }
- return uuids;
- }
-
public List<JenkinsServerDto> selectServers(boolean enabledOnly) throws SQLException {
String sql = "SELECT " + JENKINS_SERVER + " FROM jenkins_server";
@@ -131,16 +120,17 @@ public class JenkinsDao {
}
}
- public Option<JenkinsJobDto> selectJobByUrl(URI url) throws SQLException {
+ public SqlOption<JenkinsJobDto> selectJob(UUID uuid) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_JOB + " FROM jenkins_job WHERE uuid=?")) {
+ s.setString(1, uuid.toString());
+ return fromRs(s.executeQuery()).map(jenkinsJob);
+ }
+ }
+
+ public SqlOption<JenkinsJobDto> selectJobByUrl(URI url) throws SQLException {
try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_JOB + " FROM jenkins_job WHERE url=?")) {
s.setString(1, url.toASCIIString());
- ResultSet rs = s.executeQuery();
-
- if (!rs.next()) {
- return none();
- }
-
- return some(jenkinsJob(rs));
+ return fromRs(s.executeQuery()).map(jenkinsJob);
}
}
@@ -211,7 +201,16 @@ public class JenkinsDao {
}
}
- public SqlOption<JenkinsUserDto> selectUser(UUID server, String absoluteUrl) throws SQLException {
+ public SqlOption<JenkinsUserDto> selectUser(UUID uuid, UUID server) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_USER + " FROM jenkins_user WHERE uuid=? AND server=?")) {
+ int i = 1;
+ s.setString(i++, uuid.toString());
+ s.setString(i, server.toString());
+ return fromRs(s.executeQuery()).map(jenkinsUser);
+ }
+ }
+
+ public SqlOption<JenkinsUserDto> selectUserByAbsoluteUrl(UUID server, String absoluteUrl) throws SQLException {
try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_USER + " FROM jenkins_user WHERE server=? AND absolute_url=?")) {
int i = 1;
s.setString(i++, server.toString());
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java
index f4ef401..2fb5b99 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java
@@ -7,7 +7,7 @@ import org.joda.time.*;
import java.net.*;
import java.util.*;
-public class JenkinsJobDto extends AbstractDto {
+public class JenkinsJobDto extends AbstractEntity {
public final UUID server;
public final URI url;
public final Option<String> displayName;
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java
index 9f2969a..8f04faf 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java
@@ -87,13 +87,15 @@ public class JenkinsServerActor implements TransactionalActor {
UUID uuid = authors.get(url);
if(uuid == null) {
- SqlOption<JenkinsUserDto> userO = dao.selectUser(server.uuid, url);
+ SqlOption<JenkinsUserDto> userO = dao.selectUserByAbsoluteUrl(server.uuid, url);
if (userO.isNone()) {
logger.info("New user: {}", url);
uuid = dao.insertUser(server.uuid, url);
} else {
uuid = userO.get().uuid;
}
+
+ authors.put(url, uuid);
}
users.add(uuid);
@@ -106,12 +108,12 @@ public class JenkinsServerActor implements TransactionalActor {
URI jobUrl = extrapolateJobUrlFromBuildUrl(build.url.toASCIIString());
- Option<JenkinsJobDto> jobDtoOption = dao.selectJobByUrl(jobUrl);
+ SqlOption<JenkinsJobDto> jobDtoOption = dao.selectJobByUrl(jobUrl);
UUID job;
if (jobDtoOption.isSome()) {
- job = jobDtoOption.some().uuid;
+ job = jobDtoOption.get().uuid;
} else {
logger.info("New job: {}, fetching info", jobUrl);
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java
index 6ad8e36..3cf9e7c 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java
@@ -6,7 +6,7 @@ import org.joda.time.*;
import java.net.*;
import java.util.*;
-public class JenkinsServerDto extends AbstractDto {
+public class JenkinsServerDto extends AbstractEntity {
public final URI url;
public final boolean enabled;
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsUserDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsUserDto.java
index d3ea996..d54f9fc 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsUserDto.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsUserDto.java
@@ -5,7 +5,7 @@ import org.joda.time.*;
import java.util.*;
-public class JenkinsUserDto extends AbstractDto {
+public class JenkinsUserDto extends AbstractEntity {
public final UUID server;
public final String absoluteUrl;
diff --git a/src/main/resources/ddl-core.sql b/src/main/resources/ddl-core.sql
index fb85585..25f373b 100644
--- a/src/main/resources/ddl-core.sql
+++ b/src/main/resources/ddl-core.sql
@@ -1,5 +1,13 @@
BEGIN;
+DROP TABLE IF EXISTS build_participant;
+DROP TABLE IF EXISTS build;
+DROP TABLE IF EXISTS person_badge_progress;
+DROP TABLE IF EXISTS person_badge;
+DROP TABLE IF EXISTS person_jenkins_user;
+DROP TABLE IF EXISTS person;
+DROP TABLE IF EXISTS table_poller_status;
+
CREATE TABLE table_poller_status (
poller_name VARCHAR(100) NOT NULL,
last_created_date TIMESTAMP,
@@ -12,11 +20,19 @@ CREATE TABLE table_poller_status (
CREATE TABLE person (
uuid CHAR(36) NOT NULL,
created_date TIMESTAMP NOT NULL,
---The users from the different jenkins servers this user has claimed
- jenkins_users CHAR(36) [],
+ name VARCHAR(100),
CONSTRAINT pk_person PRIMARY KEY (uuid)
);
+--The users from the different jenkins servers this user has claimed
+CREATE TABLE person_jenkins_user (
+ person CHAR(36),
+ jenkins_user CHAR(36),
+ CONSTRAINT pk_person_jenkins_user PRIMARY KEY (person, jenkins_user),
+ CONSTRAINT fk_person_jenkins_user__person FOREIGN KEY (person) REFERENCES person (uuid),
+ CONSTRAINT fk_person_jenkins_user__jenkins_user FOREIGN KEY (jenkins_user) REFERENCES jenkins_user (uuid)
+);
+
-- Badges received
CREATE TABLE person_badge (
uuid CHAR(36) NOT NULL,
@@ -34,4 +50,25 @@ CREATE TABLE person_badge_progress (
CONSTRAINT pk_person_badge_progress PRIMARY KEY (uuid)
);
+CREATE TABLE build (
+ uuid CHAR(36) NOT NULL,
+ created_date TIMESTAMP NOT NULL,
+
+ timestamp TIMESTAMP NOT NULL,
+ success BOOL NOT NULL,
+
+ reference_type VARCHAR(100) NOT NULL,
+ reference_uuid CHAR(36) NOT NULL,
+
+ CONSTRAINT pk_build PRIMARY KEY (uuid)
+);
+
+CREATE TABLE build_participant (
+ build CHAR(36) NOT NULL,
+ person CHAR(36) NOT NULL,
+ CONSTRAINT pk_build_participant PRIMARY KEY (build, person),
+ CONSTRAINT fk_build_participant_build FOREIGN KEY (build) REFERENCES build (uuid),
+ CONSTRAINT fk_build_participant_person FOREIGN KEY (person) REFERENCES person (uuid)
+);
+
COMMIT;
diff --git a/src/main/resources/ddl-jenkins.sql b/src/main/resources/ddl-jenkins.sql
index 45de998..bdda74b 100644
--- a/src/main/resources/ddl-jenkins.sql
+++ b/src/main/resources/ddl-jenkins.sql
@@ -62,6 +62,7 @@ CREATE TABLE jenkins_user (
INSERT INTO jenkins_server (uuid, created_date, url, enabled) VALUES
('782a75f6-40a4-11e2-aca6-20cf30557fa0', CURRENT_TIMESTAMP, 'https://builds.apache.org', FALSE),
('4c473c86-40ad-11e2-ae61-20cf30557fa0', CURRENT_TIMESTAMP, 'http://ci.jruby.org', FALSE),
-('518c6162-411b-11e2-b63c-20cf30557fa0', CURRENT_TIMESTAMP, 'http://www.simantics.org/jenkins', FALSE);
+('518c6162-411b-11e2-b63c-20cf30557fa0', CURRENT_TIMESTAMP, 'http://www.simantics.org/jenkins', FALSE),
+('3c1a1448-422c-11e2-a7b3-20cf30557fa0', CURRENT_TIMESTAMP, 'https://jenkins.puppetlabs.com', FALSE);
COMMIT;