-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup_sample.py
210 lines (195 loc) · 7.72 KB
/
setup_sample.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import configparser
from TM1py import TM1Service, Dimension, Hierarchy, Element, Cube, NativeView, AnonymousSubset
CONFIG = "config.ini"
config = configparser.ConfigParser()
config.read(CONFIG)
with TM1Service(**config['tm1srv01']) as tm1:
# create dimensions
dimension = Dimension(
name="Py Project")
hierarchy = Hierarchy(
name="Py Project",
dimension_name="Py Project",
elements=[
Element("Project1", "Numeric"),
Element("Project2", "Numeric"),
Element("Project3", "Numeric")])
dimension.add_hierarchy(hierarchy)
if not tm1.dimensions.exists(dimension.name):
tm1.dimensions.create(dimension)
dimension = Dimension(
name="Py Quarter")
hierarchy = Hierarchy(
name="Py Quarter",
dimension_name="Py Quarter",
elements=[
Element("2018-Q1", "Numeric"),
Element("2018-Q2", "Numeric"),
Element("2018-Q3", "Numeric"),
Element("2018-Q4", "Numeric"),
Element("2019-Q1", "Numeric"),
Element("2019-Q2", "Numeric"),
Element("2019-Q3", "Numeric"),
Element("2019-Q4", "Numeric"),
Element("2020-Q1", "Numeric"),
Element("2020-Q2", "Numeric"),
Element("2020-Q3", "Numeric"),
Element("2020-Q4", "Numeric")])
dimension.add_hierarchy(hierarchy)
if not tm1.dimensions.exists(dimension.name):
tm1.dimensions.create(dimension)
dimension = Dimension(
name="Py Project Planning Measure")
hierarchy = Hierarchy(
name="Py Project Planning Measure",
dimension_name="Py Project Planning Measure",
elements=[Element("Cashflow", "Numeric")])
dimension.add_hierarchy(hierarchy)
tm1.dimensions.update_or_create(dimension)
dimension = Dimension(
name="Py Project Summary")
hierarchy = Hierarchy(
name="Py Project Summary",
dimension_name="Py Project Summary",
elements=[Element("IRR", "Numeric"), Element("NPV", "Numeric")])
dimension.add_hierarchy(hierarchy)
tm1.dimensions.update_or_create(dimension)
dimension = Dimension(
name="Py Project Summary Measure")
hierarchy = Hierarchy(
name="Py Project Summary Measure",
dimension_name="Py Project Summary Measure",
elements=[Element("Value", "Numeric")])
dimension.add_hierarchy(hierarchy)
tm1.dimensions.update_or_create(dimension)
# create cube 1
cube = Cube(
name="Py Project Planning", dimensions=["Py Project", "Py Quarter", "Py Project Planning Measure"])
if tm1.cubes.exists(cube.name):
tm1.cubes.delete(cube.name)
tm1.cubes.create(cube)
# create cube 2
cube = Cube(
name="Py Project Summary", dimensions=["Py Project", "Py Project Summary", "Py Project Summary Measure"])
if tm1.cubes.exists(cube.name):
tm1.cubes.delete(cube.name)
tm1.cubes.create(cube)
# create views
for project in ("Project1", "Project2", "Project3"):
# Project Summary
cube_name = "Py Project Summary"
view = NativeView(
cube_name=cube_name,
view_name=project + " NPV",
format_string="0.#########",
suppress_empty_columns=False,
suppress_empty_rows=False)
view.add_row(
dimension_name="Py Project Summary",
subset=AnonymousSubset(
dimension_name="Py Project Summary",
elements=["NPV"]))
view.add_title(
dimension_name="Py Project",
subset=AnonymousSubset(
dimension_name="Py Project",
elements=[project]),
selection=project)
view.add_column(
dimension_name="Py Project Summary Measure",
subset=AnonymousSubset(
dimension_name="Py Project Summary Measure",
elements=["Value"]))
tm1.views.update_or_create(view)
view = NativeView(
cube_name=cube_name,
view_name=project + " IRR",
format_string="0.#########",
suppress_empty_columns=False,
suppress_empty_rows=False)
view.add_row(
dimension_name="Py Project Summary",
subset=AnonymousSubset(
dimension_name="Py Project Summary",
elements=["IRR"]))
view.add_title(
dimension_name="Py Project",
subset=AnonymousSubset(
dimension_name="Py Project",
elements=[project]),
selection=project)
view.add_column(
dimension_name="Py Project Summary Measure",
subset=AnonymousSubset(
dimension_name="Py Project Summary Measure",
elements=["Value"]))
tm1.views.update_or_create(view)
# Project Planning
cube_name = "Py Project Planning"
view = NativeView(
cube_name=cube_name,
view_name=project,
format_string="0.#########",
suppress_empty_columns=False,
suppress_empty_rows=False)
view.add_row(
dimension_name="Py Quarter",
subset=AnonymousSubset(
dimension_name="Py Quarter",
expression="{Tm1SubsetAll([Py Quarter])}"))
view.add_title(
dimension_name="Py Project",
selection=project,
subset=AnonymousSubset(
dimension_name="Py Project",
elements=[project]))
view.add_column(
dimension_name="Py Project Planning Measure",
subset=AnonymousSubset(
dimension_name="Py Project Planning Measure",
elements=["Cashflow"])
)
tm1.views.update_or_create(view)
# write to cube 1
cellset = {
('Project1', '2018-Q1', 'Cashflow'): -100000,
('Project1', '2018-Q2', 'Cashflow'): 10000,
('Project1', '2018-Q3', 'Cashflow'): 10000,
('Project1', '2018-Q4', 'Cashflow'): 10000,
('Project1', '2019-Q1', 'Cashflow'): 10000,
('Project1', '2019-Q2', 'Cashflow'): 10000,
('Project1', '2019-Q3', 'Cashflow'): 10000,
('Project1', '2019-Q4', 'Cashflow'): 10000,
('Project1', '2020-Q1', 'Cashflow'): 10000,
('Project1', '2020-Q2', 'Cashflow'): 10000,
('Project1', '2020-Q3', 'Cashflow'): 10000,
('Project1', '2020-Q4', 'Cashflow'): 10000,
('Project2', '2018-Q1', 'Cashflow'): -100000,
('Project2', '2018-Q2', 'Cashflow'): 8000,
('Project2', '2018-Q3', 'Cashflow'): 8000,
('Project2', '2018-Q4', 'Cashflow'): 8000,
('Project2', '2019-Q1', 'Cashflow'): 8000,
('Project2', '2019-Q2', 'Cashflow'): 11000,
('Project2', '2019-Q3', 'Cashflow'): 11000,
('Project2', '2019-Q4', 'Cashflow'): 11000,
('Project2', '2020-Q1', 'Cashflow'): 12000,
('Project2', '2020-Q2', 'Cashflow'): 12000,
('Project2', '2020-Q3', 'Cashflow'): 13000,
('Project2', '2020-Q4', 'Cashflow'): 13000,
('Project3', '2018-Q1', 'Cashflow'): -90000,
('Project3', '2018-Q2', 'Cashflow'): 8000,
('Project3', '2018-Q3', 'Cashflow'): 8000,
('Project3', '2018-Q4', 'Cashflow'): 8000,
('Project3', '2019-Q1', 'Cashflow'): 8000,
('Project3', '2019-Q2', 'Cashflow'): 8000,
('Project3', '2019-Q3', 'Cashflow'): 8000,
('Project3', '2019-Q4', 'Cashflow'): 8000,
('Project3', '2020-Q1', 'Cashflow'): 8000,
('Project3', '2020-Q2', 'Cashflow'): 8000,
('Project3', '2020-Q3', 'Cashflow'): 8000,
('Project3', '2020-Q4', 'Cashflow'): 8000
}
tm1.cubes.cells.write_values(
cube_name="Py Project Planning",
cellset_as_dict=cellset
)