Skip to content

Instantly share code, notes, and snippets.

@churnikov
Last active May 26, 2021 13:09
Show Gist options
  • Save churnikov/ce61991a6ef5ac9ac6bac161cfc17dcf to your computer and use it in GitHub Desktop.
Save churnikov/ce61991a6ef5ac9ac6bac161cfc17dcf to your computer and use it in GitHub Desktop.
Как получить график количества запросов с разных клиентов из логов Loki
import json
import pandas as pd
from collections import Counter
def get_logs(paths):
logs = []
for path in paths:
df = pd.read_csv(path)
for line in df.iterrows():
logs.append(json.loads(line[1][1]))
return logs
def get_clients_counts(logs_lst):
clients = []
for l in logs:
clients.append(l["log"].split("\"-\"")[-1].split("\"")[-2])
ccounts = Counter(clients)
return pd.Series(ccounts)
logs = get_logs([])
df_counts = get_clients_counts(logs)
df_counts.index = ["Change", "to", "right", "names"]
final_counts = {key: df_counts[key].sum() for key in df_counts.index.unique()}
print(final_counts_df.sort_values(ascending=False))
fig = final_counts_df.sort_values(ascending=False).plot(kind="barh", figsize=(10, 6))
for i, v in enumerate(final_counts_df.sort_values(ascending=False).tolist()):
fig.text(v + 3, i-0.05, str(v), color='blue', fontweight='bold')
plt.tight_layout()
plt.savefig("plt.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment