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

nstream.reflect.model.ProcessStats 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.Value;

@SuppressWarnings("checkstyle:VisibilityModifier")
public class ProcessStats {
  public int cpuUsage;
  public int cpuTotal;
  public long memUsage;
  public long memTotal;

  public ProcessStats() {
    // nop
  }

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

  private static Form form;

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

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

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

  @Override
  public Item mold(ProcessStats stats) {
    if (stats != null) {
      return Record.create(4).attr(tag())
          .slot("cpuUsage", stats.cpuUsage)
          .slot("cpuTotal", stats.cpuTotal)
          .slot("memUsage", stats.memUsage)
          .slot("memTotal", stats.memTotal);
    } else {
      return Item.extant();
    }
  }

  @Override
  public ProcessStats cast(Item item) {
    final Value value = item.toValue();
    final Value header = value.header(tag());
    if (header.isDefined()) {
      final ProcessStats process = new ProcessStats();
      process.cpuUsage = value.get("cpuUsage").intValue(0);
      process.cpuTotal = value.get("cpuUsage").intValue(0);
      process.memUsage = value.get("memUsage").longValue(0L);
      process.memTotal = value.get("memTotal").longValue(0L);
      return process;
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy