import requests
import pandas as pd
import datetime
import json
import pytz
https://grafana.com/docs/loki/latest/api/#examples-8
$ curl -H "Content-Type: application/json" -XPOST -s "https://localhost:3100/api/prom/push" --data-raw \
'{"streams": [
{
"labels": "{foo=\"bar\"}",
"entries": [
{
"ts": "2018-12-18T08:28:06.801064-04:00",
"line": "fizzbuzz"
}
]
}
]
}'
url = "http://grafana-lab:3100/loki/api/v1/push"
url = "http://grafana-lab:3100/api/prom/push"
import random
for doc in df.to_dict('r'):
start = datetime.datetime(2021,5,10)
value = doc['completion_ratio']
course_title = doc['title'].replace("\"", "")
row = []
for i in range(90):
dc = start - datetime.timedelta(days=i)
dc = dc.replace(tzinfo=pytz.UTC)
value -= random.choice([0]*random.randint(100,1000)+[1,2,3,4,5])
if value < 0: value = 0
row.append({
"ts": dc.isoformat('T'),
"line": f"{{\"completion_ratio\": {value}}}"
})
payload = {
"streams": [
{
"labels": f"{{job=\"catalog\", course_title=\"{course_title}\"}}",
"entries": row[::-1]
}
]
}
headers = {
"Content-type": "application/json"
}
rs = requests.post(url, headers=headers, data=json.dumps(payload))
print(rs.text)
payload = {
"streams": [
{
"labels": "{job=\"catalog\", course_title=\"kubernetes\"}",
"entries": row
}
]
}
headers = {
"Content-type": "application/json"
}
rs = requests.post(url, headers=headers, data=json.dumps(payload))
print(rs.text)
d = '{"streams": [{ "labels": "{foo=\"bar\"}", "entries": [{ "ts": "2021-05-06T08:28:06.801064-04:00", "line": "fizzbuzz" }] }]}'
rs = requests.post(url, data=json.dumps(d), headers=headers)
print(rs.text)