All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.reflect.model.HostStats Maven / Gradle / Ivy

There is a newer version: 4.13.21
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nstream.reflect.model;

import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Text;
import swim.structure.Value;
import swim.uri.Uri;

public class HostStats {
  final Uri hostUri;
  final boolean isRemote;
  final boolean isConnected;
  final boolean isPrimary;
  final boolean isReplica;
  final boolean isMaster;
  final boolean isSlave;
  final LinkStats linkStats;
  final ProcessStats processStats;

  public HostStats(Uri hostUri, boolean isRemote, boolean isConnected,
                   boolean isPrimary, boolean isReplica, boolean isMaster,
                   boolean isSlave, LinkStats linkStats, ProcessStats processStats) {
    this.hostUri = hostUri;
    this.isRemote = isRemote;
    this.isConnected = isConnected;
    this.isPrimary = isPrimary;
    this.isReplica = isReplica;
    this.isMaster = isMaster;
    this.isSlave = isSlave;
    this.linkStats = linkStats;
    this.processStats = processStats;
  }

  public final Uri hostUri() {
    return this.hostUri;
  }

  public final boolean isRemote() {
    return this.isRemote;
  }

  public final boolean isConnected() {
    return this.isConnected;
  }

  public final boolean isPrimary() {
    return this.isPrimary;
  }

  public final boolean isReplica() {
    return this.isReplica;
  }

  public final boolean isMaster() {
    return this.isMaster;
  }

  public final boolean isSlave() {
    return this.isSlave;
  }

  public final LinkStats linkStats() {
    return this.linkStats;
  }

  public final ProcessStats processStats() {
    return this.processStats;
  }

  public Value toValue() {
    return form().mold(this).toValue();
  }

  private static Form form;

  @Kind
  public static Form form() {
    if (form == null) {
      form = new HostStatsForm();
    }
    return form;
  }
}

final class HostStatsForm extends Form {
  @Override
  public String tag() {
    return "host";
  }

  @Override
  public Class type() {
    return HostStats.class;
  }

  @Override
  public Item mold(HostStats stats) {
    if (stats != null) {
      final Value header;
      if (stats.hostUri.isDefined()) {
        header = Text.from(stats.hostUri.toString());
      } else {
        header = Value.extant();
      }
      final Record record = Record.create();
      record.attr(tag(), header);
      if (stats.isRemote) {
        record.slot("remote", stats.isRemote);
        record.slot("connected", stats.isConnected);
      }
      if (stats.isPrimary) {
        record.slot("primary", stats.isPrimary);
      }
      if (stats.isReplica && !stats.isPrimary) {
        record.slot("replica", stats.isReplica);
      }
      if (stats.isMaster) {
        record.slot("master", stats.isMaster);
      }
      if (stats.isSlave) {
        record.slot("slave", stats.isSlave);
      }
      if (stats.linkStats != null) {
        record.addAll((Record) stats.linkStats.toValue());
      }
      if (stats.processStats != null) {
        record.addAll((Record) stats.processStats.toValue());
      }
      return record;
    } else {
      return Item.extant();
    }
  }

  @Override
  public HostStats cast(Item item) {
    final Value value = item.toValue();
    final Value header = value.getAttr(tag());
    if (header.isDefined()) {
      final Uri hostUri = header.coerce(Uri.form());
      final boolean isRemote = value.get("remote").booleanValue(false);
      final boolean isConnected = value.get("connected").booleanValue(!isRemote);
      final boolean isPrimary = value.get("primary").booleanValue(false);
      final boolean isReplica = value.get("replica").booleanValue(isPrimary);
      final boolean isMaster = value.get("master").booleanValue(false);
      final boolean isSlave = value.get("slave").booleanValue(false);
      final LinkStats linkStats = LinkStats.form().cast(value);
      final ProcessStats processStats = ProcessStats.form().cast(value);
      return new HostStats(hostUri, isRemote, isConnected, isPrimary, isReplica,
                           isMaster, isSlave, linkStats, processStats);
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy