Last active
August 29, 2015 14:06
-
-
Save petrolmer/ae22606362b1e2e647fd to your computer and use it in GitHub Desktop.
GoodData Automation SDK: Replace one date dimension with another in all metrics and reports.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If your new dimension is not in the project yet: | |
# 1. Clone your project. | |
# 2. Put new fiscal dim to a project, connect it to the same datasets as the original date dimension. | |
# 3. Remove links to the original dimension but do not delete the dimension from the project. | |
# This should make most of the reports "not computable"–don't worry, that's expected. | |
# 4. Check/fix your GD Dataset Writers and reload data. | |
# 5. Run the script. | |
# replace the strings with your date dimension names: | |
# if your dataset is called "Date (Activity)", put only "Activity" here | |
old_date_dim_name = "Activity" # <<<--- REPLACE | |
new_date_dim_name = "Fiscal" # <<<--- REPLACE | |
# delete old report definitions (only the last version of each report is kept) | |
reports = project.reports; | |
reports.each do |report| | |
report.purge_report_of_unused_definitions! | |
end | |
# get all attributes of the old date dim | |
old_date_dim = Attribute.find_by_title(/(#{old_date_dim_name})/); | |
old_date_dim.each do |old_date| | |
new_date = Attribute.find_first_by_title(old_date.title.sub "(#{old_date_dim_name})", "(#{new_date_dim_name})"); | |
old_date_label = old_date.primary_label; | |
new_date_label = new_date.primary_label; | |
# replace in all metrics | |
metrics = project.metrics; | |
metrics.pmap do |metric| | |
if metric.contain?(old_date) | |
metric.replace(old_date, new_date) | |
metric.save | |
end | |
end | |
# replace in all reports | |
reports = project.reports; | |
reports.pmap do |report| | |
if report.using?(old_date) | |
rd = report.latest_report_definition | |
rd.replace(old_date, new_date) | |
rd.replace(old_date_label, new_date_label) | |
rd.save | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment