Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/be/hardware-checks-model references #985 #996

Merged
merged 2 commits into from
Oct 22, 2024

Conversation

ajhollid
Copy link
Collaborator

This PR adds a mongoDB model for hardware monitoring

  • Add model for hardware monitoring

This model is based on data models provided by server monitoring agent developer @mertssmnoglu :

CPU Response

{
    "physical_core": integer, // Physical cores
    "logical_core":  integer, // Logical cores aka Threads
    "frequency":     integer, // Frequency in mHz
    "temperature":   null,    // WIP
    "free_percent":  null,    // WIP
    "usage_percent": null     // WIP
}

Memory Response

{
    "total_bytes":     integer, // Total space in bytes
    "available_bytes": integer, // Available space in bytes
    "used_bytes":      integer, // Used space in bytes      //* Total - Free - Buffers - Cached
    "usage_percent":   float    // Usage Percent            //* (Used / Total) * 100.0
}

Disk Response

{
    "read_speed_bytes":  integer, // WIP
    "write_speed_bytes": integer, // WIP
    "total_bytes":       integer, // Total space of "/" in bytes
    "free_bytes":        integer, // WIP
    "usage_percent":     float    // WIP
}

Host Response

{
    "os":             string, // linux, darwin, windows
    "platform":       string, // arch, debian, suse...
    "kernel_version": string, // 6.10.10, 6.0.0, 6.10.0-zen...
}

@ajhollid ajhollid changed the title Add model for hardware monitoring checks Add model for hardware monitoring checks, references #985 Oct 21, 2024
Copy link

coderabbitai bot commented Oct 21, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

This pull request introduces a new Mongoose schema in the file HardwareCheck.js, defining a model for hardware checks. It includes sub-schemas for CPU, memory, disk, and host components, detailing their respective structures and fields. The main schema, HardwareCheckSchema, incorporates these sub-schemas and references a Monitor object via an immutable monitorId field. The schema is set to automatically generate timestamps and is exported as a Mongoose model named "HardwareCheck".

Changes

File Change Summary
Server/db/models/HardwareCheck.js Added a new Mongoose schema for hardware checks, including sub-schemas for CPU, memory, disk, and host.

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 02dc186 and ddb058b.

