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

Create a stats endpoint to stream server data over websocket to frontend clients #2

Open
xamfy opened this issue Sep 30, 2022 · 11 comments
Labels

Comments

@xamfy
Copy link
Owner

xamfy commented Sep 30, 2022

No description provided.

@ChellappanRajan
Copy link
Contributor

Hi I am new to rust language, however I would love to contribute to this PR can you please guide me?

@xamfy
Copy link
Owner Author

xamfy commented Oct 1, 2022

Hey @ChellappanRajan welcome to the project :)

Would suggest you to go over basics of rust for sometime. You can refer to official rust docs as well as some YT channels for picking up the basics of rust, not much advanced concepts are required as of now.

The web framework we're using in the project is actix-web which is a rust based web framework, so once you're familiar with rust, give a try to actix-web, build a GET/POST REST API.

The task deals with implementing a websocket endpoint for stats(server statistics) that the frontend clients can connect to, to get the server data in realtime. This stats can be anything like memory usage, cpu usage, load average, open file descriptors. We can initially limit this to couple of things only. One example is present in the handlers/handlers.rs that returns cpu usage of the machine in HTML format over HTTP. We're currently using systemstat rust crate(crates are similar to npm packages) to get this data from the server.

For local setup, the instructions are present in the README.md. You'll need to install rust on your system, Linux and macOS are preferable for the setup.

Do drop a star on the repo, if you like the project, that'd be helpful :)

@ChellappanRajan
Copy link
Contributor

Thanks for the explanation. I wil check and get back to you.

@ChellappanRajan
Copy link
Contributor

Hi I tried to run this app on my local machine getting below error, Could you please help to resolve this?

Finished release [optimized] target(s) in 5.35s
     Running `target/release/x-server-stats`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: missing field `server_addr`', src/main.rs:38:58
stack backtrace:

@xamfy
Copy link
Owner Author

xamfy commented Oct 3, 2022

You'll need to export few environment variables, when the app or the crate runs, it expects these values to be present in the environment in which it's running, whether a linux/macOS machine or a container.

You can set those environment variables by running following commands in your terminal.

export SERVER_ADDR=localhost:8082
export PG__USER=PG_USERNAME
export PG__PASSWORD=PG_PASSWORD
export PG__HOST=PG_HOST_URL
export PG__PORT=5432
export PG__DBNAME=PG_DB_NAME
export PG__POOL__MAX_SIZE=20
export BASE_ADDR=http://localhost:8082

PG__USER: postgres database username (this could be a local PG DB username)
PG__PASSWORD: PG DB password
PG__HOST: Will be localhost if you're running postgres on a local machine
PG__PORT: Will be 5432 for most postgres installations
PG__DBNAME: database name that you want the application connect to, if you're running postgres locally, you can keep any name for the DB.

@ChellappanRajan
Copy link
Contributor

Thanks able to run it now. Is this crates only supported for windows?

@xamfy
Copy link
Owner Author

xamfy commented Oct 4, 2022

Great!

Crate is supported on all platforms rustc (rust's compiler) is able to compile a binary in. So it'll work on windows, linux, macOS, FreeBSD etc too.

@ChellappanRajan
Copy link
Contributor

when I open this page http://localhost:8082/stats in browser getting below error in terminal

CPU load: error: Not supported
Stats { loadavg: "Error", cpu_usage: "Error", memory_usage: "Error" }
[2022-10-04T01:46:38Z INFO  actix_web::middleware::logger] ::1 "GET /stats HTTP/1.1" 200 319 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" 0.000149
[2022-10-04T01:46:38Z INFO  actix_web::middleware::logger] ::1 "GET /favicon.ico HTTP/1.1" 404 0 "http://localhost:8082/stats" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" 0.000032

@xamfy
Copy link
Owner Author

xamfy commented Oct 4, 2022

Oh yeah, the underlying crate which provides the machine data doesn't have full support for all functions in all OSes. So on macOS, a lot of functions will not work.

Workaround is to use Docker to run it. I have a sample Dockerfile.example in the root directory, please rename it to Dockerfile locally, and replace all the postgres environment variables with their correct value as per what you've set in your system, and the steps to run to build and run the docker image are:

$ docker build -t x-server-stats .
$ docker run -p 8082:8082 x-server-stats

Since the docker image will linux based, you'd see the correct cpu load value in your browser, and no error in your console.

We need to look at another package for a better support, I guess sysinfo, will have a better support for macOS as well, need to try it out.

Currently, development cycle would look like write code -> run it -> fix error and issues -> build the docker image -> run it.

@xamfy
Copy link
Owner Author

xamfy commented Oct 5, 2022

@ChellappanRajan Do let me know if you're facing any more issues. On a side note, I'd suggest to start with the issue #5 first, since it'll just require a little bit of tinkering with the existing code to complete.

@ChellappanRajan
Copy link
Contributor

Hi @xamfy Since I am new to rust it's taking a lot of time. Currently I am trying to understand the existing code and going through the creates documentation which x-server-stats using.

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

No branches or pull requests

2 participants