|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.applicationinsights.alerting.config; |
| 5 | + |
| 6 | +import com.google.auto.value.AutoValue; |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import javax.annotation.Nullable; |
| 12 | + |
| 13 | +@AutoValue |
| 14 | +public abstract class TargetedCollectionPlanConfiguration { |
| 15 | + |
| 16 | + public static TargetedCollectionPlanConfiguration create( |
| 17 | + @Nullable List<String> roles, |
| 18 | + @Nullable List<TargetedInstanceConfiguration> instances, |
| 19 | + int immediateProfilingDurationSeconds, |
| 20 | + @Nullable Instant expiration, |
| 21 | + @Nullable String settingsMoniker) { |
| 22 | + return new AutoValue_TargetedCollectionPlanConfiguration( |
| 23 | + immutableCopy(roles), |
| 24 | + immutableCopy(instances), |
| 25 | + immediateProfilingDurationSeconds, |
| 26 | + expiration, |
| 27 | + settingsMoniker); |
| 28 | + } |
| 29 | + |
| 30 | + @Nullable |
| 31 | + public abstract List<String> getRoles(); |
| 32 | + |
| 33 | + @Nullable |
| 34 | + public abstract List<TargetedInstanceConfiguration> getInstances(); |
| 35 | + |
| 36 | + public abstract int getImmediateProfilingDurationSeconds(); |
| 37 | + |
| 38 | + @Nullable |
| 39 | + public abstract Instant getExpiration(); |
| 40 | + |
| 41 | + @Nullable |
| 42 | + public abstract String getSettingsMoniker(); |
| 43 | + |
| 44 | + public boolean isValid() { |
| 45 | + List<String> roles = getRoles(); |
| 46 | + List<TargetedInstanceConfiguration> instances = getInstances(); |
| 47 | + if ((roles == null) == (instances == null) |
| 48 | + || getImmediateProfilingDurationSeconds() < 1 |
| 49 | + || getImmediateProfilingDurationSeconds() > 360 |
| 50 | + || getExpiration() == null |
| 51 | + || isBlank(getSettingsMoniker())) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if (roles != null) { |
| 56 | + if (roles.isEmpty()) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + for (String role : roles) { |
| 60 | + if (isBlank(role)) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + if (instances.isEmpty()) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + for (TargetedInstanceConfiguration instance : instances) { |
| 71 | + if (instance == null || isBlank(instance.getRole()) || isBlank(instance.getName())) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + public boolean isSelected(@Nullable String roleName, @Nullable String roleInstance) { |
| 79 | + if (!isValid() || isBlank(roleName)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + List<String> roles = getRoles(); |
| 84 | + if (roles != null) { |
| 85 | + for (String role : roles) { |
| 86 | + if (equalsNormalized(role, roleName)) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + List<TargetedInstanceConfiguration> instances = getInstances(); |
| 94 | + if (isBlank(roleInstance) || instances == null) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + for (TargetedInstanceConfiguration instance : instances) { |
| 98 | + if (instance != null |
| 99 | + && equalsNormalized(instance.getRole(), roleName) |
| 100 | + && equalsNormalized(instance.getName(), roleInstance)) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + public boolean isActionable( |
| 108 | + @Nullable String roleName, @Nullable String roleInstance, Instant now) { |
| 109 | + Instant expiration = getExpiration(); |
| 110 | + return expiration != null && now.isBefore(expiration) && isSelected(roleName, roleInstance); |
| 111 | + } |
| 112 | + |
| 113 | + @Nullable |
| 114 | + private static <T> List<T> immutableCopy(@Nullable List<T> values) { |
| 115 | + return values == null ? null : Collections.unmodifiableList(new ArrayList<>(values)); |
| 116 | + } |
| 117 | + |
| 118 | + private static boolean equalsNormalized(@Nullable String left, @Nullable String right) { |
| 119 | + return left != null && right != null && left.trim().equalsIgnoreCase(right.trim()); |
| 120 | + } |
| 121 | + |
| 122 | + private static boolean isBlank(@Nullable String value) { |
| 123 | + return value == null || value.trim().isEmpty(); |
| 124 | + } |
| 125 | +} |
0 commit comments