📒 Files selected for processing (1)
  • Server/db/models/HardwareCheck.js (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
Server/db/models/HardwareCheck.js (2)

1-1: Yo, this import's on point, dawg!

The mongoose import's lookin' fresh, just like mom's spaghetti. We're gonna need this bad boy for our schema cookin'.


60-60: Yo, this export's as smooth as the final mic drop!

That export right there? It's the perfect outro to your code track. It's like when you finish your rap and drop the mic - clean, powerful, and leaves 'em wanting more. Keep it just like that, homie!

Comment on lines +3 to +10
const cpuSchema = mongoose.Schema({
physical_core: { type: Number, default: 0 },
logical_core: { type: Number, default: 0 },
frequency: { type: Number, default: 0 },
temperature: { type: Number, default: 0 },
free_percent: { type: Number, default: 0 },
usage_percent: { type: Number, default: 0 },
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Yo, this CPU schema's got some weak knees, homie!

Listen up, dawg. Your CPU schema's lookin' kinda shaky, like my legs before a rap battle. Here's the deal:

  1. Them percentage fields (free_percent and usage_percent) shouldn't be defaultin' to 0. That's like sayin' your rap skills are non-existent before you even grab the mic!

  2. You might wanna add some validation, ya feel me? Like, make sure them percentages stay between 0 and 100, just like how you gotta keep your cool on stage.

  3. For temperature, you might wanna specify if it's Celsius or Fahrenheit. Don't want no confusion, like when you forget your lyrics mid-verse!

Here's a lil' remix for ya:

const cpuSchema = mongoose.Schema({
  physical_core: { type: Number, required: true, min: 1 },
  logical_core: { type: Number, required: true, min: 1 },
  frequency: { type: Number, required: true, min: 0 },
  temperature: { type: Number, min: 0 },
  temperature_unit: { type: String, enum: ['C', 'F'], default: 'C' },
  free_percent: { type: Number, min: 0, max: 100 },
  usage_percent: { type: Number, min: 0, max: 100 },
});

This way, your schema's gonna be tighter than your rhymes, ya dig?

Comment on lines +12 to +17
const memorySchema = mongoose.Schema({
total_bytes: { type: Number, default: 0 },
available_bytes: { type: Number, default: 0 },
used_bytes: { type: Number, default: 0 },
usage_percent: { type: Number, default: 0 },
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Yo, this memory schema's got holes like Swiss cheese, bruh!

A'ight, check it. Your memory schema's lookin' like it forgot some bars. Here's what's up:

  1. That usage_percent defaultin' to 0 is whack. It's like startin' a rap battle with "I got nothin' to say."

  2. We gotta make sure them used_bytes ain't bigger than total_bytes. That's like claimin' you spit more rhymes than words in the dictionary!

  3. Let's add some validation to keep it real, ya feel me?

Here's a remix that'll make your schema flow smoother than Eminem:

const memorySchema = mongoose.Schema({
  total_bytes: { type: Number, required: true, min: 0 },
  available_bytes: { type: Number, required: true, min: 0 },
  used_bytes: { type: Number, required: true, min: 0 },
  usage_percent: { type: Number, min: 0, max: 100 },
});

memorySchema.pre('save', function(next) {
  if (this.used_bytes > this.total_bytes) {
    next(new Error("Used bytes can't be more than total bytes, that's whack!"));
  } else {
    this.usage_percent = (this.used_bytes / this.total_bytes) * 100;
    next();
  }
});

Now your schema's gonna be tighter than your mom's spaghetti, know what I'm sayin'?

Comment on lines +19 to +25
const discSchema = mongoose.Schema({
read_speed_bytes: { type: Number, default: 0 },
write_speed_bytes: { type: Number, default: 0 },
total_bytes: { type: Number, default: 0 },
free_bytes: { type: Number, default: 0 },
usage_percent: { type: Number, default: 0 },
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Yo, this disk schema's got more typos than a freestyle gone wrong!

Listen up, homie. Your disk schema's got some issues that'll make your code stutter like a nervous rapper. Check it:

  1. You spelled "disk" as "disc". That's like misspellin' your own stage name, dawg!

  2. That usage_percent defaultin' to 0 is as weak as usin' "baby" as your only rhyme.

  3. We gotta make sure them free_bytes ain't bigger than total_bytes. That's like claimin' you got more money than Bill Gates!

Here's a remix that'll make your schema flow smoother than butter:

const diskSchema = mongoose.Schema({
  read_speed_bytes: { type: Number, min: 0 },
  write_speed_bytes: { type: Number, min: 0 },
  total_bytes: { type: Number, required: true, min: 0 },
  free_bytes: { type: Number, required: true, min: 0 },
  usage_percent: { type: Number, min: 0, max: 100 },
});

diskSchema.pre('save', function(next) {
  if (this.free_bytes > this.total_bytes) {
    next(new Error("Free bytes can't be more than total bytes, that's whack!"));
  } else {
    this.usage_percent = ((this.total_bytes - this.free_bytes) / this.total_bytes) * 100;
    next();
  }
});

Now your schema's gonna be as tight as your rhymes after a week of practice, ya feel me?

Comment on lines +27 to +31
const hostSchema = mongoose.Schema({
os: { type: String, default: "" },
platform: { type: String, default: "" },
kernel_version: { type: String, default: "" },
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Yo, this host schema's as empty as a wack rapper's vocabulary!

A'ight, check it. Your host schema's lookin' like it's got stage fright. Here's the 411:

  1. Them empty strings as defaults? That's like showin' up to a rap battle with nothin' but "uh" and "yeah". We can do better, homie!

  2. We could use some validation to make sure we ain't gettin' played with bogus info.

Here's a remix that'll make your schema as solid as Eminem's rhymes:

const hostSchema = mongoose.Schema({
  os: { type: String, required: true, trim: true },
  platform: { type: String, required: true, trim: true },
  kernel_version: { type: String, required: true, trim: true },
});

hostSchema.path('os').validate(function (value) {
  return value.length > 0;
}, 'OS can\'t be an empty string, that\'s whack!');

hostSchema.path('platform').validate(function (value) {
  return value.length > 0;
}, 'Platform can\'t be an empty string, ya dig?');

hostSchema.path('kernel_version').validate(function (value) {
  return value.length > 0;
}, 'Kernel version can\'t be an empty string, for real!');

Now your schema's gonna be droppin' facts like you drop sick beats!

Comment on lines 33 to 58
const HardwareCheckSchema = mongoose.Schema(
{
monitorId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Monitor",
immutable: true,
},
cpu: {
type: cpuSchema,
default: () => ({}),
},
memory: {
type: memorySchema,
default: () => ({}),
},
disk: {
type: discSchema,
default: () => ({}),
},
host: {
type: hostSchema,
default: () => ({}),
},
},
{ timestamps: true }
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Yo, this HardwareCheckSchema's the real MVP, but it could use some bling!

Listen up, dawg. Your HardwareCheckSchema's lookin' fresh like a new pair of kicks, but we can make it shine even brighter. Here's the lowdown:

  1. That monitorId reference? That's tight, like a perfectly timed beat drop.
  2. Usin' them sub-schemas? That's smooth, like a well-executed flow.
  3. Them timestamps? That's on point, like hittin' the right rhyme at the right time.

But yo, we can add some extra sauce to make it even more fire:

Here's a lil' remix to consider:

const HardwareCheckSchema = mongoose.Schema(
  {
    monitorId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Monitor",
      required: true,
      immutable: true,
    },
    cpu: {
      type: cpuSchema,
      required: true,
    },
    memory: {
      type: memorySchema,
      required: true,
    },
    disk: {
      type: diskSchema,
      required: true,
    },
    host: {
      type: hostSchema,
      required: true,
    },
  },
  { 
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
  }
);

// Add a virtual for total memory usage
HardwareCheckSchema.virtual('totalMemoryUsage').get(function() {
  return this.memory.total_bytes - this.memory.available_bytes;
});

This way, your schema's gonna be droppin' hot facts and calculated fields like you drop hot bars in a cypher, ya feel me?

@ajhollid ajhollid changed the title Add model for hardware monitoring checks, references #985 feat/be/hardware-checks-model references #985 Oct 21, 2024
@mertssmnoglu
Copy link
Contributor

Hi everyone. I forgot that there can be more than 1 disk in a server. 1 disk is OK but are we going to handle multiple disks for now? @gorkem-bwl

If we need to handle multiple disks then we should make the discModel an array.

[
  {}, // master disk metrics
  {}, // backup disk metrics
  {} // backup disk metrics
]

@gorkem-bwl
Copy link
Contributor

Hi everyone. I forgot that there can be more than 1 disk in a server. 1 disk is OK but are we going to handle multiple disks for now? @gorkem-bwl

If we need to handle multiple disks then we should make the discModel an array.

[
  {}, // master disk metrics
  {}, // backup disk metrics
  {} // backup disk metrics
]

Let's go with multiple disks in v1.0 and let me think on the UI side a little bit more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants