|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.spotify.reaper.resources.view; |
| 15 | + |
| 16 | +import com.spotify.reaper.core.RepairRun; |
| 17 | +import com.spotify.reaper.resources.CommonTools; |
| 18 | +import com.spotify.reaper.storage.postgresql.RepairRunMapper; |
| 19 | + |
| 20 | +import org.apache.commons.lang.time.DurationFormatUtils; |
| 21 | +import org.joda.time.DateTime; |
| 22 | +import org.joda.time.Duration; |
| 23 | +import org.skife.jdbi.v2.StatementContext; |
| 24 | +import org.skife.jdbi.v2.tweak.ResultSetMapper; |
| 25 | + |
| 26 | +import java.sql.ResultSet; |
| 27 | +import java.sql.SQLException; |
| 28 | + |
| 29 | +public class ClusterRun { |
| 30 | + |
| 31 | + public final int runId; |
| 32 | + public final String cluserName; |
| 33 | + public final String keyspaceName; |
| 34 | + public final String[] columnFamilies; |
| 35 | + public final int segmentsRepaired; |
| 36 | + public final int totalSegments; |
| 37 | + public final RepairRun.RunState state; |
| 38 | + private final DateTime startTime; |
| 39 | + public String getStartTime() { |
| 40 | + return CommonTools.dateTimeToISO8601(startTime); |
| 41 | + } |
| 42 | + private final Duration duration; |
| 43 | + public String getDuration() { |
| 44 | + if (null == duration) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + return DurationFormatUtils.formatDurationWords(duration.getMillis(), false, false); |
| 48 | + } |
| 49 | + public final String cause; |
| 50 | + public final String owner; |
| 51 | + public final String lastEvent; |
| 52 | + private final DateTime estimatedTimeOfArrival; |
| 53 | + public String getEstimatedTimeOfArrival() { |
| 54 | + return CommonTools.dateTimeToISO8601(estimatedTimeOfArrival); |
| 55 | + } |
| 56 | + |
| 57 | + public ClusterRun(int runId, String clusterName, String keyspaceName, String[] columnFamilies, |
| 58 | + int segmentsRepaired, int totalSegments, RepairRun.RunState state, DateTime startTime, |
| 59 | + DateTime endTime, String cause, String owner, String lastEvent) { |
| 60 | + this.runId = runId; |
| 61 | + this.cluserName = clusterName; |
| 62 | + this.keyspaceName = keyspaceName; |
| 63 | + this.columnFamilies = columnFamilies; |
| 64 | + this.segmentsRepaired = segmentsRepaired; |
| 65 | + this.totalSegments = totalSegments; |
| 66 | + this.state = state; |
| 67 | + this.startTime = startTime; |
| 68 | + this.cause = cause; |
| 69 | + this.owner = owner; |
| 70 | + this.lastEvent = lastEvent; |
| 71 | + |
| 72 | + if (startTime == null) { |
| 73 | + estimatedTimeOfArrival = null; |
| 74 | + duration = null; |
| 75 | + } else if (endTime == null) { |
| 76 | + duration = null; |
| 77 | + if (state == RepairRun.RunState.ERROR || state == RepairRun.RunState.DELETED) { |
| 78 | + estimatedTimeOfArrival = null; |
| 79 | + } else { |
| 80 | + long now = DateTime.now().getMillis(); |
| 81 | + estimatedTimeOfArrival = new DateTime( |
| 82 | + now + (now - startTime.getMillis()) / |
| 83 | + segmentsRepaired * (totalSegments - segmentsRepaired)); |
| 84 | + } |
| 85 | + } else { |
| 86 | + estimatedTimeOfArrival = null; |
| 87 | + duration = new Duration(startTime.toInstant(), endTime.toInstant()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static class Mapper implements ResultSetMapper<ClusterRun> { |
| 92 | + |
| 93 | + @Override |
| 94 | + public ClusterRun map(int index, ResultSet r, StatementContext ctx) throws SQLException { |
| 95 | + int runId = r.getInt("id"); |
| 96 | + String clusterName = r.getString("cluster_name"); |
| 97 | + String keyspaceName = r.getString("keyspace_name"); |
| 98 | + String[] columnFamilies = (String[]) r.getArray("column_families").getArray(); |
| 99 | + int segmentsRepaired = (int)r.getLong("count"); |
| 100 | + int totalSegments = r.getInt("segment_count"); |
| 101 | + RepairRun.RunState state = RepairRun.RunState.valueOf(r.getString("state")); |
| 102 | + DateTime startTime = RepairRunMapper.getDateTimeOrNull(r, "start_time"); |
| 103 | + DateTime endTime = RepairRunMapper.getDateTimeOrNull(r, "end_time"); |
| 104 | + String cause = r.getString("cause"); |
| 105 | + String owner = r.getString("owner"); |
| 106 | + String lastEvent = r.getString("last_event"); |
| 107 | + return new ClusterRun(runId, clusterName, keyspaceName, columnFamilies, segmentsRepaired, |
| 108 | + totalSegments, state, startTime, endTime, cause, owner, lastEvent); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments