In [67]:
import requests
import pandas as pd
import datetime
import json
import pytz
In [ ]:
 

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" 
              }
          ] 
      }
    ]
}'
In [34]:
url = "http://grafana-lab:3100/loki/api/v1/push"
url = "http://grafana-lab:3100/api/prom/push"
In [140]:
import random
In [ ]:
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)
In [142]:
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))
In [143]:
print(rs.text)

In [134]:
 

In [ ]:
 
In [73]:
d = '{"streams": [{ "labels": "{foo=\"bar\"}", "entries": [{ "ts": "2021-05-06T08:28:06.801064-04:00", "line": "fizzbuzz" }] }]}'
In [74]:
rs = requests.post(url, data=json.dumps(d), headers=headers)
In [75]:
print(rs.text)
readObjectStart: expect { or n, but found ", error found in #1 byte of ...|"{\"streams|..., bigger context ...|"{\"streams\": [{ \"labels\": \"{foo=\"bar\"}\", \"|...

In [ ]:
 
In [ ]:
 
In [ ]: