// net-host-stats.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const CpuInfoSchema = new Schema({
  user: { type: Number },
  nice: { type: Number },
  sys: { type: Number },
  idle: { type: Number },
  irq: { type: Number },
});

const MemoryInfoSchema = new Schema({
  total: { type: Number },
  free: { type: Number },
  used: { type: Number },
  active: { type: Number },
  available: { type: Number },
  buffers: { type: Number },
  cached: { type: Number },
  slab: { type: Number },
  buffcache: { type: Number },
  swaptotal: { type: Number },
  swapused: { type: Number },
  swapfree: { type: Number },
});

const CacheStatsSchema = new Schema({
  itemCount: { type: Number, required: true },
  dataSize: { type: Number, required: true },
  expireCount: { type: Number, required: true },
  expireDataSize: { type: Number, required: true },
  hitCount: { type: Number, required: true },
  hitDataSize: { type: Number, required: true },
  missCount: { type: Number, required: true },
  missDataSize: { type: Number, required: true },
});

const DiskUsageSchema = new Schema({
  total: { type: Number },
  used: { type: Number },
  available: { type: Number },
  pctUsed: { type: Number },
});

const NetworkInterfaceStatsSchema = new Schema({
  iface: { type: String, required: true },
  rxPerSecond: { type: Number },
  rxDropped: { type: Number },
  rxErrors: { type: Number },
  txPerSecond: { type: Number },
  txDropped: { type: Number },
  txErrors: { type: Number },
});

const NetHostStatsSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, index: 1, expires: '7d' },
  host: { type: Schema.ObjectId, required: true, index: 1, ref: 'NetHost' },
  load: { type: [Number], required: true },
  cpus: { type: [CpuInfoSchema], required: true },
  memory: { type: MemoryInfoSchema, required: true },
  cache: { type: CacheStatsSchema, required: true },
  disk: {
    cache: { type: DiskUsageSchema, required: true },
  },
  network: { type: [NetworkInterfaceStatsSchema], required: true },
});

module.exports = mongoose.model('NetHostStats', NetHostStatsSchema);