Closed ZHENGHECHU closed 1 year ago
TM1 version: 11.2 TM1py version: 1.11.3
Thanks for raising this issue @ZHENGHECHU I will look into this.
For the moment, as a workaround, I would like to propose two alternative approaches:
don't persist the view in a file and connect to both instances at the same time:
with TM1Service(**config["Test1"]) as tm1:
view = tm1.views.get(cube_name, view_name)
with TM1Service(**config["Test2"]) as tm1_target:
tm1_target.views.update_or_create(view)
pickle and unpickle the view object instead of writing and reading JSON.
import pickle
with TM1Service(**config["Test1"]) as tm1: view = tm1.views.get(cube_name, view_name) with open("view", "wb") as file: pickle.dump(view, file)
with TM1Service(**config["Test2"]) as tm1_target:
with open("view", "rb") as file:
view = pickle.load(file)
tm1_target.views.update_or_create(view)
Hello @MariusWirtz, I appreciate your response and the valuable alternative suggestions you provided. However, I currently have a pressing need to store the view in a file while ensuring that the structure remains comprehensible. Your assistance in finding a suitable solution would be greatly appreciated. Kindly keep me informed by tagging me. thank you for your help in advance.
@ZHENGHECHU same as in #948 the problem is that TM1 returns a slightly different JSON when you read a chore than the JSON that must be posted when creating a chore.
I am working on a fix here: https://github.com/cubewise-code/tm1py/pull/955 I will add a few more test cases, but you can already upgrade TM1py to this feature branch and test if it resolves the issue.
pip uninstall tm1py
pip install https://github.com/cubewise-code/tm1py/archive/refs/heads/feature/make-native-view-from-json-robust.zip
You will need to make a small change to your code though. Make sure to pass the cube name to the from_json
call.
view_put = NativeView.from_json(view_put, cube_name="Sales")
Hello @MariusWirtz,
Thank you for your reply,
Following the upgrade of TM1py to the feature branch, I continue to experience certain challenges. Views that include the 'Expression' element in their output exhibit no issues when utilizing the NativeView.from_json function, those lacking 'Expression' component are not functioning as intended.
Work well
{ "@odata.type": "ibm.tm1.api.v1.NativeView", "Name": "ALL", "Columns": [], "Rows": [ { "Subset": { "Hierarchy@odata.bind": "Dimensions('Geography')/Hierarchies('Geography')", "Expression": "TM1SubsetAll([Geography])" } }]},
didnt work
{ "@odata.type": "ibm.tm1.api.v1.NativeView", "Name": "Conso Prod 5144", "Columns": [ { "Subset": { "Hierarchy@odata.bind": "Dimensions('Collection')/Hierarchies('Collection')", "Elements@odata.bind": [ "Dimensions('Collection')/Hierarchies('Collection')/Elements('22K')" ] } }]},
`Traceback (most recent call last):
File "c:\TM1py_views.py", line 90, in
KeyError: 'Expression'`
Thanks for the catch @ZHENGHECHU
I updated the branch to handle this case. The bugfix is still WIP and missing unit tests, but feel free to upgrade and test already!
pip uninstall tm1py
pip install https://github.com/cubewise-code/tm1py/archive/refs/heads/feature/make-native-view-from-json-robust.zip
I will add test cases later this week.
Hello @MariusWirtz, Thank you for your dedication. Subsequent to the integration of the upgraded TM1py into the feature branch, I have observed that it functions effectively for the majority of views. However, a formatting concern has arisen. Specifically, when an element's name includes the '&' character, such as 'F & Sales', and this element is encompassed within views, the output in the generated text file will appear as 'F %26 Sales'. And the same situation, when the element contains '%', in the output file it become '%25'. Consequently, the functionality of the NativeView.from_json function becomes compromised.
While I surmise that this issue might be attributed to a reason I encountered in online sources, I cannot confirm its exact cause at this juncture.
when including '&' directly in the API request parameters without proper URL encoding, it can be misinterpreted by the server.
Here is an exmple of output { "@odata.type": "ibm.tm1.api.v1.NativeView", "Name": "test", "Rows": [ { "Subset": { "Hierarchy@odata.bind": "Dimensions('Collection')/Hierarchies('Collection')", "Elements@odata.bind": [ "Dimensions('Collection')/Hierarchies('Collection')/Elements('element1')", "Dimensions('Collection')/Hierarchies('Collection')/Elements('F %26 B Sales')"]}}]}
thanks for your help in advance
Thanks for the finding! Of course, we need to unescape the URL encoding in object names in the OData URLs.
I included this in the branch. Please upgrade and try again.
pip uninstall tm1py
pip install https://github.com/cubewise-code/tm1py/archive/refs/heads/feature/make-native-view-from-json-robust.zip
Resolved with #955
Describe what did you try to do with TM1py Hello, i run the following script aiming to convert Views between serveurs but encounters problems using function NativeView.from_json
Script:
Furthur information why i dont convert views between serveurs directly, beacuase i want the target instance only read the txt.file to import the View
The output in txt.file:
ERROR MESSAGE: