Skip to content

Commit 68b64cd

Browse files
author
matheuslab
committed
feat: optimizing refresh routine
1 parent 327c68d commit 68b64cd

6 files changed

+45
-33
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ __pycache__
44
build/
55
dist/
66
output/
7-
*.txt
7+
*.txt
8+
*.csv

src/costs.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import requests
22
import pandas as pd
33
from initial import timestamps
4+
import os
45

56

6-
def getCostsDataFrame(apiKey=None, fleetId='', timestamps=timestamps):
7+
def getCostsDataFrame(apiKey=None, fleetName='', timestamps=timestamps):
78
frames = []
9+
if os.path.exists(os.path.expanduser('~/cobliBI/costs.csv')):
10+
frames.append(pd.read_csv(os.path.expanduser('~/cobliBI/costs.csv')))
811
for month in timestamps:
912
start_timestamp, end_timestamp = timestamps[month]
1013
costsURL = f"https://api.cobli.co/herbie-1.1/costs/report?begin={start_timestamp}&end={end_timestamp}&tz=America%2FFortaleza"
1114
costsResponse = requests.get(costsURL, headers={'cobli-api-key': apiKey})
1215
frames.append(pd.read_excel(costsResponse.content, 0))
1316
dataframe = pd.concat(frames)
14-
dataframe.assign(Frota = [fleetId] * len(dataframe.index))
15-
dataframe.to_csv('costs.csv')
17+
dataframe['Frota'] = [fleetName] * len(dataframe.index)
18+
dataframe.to_csv(os.path.expanduser('~/cobliBI/costs.csv'), index = False)
1619
return dataframe
1720

1821
costs_function_name = 'getCostsDataFrame'

src/generate_bi_script.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ def check_api_keys(apiKeyList: dict):
3131

3232

3333
def generate_script(apiKeyList: dict):
34-
with open('./main_file.txt', 'w') as main_file:
35-
main_file.write(initialSource + '\n\n')
36-
main_file.write(costsScript + '\n')
37-
main_file.write(incidentsScript + '\n')
38-
main_file.write(productivityScript + '\n')
39-
for key in apiKeyList:
40-
main_file.write(
41-
f"{costs_function_name}('{apiKeyList[key]}', '{key}')\n\n")
42-
main_file.write(
43-
f"{incidents_function_name}('{apiKeyList[key]}', '{key}')\n\n")
44-
main_file.write(
45-
f"{productivity_function_name}('{apiKeyList[key]}', '{key}')\n\n")
46-
with open('./data_file.txt', 'w') as data_file:
34+
print(initialSource + '\n\n')
35+
print(costsScript + '\n')
36+
print(incidentsScript + '\n')
37+
print(productivityScript + '\n')
38+
for key in apiKeyList:
39+
print(
40+
f"costs = {costs_function_name}('{apiKeyList[key]}', '{key}')\n\n")
41+
print(
42+
f"incidents = {incidents_function_name}('{apiKeyList[key]}', '{key}')\n\n")
43+
print(
44+
f"productivity = {productivity_function_name}('{apiKeyList[key]}', '{key}')\n\n")
45+
os.makedirs(os.path.expanduser('~/cobliBI'), exist_ok=True)
46+
with open(os.path.expanduser('~/cobliBI/data_file.txt'), 'w') as data_file:
4747
data_file.write(initial.now)
4848

4949
gui(check_api_keys)

src/incidents.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import requests
22
import pandas as pd
33
from initial import timestamps
4+
import os
45

56

6-
def getIncidentsDataFrame(apiKey=None, fleetId='', timestamps=timestamps):
7+
def getIncidentsDataFrame(apiKey=None, fleetName='', timestamps=timestamps):
78
frames = []
9+
if os.path.exists(os.path.expanduser('~/cobliBI/incidents.csv')):
10+
frames.append(pd.read_csv(os.path.expanduser('~/cobliBI/incidents.csv')))
811
for month in timestamps:
912
start_timestamp, end_timestamp = timestamps[month]
1013
incidentsURL = f"https://api.cobli.co/herbie-1.1/stats/incidents/report?begin={start_timestamp}&end={end_timestamp}&tz=America%2FFortaleza"
1114
incidentsResponse = requests.get(
1215
incidentsURL, headers={'cobli-api-key': apiKey})
1316
frames.append(pd.read_excel(incidentsResponse.content, 0))
1417
dataframe = pd.concat(frames)
15-
16-
dataframe.assign(Frota = [fleetId] * len(dataframe.index))
17-
dataframe.to_csv('incidents.csv')
18+
dataframe['Frota'] = [fleetName] * len(dataframe.index)
19+
dataframe.to_csv(os.path.expanduser('~/cobliBI/incidents.csv'), index = False)
1820
return dataframe
1921

