CodeNinjaUsman
/
React-Monitor-
Cool dashboard for laptop monitoring
React monitor
Usage
This project was bootstrapped with Vite.
Project setup
npm install
Compiles and hot-reloads for development
npm run dev
Compiles and minifies for production
npm run build
Terms and License
Project setup
npm install
Compiles and hot-reloads for development
npm run dev
Compiles and minifies for production
npm run build
Backend
Cpu info api
from flask import Flask, jsonify | |
import psutil | |
from django.http import JsonResponse | |
from flask_cors import CORS | |
app = Flask(__name__) | |
CORS(app) | |
@app.route('/') | |
def system_info(): | |
cpu_usage = psutil.cpu_percent(interval=1) | |
ram_usage = psutil.virtual_memory().percent | |
disk_usage = psutil.disk_usage('/') | |
tasks = [] | |
for task in psutil.process_iter(['pid', 'name', 'username']): | |
try: | |
task_info = task.info | |
tasks.append(task_info) | |
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | |
pass | |
tasks=tasks[0:10] | |
system_info = { | |
'cpu_usage': cpu_usage, | |
'ram_usage': ram_usage, | |
'total_storage': int(disk_usage.total / (1024 ** 3)), | |
'free_storage': int(disk_usage.free / (1024 ** 3)), | |
'used_storage': int(disk_usage.used / (1024 ** 3)), | |
'running_tasks': tasks | |
} | |
return jsonify(system_info) | |
if __name__ == '__main__': | |
app.run(debug=True,port=80) |
To save data in db
import os | |
import sys | |
import time | |
import logging | |
import sqlite3 | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
class FileEventHandler(FileSystemEventHandler): | |
def __init__(self): | |
self.event_list = [] | |
def on_created(self, event): | |
self.event_list.append({ | |
'event_type': 'created', | |
'src_path': event.src_path, | |
'is_directory': event.is_directory | |
}) | |
def on_modified(self, event): | |
self.event_list.append({ | |
'event_type': 'modified', | |
'src_path': event.src_path, | |
'is_directory': event.is_directory | |
}) | |
def on_moved(self, event): | |
self.event_list.append({ | |
'event_type': 'moved', | |
'src_path': event.src_path, | |
'dest_path': event.dest_path, | |
'is_directory': event.is_directory | |
}) | |
def on_deleted(self, event): | |
self.event_list.append({ | |
'event_type': 'deleted', | |
'src_path': event.src_path, | |
'is_directory': event.is_directory | |
}) | |
def init_database(): | |
conn = sqlite3.connect('file_events.db') | |
c = conn.cursor() | |
c.execute(''' | |
CREATE TABLE IF NOT EXISTS file_events | |
(id INTEGER PRIMARY KEY AUTOINCREMENT, | |
event_type TEXT, | |
src_path TEXT, | |
dest_path TEXT, | |
is_directory INTEGER, | |
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP) | |
''') | |
conn.commit() | |
conn.close() | |
def insert_event_data(data): | |
conn = sqlite3.connect('file_events.db') | |
c = conn.cursor() | |
c.execute(''' | |
INSERT INTO file_events (event_type, src_path, dest_path, is_directory) | |
VALUES (?, ?, ?, ?) | |
''', (data['event_type'], data['src_path'], data.get('dest_path', ''), data['is_directory'])) | |
conn.commit() | |
conn.close() | |
def main(path): | |
event_handler = FileEventHandler() | |
print("Done") | |
observer = Observer() | |
observer.schedule(event_handler, path, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
if event_handler.event_list: | |
for event_data in event_handler.event_list: | |
insert_event_data(event_data) | |
event_handler.event_list.clear() | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() | |
if __name__ == "__main__": | |
init_database() | |
path = sys.argv[1] if len(sys.argv) > 1 else '.' | |
main(path) |
Api for recent changes in files
import sqlite3 | |
from flask import Flask, jsonify | |
from flask_cors import CORS | |
app = Flask(__name__) | |
CORS(app) | |
@app.route('/') | |
def file_changes(): | |
conn = sqlite3.connect('file_events.db') | |
c = conn.cursor() | |
c.execute("SELECT * FROM file_events WHERE src_path NOT LIKE ? AND src_path NOT LIKE ? ORDER BY timestamp DESC LIMIT 30", ('%C:\\Users\\Muhammad\\file_events.db%', '%C:\\Users\\Muhammad\\file_events.db-journal%')) | |
rows = c.fetchall() | |
conn.close() | |
events = [] | |
for row in rows: | |
event = { | |
'id': row[0], | |
'event_type': row[1], | |
'src_path': row[2], | |
'dest_path': row[3], | |
'is_directory': bool(row[4]), | |
'timestamp': row[5] | |
} | |
events.append(event) | |
return jsonify(events) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Run by
python watchdog_api.py C:\
python watchdog_flask.py
python cpu_monitor.py
Thanks..
Attribution is not required, although it is really appreciated. ๐๐
Top comments (1)
Where is everybody!