-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel.py
178 lines (130 loc) · 4.79 KB
/
excel.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /usr/bin/env python
import pandas as pd
import numpy as np
import datetime
import calendar
import itertools
import xlsxwriter
import openpyxl
from openpyxl import Workbook
import json
import sys
import requests
import math
import numpy
## List NOC users from opsgenie
noc_manager = <>
genie_token = <>
url = "https://api.eu.opsgenie.com/v2/users/?query=skypeUsername=NOC"
payload={}
headers = {
'Authorization': 'GenieKey '+ genie_token,
}
response = requests.request("GET", url, headers=headers, data=payload)
response_Org = response.text
sys.stdout.flush()
response_json = json.loads(response_Org)
total_count = int(response_json['totalCount'])
## Check what is the current date
now = datetime.datetime.now()
current_year = now.year
current_month = now.month
next_month = current_month + 1
## Check what is the next month
month_num_str = str(next_month)
datetime_object = datetime.datetime.strptime(month_num_str, "%m")
full_month_name = datetime_object.strftime("%B")
## Create file named by the next month name
file_loc = '~/Desktop/opsgenie/'+full_month_name+'.xlsx'
num_days_in_month = calendar.monthrange(current_year, current_month)[1]
next_date = f"'{current_year}-{next_month}-01"
rng = pd.date_range(next_date, periods=num_days_in_month, freq='D')
## Functions for date listing
def take(n, iterable):
"Return first n items of the iterable as a list"
return list(itertools.islice(iterable, n))
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def single(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a = iterable
return a
## Create date frame for the next month
day = ''
name = ''
user = ''
rotation_id = ''
times = itertools.cycle( ['01:00:00', '09:00:00', '17:00:00'])
rotations = itertools.cycle( ['Night', 'Day', 'Evening'])
empty_one = ''
employees_list = []
fullname_list = []
columns_array = ['Day','StartDate', 'EndDate', 'StartTime', 'EndTime', 'RotationName', 'RotationID', 'Name', 'User','']
for arr_index in columns_array:
employees_list.append(arr_index)
x = 0
for x in range(total_count):
fullname_list.append(response_json['data'][x]['fullName'])
dates = list(itertools.chain.from_iterable(itertools.repeat(timestamp.date(), 3) for timestamp in rng))
paired_dates = pairwise(take((num_days_in_month*3)+1, dates))
paired_times = pairwise(take((num_days_in_month*3)+1, times))
paired_rotations = single(take((num_days_in_month*3)+1, rotations))
df = pd.DataFrame([[day, start_date, end_date, start_time, end_time, rotation_name, rotation_id, name, user, empty_one] for (start_date, end_date), (start_time, end_time), (rotation_name) in zip(paired_dates, paired_times, paired_rotations)],
columns =columns_array)
## Add new columns with emplyees names
x = 0
for x in range(total_count):
df = df.assign(**{fullname_list[x]: ""})
y = 0
for y in range(num_days_in_month*3):
if y%3==1:
df[noc_manager][y] = 'V'
df['Name'][y] = noc_manager
elif y%3!=1:
df[noc_manager][y] = 'X'
## format Date cells to Short Date
df['StartDate'] = pd.to_datetime(df['StartDate'], errors='coerce').dt.strftime('%d/%m/%Y')
df['EndDate'] = pd.to_datetime(df['EndDate'], errors='coerce').dt.strftime('%d/%m/%Y')
## Add rotaion id formula
num_for_rotation = num_days_in_month * 3
i = 0
while i < num_for_rotation:
i_str = i + 2
i_str = str(i_str)
df['Day'][i] = '=TEXT(B'+i_str+',"dddd")'
df['RotationID'][i] = '=IF(F'+i_str+'="Evening","38fa4389-904e-442a-87d7-36dc0dc554b9",IF(F'+i_str+'="Night","3e558a75-9e32-48a2-8123-46f0aa448154",IF(F'+i_str+'="Day","81510d4f-fc2e-4c08-a436-35bbbb6b5384")))'
## List of Employees
username_list = []
first_name = []
formula_var = ''
split_fullname = ''
formula_temp = ''
max_if = 0
loopif = math.ceil(total_count/7)
innerloopcount = 7
x = 0
empty = '""'
for z in range(loopif):
if loopif-z == 1:
innerloopcount = total_count%7
formula_temp = ''
for in_loop in range(innerloopcount):
x = in_loop + (z*7)
username_list.append(response_json['data'][x]['username'])
fullname_list.append(response_json['data'][x]['fullName'])
formula_temp = 'IF(H'+i_str+'=''"'+fullname_list[x]+'"'',''"'+username_list[x]+'"'','+ formula_temp +')'
formula_temp=formula_temp.replace(",)",",\"\")")
if(z != 0 and z%7== 0):
formula_temp = '=' + formula_temp
break
formula_var = formula_temp + " & " + formula_var
formula_var = formula_var[:-1]
formula_var = '=' + formula_var
formula_var = formula_var[:-1]
df['User'][i] = formula_var
i = i + 1
## Write to Excel file
df.to_excel(file_loc, index = False)