This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-credits.py
66 lines (54 loc) · 1.96 KB
/
build-credits.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
import configparser
import numpy as np
import datetime
import pandas as pd
import matplotlib.pyplot as plt
cfg = configparser.ConfigParser()
cfg.read('main.cfg')
CIRCLE_TOKEN = cfg['main']['CIRCLE_TOKEN']
VCS = cfg['main']['VCS']
ORG = cfg['main']['ORG']
REPO = cfg['main']['REPO']
IGNORE_BRANCHES = ['develop']
MAX_PAGES = 20
WORKFLOW = cfg['main']['BUILD_WORKFLOW']
headers = {
'Circle-Token': CIRCLE_TOKEN
}
offset = 0
response = requests.get(
'https://circleci.com/api/v1.1/project/' + VCS + '/' + ORG + '/' + REPO + '?shallow=true&limit=100&offset=' + str(offset))
branchData = response.json()
for i in range(MAX_PAGES):
offset = offset + (100 * i)
response = requests.get(
'https://circleci.com/api/v1.1/project/' + VCS + '/' + ORG + '/' + REPO + '?shallow=true&limit=100&offset=' + str(offset))
paginatedData = response.json()
branchData = branchData + paginatedData
branches = [x['branch'] for x in branchData if x['workflows']['workflow_name']
!= 'hourly' and x['lifecycle'] == 'finished']
branches = np.unique(branches)
insightData = []
# get branch insights
for branch in branches:
response = requests.get(
'https://circleci.com/api/v2/insights/' + VCS + '/' + ORG + '/' + REPO + '/workflows/' + WORKFLOW + '?circle-token='+CIRCLE_TOKEN+'&branch='+branch)
branchInsightResp = response.json()
insightData = insightData + branchInsightResp['items']
# only want successful deploys
successfulDeploys = [
item for item in insightData if item['status'] == 'success']
# aggregated date k/v
for item in successfulDeploys:
startedDate = datetime.datetime.strptime(
item['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ')
item['date'] = startedDate.strftime('%m/%d')
# sort by date, asc order
successfulDeploys = sorted(
successfulDeploys, key=lambda i: i['date'], reverse=True)
df = pd.DataFrame(successfulDeploys)
print(df)
df.plot(x='date', y='credits_used')
plt.gca().invert_xaxis()
plt.show()