2022
incidents_function_name = 'getIncidentsDataFrame'

src/initial.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@
99
format_string = "%d/%m/%Y %H:%M:%S.%f"
1010

1111
now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S.%f")
12+
formattedCurrentDate = datetime.datetime.strptime(now, format_string)
1213

1314
timestamps = dict()
1415

16+
def utc_to_epoch(date):
17+
return int(time.mktime(datetime.datetime.strptime(date, format_string).timetuple()))*1000
18+
1519
for monthsAgo in range(6):
16-
end_date_str = (datetime.datetime.strptime(now, format_string) - pd.DateOffset(months=monthsAgo)).strftime(format_string)
17-
end_timestamp_value = int(time.mktime(
18-
datetime.datetime.strptime(end_date_str, format_string).timetuple()))*1000
20+
end_date_str = (formattedCurrentDate - pd.DateOffset(months=monthsAgo)).strftime(format_string)
21+
end_timestamp_value = utc_to_epoch(end_date_str)
1922

20-
start_date_str = (datetime.datetime.strptime(now, format_string) - pd.DateOffset(months=monthsAgo+1)).strftime(format_string)
21-
start_timestamp_value = int(time.mktime(
22-
datetime.datetime.strptime(start_date_str, format_string).timetuple()))*1000
23+
start_date_str = (formattedCurrentDate - pd.DateOffset(months=monthsAgo+1)).strftime(format_string)
24+
start_timestamp_value = utc_to_epoch(start_date_str)
2325

2426
timestamps[monthsAgo] = (start_timestamp_value, end_timestamp_value)
2527

26-
if os.path.exists('./data_file.txt'):
27-
with open('./data_file.txt', 'r') as data_file:
28+
if os.path.exists(os.path.expanduser('~/cobliBI/data_file.txt')):
29+
with open(os.path.expanduser('~/cobliBI/data_file.txt'), 'r') as data_file:
2830
last_refresh = data_file.read()
2931

3032
if last_refresh:
31-
timestamps = [last_refresh, now]
33+
shortTimestamp = dict()
34+
shortTimestamp[0] = [utc_to_epoch(last_refresh), utc_to_epoch(now)]

src/productivity.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import requests
22
import pandas as pd
33
from initial import timestamps
4+
import os
45

56

6-
def getProductivityDataFrame(apiKey=None, fleetId='', timestamps=timestamps):
7+
def getProductivityDataFrame(apiKey=None, fleetName='', timestamps=timestamps):
78
frames = []
9+
if os.path.exists(os.path.expanduser('~/cobliBI/productivity.csv')):
10+
frames.append(pd.read_csv(os.path.expanduser('~/cobliBI/productivity.csv')))
811
for month in timestamps:
912
start_timestamp, end_timestamp = timestamps[month]
1013
productivityURL = f"https://api.cobli.co/herbie-1.1/stats/performance/vehicle/report?begin={start_timestamp}&end={end_timestamp}&tz=America%2FFortaleza"
1114
productivityResponse = requests.get(
1215
productivityURL, headers={'cobli-api-key': apiKey})
1316
frames.append(pd.read_excel(productivityResponse.content, 2))
1417
dataframe = pd.concat(frames)
15-
dataframe.assign(Frota = [fleetId] * len(dataframe.index))
16-
dataframe.to_csv('productivity.csv')
18+
dataframe['Frota'] = [fleetName] * len(dataframe.index)
19+
dataframe.to_csv(os.path.expanduser('~/cobliBI/productivity.csv'), index = False)
1720
return dataframe
1821

1922
productivity_function_name = 'getProductivityDataFrame'

0 commit comments

Comments
 (0)