|
3 | 3 | import datetime |
4 | 4 | import os |
5 | 5 |
|
6 | | - |
7 | 6 | def graph_cpu_load(db_report): |
8 | 7 | now = datetime.datetime.now() |
9 | 8 | window_start = now - datetime.timedelta(hours=24) |
10 | 9 |
|
11 | | - times = [] |
12 | | - loads = [] |
| 10 | + time = [] |
| 11 | + load = [] |
13 | 12 |
|
14 | 13 | for entry in db_report: |
15 | | - t = datetime.datetime.fromtimestamp(entry['timestamp']) |
16 | | - if t >= window_start: |
17 | | - times.append(t) |
18 | | - loads.append(entry['cpu_prcnt']) |
19 | | - |
20 | | - if not times: |
21 | | - times = [window_start, now] |
22 | | - loads = [0, 0] |
23 | | - |
24 | | - times, loads = zip(*sorted(zip(times, loads))) |
| 14 | + dt = datetime.datetime.fromtimestamp(entry['timestamp']) |
| 15 | + time.append(dt) |
| 16 | + load.append(entry['cpu_prcnt']) |
25 | 17 |
|
26 | 18 | plt.figure(figsize=(14, 6)) |
27 | 19 | plt.title("CPU Usage - Last 24 Hours", fontsize=14, fontweight='bold') |
28 | 20 | plt.xlabel("Time", fontsize=12) |
29 | | - plt.ylabel("CPU Load (%)", fontsize=12) |
| 21 | + plt.ylabel("CPU Load", fontsize=12) |
30 | 22 |
|
31 | | - plt.plot(times, loads, linewidth=1.5) |
32 | | - |
33 | | - plt.grid(True, alpha=0.5, color='gray') |
34 | | - plt.ylim(0, 100) |
35 | | - |
36 | | - ax = plt.gca() |
37 | | - ax.set_xlim(window_start, now) |
38 | | - |
39 | | - ax.xaxis.set_major_locator(mdates.HourLocator(interval=2)) |
40 | | - ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) |
| 23 | + plt.plot(time, load, linewidth=1.5) |
| 24 | + plt.xlim(window_start, now) |
41 | 25 |
|
| 26 | + plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) |
| 27 | + plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=2)) |
42 | 28 | plt.gcf().autofmt_xdate() |
43 | | - plt.tight_layout() |
| 29 | + |
| 30 | + plt.grid(True, alpha=0.5, color='gray') |
| 31 | + #plt.ylim(0, 100) |
44 | 32 |
|
45 | 33 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
46 | 34 | save_path = os.path.join(script_dir, 'cpu_load.png') |
|
0 commit comments