From 78e7933c0af1d11d9476c5ee213c64ad87066f55 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 22 Dec 2012 19:33:23 +0100 Subject: o First badge: UNBREAKABLE. --- src/main/java/io/trygvis/esper/testing/Daos.java | 6 +- .../java/io/trygvis/esper/testing/EntityRef.java | 18 +++ .../io/trygvis/esper/testing/core/TablePoller.java | 20 ++- .../io/trygvis/esper/testing/core/Unbreakable.java | 4 - .../esper/testing/core/badge/UnbreakableBadge.java | 14 ++ .../core/badge/UnbreakableBadgeProgress.java | 57 ++++++++ .../testing/core/badge/UnbreakablePoller.java | 109 +++++++++++++++ .../io/trygvis/esper/testing/core/db/BuildDao.java | 65 +++++++++ .../io/trygvis/esper/testing/core/db/BuildDto.java | 10 +- .../io/trygvis/esper/testing/core/db/CoreDao.java | 73 ---------- .../esper/testing/core/db/PersonBadgeDto.java | 25 ++++ .../testing/core/db/PersonBadgeProgressDto.java | 20 +++ .../trygvis/esper/testing/core/db/PersonDao.java | 154 +++++++++++++++++++++ .../testing/core/jenkins/JenkinsBuildPoller.java | 24 ++-- .../trygvis/esper/testing/util/sql/ResultSetF.java | 7 + 15 files changed, 511 insertions(+), 95 deletions(-) delete mode 100644 src/main/java/io/trygvis/esper/testing/core/Unbreakable.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java delete mode 100644 src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java (limited to 'src/main/java') diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java index 967dae4..142efd6 100644 --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -29,7 +29,8 @@ public class Daos implements Closeable { public final GitoriousProjectDao gitoriousProjectDao; public final GitoriousRepositoryDao gitoriousRepositoryDao; public final JenkinsDao jenkinsDao; - public final CoreDao coreDao; + public final PersonDao personDao; + public final BuildDao buildDao; public final int seq; public static int counter = 1; @@ -40,7 +41,8 @@ public class Daos implements Closeable { gitoriousProjectDao = new GitoriousProjectDao(c); gitoriousRepositoryDao = new GitoriousRepositoryDao(c); jenkinsDao = new JenkinsDao(c); - coreDao = new CoreDao(c); + personDao = new PersonDao(c); + buildDao = new BuildDao(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 index 6ed4147..a4d25cd 100644 --- a/src/main/java/io/trygvis/esper/testing/EntityRef.java +++ b/src/main/java/io/trygvis/esper/testing/EntityRef.java @@ -1,5 +1,6 @@ package io.trygvis.esper.testing; +import java.sql.*; import java.util.*; public abstract class EntityRef { @@ -11,6 +12,23 @@ public abstract class EntityRef { this.type = type; } + public static EntityRef fromRs(ResultSet rs, int i) throws SQLException { + String type = rs.getString(i++); + + if (type == null) { + throw new SQLException("reference type was null."); + } + + UUID uuid = UUID.fromString(rs.getString(i)); + + switch (type) { + case "jenkins": + return new JenkinsRef(uuid); + default: + throw new SQLException("Unknown reference type: " + type); + } + } + public static class JenkinsRef extends EntityRef { private JenkinsRef(UUID uuid) { super(uuid, "jenkins"); diff --git a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java index 062f5e7..4c31ce5 100644 --- a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java @@ -21,6 +21,8 @@ public class TablePoller { private final SqlF f; private final NewRowCallback callback; + private boolean testMode; + public TablePoller(String pollerName, String tableName, String columnNames, Option filter, SqlF f, NewRowCallback callback) { this.pollerName = pollerName; this.tableName = tableName; @@ -69,17 +71,31 @@ public class TablePoller { logger.debug("No new rows."); } - Thread.sleep(10 * 1000); +// if (testMode) { +// logger.info("TEST MODE: rolling back"); +// c.rollback(); +// } dao.insertOrUpdate(o.isNone(), seq, new Timestamp(start), currentTimeMillis() - start, null); + start = currentTimeMillis(); c.commit(); + long end = currentTimeMillis(); + + logger.info("COMMIT performed in {}ms", end - start); + + Thread.sleep(10 * 1000); } } } + public TablePoller testMode(boolean testMode) { + this.testMode = testMode; + return this; + } + public static interface NewRowCallback { - void process(Connection c, A A) throws SQLException; + void process(Connection c, A A) throws Exception; } private class TablePollerDao { diff --git a/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java b/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java deleted file mode 100644 index 1200516..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.trygvis.esper.testing.core; - -public class Unbreakable { -} diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java new file mode 100644 index 0000000..a4f1e54 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java @@ -0,0 +1,14 @@ +package io.trygvis.esper.testing.core.badge; + +class UnbreakableBadge { + // Configuration for this badge + public static final int LEVEL_1_COUNT = 10; + public static final int LEVEL_2_COUNT = 20; + public static final int LEVEL_3_COUNT = 50; + + public final int level; + + UnbreakableBadge(int level) { + this.level = level; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java new file mode 100644 index 0000000..871be25 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java @@ -0,0 +1,57 @@ +package io.trygvis.esper.testing.core.badge; + +import fj.*; +import fj.data.*; +import io.trygvis.esper.testing.core.db.*; + +import java.util.*; + +import static fj.P.p; +import static fj.data.Option.some; + +class UnbreakableBadgeProgress { + public final UUID person; + public final int count; + + private UnbreakableBadgeProgress(UUID person, int count) { + this.person = person; + this.count = count; + } + + @SuppressWarnings("UnusedDeclaration") + private UnbreakableBadgeProgress() { + person = null; + count = -1; + } + + public static UnbreakableBadgeProgress initial(UUID person) { + return new UnbreakableBadgeProgress(person, 0); + } + + public P2> onBuild(BuildDto build) { + if (!build.success) { + return p(initial(person), Option.none()); + } + + int count = this.count + 1; + + if (count == UnbreakableBadge.LEVEL_3_COUNT) { + return p(initial(person), some(new UnbreakableBadge(3))); + } + + if (count == UnbreakableBadge.LEVEL_2_COUNT) { + return p(new UnbreakableBadgeProgress(person, count), some(new UnbreakableBadge(2))); + } + + if (count == UnbreakableBadge.LEVEL_1_COUNT) { + return p(new UnbreakableBadgeProgress(person, count), some(new UnbreakableBadge(1))); + } + + return p(new UnbreakableBadgeProgress(person, count), Option.none()); + } + + @Override + public String toString() { + return "UnbreakableBadgeProgress{person=" + person + ", count=" + count + '}'; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java new file mode 100644 index 0000000..38baa73 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java @@ -0,0 +1,109 @@ +package io.trygvis.esper.testing.core.badge; + +import com.jolbox.bonecp.*; +import fj.*; +import fj.data.*; +import io.trygvis.esper.testing.*; +import io.trygvis.esper.testing.core.*; +import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.util.sql.*; +import org.codehaus.jackson.map.*; +import org.slf4j.*; + +import java.io.*; +import java.sql.*; +import java.util.List; +import java.util.*; + +import static io.trygvis.esper.testing.Config.*; +import static io.trygvis.esper.testing.core.db.PersonBadgeDto.Type.*; + +public class UnbreakablePoller implements TablePoller.NewRowCallback { + Logger logger = LoggerFactory.getLogger(getClass()); + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + public static void main(String[] args) throws Exception { + String pollerName = "unbreakable"; + String tableName = "build"; + String columnNames = BuildDao.BUILD; + SqlF f = BuildDao.build; + TablePoller.NewRowCallback callback = new UnbreakablePoller(); + + Config config = loadFromDisk(); + + BoneCPDataSource dataSource = config.createBoneCp(); + + new TablePoller<>(pollerName, tableName, columnNames, Option.none(), f, callback). + testMode(true). + work(dataSource); + } + + public void process(Connection c, BuildDto build) throws SQLException { + Daos daos = new Daos(c); + + List persons = daos.buildDao.selectPersonsFromBuildParticipant(build.uuid); + logger.info("Processing build={}, #persons={}", build.uuid, persons.size()); + + for (UUID person : persons) { + logger.info("person={}", person); + + SqlOption o = daos.personDao.selectBadgeProgress(person, UNBREAKABLE); + + UnbreakableBadgeProgress badge; + + if (o.isNone()) { + badge = UnbreakableBadgeProgress.initial(person); + logger.info("New badge progress"); + String state = serialize(badge); + daos.personDao.insertBadgeProgress(person, UNBREAKABLE, state); + continue; + } + + String state = o.get().state; + try { + badge = objectMapper.readValue(state, UnbreakableBadgeProgress.class); + } catch (IOException e) { + logger.error("Could not de-serialize badge state: {}", state); + throw new RuntimeException(e); + } + + logger.info("Existing badge progress: count={}", person, badge.count); + + P2> p = badge.onBuild(build); + + badge = p._1(); + + logger.info("New badge progress: count={}", person, badge.count); + + if (p._2().isSome()) { + UnbreakableBadge b = p._2().some(); + + logger.info("New unbreakable badge: person={}, level={}", person, b.level); + + SqlOption option = daos.personDao.selectBadge(person, UNBREAKABLE, b.level); + + if (option.isNone()) { + daos.personDao.insertBadge(person, UNBREAKABLE, b.level, 1); + } else { + daos.personDao.incrementBadgeCount(person, UNBREAKABLE, b.level); + } + } + + state = serialize(badge); + + daos.personDao.updateBadgeProgress(person, UNBREAKABLE, state); + } + } + + private String serialize(UnbreakableBadgeProgress badge) { + try { + CharArrayWriter writer = new CharArrayWriter(); + objectMapper.writeValue(writer, badge); + return writer.toString(); + } catch (IOException e) { + logger.error("Could not serialize badge.", e); + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java new file mode 100644 index 0000000..24ecfd3 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java @@ -0,0 +1,65 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import io.trygvis.esper.testing.util.sql.*; +import org.joda.time.*; + +import java.sql.*; +import java.util.*; + +import static io.trygvis.esper.testing.util.sql.ResultSetF.*; +import static java.lang.System.*; + +public class BuildDao { + private final Connection c; + + public static final String BUILD = "uuid, created_date, timestamp, success, reference_type, reference_uuid"; + + public static final SqlF build = new SqlF() { + public BuildDto apply(ResultSet rs) throws SQLException { + int i = 1; + return new BuildDto( + UUID.fromString(rs.getString(i++)), + new DateTime(rs.getTimestamp(i++).getTime()), + new DateTime(rs.getTimestamp(i++).getTime()), + rs.getBoolean(i++), + EntityRef.fromRs(rs, i)); + } + }; + + public BuildDao(Connection c) { + this.c = c; + } + + 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(); + } + } + + public List selectPersonsFromBuildParticipant(UUID build) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT person FROM build_participant WHERE build=?")) { + int i = 1; + s.setString(i, build.toString()); + return Util.toList(s, getUuid); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java index 5cc6b0a..305c93a 100644 --- a/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java @@ -5,17 +5,15 @@ import org.joda.time.*; import java.util.*; -class BuildDto extends AbstractEntity { +public class BuildDto extends AbstractEntity { public final DateTime timestamp; public final boolean success; - public final UUID referenceUuid; - public final String referenceType; + public final EntityRef ref; - BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, UUID referenceUuid, String referenceType) { + public BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, EntityRef ref) { super(uuid, createdDate); this.timestamp = timestamp; this.success = success; - this.referenceUuid = referenceUuid; - this.referenceType = referenceType; + this.ref = ref; } } diff --git a/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java b/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java deleted file mode 100644 index 5eab2c9..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java +++ /dev/null @@ -1,73 +0,0 @@ -package io.trygvis.esper.testing.core.db; - -import io.trygvis.esper.testing.*; -import io.trygvis.esper.testing.util.sql.*; -import org.joda.time.*; - -import java.sql.*; -import java.util.*; - -import static io.trygvis.esper.testing.util.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 person = new SqlF() { - 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 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 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/db/PersonBadgeDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java new file mode 100644 index 0000000..5bbe159 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java @@ -0,0 +1,25 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import org.joda.time.*; + +import java.util.*; + +public class PersonBadgeDto extends AbstractEntity { + public enum Type { + UNBREAKABLE + } + + public final UUID person; + public final Type type; + public final int level; + public final int count; + + public PersonBadgeDto(UUID uuid, DateTime createdDate, UUID person, Type type, int level, int count) { + super(uuid, createdDate); + this.person = person; + this.type = type; + this.level = level; + this.count = count; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java new file mode 100644 index 0000000..e572bb2 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java @@ -0,0 +1,20 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import org.joda.time.*; + +import java.util.*; + +public class PersonBadgeProgressDto extends AbstractEntity { + + public final UUID person; + public final String badge; + public final String state; + + public PersonBadgeProgressDto(UUID uuid, DateTime createdDate, UUID person, String badge, String state) { + super(uuid, createdDate); + this.person = person; + this.badge = badge; + this.state = state; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java new file mode 100644 index 0000000..2cb6e6a --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java @@ -0,0 +1,154 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.util.sql.*; +import org.joda.time.*; + +import java.sql.*; +import java.util.*; + +import static io.trygvis.esper.testing.util.sql.SqlOption.*; +import static java.lang.System.*; + +public class PersonDao { + private final Connection c; + + public static final String PERSON = "uuid, created_date, name"; + + public static final SqlF person = new SqlF() { + 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 PERSON_BADGE = "uuid, created_date, person, name, level, count"; + + public static final SqlF personBadge = new SqlF() { + public PersonBadgeDto apply(ResultSet rs) throws SQLException { + int i = 1; + return new PersonBadgeDto( + UUID.fromString(rs.getString(i++)), + new DateTime(rs.getTimestamp(i++).getTime()), + UUID.fromString(rs.getString(i++)), + PersonBadgeDto.Type.valueOf(rs.getString(i++)), + rs.getInt(i), + rs.getInt(i)); + } + }; + + public static final String PERSON_BADGE_PROGRESS = "uuid, created_date, person, badge, state"; + + public static final SqlF personBadgeProgress = new SqlF() { + public PersonBadgeProgressDto apply(ResultSet rs) throws SQLException { + int i = 1; + return new PersonBadgeProgressDto( + UUID.fromString(rs.getString(i++)), + new DateTime(rs.getTimestamp(i++).getTime()), + UUID.fromString(rs.getString(i++)), + rs.getString(i++), + rs.getString(i)); + } + }; + + public PersonDao(Connection c) { + this.c = c; + } + + // ----------------------------------------------------------------------- + // Person + // ----------------------------------------------------------------------- + + public SqlOption 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 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); + } + } + + // ----------------------------------------------------------------------- + // Badge + // ----------------------------------------------------------------------- + + public UUID insertBadge(UUID person, PersonBadgeDto.Type type, int level, int count) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO person_badge(" + PERSON_BADGE + ") VALUES(?, ?, ?, ?, ?, ?)")) { + UUID uuid = UUID.randomUUID(); + int i = 1; + s.setString(i++, uuid.toString()); + s.setTimestamp(i++, new Timestamp(currentTimeMillis())); + s.setString(i++, person.toString()); + s.setString(i++, type.toString()); + s.setInt(i++, level); + s.setInt(i, count); + s.executeUpdate(); + return uuid; + } + } + + public void incrementBadgeCount(UUID person, PersonBadgeDto.Type type, int level) throws SQLException { + try (PreparedStatement s = c.prepareStatement("UPDATE person_badge SET count=count+1 WHERE person=? AND name=? AND level=?")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i++, type.toString()); + s.setInt(i, level); + s.executeUpdate(); + } + } + + public SqlOption selectBadge(UUID person, PersonBadgeDto.Type type, int level) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE + " FROM person_badge WHERE person=? AND name=? AND level=?")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i++, type.toString()); + s.setInt(i, level); + return fromRs(s.executeQuery()).map(personBadge); + } + } + + // ----------------------------------------------------------------------- + // Badge Progress + // ----------------------------------------------------------------------- + + public SqlOption selectBadgeProgress(UUID person, PersonBadgeDto.Type type) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE_PROGRESS + " FROM person_badge_progress WHERE person=? AND badge=?")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i, type.toString()); + return fromRs(s.executeQuery()).map(personBadgeProgress); + } + } + + public void insertBadgeProgress(UUID person, PersonBadgeDto.Type type, String state) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO person_badge_progress (" + PERSON_BADGE_PROGRESS + ") VALUES(?, ?, ?, ?, ?)")) { + UUID uuid = UUID.randomUUID(); + int i = 1; + s.setString(i++, uuid.toString()); + s.setTimestamp(i++, new Timestamp(currentTimeMillis())); + s.setString(i++, person.toString()); + s.setString(i++, type.toString()); + s.setString(i, state); + s.executeUpdate(); + } + } + + public void updateBadgeProgress(UUID person, PersonBadgeDto.Type type, String state) throws SQLException { + try (PreparedStatement s = c.prepareStatement("UPDATE person_badge_progress SET state=? WHERE person=? AND badge=?")) { + int i = 1; + s.setString(i++, state); + s.setString(i++, person.toString()); + s.setString(i, type.toString()); + s.executeUpdate(); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java index 90a4ef4..9f22166 100644 --- a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java @@ -11,9 +11,9 @@ import org.slf4j.*; import java.sql.*; import java.util.*; -import static fj.data.Option.some; +import static fj.data.Option.*; import static io.trygvis.esper.testing.Config.*; -import static io.trygvis.esper.testing.EntityRef.jenkinsRef; +import static io.trygvis.esper.testing.EntityRef.*; public class JenkinsBuildPoller implements TablePoller.NewRowCallback { Logger logger = LoggerFactory.getLogger(getClass()); @@ -34,22 +34,30 @@ public class JenkinsBuildPoller implements TablePoller.NewRowCallback personO = coreDao.selectPersonByJenkinsUuid(user); + SqlOption personO = personDao.selectPersonByJenkinsUuid(user); // This happens if no one has claimed the user id. - if(personO.isNone()) { + if (personO.isNone()) { + unknownPersons++; continue; } + knownPersons++; + UUID person = personO.get().uuid; logger.info("Created build participant, person={}", person); - coreDao.insertBuildParticipant(uuid, person); + buildDao.insertBuildParticipant(uuid, person); } + + logger.info("Created build uuid={}, #participants={}, #knownPersons={}, #unknonwnPersons={}", uuid, + jenkinsBuild.users.length, knownPersons, unknownPersons); } } diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java index 9e42242..7169372 100644 --- a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java +++ b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java @@ -1,6 +1,7 @@ package io.trygvis.esper.testing.util.sql; import java.sql.*; +import java.util.*; public class ResultSetF { public static final SqlF getInt = new SqlF() { @@ -15,4 +16,10 @@ public class ResultSetF { return rs.wasNull() ? null : i; } }; + + public static final SqlF getUuid = new SqlF() { + public UUID apply(ResultSet rs) throws SQLException { + return UUID.fromString(rs.getString(1)); + } + }; } -- cgit v1.2.3