Pulling data values from DHIS2 instance

Hello OpenFN Community,
I need to integrate two DHIS2 instances. This is, pull aggregate data from one instance and push it to the next.
Iโ€™m fiddling with the HTTP adaptor. When I query the API I get this:

dataSet": "bkBzJ3ETIBD",
  "period": "202111",
  "orgUnit": "hfal93WttYV",
  "dataValues": [
    {
      "dataElement": "XatzAFRX2pJ",
      "period": "202111",
      "orgUnit": "hfal93WttYV",
      "categoryOptionCombo": "L1C8I3eiLDX",
      "attributeOptionCombo": "qZPpgD4Ykqh",
      "value": "0",
      "storedBy": "usernameremoved",
      "created": "2021-12-03T16:38:17.963+0000",
      "lastUpdated": "2021-12-03T16:38:17.963+0000",
      "followup": false
    }

I built my get function like so:

get("http://DHIS2-URL/api/dataValueSets", {
     query: {
       dataSet: "bkBzJ3ETIBD", 
       orgUnit: "hfal93WttYV",
       period:  "202111",
       children: "true"
     },
     headers: {"content-type": "application/json"}
   },
   function(state) {
     console.log("-------------debugging-------------");
     console.table("data values are: " + state.data.dataValues.value);
     console.log("-------------debugging-------------");
     return state;
   }
 );

I thought by doing the above iโ€™d get an array of data values but i got this:

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ โ—ฒ โ—ฑ  @openfn/core#v1.4.5 (Node.js v14.18.2) โ”‚
โ”‚ โ—ณ โ—ฐ            @openfn/language-http@3.1.11 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
GET request succeeded with 200 โœ“
-------------debugging-------------
data values are: undefined
-------------debugging-------------
Finished.

and when i console.log(state) i get this:

data: {
    dataSet: 'bkBzJ3ETIBD',
    period: '202111',
    orgUnit: 'hfal93WttYV',
    dataValues: [
      [Object], [Object], [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object], [Object], [Object],
      [Object], [Object], [Object], [Object], [Object], [Object],
      [Object],    ... 159 more items
    ]

So my question is, how do I see the returned data values. My inbox is also empty.

Thanks

1 Like

The quick answer is that, looking at your logs, state.data.dataValues is an array. I think you can pass JSON arrays directly to that console.table() function. If you want to see all 159+ items in that array, you could write:

get(
  "http://DHIS2-URL/api/dataValueSets",
  {
    query: {
      dataSet: "bkBzJ3ETIBD", 
      orgUnit: "hfal93WttYV",
      period:  "202111",
      children: "true"
    },
    headers: {"content-type": "application/json"}
  },
  state => {
     console.log("-------------debugging-------------");
     console.table(state.data.dataValues);
     console.log("-------------debugging-------------");
     return state;
  }
);

Or even iterate through them and log something of interest like:

state.data.dataValues.forEach(dv => {
  console.log(dv.value) // or some attribute of the data value
});

But on the design more generally

Iโ€™d recommend using the dhis2 adaptor rather than the http adaptor, and if you want to get dataValues from one DHIS2 instance and then upsert them in another, youโ€™ll need to chain two jobs together.

job 1, using your โ€œfirst dhis2 systemโ€ credential:

getDataValues(...)

job 2, using your โ€œsecond dhis2 systemโ€ credential:

createDataValues(...)

where that second job is triggered by a โ€œflow triggerโ€ that instantiates a run when job 1 succeeds.

Alternatively, if you want to do the whole thing in a single job you could stick with language-http and create a โ€œraw JSONโ€ credential that holds authentication information for both systems.

By the way, @khatman , this sounds like a super cool use case. Iโ€™d be happy to personally help you build these jobsโ€ฆ maybe using the yet-to-be-publicly-released new version of the dhis2 adaptor that @elias and I have been working on. Shoot me a note on email if youโ€™d like our support here. In return, weโ€™d ask that you make the job publicly visible in the job library so that others could benefit!

1 Like

Thanks for reaching out @taylordowns2000
I donโ€™t have any preference on the adaptors, I can use whichever adaptor that gets the job done.
Sounds like the language-dhis2 adaptor is neater.
Let me go ahead and make the job public and we can arrange how to move forward with it.
Thanks again

Hey @khatman , I think youโ€™ll need to โ€œenable OpenFn support accessโ€ and send me a link to the job itself. Maybe best over emailโ€”Iโ€™m taylor@openfn.org

Iโ€™ll recommend we use the pre-release of the new dhis2 adaptor: Release v3.0.0-0 ยท OpenFn/language-dhis2 ยท GitHub

Will look out for your follow-up on email to make the job public, then we can keep the conversation going out here so that everyone can benefit!