-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
41 lines (35 loc) · 1.35 KB
/
Copy pathanalysis.py
File metadata and controls
41 lines (35 loc) · 1.35 KB
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
from io import StringIO
import pandas as pd
import pytz
import requests
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('token')
args = parser.parse_args()
token = args.token
url = 'http://localhost:8087/api/v2/query?org=personal'
header = {
"Authorization": f"Token {token}",
'Accept': 'application/csv',
'Content-type': 'application/vnd.flux',
}
data = """
from(bucket: "currently-airing-anime")
|> range(start: -7d)
|> filter(fn: (r) => r["title"] == "Blue Lock")
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
|> drop (columns: ["_start","_stop"])
|> yield(name: "last")
"""
response = requests.post(url, headers=header, data=data).text
# process multiple yield statements
for table in response.split('\r\n\r\n')[:-1]:
df = pd.read_csv(StringIO(table))
df = df.dropna(how='all', axis='columns')
df = df.drop(columns=['table', 'result', '_measurement'])
df["_time"] = pd.to_datetime(df["_time"], infer_datetime_format=True, utc=True).dt.tz_convert(
pytz.timezone('US/Eastern')
)
df = df.rename(columns={'_time': 'time'})
df.to_csv('data/data.csv')