Skip to content

Commit

Permalink
now main.py is able to build assets to the report level
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrohmvieira committed Sep 28, 2018
1 parent 265c033 commit 8b5abd9
Showing 1 changed file with 83 additions and 51 deletions.
134 changes: 83 additions & 51 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# Step 2: Register bundle
result = igc.register_bundle(repo)

result.content

# Step 3: Download Power BI db2_reports
#pbi.download_all_reports(repo)

Expand All @@ -45,73 +47,103 @@

# Step 5: Generate a pandas dataframe for hosts, folders, reports and schemas



#host
host = [{"name":os.getenv("SERVER"),
"short_description":os.getenv("DATABASE"),
"long_description":"",
"phase":"DEV"
"$phase":"DEV",
"internal_id":igc.internal_id
}]
hosts = pd.DataFrame(host)
hosts.drop('internal_id',inplace=True,axis=1)## TODO: fix this shit later

#folders
folders = pbi.get_folder_list()

#create and populate a internal_id field, which is used to perform containment relationships
for idx,row in folders.iterrows():
folders.at[idx,"internal_id"] = igc.internal_id


#getting internal_id from parent assets
#create a dataframe with folders that are parent
folders2 = folders[folders.itemid.isin(folders.parentid)]

#join child and father dataframes, to have both internal ids on a row
folders = folders.set_index('parentid').join(folders2.set_index('itemid'),rsuffix='_parent')
folders.parentid.fillna('',inplace=True)

#reports
reports = pbi.get_report_list()

from lxml import etree
#create and populate a internal_id field, which is used to perform containment relationships
for idx,row in reports.iterrows():
reports.at[idx,"internal_id"] = igc.internal_id

#join child and father dataframes, to have both internal ids on a row
reports = reports.set_index('parentid').join(folders.set_index('itemid'),rsuffix='_folder')
folders.parentid.fillna('',inplace=True)

from lxml import etree

#create doc
doc = etree.Element("doc",{"xmlns":"http://www.ibm.com/iis/flow-doc"})
#create assets
assets = etree.SubElement(doc,"assets")
#create asset
asset = etree.SubElement(assets,"asset",{"class":"$PowerBI-PbiServer","repr":"Servidor Produção","ID":igc.internal_id})

for row in hosts.iterrows():
for column in row[1].keys():
asset.append(etree.Element("attribute",{"name":column,"value":row[1][column]}))

request = igc.insert_all_assets(etree.tostring(doc))

request.headers
#
#
#
#
#
# # serie = row1[1]
# #
# # for column in serie.keys():
# # print(column,serie[column])
# #
# #
# # <asset class="$PowerBI-PbiServer" repr="Servidor Produção" ID="a1">
# # <attribute name="name" value="Servidor Produção"/>
# # <attribute name="short_description" value="Servidor Produção 2"/>
# # <attribute name="$phase" value="PROD"/>
# # </asset>
#
#
#
#
# #function applying methods:
# reports['db2'] = np.vectorize(pqp.has_db2_sources)(reports['itemid'])
# #reports['db2'] = reports['db2'].map(pqp.has_db2_sources)
#
# db2_reports = reports[reports.db2].reset_index()
#
# db2_reports['itemid'][0]
#
# meta = pqp.get_metadata(db2_reports['itemid'][0])
#
# meta
#
# query = meta[0].get('query')
#
# query_meta = pqp.get_query_from_tables.findall(query)
#
# query_meta
#create host
asset = etree.SubElement(assets,"asset",{"class":"$PowerBI-PbiServer","repr":os.getenv("SERVER"),"ID":host[0]['internal_id']})
#create host attributes
for idx,series in hosts.iterrows():
for column in series.keys():
asset.append(etree.Element("attribute",{"name":column,"value":series[column]}))

#create folder assets
for idx,row in folders.iterrows():
asset = etree.SubElement(assets,"asset",{"class":"$PowerBI-PbiFolder","repr":row['name'],"ID":row.internal_id})
#create folder attributes
asset.append(etree.Element("attribute",{"name":"name","value":row['name']}))
#create containment reference
if len(row.parentid) == 0:
asset.append(etree.Element("reference",{"name":"$PbiServer","assetIDs":host[0]['internal_id']}))
else:
asset.append(etree.Element("reference",{"name":"$PbiFolder","assetIDs":row.internal_id_parent}))

#create report assets
for idx,row in reports.iterrows():
asset = etree.SubElement(assets,"asset",{"class":"$PowerBI-PbiReport","repr":row['name'],"ID":row.internal_id})
#create folder attributes
asset.append(etree.Element("attribute",{"name":"name","value":row['name']}))
#create containment reference
asset.append(etree.Element("reference",{"name":"$PbiFolder","assetIDs":row.internal_id_folder}))


# <headerSection>
# <attribute localId="version" type="String" >
# <label key="attr.PbiReport.version" inDefaultLocale="Version" />
# </attribute>
# </headerSection>
#
# <section>
# <label key="section.PbiReport.Details" inDefaultLocale="Report Details" />
#
# <attribute localId="author" type="String">
# <label key="attr.PbiReport.author" inDefaultLocale="Author" />
# </attribute>
#
# </section>


#create importAction

importAction = etree.SubElement(doc,"importAction",{"partialAssetIDs":"a1"})



with open('output/generated.xml','wb') as f:
f.write(etree.tostring(doc,pretty_print=True))

xml = etree.tostring(doc, pretty_print=True).decode('UTF-8')

request = igc.insert_all_assets(xml)

request.content

0 comments on commit 8b5abd9

Please sign in to comment.