Skip to content

Instantly share code, notes, and snippets.

@madhukar93
Created April 25, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhukar93/bcea21502de22ad68a0dd823b39f3970 to your computer and use it in GitHub Desktop.
Save madhukar93/bcea21502de22ad68a0dd823b39f3970 to your computer and use it in GitHub Desktop.
from influxdb import InfluxDBClient
db = "ci-metrics-testing"
client = InfluxDBClient("localhost", 8086, "root", "root", db)
client.create_database(db) # TODO: check if this overrites previous db
days = [
# 1
{
"measurement": "commits",
"tags": {"author": "A", "repo": "R1"},
"time": "2009-11-10T23:00:00Z",
"fields": {"sha": "0"},
},
{
"measurement": "commits",
"tags": {"author": "B", "repo": "R1"},
"time": "2009-11-10T23:00:00Z",
"fields": {"sha": "1"},
},
# 2
{
"measurement": "commits",
"tags": {"author": "A", "repo": "R1"},
"time": "2009-11-11T08:00:00Z",
"fields": {"sha": "2"},
},
{
"measurement": "commits",
"tags": {"author": "A", "repo": "R2"},
"time": "2009-11-11T22:00:00Z",
"fields": {"sha": "3"},
},
]
prs = [
{
"measurement": "prs",
"tags": {"repo": "A"},
"time": "2009-11-11T22:00:00Z",
"fields": {"state": "open"},
},
{
"measurement": "prs",
"tags": {"repo": "A"},
"time": "2009-11-12T22:00:00Z",
"fields": {"state": "open"},
},
{
"measurement": "prs",
"tags": {"repo": "A"},
"time": "2009-11-12T23:00:00Z",
"fields": {"state": "closed"},
},
{
"measurement": "prs",
"tags": {"repo": "B"},
"time": "2009-11-12T20:00:00Z",
"fields": {"state": "open"},
},
{
"measurement": "prs",
"tags": {"repo": "B"},
"time": "2009-11-12T23:00:00Z",
"fields": {"state": "closed"},
},
]
if __name__ == "__main__":
from pprint import pprint
client.write_points(days + prs)
result = client.query("""
select count(*) from commits
where time < '2009-11-12T23:59:00Z'
group by time(1d), author;
""")
pprint(result.raw)
result = client.query("""
select count(*) from commits
where time < '2009-11-12T23:59:00Z'
group by time(1d), repo;
""")
pprint(result.raw)
result = client.query("""
select repo, state from
(
select repo, last(state) as state from prs
where state='open'
group by repo
),
(
select repo, state from prs
where state='closed'
group by repo
);
""")
pprint(result.raw)
result = client.query("""
select elapsed(state, 1h) from
(
select repo, last(state) as state from prs
where state='open'
group by repo
),
(
select repo, last(state) from prs
where state='closed'
group by repo
)
group by repo;
""")
pprint(result.raw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment