SAP Logo LeanIX is now part of SAP
Question

Graphql / Python: Way to flatten lifecycle data to avoid additional loops?

  • 14 February 2024
  • 2 replies
  • 98 views

Userlevel 4
Badge

Hello,
to tag factsheets with underlying factsheets at end of life I use the following request in python:
 

        graphql_queryApp = """

{

  allFactSheets(

    first: 100,

    after:"%s",

    filter: {facetFilters: [

                {

                  facetKey: "FactSheetTypes",

                  operator: OR,

                  keys: ["%s"

                  ]

                },

                {

                  facetKey: "lxState",

                  operator: OR,

                  keys: [

                    "BROKEN_QUALITY_SEAL",

                    "APPROVED"

                  ]

                },

                {

                  facetKey: "lifecycle",

                  operator: OR,

                  keys: [

                        "phaseIn",

                        "active",

                        "phaseOut"

                    ],

                    dateFilter: {

                        type: TODAY,

                        from: "%s",

                        to: "%s"

                    }

                },

                {

                  facetKey:"_TAGS_",

                  operator: %s,

                  keys:["f2c13cdf-61b9-40e4-ad13-e0aafb126581"]

                },                

                {

                  facetKey: "%s",

                  operator: OR,

                    keys: [],

                    relationFieldsFilterOperator: INCLUSIVE,

                    relationFieldsFilter: [],

                    subFilter: {

                        facetFilters: [

                            {

                                facetKey: "FactSheetTypes",

                                operator: OR,

                                keys: [

                                    "%s"

                                ],

                            },

                            {

                                facetKey: "lifecycle",

                                operator: OR,

                                keys: [

                                    "endOfLife"

                                ],

                                dateFilter: {

                                    from: "%s",

                                    to: "%s",

                                    type: RANGE

                                }

                            }

                        ]

                    }

                }

        ]

    }

  ) {

    totalCount

    pageInfo {

      hasNextPage

      hasPreviousPage

      startCursor

      endCursor

    }

    edges {

      node {

        ... on %s {

          id

          displayName

          createdAt

          displayName

          type

          lxState

          subscriptions {

            edges {

              node {

                id

                user {

                  id

                  displayName

                  email

                }

                type

                roles {

                  id

                  name

                  subscriptionType

                  comment

                }

                createdAt

              }

            }

          }

          lifecycle {

            asString

            phases {

              phase

              startDate

              milestoneId

            }

          }

          %s {

            edges {

              node {

                id

                factSheet {

                  ... on %s {

                  displayName

                  id

                  type

                  lifecycle {

                    asString

                      phases {

                        phase

                        startDate

                        milestoneId

                      }

                    }

                    relToSuccessor {

                      edges {

                        node {

                          factSheet {

                            ... on %s {

                              id

                              name

                              lifecycle {

                                phases {

                                  milestoneId

                                  phase

                                  startDate

                                }

                                asString

                              }

                            }

                          }

                        }

                      }

                    }            

                  }

                }

              }

            }

          }

          id

          rev

        }

      }

    }

  }

}    """ % (reqcursor,sourceFs,sdate,sdate,op,relationName,relatedFs,sdate,edate,sourceFs,relationName,relatedFs,relatedFs)

It is flexible e.g. you can use it with relApplicationToITComponent, …, relInterfaceToProviderApplication, relInterfaceToConsumerApplication etc.

 

Is there a simple way to flatten the lifecycle data output? 

From:

                                            "lifecycle": {                                                 "asString": "endOfLife",                                                 "phases": [                                                     {                                                         "phase": "active",                                                         "startDate": "2018-09-01",                                                         "milestoneId": null                                                     },                                                     {                                                         "phase": "phaseOut",                                                         "startDate": "2021-07-01",                                                         "milestoneId": null                                                     },                                                     {                                                         "phase": "endOfLife",                                                         "startDate": "2021-08-01",                                                         "milestoneId": null                                                     }                                                 ]                                             }

In a way that you can simply get the start date of endOfLife without iterating through the phases?
 

Perhaps something like

"lifecycle": {

                           "asString": "endOfLife",

                           "active":{"startDate": "2018-09-01","milestoneId": null},

                           "phaseOut":{"startDate": "2021-07-01", "milestoneId": null},

                           "endOfLife":{startDate": "2021-08-01","milestoneId": null}

             }

Thank you in advance and best regards,

Carsten


2 replies

Userlevel 3
Badge

Hello @Carsten, apologies for the delayed reply, your post somehow slipped my attention. In terms of your query, it's not possible to achieve this directly through GraphQL. The most feasible approach to flatten the structure according to your needs would be via the script you utilize to parse the results.
Since you use Python, the following code block should work:
 

def transform_phases(phases):
transformed_phases = list()
for phase in phases:
transformed_phase = {phase.get("phase"): {"startDate": phase.get("startDate"), "milestoneId": phase.get("milestoneId")}}
transformed_phases.append(transformed_phase)
return transformed_phases


 

 

Userlevel 4
Badge

Hi @Kostas-LeanIX ,

I did something similar, thank you!

Reply