-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
126 lines (110 loc) · 4.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let jsonData;
document.addEventListener('DOMContentLoaded', function() {
// Fetch DHIS2 datasets and populate the drop-down menu
fetchDHIS2Data('dataset-select', 'dataSets.json');
fetchDHIS2Data('legend-select', 'legendSets.json');
fetchDHIS2Data('data-element-select', 'dataElements.json');
fetchDHIS2Data('indicator-select', 'indicators.json');
fetchDHIS2Data('org-unit-select', 'organisationUnits.json');
fetchJsonData();
});
function fetchJsonData() {
// Construct the DHIS2 API endpoint to fetch the JSON data
const apiUrl = 'dataValueSets.json'; // This endpoint fetches the data values directly
// Fetch JSON data from the DHIS2 API
const dhis2ApiUrl = `https://play.dhis2.org/40.3.0/api/29/${apiUrl}`;
fetch(dhis2ApiUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
// Assign the fetched JSON data to the global variable
jsonData = data;
})
.catch(error => {
console.error('Error fetching JSON data:', error);
});
}
function fetchDHIS2Data(selectId, apiUrl) {
// Use DHIS2 API to fetch data
const dhis2ApiUrl = `https://play.dhis2.org/40.3.0/api/29/${apiUrl}`;
fetch(dhis2ApiUrl)
.then(response => response.json())
.then(data => {
const select = document.getElementById(selectId);
// Clear existing options
select.innerHTML = '';
// Populate the drop-down menu with data options
data[apiUrl.split('.')[0]].forEach(item => {
const option = document.createElement('option');
option.value = item.id;
option.text = item.displayName;
select.add(option);
});
})
.catch(error => console.error(`Error fetching ${apiUrl}: `, error));
}
function fetchData() {
// Fetch data based on selected options
const selectedDatasetIds = getSelectedValues('dataset-select');
const selectedLegendIds = getSelectedValues('legend-select');
const selectedDataElementIds = getSelectedValues('data-element-select');
const selectedIndicatorIds = getSelectedValues('indicator-select');
const selectedOrgUnitIds = getSelectedValues('org-unit-select');
const fromDate = document.getElementById('from-date').value;
const toDate = document.getElementById('to-date').value;
// Construct the API endpoint with the selected parameters
const apiEndpoint = `https://play.dhis2.org/40.3.0/api/29/dataValueSets.json?dataSet=${selectedDatasetIds.join(',')}&legendSet=${selectedLegendIds.join(',')}&dataElement=${selectedDataElementIds.join(',')}&indicator=${selectedIndicatorIds.join(',')}&orgUnit=${selectedOrgUnitIds.join(',')}&startDate=${fromDate}&endDate=${toDate}`;
// Fetch data from DHIS2
fetch(apiEndpoint)
.then(response => {
if (!response.ok) {
return response.text().then(text => { throw new Error(`${response.status} - ${text}`) });
}
return response.json();
})
.then(data => {
// Display the fetched data
const responseOutput = document.getElementById('response-output');
responseOutput.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
console.error('Error fetching data from DHIS2: ', error);
const responseOutput = document.getElementById('response-output');
responseOutput.textContent = `Failed to load response: ${error.message}`;
});
}
function getSelectedValues(selectId) {
const select = document.getElementById(selectId);
const selectedOptions = Array.from(select.selectedOptions);
return selectedOptions.map(option => option.value);
}
document.getElementById('runCode').addEventListener('click', function() {
var code = document.getElementById('codeInput').value.trim();
var resultContainer = document.getElementById('response-output');
resultContainer.textContent = "Running...";
// Adjust the OpenCPU API endpoint for running R code
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://cloud.opencpu.org/ocpu/library/stats/R', true); // Adjust the endpoint
xhr.setRequestHeader('Content-Type', 'application/json');
// Pass the R code as a parameter
var params = {
code: code
};
xhr.onreadystatechange = function() {
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
resultContainer.textContent = xhr.responseText;
} else {
// Oh no! There has been an error with the request!
resultContainer.textContent = "Error: " + xhr.statusText;
}
}
};
xhr.send(JSON.stringify(params));
});