From 4edfbe664f2fd0975ff69febbff3ff58564b82e8 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Wed, 1 Mar 2017 10:42:27 +0000 Subject: [PATCH] [ML] Delete unused AuditActivity class (elastic/x-pack-elasticsearch#672) Original commit: elastic/x-pack-elasticsearch@185ceab4b49061747b4f2d85417a370b8414fc88 --- .../ml/MachineLearningTemplateRegistry.java | 2 - .../persistence/ElasticsearchMappings.java | 14 -- .../xpack/ml/notifications/AuditActivity.java | 168 ------------------ .../MachineLearningTemplateRegistryTests.java | 4 +- .../ml/notifications/AuditActivityTests.java | 78 -------- 5 files changed, 1 insertion(+), 265 deletions(-) delete mode 100644 plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/AuditActivity.java delete mode 100644 plugin/src/test/java/org/elasticsearch/xpack/ml/notifications/AuditActivityTests.java diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java index 55e36849b97..f3c3f79fb53 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java @@ -35,7 +35,6 @@ import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelState; import org.elasticsearch.xpack.ml.job.process.autodetect.state.Quantiles; import org.elasticsearch.xpack.ml.job.results.CategoryDefinition; import org.elasticsearch.xpack.ml.job.results.Result; -import org.elasticsearch.xpack.ml.notifications.AuditActivity; import org.elasticsearch.xpack.ml.notifications.AuditMessage; import org.elasticsearch.xpack.ml.notifications.Auditor; @@ -222,7 +221,6 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen templateRequest.patterns(Collections.singletonList(Auditor.NOTIFICATIONS_INDEX)); templateRequest.settings(mlNotificationIndexSettings()); templateRequest.mapping(AuditMessage.TYPE.getPreferredName(), ElasticsearchMappings.auditMessageMapping()); - templateRequest.mapping(AuditActivity.TYPE.getPreferredName(), ElasticsearchMappings.auditActivityMapping()); templateRequest.version(Version.CURRENT.id); client.admin().indices().putTemplate(templateRequest, ActionListener.wrap(r -> listener.accept(true, null), e -> listener.accept(false, e))); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ElasticsearchMappings.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ElasticsearchMappings.java index bb12eef1a55..195505b3683 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ElasticsearchMappings.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ElasticsearchMappings.java @@ -24,7 +24,6 @@ import org.elasticsearch.xpack.ml.job.results.ModelDebugOutput; import org.elasticsearch.xpack.ml.job.results.PerPartitionMaxProbabilities; import org.elasticsearch.xpack.ml.job.results.ReservedFieldNames; import org.elasticsearch.xpack.ml.job.results.Result; -import org.elasticsearch.xpack.ml.notifications.AuditActivity; import org.elasticsearch.xpack.ml.notifications.AuditMessage; import java.io.IOException; @@ -668,17 +667,4 @@ public class ElasticsearchMappings { .endObject() .endObject(); } - - public static XContentBuilder auditActivityMapping() throws IOException { - return jsonBuilder() - .startObject() - .startObject(AuditActivity.TYPE.getPreferredName()) - .startObject(PROPERTIES) - .startObject(AuditActivity.TIMESTAMP.getPreferredName()) - .field(TYPE, DATE) - .endObject() - .endObject() - .endObject() - .endObject(); - } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/AuditActivity.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/AuditActivity.java deleted file mode 100644 index 159dcc909fb..00000000000 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/AuditActivity.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.notifications; - -import org.elasticsearch.action.support.ToXContentToBytes; -import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.ObjectParser.ValueType; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParser.Token; -import org.elasticsearch.xpack.ml.utils.time.TimeUtils; - -import java.io.IOException; -import java.util.Date; -import java.util.Objects; - -public class AuditActivity extends ToXContentToBytes implements Writeable { - public static final ParseField TYPE = new ParseField("audit_activity"); - - public static final ParseField TOTAL_JOBS = new ParseField("total_jobs"); - public static final ParseField TOTAL_DETECTORS = new ParseField("total_detectors"); - public static final ParseField RUNNING_JOBS = new ParseField("running_jobs"); - public static final ParseField RUNNING_DETECTORS = new ParseField("running_detectors"); - public static final ParseField TIMESTAMP = new ParseField("timestamp"); - - public static final ObjectParser PARSER = new ObjectParser<>(TYPE.getPreferredName(), - AuditActivity::new); - - static { - PARSER.declareInt(AuditActivity::setTotalJobs, TOTAL_JOBS); - PARSER.declareInt(AuditActivity::setTotalDetectors, TOTAL_DETECTORS); - PARSER.declareInt(AuditActivity::setRunningJobs, RUNNING_JOBS); - PARSER.declareInt(AuditActivity::setRunningDetectors, RUNNING_DETECTORS); - PARSER.declareField(AuditActivity::setTimestamp, p -> { - if (p.currentToken() == Token.VALUE_NUMBER) { - return new Date(p.longValue()); - } else if (p.currentToken() == Token.VALUE_STRING) { - return new Date(TimeUtils.dateStringToEpoch(p.text())); - } - throw new IllegalArgumentException("unexpected token [" + p.currentToken() + "] for [" + TIMESTAMP.getPreferredName() + "]"); - }, TIMESTAMP, ValueType.VALUE); - } - - private int totalJobs; - private int totalDetectors; - private int runningJobs; - private int runningDetectors; - private Date timestamp; - - public AuditActivity() { - } - - private AuditActivity(int totalJobs, int totalDetectors, int runningJobs, int runningDetectors) { - this.totalJobs = totalJobs; - this.totalDetectors = totalDetectors; - this.runningJobs = runningJobs; - this.runningDetectors = runningDetectors; - timestamp = new Date(); - } - - public AuditActivity(StreamInput in) throws IOException { - totalJobs = in.readInt(); - totalDetectors = in.readInt(); - runningJobs = in.readInt(); - runningDetectors = in.readInt(); - if (in.readBoolean()) { - timestamp = new Date(in.readLong()); - } - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeInt(totalJobs); - out.writeInt(totalDetectors); - out.writeInt(runningJobs); - out.writeInt(runningDetectors); - boolean hasTimestamp = timestamp != null; - out.writeBoolean(hasTimestamp); - if (hasTimestamp) { - out.writeLong(timestamp.getTime()); - } - } - - public int getTotalJobs() { - return totalJobs; - } - - public void setTotalJobs(int totalJobs) { - this.totalJobs = totalJobs; - } - - public int getTotalDetectors() { - return totalDetectors; - } - - public void setTotalDetectors(int totalDetectors) { - this.totalDetectors = totalDetectors; - } - - public int getRunningJobs() { - return runningJobs; - } - - public void setRunningJobs(int runningJobs) { - this.runningJobs = runningJobs; - } - - public int getRunningDetectors() { - return runningDetectors; - } - - public void setRunningDetectors(int runningDetectors) { - this.runningDetectors = runningDetectors; - } - - public Date getTimestamp() { - return timestamp; - } - - public void setTimestamp(Date timestamp) { - this.timestamp = timestamp; - } - - public static AuditActivity newActivity(int totalJobs, int totalDetectors, int runningJobs, int runningDetectors) { - return new AuditActivity(totalJobs, totalDetectors, runningJobs, runningDetectors); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(TOTAL_JOBS.getPreferredName(), totalJobs); - builder.field(TOTAL_DETECTORS.getPreferredName(), totalDetectors); - builder.field(RUNNING_JOBS.getPreferredName(), runningJobs); - builder.field(RUNNING_DETECTORS.getPreferredName(), runningDetectors); - if (timestamp != null) { - builder.field(TIMESTAMP.getPreferredName(), timestamp.getTime()); - } - builder.endObject(); - return builder; - } - - @Override - public int hashCode() { - return Objects.hash(totalDetectors, totalJobs, runningDetectors, runningJobs, timestamp); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - AuditActivity other = (AuditActivity) obj; - return Objects.equals(totalDetectors, other.totalDetectors) && - Objects.equals(totalJobs, other.totalJobs) && - Objects.equals(runningDetectors, other.runningDetectors) && - Objects.equals(runningJobs, other.runningJobs) && - Objects.equals(timestamp, other.timestamp); - } -} diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java index 30e635bf106..333a488599f 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java @@ -33,7 +33,6 @@ import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelState; import org.elasticsearch.xpack.ml.job.process.autodetect.state.Quantiles; import org.elasticsearch.xpack.ml.job.results.CategoryDefinition; import org.elasticsearch.xpack.ml.job.results.Result; -import org.elasticsearch.xpack.ml.notifications.AuditActivity; import org.elasticsearch.xpack.ml.notifications.AuditMessage; import org.elasticsearch.xpack.ml.notifications.Auditor; import org.junit.Before; @@ -201,8 +200,7 @@ public class MachineLearningTemplateRegistryTests extends ESTestCase { assertNotNull(request); assertEquals(templateRegistry.mlNotificationIndexSettings().build(), request.settings()); assertTrue(request.mappings().containsKey(AuditMessage.TYPE.getPreferredName())); - assertTrue(request.mappings().containsKey(AuditActivity.TYPE.getPreferredName())); - assertEquals(2, request.mappings().size()); + assertEquals(1, request.mappings().size()); assertEquals(Collections.singletonList(Auditor.NOTIFICATIONS_INDEX), request.patterns()); assertEquals(new Integer(Version.CURRENT.id), request.version()); }); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/notifications/AuditActivityTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/notifications/AuditActivityTests.java deleted file mode 100644 index 58e28d27b39..00000000000 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/notifications/AuditActivityTests.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.notifications; - -import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.ml.support.AbstractSerializingTestCase; -import org.elasticsearch.xpack.ml.utils.time.TimeUtils; -import org.junit.Before; - -import java.util.Date; - -public class AuditActivityTests extends AbstractSerializingTestCase { - private long startMillis; - - @Before - public void setStartTime() { - startMillis = System.currentTimeMillis(); - } - - public void testDefaultConstructor() { - AuditActivity activity = new AuditActivity(); - assertEquals(0, activity.getTotalJobs()); - assertEquals(0, activity.getTotalDetectors()); - assertEquals(0, activity.getRunningJobs()); - assertEquals(0, activity.getRunningDetectors()); - assertNull(activity.getTimestamp()); - } - - public void testNewActivity() { - AuditActivity activity = AuditActivity.newActivity(10, 100, 5, 50); - assertEquals(10, activity.getTotalJobs()); - assertEquals(100, activity.getTotalDetectors()); - assertEquals(5, activity.getRunningJobs()); - assertEquals(50, activity.getRunningDetectors()); - assertDateBetweenStartAndNow(activity.getTimestamp()); - } - - private void assertDateBetweenStartAndNow(Date timestamp) { - long timestampMillis = timestamp.getTime(); - assertTrue(timestampMillis >= startMillis); - assertTrue(timestampMillis <= System.currentTimeMillis()); - } - - @Override - protected AuditActivity parseInstance(XContentParser parser) { - return AuditActivity.PARSER.apply(parser, null); - } - - @Override - protected AuditActivity createTestInstance() { - AuditActivity message = new AuditActivity(); - if (randomBoolean()) { - message.setRunningJobs(randomInt()); - } - if (randomBoolean()) { - message.setRunningDetectors(randomInt()); - } - if (randomBoolean()) { - message.setTotalJobs(randomInt()); - } - if (randomBoolean()) { - message.setTotalDetectors(randomInt()); - } - if (randomBoolean()) { - message.setTimestamp(new Date(TimeUtils.dateStringToEpoch(randomTimeValue()))); - } - return message; - } - - @Override - protected Reader instanceReader() { - return AuditActivity::new; - } -}