-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_analysis.py
196 lines (163 loc) · 5.23 KB
/
log_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import time
import pandas
import matplotlib.pyplot as plt
log_folder = r"logs"
def plot_logs(save = False):
#grab all the raw lines
lines = []
for log_file in os.listdir(log_folder):
file_path = os.path.join(log_folder, log_file)
with open(file_path, "r") as f:
lines.extend(f.readlines())
lines = [l for l in lines if l != "" and l != "\n"]
#filter for lines that fit the format
good_lines = []
for line in lines:
#split by spaces
parts = line.split(' ')
#should be at least 2 parts
if len(parts) < 2:
print(f'Parts are less than 2: {parts}')
continue
#first part must be a float
try:
float(parts[0])
except:
continue
#parse the good line
line = line.replace('\n','')
good_lines.append(line)
lines = good_lines
#sort by the first part when split by spaces
lines = sorted(lines, key=lambda x: float(x.split(' ')[0]))
columns = [
"timestamp",
"post_rate_limits",
"post_successfuls",
"images",
"featured_scrape_successfuls",
"featured_scrape_rate_limits",
"collective_scrape_successfuls",
"collective_scrape_rate_limits",
]
# Initialize the data list for rows
data = []
current_data_dict = {
"post_rate_limit": 0,
"post_successful": 0,
"images": 0,
"featured_scrape_successful": 0,
"featured_scrape_rate_limit": 0,
"collective_scrape_successful": 0,
"collective_scrape_rate_limit": 0,
}
first_timestamp = None
for line in lines:
# Skip empty lines
line = line.lower()
if line == "":
continue
# Update counters based on log line content
for key in current_data_dict.keys():
if "image_count" in line:
current_data_dict["images"] = int(line.split(" ")[-1])
continue
if key in line:
current_data_dict[key] += 1
# Add the current state of the dictionary to the data list
data.append(
{
"timestamp": float(line.split(' ')[0]),
"post_rate_limits": current_data_dict["post_rate_limit"],
"post_successfuls": current_data_dict["post_successful"],
"images": current_data_dict["images"],
"featured_scrape_successfuls": current_data_dict[
"featured_scrape_successful"
],
"featured_scrape_rate_limits": current_data_dict[
"featured_scrape_rate_limit"
],
"collective_scrape_successfuls": current_data_dict[
"collective_scrape_successful"
],
"collective_scrape_rate_limits": current_data_dict[
"collective_scrape_rate_limit"
],
}
)
# Create a DataFrame from the collected data
df = pandas.DataFrame(data, columns=columns)
# Create a single plot with all metrics
fig, ax1 = plt.subplots(figsize=(15, 8))
ax1.plot(
df["timestamp"],
df["post_rate_limits"],
label="Post Rate Limits",
marker="x",
color="red",
)
ax1.plot(
df["timestamp"],
df["post_successfuls"],
label="Post Successfuls",
marker="s",
color="green",
)
# Add scraping metrics to the same axis
ax1.plot(
df["timestamp"],
df["featured_scrape_successfuls"],
label="Featured Scrape Successfuls",
linestyle="--",
marker="x",
color="orange",
)
ax1.plot(
df["timestamp"],
df["featured_scrape_rate_limits"],
label="Featured Scrape Rate Limits",
linestyle="--",
marker="s",
color="brown",
)
ax1.plot(
df["timestamp"],
df["collective_scrape_successfuls"],
label="Collective Scrape Successfuls",
linestyle=":",
marker="x",
color="pink",
)
ax1.plot(
df["timestamp"],
df["collective_scrape_rate_limits"],
label="Collective Scrape Rate Limits",
linestyle=":",
marker="s",
color="gray",
)
# Plot images on a secondary y-axis
ax2 = ax1.twinx()
ax2.plot(
df["timestamp"], df["images"], label="Images in Folder", color="gold", linewidth=2
)
# Customize axes
ax1.set_xlabel("Time (Minutes)", fontsize=12)
ax1.set_ylabel("Counts (Post and Scrape Metrics)", fontsize=12)
ax2.set_ylabel("Image Count", fontsize=12, color="gold")
ax2.tick_params(axis="y", labelcolor="gold")
# Combine legends from both axes
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc="upper left", fontsize=10)
# Add grid and title
ax1.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.title("Metrics Over Time", fontsize=14)
# Save and show the plot
plt.tight_layout()
plt.show()
if save is True:
plt.savefig('autopost_stats.png')
if __name__ == '__main__':
plot_logs(save=False)