-
Notifications
You must be signed in to change notification settings - Fork 59
/
filter.py
162 lines (138 loc) · 5.31 KB
/
filter.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
"""This is the sample script to show how to manage filters.
This script will not work without replacing parameters with real values.
Its basic goal is to present what can be done with this module and to ease
its usage.
"""
from mstrio.connection import Connection, get_connection
from mstrio.modeling import (
ExpressionFormat, Filter, list_filters, SchemaManagement, SchemaUpdateType
)
# Define a variable which can be later used in a script
PROJECT_NAME = $project_name # Insert name of project here
conn: Connection = get_connection(workstationData, PROJECT_NAME)
# Example filter qualification data with expression specified as tree
FILTER_QUALIFICATION_AS_TREE = {
"tree": {
"type": "predicate_metric_qualification",
"predicateTree": {
"function": "greater",
"parameters": [
{
"parameterType": "constant",
"constant": {
"type": "int64", "value": "1"
},
}
],
"levelType": "none",
"metric": {
"objectId": "CE8DAE7048998FFECF77418BDF2343B8",
"subType": "metric",
"name": "Item Count",
},
"metricFunction": "value",
"isIndependent": 0,
"nullInclude": 0,
},
},
}
# Example filter qualification data with expression specified as tokens
FILTER_QUALIFICATION_AS_TOKENS = {
"tokens": [
{
"level": "resolved",
"state": "initial",
"value": "%",
"type": "character",
},
{
"level": "resolved",
"state": "initial",
"value": "[Item Count]",
"type": "object_reference",
},
{
"level": "resolved",
"state": "initial",
"value": ">",
"type": "character",
},
{
"level": "resolved", "state": "initial", "value": "1", "type": "unknown"
},
{
"level": "resolved",
"state": "initial",
"value": "",
"type": "end_of_text",
},
],
}
# Define a variable which can be later used in a script
SEARCH_NAME = $search_name # Insert part of the filter name to be searched for
# Filters management
# Get list of filters, with examples of different conditions
list_of_all_filters = list_filters(connection=conn)
list_of_limited_filters = list_filters(connection=conn, limit=10)
list_of_limited_filters_to_dict = list_filters(connection=conn, limit=10, to_dictionary=True)
list_of_filters_by_name = list_filters(connection=conn, name=SEARCH_NAME)
# list of filters with expressions represented as tree
list_of_filters_as_trees = list_filters(connection=conn, show_expression_as=ExpressionFormat.TREE)
# list of filters with expressions represented as tokens
list_of_filters_as_tokens = list_filters(
connection=conn, show_expression_as=ExpressionFormat.TOKENS
)
# list of filters with qualification represented also in tokens format
list_of_filters_with_qualification_as_tokens = list_filters(
connection=conn, show_filter_tokens=True
)
# Define variables which can be later used in a script
FILTER_ID = $filter_id # Insert ID of existing filter here
FILTER_NAME = $filter_name # Insert name of a new filter to be created
# Get specific filter by id with expressions represented as trees (default)
filter = Filter(connection=conn, id=FILTER_ID)
# Get specified filter by id with expressions represented as tokens
filter_tokens_expression = Filter(
connection=conn, id=FILTER_ID, show_expression_as=ExpressionFormat.TOKENS
)
# Get specified filter by id with qualification also in tokens format
filter_tokens_qualification = Filter(connection=conn, id=FILTER_ID, show_filter_tokens=True)
# Get filter by name
filter = Filter(connection=conn, name=FILTER_NAME)
# List filter properties
properties = filter.list_properties()
# Define a variable which can be later used in a script
FOLDER_ID = $folder_id # Insert folder ID here
# create new empty filter
test_filter = Filter.create(
connection=conn,
name=FILTER_NAME,
destination_folder=FOLDER_ID,
)
# create new filter using qualification as tree and
# specify to return qualification also in tokens format
test_filter_tree = Filter.create(
connection=conn,
name=(FILTER_NAME + "_tree"),
destination_folder=FOLDER_ID,
show_filter_tokens=True,
qualification=FILTER_QUALIFICATION_AS_TREE,
)
# create new filter using qualification as tokens
test_filter_tokens = Filter.create(
connection=conn,
name=(FILTER_NAME + "_tokens"),
destination_folder=FOLDER_ID,
qualification=FILTER_QUALIFICATION_AS_TOKENS,
)
# Any changes to schema object must be followed by schema_reload
# in order to use them in reports, dashboards and so on
schema_manager = SchemaManagement(connection=conn, project_id=conn.project_id)
task = schema_manager.reload(update_types=[SchemaUpdateType.LOGICAL_SIZE])
# Define variables which can be later used in a script
FILTER_NEW_NAME = $filter_new_name # Insert new name of edited Filter here
FILTER_NEW_DESC = $filter_new_desc # Insert new description of edited filter here
# Alter filter
test_filter.alter(name=FILTER_NEW_NAME, description=FILTER_NEW_DESC)
# Delete newly created filter
test_filter.delete(force=True)