├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── MAINTAINERS.md ├── README.md ├── data-service ├── API.md ├── Dockerfile ├── README.md ├── app.js ├── bin │ └── www ├── package.json ├── routes │ ├── allergies.js │ ├── cities.js │ ├── generate.js │ ├── population.js │ └── update.js ├── service │ ├── datalake.js │ ├── datasource.js │ └── synthea.js └── swagger.yaml ├── deploy-dataservice.yml ├── deploy-mongodb.yml ├── deploy-webapp.yml ├── docker-compose.yml ├── generate ├── README.md ├── generate.sh └── package.json ├── ingress-dataservice.yml ├── ingress-webapp.yml ├── manifest.yml ├── readme_images ├── bar_chart.png ├── cf_mongo_name.png ├── cf_mongo_url.png ├── cf_node_connect_mongo.png ├── cf_node_host_domain.png ├── cf_node_name.png ├── cloudfoundry_architecture.png ├── cto_architecture.png ├── kubernetes_architecture.png ├── map.png ├── screenshot.png └── treemap.png ├── travis-tests └── docker-compose.sh └── web ├── Dockerfile ├── README.md ├── app.js ├── bin └── www ├── package.json ├── public ├── images │ ├── examplelogo@2x.png │ └── logo.svg ├── index.html ├── javascripts │ ├── Chart.js │ ├── Chart.min.js │ ├── chartHandler.js │ ├── dataHandler.js │ ├── mapHandler.js │ └── navbarHandler.js └── stylesheets │ └── style.css ├── routes └── index.js └── service └── analytics.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | *.DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | cache: pip 18 | language: java 19 | services: 20 | - docker 21 | before_install: 22 | - sudo apt-get install shellcheck 23 | - sudo pip install yamllint 24 | - git clone https://github.com/IBM/pattern-ci 25 | - "./pattern-ci/scripts/install-nodejs.sh" 26 | - sudo npm i -g csvtojson 27 | before_script: 28 | - "./pattern-ci/tests/shellcheck-lint.sh" 29 | jobs: 30 | include: 31 | - script: ./travis-tests/docker-compose.sh 32 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | This is an open source project, and we appreciate your help! 4 | 5 | We use the GitHub issue tracker to discuss new features and non-trivial bugs. 6 | 7 | In addition to the issue tracker, [#journeys on 8 | Slack](https://dwopen.slack.com) is the best way to get into contact with the 9 | project's maintainers. 10 | 11 | To contribute code, documentation, or tests, please submit a pull request to 12 | the GitHub repository. Generally, we expect two maintainers to review your pull 13 | request before it is approved for merging. For more details, see the 14 | [MAINTAINERS](MAINTAINERS.md) page. 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 IBM Corp. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | # Maintainers Guide 2 | 3 | This guide is intended for maintainers - anybody with commit access to one or 4 | more Code Pattern repositories. 5 | 6 | ## Methodology 7 | 8 | This repository does not have a traditional release management cycle, but 9 | should instead be maintained as a useful, working, and polished reference at 10 | all times. While all work can therefore be focused on the master branch, the 11 | quality of this branch should never be compromised. 12 | 13 | The remainder of this document details how to merge pull requests to the 14 | repositories. 15 | 16 | ## Merge approval 17 | 18 | The project maintainers use LGTM (Looks Good To Me) in comments on the pull 19 | request to indicate acceptance prior to merging. A change requires LGTMs from 20 | two project maintainers. If the code is written by a maintainer, the change 21 | only requires one additional LGTM. 22 | 23 | ## Reviewing Pull Requests 24 | 25 | We recommend reviewing pull requests directly within GitHub. This allows a 26 | public commentary on changes, providing transparency for all users. When 27 | providing feedback be civil, courteous, and kind. Disagreement is fine, so long 28 | as the discourse is carried out politely. If we see a record of uncivil or 29 | abusive comments, we will revoke your commit privileges and invite you to leave 30 | the project. 31 | 32 | During your review, consider the following points: 33 | 34 | ### Does the change have positive impact? 35 | 36 | Some proposed changes may not represent a positive impact to the project. Ask 37 | whether or not the change will make understanding the code easier, or if it 38 | could simply be a personal preference on the part of the author (see 39 | [bikeshedding](https://en.wiktionary.org/wiki/bikeshedding)). 40 | 41 | Pull requests that do not have a clear positive impact should be closed without 42 | merging. 43 | 44 | ### Do the changes make sense? 45 | 46 | If you do not understand what the changes are or what they accomplish, ask the 47 | author for clarification. Ask the author to add comments and/or clarify test 48 | case names to make the intentions clear. 49 | 50 | At times, such clarification will reveal that the author may not be using the 51 | code correctly, or is unaware of features that accommodate their needs. If you 52 | feel this is the case, work up a code sample that would address the pull 53 | request for them, and feel free to close the pull request once they confirm. 54 | 55 | ### Does the change introduce a new feature? 56 | 57 | For any given pull request, ask yourself "is this a new feature?" If so, does 58 | the pull request (or associated issue) contain narrative indicating the need 59 | for the feature? If not, ask them to provide that information. 60 | 61 | Are new unit tests in place that test all new behaviors introduced? If not, do 62 | not merge the feature until they are! Is documentation in place for the new 63 | feature? (See the documentation guidelines). If not do not merge the feature 64 | until it is! Is the feature necessary for general use cases? Try and keep the 65 | scope of any given component narrow. If a proposed feature does not fit that 66 | scope, recommend to the user that they maintain the feature on their own, and 67 | close the request. You may also recommend that they see if the feature gains 68 | traction among other users, and suggest they re-submit when they can show such 69 | support. 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.com/IBM/example-health-analytics.svg?branch=master)](https://travis-ci.com/IBM/example-health-analytics) 2 | 3 | # Example Health Analytics 4 | 5 | This project is a conceptual Node.js analytics web application for a health records system, designed to showcase best in class integration of modern cloud technology, in collaboration with [legacy mainframe code](https://github.com/IBM/example-health-apis). 6 | > NOTE: This project is also compatible with the [Example Health JEE Application on Openshift](https://github.com/IBM/example-health-jee-openshift). See notes below for details. 7 | 8 | ## Example Health Context 9 | 10 | Example Health is a conceptual healthcare/insurance type company. It has been around a long time, and has 100s of thousands of patient records in a SQL database on a mainframe running z/OS. Their health records look very similar to the health records of most insurance companies. 11 | 12 | Here's a view a data analyst might see when they interact with the Example Health Analytics Application: 13 | 14 | ![](readme_images/screenshot.png) 15 | 16 | Example Health has recently started understanding how data science/analytics on some of the patient records might surface interesting insights. There is lots of talk about this among some of the big data companies. 17 | 18 | Example Health has also heard a great deal about cloud computing. There is a lot of legacy code in the mainframe, and it works well for now, but they think it may be a complimentary opportunity to explore some data science/analytics in the cloud. 19 | 20 | Their CTO sees an architecture for Example Health like this: 21 | 22 | ![](readme_images/cto_architecture.png) 23 | 24 | # Architecture 25 | 26 | ## Using Kubernetes 27 | 28 | ![](readme_images/kubernetes_architecture.png) 29 | 30 | ## Using Cloud Foundry 31 | 32 | ![](readme_images/cloudfoundry_architecture.png) 33 | 34 | 1. Data Service API acts as a data pipeline and is triggered for updating data lake with updated health records data by calling API Connect APIs associated with the z/OS Mainframe. 35 | 2. API Connect APIs process relevant health records data from z/OS Mainframe data warehouse and send the data through the data pipeline. 36 | 3. The Data Service data pipeline processes z/OS Mainframe data warehouse data and updates MongoDB data lake. 37 | 4. User interacts with the UI to view and analyze analytics. 38 | 5. The functionality of the App UI that the User interacts with is handled by Node.js. Node.js is where the API calls are initialized. 39 | 6. The API calls are processed in the Node.js data service and are handled accordingly. 40 | 7. The data is gathered from the MongoDB data lake from API calls. 41 | 8. The responses from the API calls are handled accordingly by the App UI. 42 | 43 | # Steps 44 | 45 | Follow these steps to setup and run this code pattern locally and on the Cloud. The steps are described in detail below. 46 | 47 | 1. [Clone the repo](#1-clone-the-repo) 48 | 2. [Prerequisites](#2-prerequisites) 49 | 3. [Get Mapbox Access Token](#3-get-mapbox-access-token) 50 | 4. [Run the application](#4-run-the-application) 51 | 5. [Deploy to IBM Cloud](#5-deploy-to-ibm-cloud) 52 | 53 | ## 1. Clone the repo 54 | 55 | Clone the `example-health-analytics` repo locally. In a terminal, run: 56 | 57 | ```bash 58 | git clone https://github.com/IBM/example-health-analytics 59 | cd example-health-analytics 60 | ``` 61 | 62 | ## 2. Prerequisites 63 | 64 | * [Docker](https://www.docker.com/products/docker-desktop) 65 | * [IBM Cloud account](https://cloud.ibm.com/registration) 66 | * [IBM Cloud CLI](https://cloud.ibm.com/docs/cli?topic=cloud-cli-ibmcloud-cli&locale=en-US#overview) 67 | 68 | For running these services locally without Docker containers, the following will be needed: 69 | 70 | * [MongoDB](https://www.mongodb.com/download-center/v2/community) 71 | * [NodeJS](https://nodejs.org/en/download/) 72 | * [NPM](https://www.npmjs.com/get-npm) 73 | * Relevant Node Components: Use `npm install` in `/data-service` and `/web` 74 | > NOTE: Run the command `csvtojson` in `/generate`, if there is an error `csvtojson command not found`, run `sudo npm install -g csvtojson@latest`. If the error still persists, uninstall Node and reinstall [Node.js LTS](https://nodejs.org/en/), run the command `sudo npm install -g npm` and run `sudo npm install -g csvtojson@latest` & `csvtojson`. 75 | 76 | ## 3. Get Mapbox Access Token 77 | 78 | 1. In order to make API calls to help in populating the Mapbox map used, a [Mapbox access token](https://www.mapbox.com/account/access-tokens) will be needed. 79 | 2. Assign the access token to `MAPBOX_ACCESS_TOKEN` in [docker-compose.yml](docker-compose.yml). 80 | 81 | ## 4. Run the application 82 | 83 | * [z/OS Mainframe Data](#z/os-mainframe-data) 84 | * [Generate Data](#generate-data) 85 | 86 | ### z/OS Mainframe Data 87 | > NOTE: If using the [Example Health JEE Application on Openshift](https://github.com/IBM/example-health-jee-openshift) as your data source, follow these steps. 88 | 89 | If your data source for this application is on a z/OS Mainframe, follow these steps for populating the datalake and running the application: 90 | 91 | 1. Assign the API Connect URL to `DATA_SOURCE_API` in [docker-compose.yml](docker-compose.yml) 92 | > NOTE: If using the [Example Health JEE Application on Openshift](https://github.com/IBM/example-health-jee-openshift) as your data source, assign that API URL to `DATA_SOURCE_API` 93 | 2. Start the application by running `docker-compose up --build` in this repo's root directory. 94 | 3. Once the containers are created and the application is running, use the Open API Doc (Swagger) at `http://localhost:3000` and [API.md](data-service/API.md) for instructions on how to use the APIs. 95 | 4. Run `curl localhost:3000/api/v1/update -X PUT` to connect to the z/OS Mainframe and populate the data lake. For information on the data lake and data service, read the data service [README.md](data-service/README.md). 96 | 5. Once the data has been populated in the data lake, use `http://localhost:4000` to access the Example Health Analytics UI. For information on the analytics data and UI, read the web [README.md](web/README.md). 97 | 98 | ### Generate Data 99 | 100 | If you do not have a data source for this application and would like to generate mock data, follow these steps for populating the datalake and running the application: 101 | 102 | 1. Start the application by running `docker-compose up --build` in this repo's root directory. 103 | 2. Once the containers are created and the application is running, use the Open API Doc (Swagger) at `http://localhost:3000` and [API.md](data-service/API.md) for instructions on how to use the APIs. 104 | 3. Use the provided `generate/generate.sh` script to generate and populate data. Read [README.md](generate/README.md) for instructions on how to use the script. For information on the data lake and data service, read the data service [README.md](data-service/README.md). 105 | 4. Once the data has been populated in the data lake, use `http://localhost:4000` to access the Example Health Analytics UI. For information on the analytics data and UI, read the web [README.md](web/README.md). 106 | 107 | ## 5. Deploy to IBM Cloud 108 | 109 | * [Kubernetes](#kubernetes) 110 | * [Cloud Foundry](#cloud-foundry) 111 | 112 | ### Kubernetes 113 | 114 | 1. To allow changes to the Data Service or the UI, create a repo on [Docker Cloud](https://cloud.docker.com/) where the new modified containers will be pushed to. 115 | > NOTE: If a new repo is used for the Docker containers, the container `image` will need to be modified to the name of the new repo used in [deploy-dataservice.yml](deploy-dataservice.yml) and/or [deploy-webapp.yml](deploy-webapp.yml). 116 | 117 | ```bash 118 | export DOCKERHUB_USERNAME= 119 | 120 | docker build -t $DOCKERHUB_USERNAME/examplehealthanalyticsdata:latest data-service/ 121 | docker build -t $DOCKERHUB_USERNAME/examplehealthanalyticsweb:latest web/ 122 | 123 | docker login 124 | 125 | docker push $DOCKERHUB_USERNAME/examplehealthanalyticsdata:latest 126 | docker push $DOCKERHUB_USERNAME/examplehealthanalyticsweb:latest 127 | ``` 128 | 129 | 2. Provision the [IBM Cloud Kubernetes Service](https://cloud.ibm.com/kubernetes/catalog/cluster) and follow the set of instructions for creating a Container and Cluster based on your cluster type, `Standard` vs `Lite`. 130 | 131 | * Login to the IBM Cloud using the [Developer Tools CLI](https://www.ibm.com/cloud/cli): 132 | > NOTE use `--sso` if you have a single sign on account, or delete for username/password login 133 | 134 | ```bash 135 | ibmcloud login --sso 136 | ``` 137 | 138 | * Set the Kubernetes environment to work with your cluster: 139 | 140 | ```bash 141 | ibmcloud cs cluster-config $CLUSTER_NAME 142 | ``` 143 | 144 | The output of this command will contain a KUBECONFIG environment variable that must be exported in order to set the context. Copy and paste the output in the terminal window. An example is: 145 | 146 | ```bash 147 | export KUBECONFIG=/home/rak/.bluemix/plugins/container-service/clusters/Kate/kube-config-prod-dal10-.yml 148 | ``` 149 | 150 | #### Lite Cluster Instructions 151 | 152 | 3. Get the workers for your Kubernetes cluster: 153 | 154 | ```bash 155 | ibmcloud cs workers 156 | ``` 157 | and locate the `Public IP`. This IP is used to access the Data Service and UI on the Cloud. Update the `env` values for `HOST_IP` in [deploy-dataservice.yml](deploy-dataservice.yml) to `:32000` and `DATA_SERVER` in [deploy-webapp.yml](deploy-webapp.yml) to `http://:32000`. Also in [deploy-dataservice.yml](deploy-dataservice.yml), update the `env` value for `SCHEME` to `http`. 158 | 159 | 4. Assign the Mapbox access token to `MAPBOX_ACCESS_TOKEN` in [deploy-dataservice.yml](deploy-dataservice.yml) and [deploy-webapp.yml](deploy-webapp.yml). If your data source for this application is on a z/OS Mainframe, assign the API Connect URL to `DATA_SOURCE_API` in [deploy-dataservice.yml](deploy-dataservice.yml). 160 | > NOTE: If using the [Example Health JEE Application on Openshift](https://github.com/IBM/example-health-jee-openshift) as your data source, assign that API URL to `DATA_SOURCE_API` 161 | 162 | 5. To deploy the services to the IBM Cloud Kubernetes Service, run: 163 | 164 | ```bash 165 | kubectl apply -f deploy-mongodb.yml 166 | kubectl apply -f deploy-dataservice.yml 167 | kubectl apply -f deploy-webapp.yml 168 | 169 | ## Confirm the services are running - this may take a minute 170 | kubectl get pods 171 | ``` 172 | 173 | 6. Use `http://PUBLIC_IP:32001` to access the UI and the Open API Doc (Swagger) at `http://PUBLIC_IP:32000` for instructions on how to make API calls. 174 | 175 | #### Standard Cluster Instructions 176 | 177 | 3. Run `ibmcloud cs cluster-get ` and locate the `Ingress Subdomain` and `Ingress Secret`. This is the domain of the URL that is to be used to access the Data Service and UI on the Cloud. Update the `env` values for `HOST_IP` in [deploy-dataservice.yml](deploy-dataservice.yml) to `api.` and `DATA_SERVER` in [deploy-webapp.yml](deploy-webapp.yml) to `https://api.`. Also in [deploy-dataservice.yml](deploy-dataservice.yml), update the `env` value for `SCHEME` to `https`. In addition, update the `host` and `secretName` in [ingress-dataservice.yml](ingress-dataservice.yml) and [ingress-webapp.yml](ingress-webapp.yml) to `Ingress Subdomain` and `Ingress Secret`. 178 | 179 | 4. Assign the Mapbox access token to `MAPBOX_ACCESS_TOKEN` in [deploy-dataservice.yml](deploy-dataservice.yml) and [deploy-webapp.yml](deploy-webapp.yml). If your data source for this application is on a z/OS Mainframe, assign the API Connect URL to `DATA_SOURCE_API` in [deploy-dataservice.yml](deploy-dataservice.yml). 180 | > NOTE: If using the [Example Health JEE Application on Openshift](https://github.com/IBM/example-health-jee-openshift) as your data source, assign that API URL to `DATA_SOURCE_API` 181 | 182 | 5. To deploy the services to the IBM Cloud Kubernetes Service, run: 183 | 184 | ```bash 185 | kubectl apply -f deploy-mongodb.yml 186 | kubectl apply -f deploy-dataservice.yml 187 | kubectl apply -f deploy-webapp.yml 188 | 189 | ## Confirm the services are running - this may take a minute 190 | kubectl get pods 191 | 192 | ## Update protocol being used to https 193 | kubectl apply -f ingress-dataservice.yml 194 | kubectl apply -f ingress-webapp.yml 195 | ``` 196 | 197 | 6. Use `https://` to access the UI and the Open API Doc (Swagger) at `https://api.` for instructions on how to make API calls. 198 | 199 | ### Cloud Foundry 200 | 201 | 1. Provision two [SDK for Node.js](https://cloud.ibm.com/catalog/starters/sdk-for-nodejs) applications. One will be for `./data-service` and the other will be for `./web`. 202 | 203 | 2. Provision a [Compose for MongoDB](https://cloud.ibm.com/catalog/services/compose-for-mongodb) database. 204 | 205 | 3. Update the following in the [manifest.yml](manifest.yml) file: 206 | 207 | 208 | * `name` for both Cloud Foundry application names provisioned from Step 1. 209 | 210 | ![](readme_images/cf_node_name.png) 211 | 212 | 213 | * `services` with the name of the MongoDB service provisioned from Step 2. 214 | 215 | ![](readme_images/cf_mongo_name.png) 216 | 217 | 218 | * `HOST_IP` and `DATA_SERVER` with the host name and domain of the `data-service` from Step 1. 219 | 220 | ![](readme_images/cf_node_host_domain.png) 221 | 222 | 223 | * `MONGODB` with the HTTPS Connection String of the MongoDB provisioned from Step 2. This can be found under *Manage > Overview* of the database dashboard. 224 | 225 | ![](readme_images/cf_mongo_url.png) 226 | 227 | * `MAPBOX_ACCESS_TOKEN` with the Mapbox access token. 228 | 229 | * `DATA_SOURCE_API` with the API Connect URL if your data source for this application is on a z/OS Mainframe. 230 | > NOTE: If using the [Example Health JEE Application on Openshift](https://github.com/IBM/example-health-jee-openshift) as your data source, assign that API URL to `DATA_SOURCE_API` 231 | 232 | 4. Connect the Compose for MongoDB database with the data service Node.js app by going to *Connections* on the dashboard of the data service app provisioned and clicking *Create Connection*. Locate the Compose for MongoDB database you provisioned and press *connect*. 233 | 234 | ![](readme_images/cf_node_connect_mongo.png) 235 | 236 | 5. To deploy the services to IBM Cloud Foundry, go to one of the dashboards of the apps provisioned from Step 1 and follow the *Getting Started* instructions for connecting and logging in to IBM Cloud from the console (Step 3 of *Getting Started*). Once logged in, run `ibmcloud app push` from the root directory. 237 | 238 | 6. Use `https://.` to access the UI and the Open API Doc (Swagger) at `https://.` for instructions on how to make API calls. 239 | 240 | # License 241 | 242 | This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1](https://developercertificate.org/) and the [Apache License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.txt). 243 | 244 | [Apache License FAQ](https://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN) 245 | -------------------------------------------------------------------------------- /data-service/API.md: -------------------------------------------------------------------------------- 1 | ### Table of Contents 2 | 3 | * [Update](#update) 4 | * [Generate](#generate) 5 | * [Population](#population) 6 | * [Cities](#cities) 7 | * [Allergies](#allergies) 8 | 9 | 10 | # Update 11 | 12 | This works as a data pipeline by handling and updating patient and allergy data from the Patient Records Database from the data source to the MongoDB Data Lake 13 | 14 | __Command__ 15 | 16 | ``` 17 | curl http://localhost:3000/api/v1/update -X PUT 18 | ``` 19 | 20 | __Response__ 21 | 22 | Success: The response is a 200 status code. 23 | 24 | Data not transferred: When the data is not transferred and updated in the Data Lake (ex. can't access database), the response is a 500 status code. 25 | 26 | Data Source error: When there is an error calling the APIs associated with the data source, the response is a 502 status code. 27 | 28 | # Generate 29 | 30 | This is used as an alternative data pipeline to the data source by using locally generated Synthea data to populate the MongoDB Data Lake 31 | 32 | __Command__ 33 | 34 | ``` 35 | curl http://localhost:3000/api/v1/generate -H "Content-Type: application/json" -X PUT -d "@apidata.json" 36 | ``` 37 | 38 | __Response__ 39 | 40 | Success: The response is a 200 status code. 41 | 42 | Data not transferred: When the data is not transferred and updated in the Data Lake (ex. can't access database), the response is a 500 status code. 43 | 44 | # Population 45 | 46 | This returns the total population of the patients in a given state. 47 | 48 | ``` 49 | { 50 | "population": Int 51 | } 52 | ``` 53 | 54 | __Command__ 55 | 56 | ``` 57 | curl http://localhost:3000/api/v1/population -X GET 58 | ``` 59 | 60 | __Response__ 61 | 62 | Success: The response is a 200 status code. The response body is a JSON object containing the population. 63 | 64 | Internal Error: When there is an internal error (ex. can't access database), the response is a 500 status code. The response body is a JSON object containing an error message. 65 | 66 | # Cities 67 | 68 | This returns a list of cities where there are patients in a given state. 69 | 70 | ``` 71 | { 72 | "cities": [{ 73 | "city": String, 74 | "population": Int, 75 | "allergies": [{ 76 | "allergy": String, 77 | "type": String, 78 | "developed": [Int], //age 79 | "outgrown": [Int] //age 80 | }] 81 | } 82 | ``` 83 | 84 | __Command__ 85 | 86 | ``` 87 | curl http://localhost:3000/api/v1/cities -X GET 88 | ``` 89 | 90 | __Response__ 91 | 92 | Success: The response is a 200 status code. The response body is a JSON object containing the list of cities. 93 | 94 | Internal Error: When there is an internal error (ex. can't access database), the response is a 500 status code. The response body is a JSON object containing an error message. 95 | 96 | # Allergies 97 | 98 | This returns a list of allergies that are present in the Data Lake. 99 | 100 | ``` 101 | { 102 | "allergies": [String] 103 | } 104 | ``` 105 | 106 | __Command__ 107 | 108 | ``` 109 | curl http://localhost:3000/api/v1/allergies -X GET 110 | ``` 111 | 112 | __Response__ 113 | 114 | Success: The response is a 200 status code. The response body is a JSON object containing the list of allergies. 115 | 116 | Internal Error: When there is an internal error (ex. can't access database), the response is a 500 status code. The response body is a JSON object containing an error message. 117 | 118 | ### License 119 | Copyright 2019 IBM Corp. All Rights Reserved. 120 | 121 | Licensed under the Apache License, Version 2.0 (the "License"); 122 | you may not use this file except in compliance with the License. 123 | You may obtain a copy of the License at 124 | 125 | http://www.apache.org/licenses/LICENSE-2.0 126 | 127 | Unless required by applicable law or agreed to in writing, software 128 | distributed under the License is distributed on an "AS IS" BASIS, 129 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130 | See the License for the specific language governing permissions and 131 | limitations under the License. -------------------------------------------------------------------------------- /data-service/Dockerfile: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | FROM node:10-alpine 17 | MAINTAINER Max Shapiro "maxshapiro32@ibm.com" 18 | COPY . /data-service 19 | WORKDIR /data-service 20 | RUN npm install 21 | EXPOSE 3000 22 | CMD ["npm", "start"] 23 | -------------------------------------------------------------------------------- /data-service/README.md: -------------------------------------------------------------------------------- 1 | # Example Health Analytics Data Service 2 | 3 | * [About](#about) 4 | * [Datalake](#datalake) 5 | 6 | # About 7 | 8 | This part of Example Health Analytics is responsible for using APIs to: 9 | * Act as a data pipeline between the data warehouse (either from a data source or generated Synthea data) and the MongoDB data lake 10 | * Retrieve data from MongoDB data lake to be used for analytical analysis 11 | 12 | # Datalake 13 | 14 | The data lake is a MongoDB database that updates and overwrites itself from data passed through the data pipeline from the data warehouse. 15 | 16 | __Structure__ 17 | 18 | ``` 19 | { 20 | "_id": ObjectID, 21 | "population": Integer, 22 | "allergies": [String], 23 | "cities": [{ 24 | "city": String, 25 | "state": String, 26 | "population": Integer, 27 | "allergies": [{ 28 | "allergy": String, 29 | "type": String, 30 | "developed": [Integer], 31 | "outgrown": [Integer] 32 | }] 33 | }] 34 | } 35 | ``` 36 | 37 | * **_id**: ObjectID for the document in the database 38 | * **population**: Total population for all cities in the database 39 | * **allergies**: A list of all allergies present in the database 40 | * **cities**: A list of cities where each city includes: 41 | * **city**: The name of a city 42 | * **state**: The name of a state a city is in 43 | * **population**: Population for a city 44 | * **allergies**: A list of allergies for a city that includes: 45 | * **allergy**: The name of the allergy 46 | * **type**: The name of the type of allergy 47 | * **developed**: A list of ages of people in a city that have developed an allergy 48 | * **outgrown**: A list of ages of people in a city that have outgrown an allergy 49 | 50 | ### License 51 | Copyright 2019 IBM Corp. All Rights Reserved. 52 | 53 | Licensed under the Apache License, Version 2.0 (the "License"); 54 | you may not use this file except in compliance with the License. 55 | You may obtain a copy of the License at 56 | 57 | http://www.apache.org/licenses/LICENSE-2.0 58 | 59 | Unless required by applicable law or agreed to in writing, software 60 | distributed under the License is distributed on an "AS IS" BASIS, 61 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 62 | See the License for the specific language governing permissions and 63 | limitations under the License. -------------------------------------------------------------------------------- /data-service/app.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Main app.js file for data service of Example Health Analytics 18 | */ 19 | 20 | var createError = require('http-errors'); 21 | var express = require('express'); 22 | var bodyParser = require('body-parser'); 23 | var logger = require('morgan'); 24 | var swaggerUi = require('swagger-ui-express'); 25 | var YAML = require('yamljs'); 26 | var swaggerDocument = YAML.load('swagger.yaml'); 27 | swaggerDocument.host = process.env.HOST_IP || "localhost:3000"; 28 | var scheme = process.env.SCHEME || "http"; 29 | swaggerDocument.schemes = [scheme]; 30 | 31 | var updateRouter = require('./routes/update'); 32 | var populationRouter = require('./routes/population'); 33 | var citiesRouter = require('./routes/cities'); 34 | var allergiesRouter = require('./routes/allergies'); 35 | var generateRouter = require('./routes/generate'); 36 | 37 | var app = express(); 38 | var api = "/api/v1"; 39 | 40 | app.use(logger('dev')); 41 | app.use(bodyParser.json({limit: '25mb'})); 42 | app.use(bodyParser.urlencoded({limit: '25mb', extended: true})); 43 | 44 | app.use(api + '/update', updateRouter); 45 | app.use(api + '/population', populationRouter); 46 | app.use(api + '/cities', citiesRouter); 47 | app.use(api + '/allergies', allergiesRouter); 48 | app.use(api + '/generate', generateRouter); 49 | 50 | app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); 51 | 52 | // catch 404 and forward to error handler 53 | app.use(function(req, res, next) { 54 | next(createError(404)); 55 | }); 56 | 57 | // error handler 58 | app.use(function(err, req, res, next) { 59 | // set locals, only providing error in development 60 | res.locals.message = err.message; 61 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 62 | 63 | res.status(err.status || 500); 64 | }); 65 | 66 | module.exports = app; 67 | -------------------------------------------------------------------------------- /data-service/bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | /*############################################################################## 3 | # Copyright 2019 IBM Corp. All Rights Reserved. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | ##############################################################################*/ 17 | 18 | /** 19 | * Server Initialization 20 | */ 21 | 22 | /** 23 | * Module dependencies. 24 | */ 25 | 26 | var app = require('../app'); 27 | var debug = require('debug')('data-service:server'); 28 | var http = require('http'); 29 | 30 | /** 31 | * Get port from environment and store in Express. 32 | */ 33 | 34 | var port = normalizePort(process.env.PORT || '3000'); 35 | app.set('port', port); 36 | 37 | /** 38 | * Create HTTP server. 39 | */ 40 | 41 | var server = http.createServer(app); 42 | 43 | /** 44 | * Listen on provided port, on all network interfaces. 45 | */ 46 | 47 | server.listen(port); 48 | server.on('error', onError); 49 | server.on('listening', onListening); 50 | 51 | /** 52 | * Normalize a port into a number, string, or false. 53 | */ 54 | 55 | function normalizePort(val) { 56 | var port = parseInt(val, 10); 57 | 58 | if (isNaN(port)) { 59 | // named pipe 60 | return val; 61 | } 62 | 63 | if (port >= 0) { 64 | // port number 65 | return port; 66 | } 67 | 68 | return false; 69 | } 70 | 71 | /** 72 | * Event listener for HTTP server "error" event. 73 | */ 74 | 75 | function onError(error) { 76 | if (error.syscall !== 'listen') { 77 | throw error; 78 | } 79 | 80 | var bind = typeof port === 'string' 81 | ? 'Pipe ' + port 82 | : 'Port ' + port; 83 | 84 | // handle specific listen errors with friendly messages 85 | switch (error.code) { 86 | case 'EACCES': 87 | console.error(bind + ' requires elevated privileges'); 88 | process.exit(1); 89 | break; 90 | case 'EADDRINUSE': 91 | console.error(bind + ' is already in use'); 92 | process.exit(1); 93 | break; 94 | default: 95 | throw error; 96 | } 97 | } 98 | 99 | /** 100 | * Event listener for HTTP server "listening" event. 101 | */ 102 | 103 | function onListening() { 104 | var addr = server.address(); 105 | var bind = typeof addr === 'string' 106 | ? 'pipe ' + addr 107 | : 'port ' + addr.port; 108 | debug('Listening on ' + bind); 109 | } 110 | -------------------------------------------------------------------------------- /data-service/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "data-service", 3 | "version": "0.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "body-parser": "^1.18.3", 10 | "cookie-parser": "~1.4.3", 11 | "debug": "~2.6.9", 12 | "express": "~4.16.0", 13 | "http-errors": "~1.6.2", 14 | "jade": "~1.11.0", 15 | "mongodb": "3.1.13", 16 | "morgan": "~1.9.0", 17 | "node-fetch": "^2.3.0", 18 | "request": "^2.88.0", 19 | "request-promise": "^4.2.2", 20 | "swagger-ui-express": "^4.0.2", 21 | "yamljs": "^0.3.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /data-service/routes/allergies.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Router for allergies 18 | */ 19 | 20 | var express = require('express'); 21 | var router = express.Router(); 22 | 23 | var datalake = require('../service/datalake'); 24 | 25 | /* GET list of allergies from Data Lake. */ 26 | router.get('/', function(req, res, next) { 27 | 28 | var getAllergyList = datalake.getAllergyList(); 29 | 30 | getAllergyList.then(function(allergies) { 31 | res.send({allergies:allergies}); 32 | }).catch(function(err) { 33 | res.sendStatus(500); 34 | }) 35 | }); 36 | 37 | module.exports = router; 38 | -------------------------------------------------------------------------------- /data-service/routes/cities.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Router for cities 18 | */ 19 | 20 | var express = require('express'); 21 | var router = express.Router(); 22 | 23 | var datalake = require('../service/datalake'); 24 | 25 | /* GET list of cities from Data Lake. */ 26 | router.get('/', function(req, res, next) { 27 | 28 | var getCityList = datalake.getCityList(); 29 | 30 | getCityList.then(function(cities) { 31 | res.send({cities:cities}); 32 | }).catch(function(err) { 33 | res.sendStatus(500); 34 | }) 35 | }); 36 | 37 | module.exports = router; 38 | -------------------------------------------------------------------------------- /data-service/routes/generate.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Router for updating datalake from generated synthea data 18 | */ 19 | 20 | var express = require('express'); 21 | var router = express.Router(); 22 | 23 | var synthea = require('../service/synthea'); 24 | var datalake = require('../service/datalake'); 25 | 26 | /* Updates Data Lake */ 27 | router.put('/', function(req, res, next) { 28 | 29 | var datalakeData = synthea.generateFromSynthea(req.body); 30 | 31 | var updateAnalytics = datalake.updateAnalytics(datalakeData); 32 | 33 | updateAnalytics.then(function(success) { 34 | res.sendStatus(200); 35 | }).catch(function (err) { 36 | res.sendStatus(500); 37 | }); 38 | }); 39 | 40 | module.exports = router; 41 | -------------------------------------------------------------------------------- /data-service/routes/population.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Router for population 18 | */ 19 | 20 | var express = require('express'); 21 | var router = express.Router(); 22 | 23 | var datalake = require('../service/datalake'); 24 | 25 | /* GET pupulation from Data Lake. */ 26 | router.get('/', function(req, res, next) { 27 | 28 | var getTotalPopulation = datalake.getTotalPopulation(); 29 | 30 | getTotalPopulation.then(function(population) { 31 | res.send({population:population}); 32 | }).catch(function(err) { 33 | res.send(500); 34 | }) 35 | }); 36 | 37 | module.exports = router; 38 | -------------------------------------------------------------------------------- /data-service/routes/update.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Router for updating data lake from data source 18 | */ 19 | 20 | var express = require('express'); 21 | var router = express.Router(); 22 | 23 | var datasource = require('../service/datasource'); 24 | var datalake = require('../service/datalake'); 25 | 26 | const util = require('util'); 27 | 28 | /* Updates Data Lake */ 29 | router.put('/', function(req, res, next) { 30 | 31 | datasource.getDataFromSource().then(datalakeData => { 32 | 33 | if (!datalakeData) { 34 | res.sendStatus(502); 35 | } else { 36 | var updateAnalytics = datalake.updateAnalytics(datalakeData); 37 | 38 | updateAnalytics.then(function(success) { 39 | res.sendStatus(200); 40 | }).catch(function (err) { 41 | res.sendStatus(500); 42 | }); 43 | } 44 | }) 45 | 46 | }); 47 | 48 | module.exports = router; 49 | -------------------------------------------------------------------------------- /data-service/service/datalake.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Service for interacting with MongoDB data lake 18 | */ 19 | 20 | var mongo = require('mongodb'); 21 | var MongoClient = require('mongodb').MongoClient; 22 | 23 | var url = process.env.MONGODB || "mongodb://mongo:27017/examplehealth"; 24 | 25 | /** 26 | * Updates and replaces analytics data in data lake 27 | * See README.md for more information on structure of analytics object for data lake 28 | * 29 | * @param {Object} newAnalytics 30 | */ 31 | function updateAnalytics(newAnalytics) { 32 | return new Promise(function(resolve, reject) { 33 | MongoClient.connect(url).then(function(db) { 34 | var dbo = db.db("examplehealth"); 35 | dbo.collection("analytics").deleteOne({}, function(err, obj){ 36 | if (err) reject(err); 37 | dbo.collection("analytics").insertOne(newAnalytics, {upsert: true}, function(err, res) { 38 | if (err) reject(err); 39 | db.close(); 40 | resolve(true); 41 | }); 42 | }) 43 | }).catch(function(err) { 44 | reject(err); 45 | }); 46 | }) 47 | } 48 | 49 | /** 50 | * Gets the total population from data lake 51 | */ 52 | function getTotalPopulation() { 53 | return new Promise(function(resolve, reject) { 54 | MongoClient.connect(url).then(function(db) { 55 | var dbo = db.db("examplehealth"); 56 | dbo.collection("analytics").findOne({}, function(e, result) { 57 | if (e) reject(e); 58 | db.close(); 59 | resolve(result.population); 60 | }); 61 | }).catch(function(err) { 62 | reject(err); 63 | }) 64 | }) 65 | } 66 | 67 | /** 68 | * Gets cities data from data lake that contains each city's respective allergy analytics data 69 | */ 70 | function getCityList() { 71 | return new Promise(function(resolve, reject) { 72 | MongoClient.connect(url).then(function(db) { 73 | var dbo = db.db("examplehealth"); 74 | dbo.collection("analytics").findOne({}, function(e, result) { 75 | if (e) reject(e); 76 | db.close(); 77 | resolve(result.cities); 78 | }); 79 | }).catch(function(err) { 80 | reject(err); 81 | }) 82 | }) 83 | } 84 | 85 | /** 86 | * Gets a list of all allergies included in the data lake 87 | */ 88 | function getAllergyList() { 89 | return new Promise(function(resolve, reject) { 90 | MongoClient.connect(url).then(function(db) { 91 | var dbo = db.db("examplehealth"); 92 | dbo.collection("analytics").findOne({}, function(e, result) { 93 | if (e) reject(e); 94 | db.close(); 95 | resolve(result.allergies); 96 | }) 97 | }).catch(function(err) { 98 | reject(err); 99 | }) 100 | }) 101 | } 102 | 103 | module.exports.updateAnalytics = updateAnalytics; 104 | module.exports.getTotalPopulation = getTotalPopulation; 105 | module.exports.getCityList = getCityList; 106 | module.exports.getAllergyList = getAllergyList; 107 | -------------------------------------------------------------------------------- /data-service/service/datasource.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Service for handling and processing data from data source 18 | */ 19 | 20 | var request = require('request'); 21 | const fetch = require("node-fetch"); 22 | var API_URL = process.env.DATA_SOURCE_API || ""; 23 | 24 | var allergyTypes = [ 25 | {synthea: "Allergy to peanuts", 26 | analytics: "Peanut", 27 | type: "Food"}, 28 | {synthea: "Allergy to nut", 29 | analytics: "Tree nut", 30 | type: "Food"}, 31 | {synthea: "Allergy to fish", 32 | analytics: "Fish", 33 | type: "Food"}, 34 | {synthea: "Shellfish allergy", 35 | analytics: "Shellfish", 36 | type: "Food"}, 37 | {synthea: "Allergy to wheat", 38 | analytics: "Wheat", 39 | type: "Food"}, 40 | {synthea: "Allergy to eggs", 41 | analytics: "Egg", 42 | type: "Food"}, 43 | {synthea: "Allergy to soya", 44 | analytics: "Soy", 45 | type: "Food"}, 46 | {synthea: "Allergy to dairy product", 47 | analytics: "Dairy", 48 | type: "Food"}, 49 | {synthea: "Allergy to tree pollen", 50 | analytics: "Tree Pollen", 51 | type: "Outdoor"}, 52 | {synthea: "Allergy to grass pollen", 53 | analytics: "Grass Pollen", 54 | type: "Outdoor"}, 55 | {synthea: "Dander (animal) allergy", 56 | analytics: "Pet Dander", 57 | type: "Outdoor"}, 58 | {synthea: "House dust mite allergy", 59 | analytics: "Dust Mite", 60 | type: "Outdoor"}, 61 | {synthea: "Allergy to mould", 62 | analytics: "Mold", 63 | type: "Outdoor"}, 64 | {synthea: "Allergy to bee venom", 65 | analytics: "Bee Sting", 66 | type: "Outdoor"}, 67 | {synthea: "Latex allergy", 68 | analytics: "Latex", 69 | type: "Other"} 70 | ] 71 | 72 | /** 73 | * Gets the city population from data source API 74 | */ 75 | function getCityPopulation() { 76 | return new Promise(function(resolve, reject) { 77 | request(API_URL + "countCities/", function (error, response, body) { 78 | if (!error && response.statusCode == 200) { 79 | body = JSON.parse(body); 80 | var cities = body["ResultSet Output"]; 81 | return resolve(cities); 82 | } else { 83 | return resolve(null); 84 | } 85 | }); 86 | }) 87 | } 88 | 89 | /** 90 | * Gets the allergy data from data source API 91 | */ 92 | function getAllergies() { 93 | return new Promise(function(resolve, reject) { 94 | request(API_URL + "showAllergies/", function (error, response, body) { 95 | if (!error && response.statusCode == 200) { 96 | body = JSON.parse(body); 97 | var allergies = body["ResultSet Output"]; 98 | return resolve(allergies); 99 | } else { 100 | return resolve(null); 101 | } 102 | }); 103 | }) 104 | } 105 | 106 | /** 107 | * Gets the state for a city based on zip code 108 | * 109 | * @param {String} zipcode 110 | * @param {Number} city 111 | */ 112 | function getState(zipcode, city) { 113 | return new Promise(function(resolve, reject) { 114 | var accessToken = process.env.MAPBOX_ACCESS_TOKEN || ""; 115 | var url = "https://api.mapbox.com/geocoding/v5/mapbox.places/" + zipcode + "%20United States.json?access_token="; 116 | 117 | fetch(url+accessToken) 118 | .then(response => response.json()) 119 | .then(zipcodeData => { 120 | if (zipcodeData && zipcodeData.features && zipcodeData.features.length > 0 && zipcodeData.features[0].context.length == 3 && zipcodeData.features[0].context[2].text == "United States") { 121 | resolve([zipcodeData.features[0].context[1].text,city]); 122 | } 123 | resolve([null,city]); 124 | }) 125 | }) 126 | } 127 | 128 | /** 129 | * Processes and returns data from data source 130 | */ 131 | function getDataFromSource() { 132 | 133 | return new Promise(function(resolve, reject) { 134 | 135 | var datalakeData = { 136 | population: 0, 137 | allergies: [], 138 | cities: [] 139 | }; 140 | 141 | var allergies = []; 142 | 143 | getCityPopulation().then(cities => { 144 | getAllergies().then(allergies => { 145 | 146 | if (!cities || !allergies) { 147 | resolve(null); 148 | } 149 | 150 | var nullCities = 0; 151 | var duplicateCities = 0; 152 | 153 | for (var city = 0; city < cities.length; city++) { 154 | if (cities[city].CITY == null) { 155 | nullCities = nullCities + 1; 156 | continue; 157 | } 158 | 159 | getState(cities[city].POSTCODE.trim(), city).then(stateData => { 160 | 161 | var state = stateData[0]; 162 | var city = stateData[1]; 163 | 164 | if (state) { 165 | 166 | datalakeData.population = datalakeData.population + cities[city].NUM_IN_CITY; 167 | 168 | var currentCity = { 169 | city: cities[city].CITY.trim(), 170 | state: state, 171 | population: cities[city].NUM_IN_CITY, 172 | allergies: [] 173 | } 174 | 175 | for (var datalakeCity = 0; datalakeCity < datalakeData.cities.length; datalakeCity++) { 176 | if (datalakeData.cities[datalakeCity].city == cities[city].CITY.trim() && datalakeData.cities[datalakeCity].state == state) { 177 | duplicateCities = duplicateCities + 1; 178 | currentCity.population = currentCity.population + datalakeData.cities[datalakeCity].population; 179 | currentCity.allergies = datalakeData.cities[datalakeCity].allergies; 180 | datalakeData.cities.splice(datalakeCity, 1); 181 | break; 182 | } 183 | } 184 | 185 | for (var allergy = 0; allergy < allergies.length; allergy++) { 186 | if (allergies[allergy].CITY.trim() == currentCity.city && allergies[allergy].POSTCODE.trim() == cities[city].POSTCODE.trim()) { 187 | for (var allergyType = 0; allergyType < allergyTypes.length; allergyType++) { 188 | if (allergyTypes[allergyType].synthea == allergies[allergy].DESCRIPTION) { 189 | if (datalakeData.allergies.indexOf(allergyTypes[allergyType].analytics) == -1) { 190 | datalakeData.allergies.push(allergyTypes[allergyType].analytics); 191 | } 192 | 193 | var allergyInCity = false; 194 | 195 | for (var cityAllergy = 0; cityAllergy < currentCity.allergies.length; cityAllergy++) { 196 | if (currentCity.allergies[cityAllergy].allergy == allergyTypes[allergyType].analytics) { 197 | allergyInCity = true; 198 | break; 199 | } 200 | } 201 | 202 | if (!allergyInCity) { 203 | currentCity.allergies.push({ 204 | allergy: allergyTypes[allergyType].analytics, 205 | type: allergyTypes[allergyType].type, 206 | developed: [], 207 | outgrown: [] 208 | }) 209 | } 210 | 211 | var developedDate = new Date(allergies[allergy].ALLERGY_START); 212 | var birthDate = new Date(allergies[allergy].BIRTHDATE); 213 | 214 | currentCity.allergies[cityAllergy].developed.push(developedDate.getFullYear()-birthDate.getFullYear()); 215 | 216 | if (allergies[allergy].ALLERGY_STOP != null && allergies[allergy].ALLERGY_STOP.trim().length > 0) { 217 | var outgrownDate = new Date(allergies[allergy].ALLERGY_STOP); 218 | currentCity.allergies[cityAllergy].outgrown.push(outgrownDate.getFullYear()-birthDate.getFullYear()); 219 | } 220 | } 221 | } 222 | } 223 | } 224 | datalakeData.cities.push(currentCity); 225 | } else { 226 | nullCities = nullCities + 1; 227 | } 228 | 229 | if (datalakeData.cities.length == cities.length - nullCities - duplicateCities) { 230 | resolve(datalakeData); 231 | } 232 | }) 233 | } 234 | }).catch(function (err) { 235 | resolve(datalakeData); 236 | }) 237 | }).catch(function (err) { 238 | resolve(datalakeData); 239 | }) 240 | 241 | }) 242 | } 243 | 244 | module.exports.getDataFromSource = getDataFromSource; 245 | -------------------------------------------------------------------------------- /data-service/service/synthea.js: -------------------------------------------------------------------------------- 1 | /*############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ##############################################################################*/ 16 | /** 17 | * Service for converting generated synthea data into data lake data 18 | */ 19 | 20 | var allergyTypes = [ 21 | {synthea: "Allergy to peanuts", 22 | analytics: "Peanut", 23 | type: "Food"}, 24 | {synthea: "Allergy to nut", 25 | analytics: "Tree nut", 26 | type: "Food"}, 27 | {synthea: "Allergy to fish", 28 | analytics: "Fish", 29 | type: "Food"}, 30 | {synthea: "Shellfish allergy", 31 | analytics: "Shellfish", 32 | type: "Food"}, 33 | {synthea: "Allergy to wheat", 34 | analytics: "Wheat", 35 | type: "Food"}, 36 | {synthea: "Allergy to eggs", 37 | analytics: "Egg", 38 | type: "Food"}, 39 | {synthea: "Allergy to soya", 40 | analytics: "Soy", 41 | type: "Food"}, 42 | {synthea: "Allergy to dairy product", 43 | analytics: "Dairy", 44 | type: "Food"}, 45 | {synthea: "Allergy to tree pollen", 46 | analytics: "Tree Pollen", 47 | type: "Outdoor"}, 48 | {synthea: "Allergy to grass pollen", 49 | analytics: "Grass Pollen", 50 | type: "Outdoor"}, 51 | {synthea: "Dander (animal) allergy", 52 | analytics: "Pet Dander", 53 | type: "Outdoor"}, 54 | {synthea: "House dust mite allergy", 55 | analytics: "Dust Mite", 56 | type: "Outdoor"}, 57 | {synthea: "Allergy to mould", 58 | analytics: "Mold", 59 | type: "Outdoor"}, 60 | {synthea: "Allergy to bee venom", 61 | analytics: "Bee Sting", 62 | type: "Outdoor"}, 63 | {synthea: "Latex allergy", 64 | analytics: "Latex", 65 | type: "Other"} 66 | ] 67 | 68 | /** 69 | * Converts generated synthea data into data lake data 70 | * 71 | * @param {Object} syntheaData 72 | */ 73 | function generateFromSynthea(syntheaData) { 74 | var datalakeData = { 75 | population: syntheaData.patients.length, 76 | allergies: [], 77 | cities: [] 78 | }; 79 | 80 | for (var patient = 0; patient < syntheaData.patients.length; patient++) { 81 | var cityInData = false; 82 | for (var city = 0; city < datalakeData.cities.length; city++) { 83 | if (datalakeData.cities[city].city == syntheaData.patients[patient].CITY) { 84 | cityInData = true; 85 | break; 86 | } 87 | } 88 | 89 | if (!cityInData) { 90 | datalakeData.cities.push({ 91 | city: syntheaData.patients[patient].CITY, 92 | state: syntheaData.patients[patient].STATE, 93 | population: 0, 94 | allergies: [] 95 | }) 96 | } 97 | 98 | datalakeData.cities[city].population = datalakeData.cities[city].population + 1; 99 | 100 | for (var allergy = 0; allergy < syntheaData.allergies.length; allergy++) { 101 | if (syntheaData.allergies[allergy].PATIENT == syntheaData.patients[patient].Id) { 102 | for (var allergyType = 0; allergyType < allergyTypes.length; allergyType++) { 103 | if (allergyTypes[allergyType].synthea == syntheaData.allergies[allergy].DESCRIPTION) { 104 | if (datalakeData.allergies.indexOf(allergyTypes[allergyType].analytics) == -1) { 105 | datalakeData.allergies.push(allergyTypes[allergyType].analytics); 106 | } 107 | 108 | var allergyInCity = false; 109 | 110 | for (var cityAllergy = 0; cityAllergy < datalakeData.cities[city].allergies.length; cityAllergy++) { 111 | if (datalakeData.cities[city].allergies[cityAllergy].allergy == allergyTypes[allergyType].analytics) { 112 | allergyInCity = true; 113 | break; 114 | } 115 | } 116 | 117 | if (!allergyInCity) { 118 | datalakeData.cities[city].allergies.push({ 119 | allergy: allergyTypes[allergyType].analytics, 120 | type: allergyTypes[allergyType].type, 121 | developed: [], 122 | outgrown: [] 123 | }) 124 | } 125 | 126 | var developedDate = new Date(syntheaData.allergies[allergy].START); 127 | var birthDate = new Date(syntheaData.patients[patient].BIRTHDATE); 128 | 129 | datalakeData.cities[city].allergies[cityAllergy].developed.push(developedDate.getFullYear()-birthDate.getFullYear()); 130 | 131 | if (syntheaData.allergies[allergy].STOP != "") { 132 | var outgrownDate = new Date(syntheaData.allergies[allergy].STOP); 133 | datalakeData.cities[city].allergies[cityAllergy].outgrown.push(outgrownDate.getFullYear()-birthDate.getFullYear()); 134 | } 135 | } 136 | } 137 | } 138 | } 139 | 140 | } 141 | 142 | return datalakeData; 143 | } 144 | 145 | module.exports.generateFromSynthea = generateFromSynthea; 146 | -------------------------------------------------------------------------------- /data-service/swagger.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | swagger: '2.0' 17 | info: 18 | description: >- 19 | This is a data service that handles analytics data for Example Health 20 | Analytics 21 | version: 1.0.0 22 | title: Example Health Analytics Data Service 23 | contact: 24 | email: maxshapiro32@ibm.com 25 | license: 26 | name: Apache 2.0 27 | url: 'https://github.com/IBM/example-health-analytics/blob/master/LICENSE' 28 | host: 'localhost:3000' 29 | basePath: /api/v1 30 | tags: 31 | - name: Update 32 | description: Operations associated with interacting with data source 33 | - name: Generate 34 | description: Operations associated with generated synthea data 35 | - name: Population 36 | description: Operations associated with population 37 | - name: Cities 38 | description: Operations associated with cities 39 | - name: Allergies 40 | description: Operations associated with allergies 41 | schemes: 42 | - http 43 | paths: 44 | /update: 45 | put: 46 | tags: 47 | - Update 48 | summary: Update data lake data from data source 49 | description: '' 50 | produces: 51 | - application/json 52 | responses: 53 | '200': 54 | description: Successfully updated data lake 55 | '500': 56 | description: Internal Server Error 57 | '502': 58 | description: Error connecting to data source 59 | /generate: 60 | put: 61 | tags: 62 | - Generate 63 | summary: Update data lake data from generated synthea data 64 | description: '' 65 | consumes: 66 | - application/json 67 | produces: 68 | - application/json 69 | parameters: 70 | - in: body 71 | name: data 72 | description: Raw data from generated synthea data 73 | required: true 74 | schema: 75 | $ref: '#/definitions/Synthea' 76 | responses: 77 | '200': 78 | description: Successfully updated data lake 79 | '500': 80 | description: Internal Server Error 81 | /population: 82 | get: 83 | tags: 84 | - Population 85 | summary: Gets total population from data lake 86 | description: '' 87 | produces: 88 | - application/json 89 | responses: 90 | '200': 91 | description: Successfully got total population from data lake 92 | '500': 93 | description: Internal Server Error 94 | /cities: 95 | get: 96 | tags: 97 | - Cities 98 | summary: Gets list of cities from data lake 99 | description: '' 100 | produces: 101 | - application/json 102 | responses: 103 | '200': 104 | description: Successfully got cities data from data lake 105 | '500': 106 | description: Internal Server Error 107 | /allergies: 108 | get: 109 | tags: 110 | - Allergies 111 | summary: Gets list of allergies from data lake 112 | description: '' 113 | produces: 114 | - application/json 115 | responses: 116 | '200': 117 | description: Successfully got allergies from data lake 118 | '500': 119 | description: Internal Server Error 120 | definitions: 121 | Synthea: 122 | type: object 123 | properties: 124 | allergies: 125 | type: array 126 | items: 127 | $ref: '#/definitions/SyntheaAllergy' 128 | patients: 129 | type: array 130 | items: 131 | $ref: '#/definitions/SyntheaPatients' 132 | SyntheaAllergy: 133 | type: object 134 | properties: 135 | START: 136 | type: string 137 | STOP: 138 | type: string 139 | PATIENT: 140 | type: string 141 | ENCOUNTER: 142 | type: string 143 | CODE: 144 | type: string 145 | DESCRIPTION: 146 | type: string 147 | SyntheaPatients: 148 | type: object 149 | properties: 150 | Id: 151 | type: string 152 | BIRTHDATE: 153 | type: string 154 | DEATHDATE: 155 | type: string 156 | SSN: 157 | type: string 158 | DRIVERS: 159 | type: string 160 | PASSPORT: 161 | type: string 162 | PREFIX: 163 | type: string 164 | FIRST: 165 | type: string 166 | LAST: 167 | type: string 168 | SUFFIX: 169 | type: string 170 | MAIDEN: 171 | type: string 172 | MARITAL: 173 | type: string 174 | Race: 175 | type: string 176 | ETHNICITY: 177 | type: string 178 | GENDER: 179 | type: string 180 | BIRTHPLACE: 181 | type: string 182 | ADDRESS: 183 | type: string 184 | CITY: 185 | type: string 186 | STATE: 187 | type: string 188 | ZIP: 189 | type: string 190 | -------------------------------------------------------------------------------- /deploy-dataservice.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | apiVersion: apps/v1 18 | kind: Deployment 19 | metadata: 20 | name: dataservice 21 | spec: 22 | selector: 23 | matchLabels: 24 | name: dataservice-deployment 25 | version: v1 26 | replicas: 1 27 | template: 28 | metadata: 29 | labels: 30 | name: dataservice-deployment 31 | version: v1 32 | spec: 33 | containers: 34 | - name: dataservice 35 | image: maxshapiro32/examplehealthanalyticsdata 36 | imagePullPolicy: Always 37 | resources: 38 | limits: 39 | cpu: 200m 40 | memory: 500Mi 41 | requests: 42 | cpu: 100m 43 | memory: 200Mi 44 | ports: 45 | - containerPort: 3000 46 | env: 47 | - name: HOST_IP 48 | value: 49 | - name: SCHEME 50 | value: 51 | - name: MAPBOX_ACCESS_TOKEN 52 | value: 53 | - name: DATA_SOURCE_API 54 | value: 55 | --- 56 | apiVersion: v1 57 | kind: Service 58 | metadata: 59 | name: dataservice-service 60 | spec: 61 | ports: 62 | - port: 3000 63 | targetPort: 3000 64 | nodePort: 32000 65 | name: http 66 | type: NodePort 67 | selector: 68 | name: dataservice-deployment 69 | -------------------------------------------------------------------------------- /deploy-mongodb.yml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: apps/v1 3 | kind: Deployment 4 | metadata: 5 | name: mongo 6 | spec: 7 | selector: 8 | matchLabels: 9 | name: mongo 10 | version: v1 11 | replicas: 1 12 | template: 13 | metadata: 14 | labels: 15 | name: mongo 16 | version: v1 17 | spec: 18 | containers: 19 | - image: mongo 20 | name: mongo 21 | resources: 22 | limits: 23 | cpu: 200m 24 | memory: 500Mi 25 | requests: 26 | cpu: 100m 27 | memory: 200Mi 28 | ports: 29 | - containerPort: 27017 30 | --- 31 | apiVersion: v1 32 | kind: Service 33 | metadata: 34 | name: mongo 35 | spec: 36 | ports: 37 | - port: 27017 38 | targetPort: 27017 39 | selector: 40 | name: mongo 41 | -------------------------------------------------------------------------------- /deploy-webapp.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | apiVersion: apps/v1 18 | kind: Deployment 19 | metadata: 20 | name: webapp 21 | spec: 22 | selector: 23 | matchLabels: 24 | name: webapp-deployment 25 | version: v1 26 | replicas: 1 27 | template: 28 | metadata: 29 | labels: 30 | name: webapp-deployment 31 | version: v1 32 | spec: 33 | containers: 34 | - name: webapp 35 | image: maxshapiro32/examplehealthanalyticsweb 36 | imagePullPolicy: Always 37 | resources: 38 | limits: 39 | cpu: 200m 40 | memory: 500Mi 41 | requests: 42 | cpu: 100m 43 | memory: 200Mi 44 | ports: 45 | - containerPort: 4000 46 | env: 47 | - name: DATA_SERVER 48 | value: :// 49 | - name: MAPBOX_ACCESS_TOKEN 50 | value: 51 | --- 52 | apiVersion: v1 53 | kind: Service 54 | metadata: 55 | name: webapp-service 56 | spec: 57 | ports: 58 | - port: 4000 59 | targetPort: 4000 60 | nodePort: 32001 61 | name: http 62 | type: NodePort 63 | selector: 64 | name: webapp-deployment 65 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | version: '3.3' 18 | 19 | services: 20 | mongo: 21 | container_name: mongo 22 | ports: 23 | - "27017:27017" 24 | image: mongo 25 | restart: always 26 | 27 | mongo-express: 28 | image: mongo-express 29 | restart: always 30 | ports: 31 | - "8081:8081" 32 | 33 | data: 34 | container_name: examplehealthdata 35 | build: data-service 36 | ports: 37 | - "3000:3000" 38 | depends_on: 39 | - mongo 40 | environment: 41 | - MAPBOX_ACCESS_TOKEN= 42 | - DATA_SOURCE_API= 43 | 44 | web: 45 | container_name: examplehealthweb 46 | build: web 47 | ports: 48 | - "4000:4000" 49 | depends_on: 50 | - data 51 | environment: 52 | - DATA_SERVER=http://examplehealthdata:3000 53 | - MAPBOX_ACCESS_TOKEN= 54 | -------------------------------------------------------------------------------- /generate/README.md: -------------------------------------------------------------------------------- 1 | # Generate Synthea Data 2 | 3 | ## About 4 | 5 | This is an alternative to populate Example Health Analytics with patient and allergy data as opposed to getting data from a data source. The bash script works by cloning [Synthea](https://github.com/synthetichealth/synthea), running Synthea to generate data, converting the Synthea data output to a JSON file, and sending the `apidata.json` JSON file to the data service of Example Health Analytics where it is processed and stored in the datalake. 6 | 7 | ## Prerequisites 8 | 9 | * NPM: Install [here](https://www.npmjs.com/get-npm) 10 | * Install dependencies: 11 | 12 | ```bash 13 | npm install 14 | ``` 15 | 16 | * Java 1.8 or above: Install [here](https://www.oracle.com/technetwork/java/javase/downloads/index.html) 17 | 18 | ## Running 19 | 20 | ```bash 21 | ./generate.sh -p [population] -s [state] -u [url] 22 | ``` 23 | 24 | **Flags**: 25 | * p 26 | * The population of the generated Synthea data. If flag is not used, defaults to 100. 27 | * s 28 | * The state for where the Synthea data will be generated. If flag is not used, defaults to California. 29 | * u 30 | * The API url for the data service of Example Health Analytics. If flag is not used, defaults to http://localhost:3000 31 | 32 | # License 33 | 34 | This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1](https://developercertificate.org/) and the [Apache License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.txt). 35 | 36 | [Apache License FAQ](https://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN) -------------------------------------------------------------------------------- /generate/generate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ############################################################################## 3 | # Copyright 2019 IBM Corp. All Rights Reserved. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | ############################################################################## 17 | population=100 18 | state=California 19 | url="http://localhost:3000" 20 | while getopts p:s:u: option 21 | do 22 | case "${option}" 23 | in 24 | p) population=${OPTARG};; 25 | s) state=${OPTARG};; 26 | u) url=${OPTARG};; 27 | *) continue;; 28 | esac 29 | done 30 | git clone https://github.com/synthetichealth/synthea.git 31 | cd synthea || exit 1 32 | sed -e 's/^\(exporter.years_of_history =\).*/\1 0/' -e 's/^\(exporter.csv.export =\).*/\1 true/' src/main/resources/synthea.properties > src/main/resources/synthea.properties.new 33 | mv src/main/resources/synthea.properties.new src/main/resources/synthea.properties 34 | ./run_synthea -s 32 -p "$population" "$state" 35 | mv output/csv/allergies.csv ../allergies.csv 36 | mv output/csv/patients.csv ../patients.csv 37 | cd .. 38 | rm -rf synthea 39 | csvtojson allergies.csv > allergies.json 40 | csvtojson patients.csv > patients.json 41 | sed -e '1s/^/{"allergies":/' allergies.json > apidata.json 42 | { 43 | echo ',"patients":' 44 | cat patients.json 45 | echo "}" 46 | } >> apidata.json 47 | rm -rf allergies.csv 48 | rm -rf allergies.json 49 | rm -rf patients.csv 50 | rm -rf patients.json 51 | curl "$url/api/v1/generate" -H "Content-Type: application/json" -X PUT -d "@apidata.json" 52 | -------------------------------------------------------------------------------- /generate/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "synthea-generate", 3 | "version": "1.0.0", 4 | "description": "Generate mock data with Synthea", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/IBM/summit-health-analytics.git" 8 | }, 9 | "author": "", 10 | "license": "Apache-2.0", 11 | "homepage": "https://github.com/IBM/summit-health-analytics#readme", 12 | "devDependencies": { 13 | "csvtojson": "" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ingress-dataservice.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | apiVersion: extensions/v1beta1 18 | kind: Ingress 19 | metadata: 20 | annotations: 21 | ingress.bluemix.net/redirect-to-https: "True" 22 | ingress.bluemix.net/client-max-body-size: size=25m 23 | name: ingress-dataservice 24 | spec: 25 | tls: 26 | - hosts: 27 | - api. 28 | secretName: 29 | backend: 30 | serviceName: dataservice-service 31 | servicePort: 3000 32 | rules: 33 | - host: api. 34 | http: 35 | paths: 36 | - path: / 37 | backend: 38 | serviceName: dataservice-service 39 | servicePort: 3000 40 | -------------------------------------------------------------------------------- /ingress-webapp.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | apiVersion: extensions/v1beta1 18 | kind: Ingress 19 | metadata: 20 | annotations: 21 | ingress.bluemix.net/redirect-to-https: "True" 22 | name: ingress-webapp 23 | spec: 24 | tls: 25 | - hosts: 26 | - 27 | secretName: 28 | backend: 29 | serviceName: webapp-service 30 | servicePort: 4000 31 | rules: 32 | - host: 33 | http: 34 | paths: 35 | - path: / 36 | backend: 37 | serviceName: webapp-service 38 | servicePort: 4000 39 | -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | --- 17 | applications: 18 | - name: 19 | path: ./data-service 20 | env: 21 | HOST_IP: . 22 | MONGODB: 23 | SCHEME: https 24 | MAPBOX_ACCESS_TOKEN: 25 | DATA_SOURCE_API: 26 | services: 27 | - 28 | - name: 29 | path: ./web 30 | env: 31 | DATA_SERVER: https://. 32 | MAPBOX_ACCESS_TOKEN: 33 | -------------------------------------------------------------------------------- /readme_images/bar_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/bar_chart.png -------------------------------------------------------------------------------- /readme_images/cf_mongo_name.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cf_mongo_name.png -------------------------------------------------------------------------------- /readme_images/cf_mongo_url.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cf_mongo_url.png -------------------------------------------------------------------------------- /readme_images/cf_node_connect_mongo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cf_node_connect_mongo.png -------------------------------------------------------------------------------- /readme_images/cf_node_host_domain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cf_node_host_domain.png -------------------------------------------------------------------------------- /readme_images/cf_node_name.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cf_node_name.png -------------------------------------------------------------------------------- /readme_images/cloudfoundry_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cloudfoundry_architecture.png -------------------------------------------------------------------------------- /readme_images/cto_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/cto_architecture.png -------------------------------------------------------------------------------- /readme_images/kubernetes_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/kubernetes_architecture.png -------------------------------------------------------------------------------- /readme_images/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/map.png -------------------------------------------------------------------------------- /readme_images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/screenshot.png -------------------------------------------------------------------------------- /readme_images/treemap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/readme_images/treemap.png -------------------------------------------------------------------------------- /travis-tests/docker-compose.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | ############################################################################## 3 | # Copyright 2019 IBM Corp. All Rights Reserved. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | ############################################################################## 17 | 18 | # shellcheck disable=SC1090 19 | source "$(dirname "$0")"/../pattern-ci/scripts/resources.sh 20 | main(){ 21 | if ! docker-compose up -d; then 22 | test_failed "$0" 23 | fi 24 | if ! docker-compose ps; then 25 | test_failed "$0" 26 | fi 27 | if ! cd generate; then 28 | test_failed "$0" 29 | fi 30 | if ! ./generate.sh; then 31 | test_failed "$0" 32 | fi 33 | 34 | test_passed "$0" 35 | } 36 | main "$@" 37 | -------------------------------------------------------------------------------- /web/Dockerfile: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # Copyright 2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################## 16 | 17 | FROM node:10-alpine 18 | MAINTAINER Max Shapiro "maxshapiro32@ibm.com" 19 | COPY . /web 20 | WORKDIR /web 21 | RUN npm install 22 | EXPOSE 4000 23 | CMD ["npm", "start"] 24 | -------------------------------------------------------------------------------- /web/README.md: -------------------------------------------------------------------------------- 1 | # Example Health Analytics Front End 2 | 3 | * [About](#about) 4 | * [Analytics](#analytics) 5 | * [Visuals](#visuals) 6 | 7 | # About 8 | 9 | This part of Example Health Analytics is responsible for calculating and displaying analytics on the paitent/allergy data stored in the MongoDB data lake. 10 | 11 | # Analytics 12 | 13 | There are two main groups of analytics being calculated from the data lake: Population and Allergy statistics. 14 | 15 | ## Population 16 | 17 | The population statistics are looked at from a city level and include both people with and without allergies. 18 | 19 | __Data Structure__ 20 | 21 | ``` 22 | { 23 | cities: [{ 24 | city: String, 25 | state: String, 26 | percentage: Float, 27 | population: Int 28 | }], 29 | min: { 30 | city: String, 31 | state: String, 32 | percentage: Float, 33 | population: Int 34 | }, 35 | max: { 36 | city: String, 37 | state: String, 38 | percentage: Float, 39 | population: Int 40 | }, 41 | mean: Float 42 | } 43 | ``` 44 | 45 | * **cities**: A list of cities where each city includes: 46 | * **city**: The name of a city 47 | * **state**: The name of a state a city is in 48 | * **percentage**: The percentage of the state's population that represents a city's population 49 | * **population**: The total population for a city 50 | * **min**: The city with the minimum population 51 | * **max**: The city with the maximum population 52 | * **mean**: The average population of cities in a state 53 | 54 | ## Allergy 55 | 56 | The allergy statistics are looked at from a city level. The statisitcs focus on specific allergies (developed and outgrown). 57 | 58 | __Data Structure__ 59 | 60 | ``` 61 | { 62 | cities: [{ 63 | city: String, 64 | state: String, 65 | allergies: [{ 66 | allergy: String, 67 | type: String, 68 | outgrown: { 69 | total: Int, 70 | percentage: Float, 71 | ages: [Int] 72 | }, 73 | developed: { 74 | total: Int, 75 | percentage: Float, 76 | ages: [Int] 77 | } 78 | }] 79 | }], 80 | stats: { 81 | outgrown: [{ 82 | allergy: String, 83 | min: { 84 | total: { 85 | city: String, 86 | state: String, 87 | min: Int 88 | }, 89 | percentage: { 90 | city: String, 91 | state: String, 92 | min: Float 93 | } 94 | }, 95 | max: { 96 | total: { 97 | city: String, 98 | state: String, 99 | max: Int 100 | }, 101 | percentage: { 102 | city: String, 103 | state: String, 104 | max: Float 105 | } 106 | }, 107 | mean: { 108 | total: Float, 109 | percentage: Float 110 | } 111 | }], 112 | developed: [{ 113 | allergy: String, 114 | min: { 115 | total: { 116 | city: String, 117 | state: String, 118 | min: Int 119 | }, 120 | percentage: { 121 | city: String, 122 | state: String, 123 | min: Float 124 | } 125 | }, 126 | max: { 127 | total: { 128 | city: String, 129 | state: String, 130 | max: Int 131 | }, 132 | percentage: { 133 | city: String, 134 | state: String, 135 | max: Float 136 | } 137 | }, 138 | mean: { 139 | total: Float, 140 | percentage: Float 141 | } 142 | }] 143 | } 144 | } 145 | ``` 146 | 147 | * **cities**: A list of cities where each city includes: 148 | * **city**: The name of a city 149 | * **state**: The name of a state a city is in 150 | * **allergies**: A list of alleriges for a city that includes: 151 | * **allergy**: The name of the allergy 152 | * **type**: The type of the allergy 153 | * **outgrown**: City population that have outgrown an allergy that includes: 154 | * **total**: The total city population that has outgrown an allergy 155 | * **percentage**: The percentage of a city population that has an allergy that outgrew the allergy 156 | * **ages**: A list of the ages of a city population that outgrew an allergy 157 | * **developed**: City population that have developed an allergy that includes: 158 | * **total**: The total city population that has developed an allergy 159 | * **percentage**: The percentage of a city population that has developed an allergy 160 | * **ages**: A list of the ages of a city population that has developed an allergy 161 | * **stats**: Minimum, maximum, and mean stats for the following: 162 | > NOTE: Stats are broken down by total and percentage, because they may not both refer to the same city 163 | 164 | * **outgrown**: A list of different allergies outgrown and their respective stats 165 | * **developed**: A list of different allergies developed and their respective stats 166 | 167 | # Visuals 168 | 169 | * [Map](#map) 170 | * [Treemap](#treemap) 171 | * [Bar Chart](#bar-chart) 172 | 173 | ![](../readme_images/screenshot.png) 174 | 175 | ## Map 176 | 177 | ![](../readme_images/map.png) 178 | 179 | The map is a [Mapbox](https://www.mapbox.com/) map that dsiplays the analytics data in the form of a [heatmap](https://docs.mapbox.com/mapbox-gl-js/example/heatmap-layer/). The number in parenthesis refers to the data for that city. The heat intensity when viewing `total` refers to the number of people for the selected data source. For `percentage`, the heat intensity refers to the percentage of people for the selected data source. For more information on the data you are viewing, hover over the information icons next to `total` and `percentage`. 180 | 181 | ## Treemap 182 | 183 | ![](../readme_images/treemap.png) 184 | 185 | The [treemap](https://developers.google.com/chart/interactive/docs/gallery/treemap) displays the analytics data broken down by the following layers: State, County, and City. The size and color of each element when viewing `total` refers to the number of people for the selected data source. For `percentage`, the size and color refers to the percentage of people for the selected data source. For more information on the data you are viewing, hover over the information icons next to `total` and `percentage`. In addition, hovering over each element on the treemap shows the data associated with that element. 186 | 187 | ## Bar Chart 188 | 189 | ![](../readme_images/bar_chart.png) 190 | 191 | The [bar chart](https://www.chartjs.org/docs/latest/charts/bar.html) is only viewable when the data source is developing/outgrowing allergies. The chart shows the frequency of the ages of people that developed/outgrew an allergy. The age ranges are broken down into 5 year periods. 192 | 193 | # License 194 | 195 | This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1](https://developercertificate.org/) and the [Apache License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.txt). 196 | 197 | [Apache License FAQ](https://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN) 198 | -------------------------------------------------------------------------------- /web/app.js: -------------------------------------------------------------------------------- 1 | /** 2 | ############################################################################## 3 | # Copyright 2019 IBM Corp. All Rights Reserved. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | ############################################################################## 17 | * Main app.js file for web app of Example Health Analytics 18 | */ 19 | 20 | var createError = require('http-errors'); 21 | var express = require('express'); 22 | var bodyParser = require('body-parser'); 23 | var logger = require('morgan'); 24 | 25 | var app = express(); 26 | 27 | var indexRouter = require('./routes/index'); 28 | 29 | app.use(logger('dev')); 30 | 31 | app.use(express.static('./public')); // load UI from public folder 32 | app.use(bodyParser.json()) 33 | 34 | app.use('/data', indexRouter); 35 | 36 | // catch 404 and forward to error handler 37 | app.use(function(req, res, next) { 38 | next(createError(404)); 39 | }); 40 | 41 | // error handler 42 | app.use(function(err, req, res, next) { 43 | // set locals, only providing error in development 44 | res.locals.message = err.message; 45 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 46 | 47 | res.status(err.status || 500); 48 | }); 49 | 50 | module.exports = app; 51 | -------------------------------------------------------------------------------- /web/bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | ############################################################################## 5 | # Copyright 2019 IBM Corp. All Rights Reserved. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | ############################################################################## 19 | * Server Initialization 20 | */ 21 | 22 | /** 23 | * Module dependencies. 24 | */ 25 | 26 | var app = require('../app'); 27 | var debug = require('debug')('web:server'); 28 | var http = require('http'); 29 | 30 | /** 31 | * Get port from environment and store in Express. 32 | */ 33 | 34 | var port = normalizePort(process.env.PORT || '4000'); 35 | app.set('port', port); 36 | 37 | /** 38 | * Create HTTP server. 39 | */ 40 | 41 | var server = http.createServer(app); 42 | 43 | /** 44 | * Listen on provided port, on all network interfaces. 45 | */ 46 | 47 | server.listen(port); 48 | server.on('error', onError); 49 | server.on('listening', onListening); 50 | 51 | /** 52 | * Normalize a port into a number, string, or false. 53 | */ 54 | 55 | function normalizePort(val) { 56 | var port = parseInt(val, 10); 57 | 58 | if (isNaN(port)) { 59 | // named pipe 60 | return val; 61 | } 62 | 63 | if (port >= 0) { 64 | // port number 65 | return port; 66 | } 67 | 68 | return false; 69 | } 70 | 71 | /** 72 | * Event listener for HTTP server "error" event. 73 | */ 74 | 75 | function onError(error) { 76 | if (error.syscall !== 'listen') { 77 | throw error; 78 | } 79 | 80 | var bind = typeof port === 'string' 81 | ? 'Pipe ' + port 82 | : 'Port ' + port; 83 | 84 | // handle specific listen errors with friendly messages 85 | switch (error.code) { 86 | case 'EACCES': 87 | console.error(bind + ' requires elevated privileges'); 88 | process.exit(1); 89 | break; 90 | case 'EADDRINUSE': 91 | console.error(bind + ' is already in use'); 92 | process.exit(1); 93 | break; 94 | default: 95 | throw error; 96 | } 97 | } 98 | 99 | /** 100 | * Event listener for HTTP server "listening" event. 101 | */ 102 | 103 | function onListening() { 104 | var addr = server.address(); 105 | var bind = typeof addr === 'string' 106 | ? 'pipe ' + addr 107 | : 'port ' + addr.port; 108 | debug('Listening on ' + bind); 109 | } 110 | -------------------------------------------------------------------------------- /web/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web", 3 | "version": "0.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "chart.js": "^2.7.3", 10 | "cookie-parser": "~1.4.3", 11 | "debug": "~2.6.9", 12 | "express": "~4.16.0", 13 | "http-errors": "~1.6.2", 14 | "jade": "~1.11.0", 15 | "morgan": "~1.9.0", 16 | "request": "^2.88.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /web/public/images/examplelogo@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/example-health-analytics/427dc273f4528b4c2ce97935acd59ed182608cbc/web/public/images/examplelogo@2x.png -------------------------------------------------------------------------------- /web/public/images/logo.svg: -------------------------------------------------------------------------------- 1 | logo -------------------------------------------------------------------------------- /web/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Example Health Analytics 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 31 | 32 |
33 | 34 |
35 |
36 |
37 |
38 |
Data Source
39 | 40 |
41 | 47 |
48 |
49 |
50 |
51 | 56 | 60 | 65 |
66 | 85 |
86 |
87 | 88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 | 97 |
98 |
99 |
100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /web/public/javascripts/chartHandler.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Manages and updates Chart.js and Google charts with data from the datalake 3 | */ 4 | 5 | /** 6 | * Updates Tree Chart with the data that is associated with the map 7 | * 8 | * @param {[String]} chartLabels 9 | * @param {[[String,String,String,Number]]} chartData 10 | * @param {String} dataValueType 11 | */ 12 | function updateMapChart(chartLabels, chartData, dataValueType) { 13 | google.charts.load("current", { 14 | "packages": ["treemap"] 15 | }); 16 | google.charts.setOnLoadCallback(drawChart); 17 | 18 | function drawChart() { 19 | 20 | var dataForDataTable = [chartLabels, ["United States", null, 0]]; 21 | var countiesInData = []; 22 | var statesInData = []; 23 | 24 | for (var city = 0; city < chartData.length; city++) { 25 | if (!statesInData.includes(chartData[city][2])) { 26 | statesInData.push(chartData[city][2]); 27 | dataForDataTable.push([chartData[city][2], "United States", 0]); 28 | } 29 | 30 | if (!countiesInData.includes(chartData[city][1])) { 31 | countiesInData.push(chartData[city][1]); 32 | 33 | if (chartData[city][1] == chartData[city][0]) { 34 | dataForDataTable.push([chartData[city][1], chartData[city][2], chartData[city][3]]); 35 | } else { 36 | dataForDataTable.push([chartData[city][1], chartData[city][2], 0]); 37 | } 38 | } 39 | 40 | if (chartData[city][1] != chartData[city][0]) { 41 | dataForDataTable.push([chartData[city][0], chartData[city][1], chartData[city][3]]); 42 | } 43 | } 44 | 45 | if (statesInData.length == 1) { 46 | dataForDataTable.splice(1,1); 47 | dataForDataTable[1][1] = null; 48 | } 49 | 50 | var data = google.visualization.arrayToDataTable(dataForDataTable); 51 | 52 | tree = new google.visualization.TreeMap(document.getElementById("mapChart")); 53 | 54 | tree.draw(data, { 55 | 56 | minColor: '#CCEEF2', 57 | midColor: '#00ABC0', 58 | maxColor: '#F88F58', 59 | headerHeight: 15, 60 | fontColor: "black", 61 | showScale: true, 62 | generateTooltip: showFullTooltip 63 | }); 64 | 65 | function showFullTooltip(row, size, value) { 66 | switch (dataValueType) { 67 | case "total": 68 | return "
" + 69 | "" + data.getValue(row, 0) + "
" + 70 | data.getColumnLabel(2) + ": " + size + "
"; 71 | 72 | break; 73 | case "percentage": 74 | if (data.getValue(row, 1) && data.getValue(row, 1).includes("County")) { 75 | return "
" + 76 | "" + data.getValue(row, 0) + "
" + 77 | data.getColumnLabel(2) + ": " + size + "
"; 78 | } else { 79 | return "
" + 80 | "" + data.getValue(row, 0) + ""; 81 | } 82 | 83 | break; 84 | default: 85 | } 86 | } 87 | 88 | } 89 | 90 | } 91 | 92 | /** 93 | * Updates bar chart for age of developing/outgrowing allergies data 94 | * 95 | * @param {[Number]} chartData 96 | * @param {String} dataLabel 97 | */ 98 | function updateAgeChart(chartData, dataLabel) { 99 | removeAgeChart(); 100 | 101 | var result = getAgeChartData(chartData); 102 | var chartLabels = result[0]; 103 | chartData = result[1]; 104 | 105 | var chartDiv = document.getElementById("chart"); 106 | var newCanvas = document.createElement('canvas'); 107 | newCanvas.setAttribute('id', 'ageChart'); 108 | chartDiv.appendChild(newCanvas); 109 | var ctx = document.getElementById("ageChart").getContext('2d'); 110 | var myChart = new Chart(ctx, { 111 | type: 'bar', 112 | data: { 113 | labels: chartLabels, 114 | datasets: [{ 115 | label: dataLabel, 116 | data: chartData, 117 | borderWidth: 1, 118 | backgroundColor: '#CCEEF2' 119 | }] 120 | }, 121 | options: { 122 | scales: { 123 | yAxes: [{ 124 | ticks: { 125 | beginAtZero: true 126 | } 127 | }], 128 | xAxes: [{ 129 | scaleLabel: { 130 | display: true, 131 | labelString: 'Ages' 132 | } 133 | }] 134 | } 135 | } 136 | }); 137 | } 138 | 139 | /** 140 | * Removes age chart from web page 141 | */ 142 | function removeAgeChart() { 143 | var ageChart = document.getElementById("ageChart"); 144 | if (ageChart) { 145 | ageChart.remove(); 146 | } 147 | } 148 | 149 | /** 150 | * Gets frequency of ages from the data by age ranges 151 | * 152 | * @param {[Number]} chartData 153 | */ 154 | function getAgeChartData(chartData) { 155 | 156 | var labels = []; 157 | var data = []; 158 | 159 | for (var label = 0; label < 29; label++) { 160 | labels.push((5*label) + "-" + (5*label + 4)); 161 | data.push(0); 162 | } 163 | 164 | for (var age = 0; age < chartData.length; age++) { 165 | var index = Math.floor(chartData[age] / 5); 166 | data[index] = data[index] + 1; 167 | } 168 | 169 | for (var index = 28; index >= 0; index--) { 170 | if (data[index] != 0) { 171 | break; 172 | } 173 | } 174 | 175 | if (index != 28) { 176 | labels.splice(index+1, 28 - index); 177 | data.splice(index+1, 28 - index); 178 | } 179 | 180 | return [labels, data]; 181 | } 182 | 183 | /** 184 | * Updates statistics that are associated with the data from the map 185 | * 186 | * @param {String} dataType 187 | * @param {Object} data 188 | * @param {String} allergy 189 | * @param {String} dataValueType 190 | */ 191 | function updateStats(dataType, data, allergy, dataValueType) { 192 | var titleDiv = document.getElementById('statTitle'); 193 | var minDiv = document.getElementById('min'); 194 | var maxDiv = document.getElementById('max'); 195 | var meanDiv = document.getElementById('mean'); 196 | var totalTooltip = document.getElementById('totalTooltiptext'); 197 | var populationTooltip = document.getElementById('percentageTooltiptext'); 198 | 199 | switch (dataType) { 200 | case "population": 201 | totalTooltip.innerHTML = "Total population by city"; 202 | populationTooltip.innerHTML = "City's percentage of total population"; 203 | titleDiv.innerHTML = "Population stats"; 204 | 205 | switch (dataValueType) { 206 | case "total": 207 | minDiv.innerHTML = "Min: " + data.populationStats.min.population + " (" + data.populationStats.min.city + ", " + data.populationStats.min.state + ")"; 208 | maxDiv.innerHTML = "Max: " + data.populationStats.max.population + " (" + data.populationStats.max.city + ", " + data.populationStats.max.state + ")"; 209 | meanDiv.innerHTML = "Mean: " + Math.round(data.populationStats.mean * 100) / 100; 210 | 211 | break; 212 | case "percentage": 213 | var min = data.populationStats.min.percentage*100; 214 | var max = data.populationStats.max.percentage*100; 215 | var mean = (1/data.populationStats.cities.length)*100; 216 | 217 | minDiv.innerHTML = "Min: " + Math.round(min * 100) / 100 + "% (" + data.populationStats.min.city + ", " + data.populationStats.min.state + ")"; 218 | maxDiv.innerHTML = "Max: " + Math.round(max * 100) / 100 + "% (" + data.populationStats.max.city + ", " + data.populationStats.max.state + ")"; 219 | meanDiv.innerHTML = "Mean: " + Math.round(mean * 100) / 100 + "%"; 220 | 221 | break; 222 | default: 223 | } 224 | 225 | break; 226 | case "developed": 227 | totalTooltip.innerHTML = "Population by city that developed " + allergy + " allergy"; 228 | populationTooltip.innerHTML = "Percentage of each city that developed " + allergy + " allergy"; 229 | titleDiv.innerHTML = "Developing allergy stats for cities with " + allergy + " allergy"; 230 | 231 | switch (dataValueType) { 232 | case "total": 233 | for (let i = 0; i < data.allergyStats.stats.developed.length; i++) { 234 | if (allergy == data.allergyStats.stats.developed[i].allergy) { 235 | minDiv.innerHTML = "Min: " + data.allergyStats.stats.developed[i].min.total.min + " (" + data.allergyStats.stats.developed[i].min.total.city + ", " + data.allergyStats.stats.developed[i].min.total.state + ")"; 236 | maxDiv.innerHTML = "Max: " + data.allergyStats.stats.developed[i].max.total.max + " (" + data.allergyStats.stats.developed[i].max.total.city + ", " + data.allergyStats.stats.developed[i].max.total.state + ")"; 237 | meanDiv.innerHTML = "Mean: " + Math.round(data.allergyStats.stats.developed[i].mean.total * 100) / 100; 238 | } 239 | } 240 | 241 | break; 242 | case "percentage": 243 | for (let i = 0; i < data.allergyStats.stats.developed.length; i++) { 244 | if (allergy == data.allergyStats.stats.developed[i].allergy) { 245 | var min = data.allergyStats.stats.developed[i].min.percentage.min*100; 246 | var max = data.allergyStats.stats.developed[i].max.percentage.max*100; 247 | var mean = data.allergyStats.stats.developed[i].mean.percentage*100; 248 | 249 | minDiv.innerHTML = "Min: " + Math.round(min * 100) / 100 + "% (" + data.allergyStats.stats.developed[i].min.percentage.city + ", " + data.allergyStats.stats.developed[i].min.percentage.state + ")"; 250 | maxDiv.innerHTML = "Max: " + Math.round(max * 100) / 100 + "% (" + data.allergyStats.stats.developed[i].max.percentage.city + ", " + data.allergyStats.stats.developed[i].max.percentage.state + ")"; 251 | meanDiv.innerHTML = "Mean: " + Math.round(mean * 100) / 100 + "%"; 252 | } 253 | } 254 | 255 | break; 256 | default: 257 | } 258 | 259 | break; 260 | case "outgrown": 261 | totalTooltip.innerHTML = "Population by city that outgrew " + allergy + " allergy"; 262 | populationTooltip.innerHTML = "Percentage of each city outgrew " + allergy + " allergy"; 263 | titleDiv.innerHTML = "Outgrowing allergy stats for cities with " + allergy + " allergy"; 264 | 265 | switch (dataValueType) { 266 | case "total": 267 | for (let i = 0; i < data.allergyStats.stats.outgrown.length; i++) { 268 | if (allergy == data.allergyStats.stats.outgrown[i].allergy) { 269 | minDiv.innerHTML = "Min: " + data.allergyStats.stats.outgrown[i].min.total.min + " (" + data.allergyStats.stats.outgrown[i].min.total.city + ", " + data.allergyStats.stats.outgrown[i].min.total.state + ")"; 270 | maxDiv.innerHTML = "Max: " + data.allergyStats.stats.outgrown[i].max.total.max + " (" + data.allergyStats.stats.outgrown[i].max.total.city + ", " + data.allergyStats.stats.outgrown[i].max.total.state + ")"; 271 | meanDiv.innerHTML = "Mean: " + Math.round(data.allergyStats.stats.outgrown[i].mean.total * 100) / 100; 272 | } 273 | } 274 | 275 | break; 276 | case "percentage": 277 | for (let i = 0; i < data.allergyStats.stats.outgrown.length; i++) { 278 | if (allergy == data.allergyStats.stats.outgrown[i].allergy) { 279 | var min = data.allergyStats.stats.outgrown[i].min.percentage.min*100; 280 | var max = data.allergyStats.stats.outgrown[i].max.percentage.max*100; 281 | var mean = data.allergyStats.stats.outgrown[i].mean.percentage*100; 282 | 283 | minDiv.innerHTML = "Min: " + Math.round(min * 100) / 100 + "% (" + data.allergyStats.stats.outgrown[i].min.percentage.city + ", " + data.allergyStats.stats.outgrown[i].min.percentage.state + ")"; 284 | maxDiv.innerHTML = "Max: " + Math.round(max * 100) / 100 + "% (" + data.allergyStats.stats.outgrown[i].max.percentage.city + ", " + data.allergyStats.stats.outgrown[i].max.percentage.state + ")"; 285 | meanDiv.innerHTML = "Mean: " + Math.round(mean * 100) / 100 + "%"; 286 | } 287 | } 288 | 289 | break; 290 | default: 291 | } 292 | 293 | break; 294 | default: 295 | } 296 | } 297 | -------------------------------------------------------------------------------- /web/public/javascripts/dataHandler.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Responsible for getting and handling data 3 | */ 4 | 5 | /** 6 | * Updates data type in storage and reloads map/charts with data based on the data type 7 | * 8 | * @param {Object} dataSelection 9 | */ 10 | function updateDataType(dataSelection) { 11 | var dataTypes = ["population", "developed", "outgrown"]; 12 | 13 | if(dataSelection) { 14 | setSessionStorage("dataType", dataTypes[dataSelection.selectedIndex]); 15 | } else { 16 | if (!getSessionStorage("dataType")) { 17 | setSessionStorage("dataType", dataTypes[0]); 18 | } 19 | } 20 | 21 | load(); 22 | } 23 | 24 | /** 25 | * Wrapper for getting session storage value 26 | * 27 | * @param {String} key 28 | */ 29 | function getSessionStorage(key) { 30 | return sessionStorage.getItem(key); 31 | } 32 | 33 | /** 34 | * Wrapper for setting session storage key/value pair 35 | * 36 | * @param {String} key 37 | * @param {*} value 38 | */ 39 | function setSessionStorage(key,value) { 40 | sessionStorage.setItem(key, value); 41 | } 42 | 43 | /** 44 | * Gets analytics data to be processed by front end 45 | * 46 | * @param {String} dataType 47 | * @param {String} allergy 48 | * @param {String} dataValueType 49 | */ 50 | getData = async(dataType, allergy, dataValueType) => { 51 | 52 | return new Promise(function(resolve, reject) { 53 | var url = "./data"; 54 | 55 | var http = new XMLHttpRequest(); 56 | 57 | http.open("GET", url, true); 58 | 59 | http.onreadystatechange = function() 60 | { 61 | if(http.readyState == 4 && (http.status == 200 || http.status == 304)) { 62 | var data = JSON.parse(http.responseText); 63 | 64 | processData(dataType, data, allergy, dataValueType).then(mapData => { 65 | updateStats(dataType, data, allergy, dataValueType); 66 | resolve(mapData); 67 | }) 68 | } 69 | } 70 | http.send(null); 71 | }) 72 | } 73 | 74 | /** 75 | * Processes analytics data for mapbox map and updates charts with processed data 76 | * 77 | * @param {Stirng} dataType 78 | * @param {Stirng} data 79 | * @param {Stirng} allergy 80 | * @param {Stirng} dataValueType 81 | */ 82 | processData = async(dataType, data, allergy, dataValueType) => { 83 | 84 | mapData = [data.mapboxAccessToken]; 85 | 86 | var mapChartLabels = ['Location', 'Parent']; 87 | var mapChartData = []; 88 | var ageChartData = []; 89 | 90 | switch (dataType) { 91 | case "population": 92 | for (let city = 0; city < data.populationStats.cities.length; city++) { 93 | 94 | getCoordinates(data.mapboxAccessToken, data.populationStats.cities[city].city, data.populationStats.cities[city].state).then(coordinateData => { 95 | getCounty(data.populationStats.cities[city].city, data.populationStats.cities[city].state, coordinateData).then(county => { 96 | 97 | switch (dataValueType) { 98 | case "total": 99 | mapData.push({ 100 | "type": "Feature", 101 | "geometry": { 102 | "type": "Point", 103 | "coordinates": coordinateData 104 | }, 105 | "properties": { 106 | "title": data.populationStats.cities[city].city + " (" + data.populationStats.cities[city].population + ")", 107 | "data": data.populationStats.cities[city].population 108 | } 109 | }) 110 | 111 | mapChartData.push([data.populationStats.cities[city].city, 112 | county, 113 | data.populationStats.cities[city].state, 114 | data.populationStats.cities[city].population]); 115 | 116 | if (mapChartData.length == data.populationStats.cities.length) { 117 | mapChartLabels.push("population"); 118 | mapChartData = fixDuplicates(mapChartData); 119 | updateMapChart(mapChartLabels, mapChartData, dataValueType); 120 | removeAgeChart(); 121 | } 122 | 123 | break; 124 | case "percentage": 125 | var percentageToRound = data.populationStats.cities[city].percentage*100; 126 | var roundedPercentage = Math.round(percentageToRound * 100) / 100; 127 | 128 | mapData.push({ 129 | "type": "Feature", 130 | "geometry": { 131 | "type": "Point", 132 | "coordinates": coordinateData 133 | }, 134 | "properties": { 135 | "title": data.populationStats.cities[city].city + " (" + roundedPercentage + "%)", 136 | "data": roundedPercentage 137 | } 138 | }) 139 | 140 | mapChartData.push([data.populationStats.cities[city].city, 141 | county, 142 | data.populationStats.cities[city].state, 143 | roundedPercentage]); 144 | 145 | if (mapChartData.length == data.populationStats.cities.length) { 146 | mapChartLabels.push("% of population"); 147 | mapChartData = fixDuplicates(mapChartData); 148 | updateMapChart(mapChartLabels, mapChartData, dataValueType); 149 | removeAgeChart(); 150 | } 151 | 152 | break; 153 | default: 154 | } 155 | }) 156 | }) 157 | } 158 | break; 159 | case "developed": 160 | for (let city = 0; city < data.allergyStats.cities.length; city++) { 161 | 162 | getCoordinates(data.mapboxAccessToken, data.allergyStats.cities[city].city, data.allergyStats.cities[city].state).then(coordinateData => { 163 | getCounty(data.allergyStats.cities[city].city, data.allergyStats.cities[city].state, coordinateData).then(county => { 164 | 165 | var hasAllergy = false; 166 | 167 | switch (dataValueType) { 168 | case "total": 169 | for (let allergyIndex = 0; allergyIndex < data.allergyStats.cities[city].allergies.length; allergyIndex++) { 170 | if (allergy == data.allergyStats.cities[city].allergies[allergyIndex].allergy) { 171 | hasAllergy = true; 172 | 173 | mapData.push({ 174 | "type": "Feature", 175 | "geometry": { 176 | "type": "Point", 177 | "coordinates": coordinateData 178 | }, 179 | "properties": { 180 | "title": data.allergyStats.cities[city].city + " (" + data.allergyStats.cities[city].allergies[allergyIndex].developed.total +")", 181 | "data": data.allergyStats.cities[city].allergies[allergyIndex].developed.total 182 | } 183 | }) 184 | 185 | mapChartData.push([data.allergyStats.cities[city].city, 186 | county, 187 | data.allergyStats.cities[city].state, 188 | data.allergyStats.cities[city].allergies[allergyIndex].developed.total]); 189 | ageChartData.push.apply(ageChartData,data.allergyStats.cities[city].allergies[allergyIndex].developed.ages); 190 | } 191 | } 192 | 193 | if (!hasAllergy) { 194 | mapData.push({ 195 | "type": "Feature", 196 | "geometry": { 197 | "type": "Point", 198 | "coordinates": coordinateData 199 | }, 200 | "properties": { 201 | "title": data.allergyStats.cities[city].city + " (0)", 202 | "data": 0 203 | } 204 | }) 205 | 206 | mapChartData.push([data.allergyStats.cities[city].city, 207 | county, 208 | data.allergyStats.cities[city].state, 209 | 0]); 210 | } 211 | 212 | if (mapChartData.length == data.allergyStats.cities.length) { 213 | mapChartLabels.push("Total number of " + allergy + " allergy"); 214 | mapChartData = fixDuplicates(mapChartData); 215 | updateMapChart(mapChartLabels, mapChartData, dataValueType); 216 | updateAgeChart(ageChartData, "Frequency of age developed " + allergy + " allergy"); 217 | } 218 | 219 | break; 220 | case "percentage": 221 | for (let allergyIndex = 0; allergyIndex < data.allergyStats.cities[city].allergies.length; allergyIndex++) { 222 | if (allergy == data.allergyStats.cities[city].allergies[allergyIndex].allergy) { 223 | hasAllergy = true; 224 | 225 | var percentageToRound = data.allergyStats.cities[city].allergies[allergyIndex].developed.percentage*100; 226 | var roundedPercentage = Math.round(percentageToRound * 100) / 100; 227 | 228 | mapData.push({ 229 | "type": "Feature", 230 | "geometry": { 231 | "type": "Point", 232 | "coordinates": coordinateData 233 | }, 234 | "properties": { 235 | "title": data.allergyStats.cities[city].city + " (" + roundedPercentage +"%)", 236 | "data": roundedPercentage 237 | } 238 | }) 239 | 240 | mapChartData.push([data.allergyStats.cities[city].city, 241 | county, 242 | data.allergyStats.cities[city].state, 243 | roundedPercentage]); 244 | ageChartData.push.apply(ageChartData,data.allergyStats.cities[city].allergies[allergyIndex].developed.ages); 245 | } 246 | } 247 | 248 | if (!hasAllergy) { 249 | mapData.push({ 250 | "type": "Feature", 251 | "geometry": { 252 | "type": "Point", 253 | "coordinates": coordinateData 254 | }, 255 | "properties": { 256 | "title": data.allergyStats.cities[city].city + " (0%)", 257 | "data": 0 258 | } 259 | }) 260 | 261 | mapChartData.push([data.allergyStats.cities[city].city, 262 | county, 263 | data.allergyStats.cities[city].state, 264 | 0]); 265 | } 266 | 267 | if (mapChartData.length == data.allergyStats.cities.length) { 268 | mapChartLabels.push("% of " + allergy + " allergy"); 269 | mapChartData = fixDuplicates(mapChartData); 270 | updateMapChart(mapChartLabels, mapChartData, dataValueType); 271 | updateAgeChart(ageChartData, "Frequency of age developed " + allergy + " allergy"); 272 | } 273 | 274 | break; 275 | default: 276 | } 277 | }) 278 | }) 279 | } 280 | break; 281 | case "outgrown": 282 | for (let city = 0; city < data.allergyStats.cities.length; city++) { 283 | 284 | getCoordinates(data.mapboxAccessToken, data.allergyStats.cities[city].city, data.allergyStats.cities[city].state).then(coordinateData => { 285 | getCounty(data.allergyStats.cities[city].city, data.allergyStats.cities[city].state, coordinateData).then(county => { 286 | 287 | var hasOutgrown = false; 288 | 289 | switch (dataValueType) { 290 | case "total": 291 | for (let allergyIndex = 0; allergyIndex < data.allergyStats.cities[city].allergies.length; allergyIndex++) { 292 | if (allergy == data.allergyStats.cities[city].allergies[allergyIndex].allergy) { 293 | hasOutgrown = true; 294 | 295 | mapData.push({ 296 | "type": "Feature", 297 | "geometry": { 298 | "type": "Point", 299 | "coordinates": coordinateData 300 | }, 301 | "properties": { 302 | "title": data.allergyStats.cities[city].city + " (" + data.allergyStats.cities[city].allergies[allergyIndex].outgrown.total +")", 303 | "data": data.allergyStats.cities[city].allergies[allergyIndex].outgrown.total 304 | } 305 | }) 306 | 307 | mapChartData.push([data.allergyStats.cities[city].city, 308 | county, 309 | data.allergyStats.cities[city].state, 310 | data.allergyStats.cities[city].allergies[allergyIndex].outgrown.total]); 311 | ageChartData.push.apply(ageChartData,data.allergyStats.cities[city].allergies[allergyIndex].outgrown.ages); 312 | } 313 | } 314 | 315 | if (!hasOutgrown) { 316 | mapData.push({ 317 | "type": "Feature", 318 | "geometry": { 319 | "type": "Point", 320 | "coordinates": coordinateData 321 | }, 322 | "properties": { 323 | "title": data.allergyStats.cities[city].city + " (0)", 324 | "data": 0 325 | 326 | } 327 | }) 328 | 329 | mapChartData.push([data.allergyStats.cities[city].city, 330 | county, 331 | data.allergyStats.cities[city].state, 332 | 0]); 333 | } 334 | 335 | if (mapChartData.length == data.allergyStats.cities.length) { 336 | mapChartLabels.push("Total number of outgrowing " + allergy + " allergy"); 337 | mapChartData = fixDuplicates(mapChartData); 338 | updateMapChart(mapChartLabels, mapChartData, dataValueType); 339 | updateAgeChart(ageChartData, "Frequency of age outgrowing " + allergy + " allergy"); 340 | } 341 | 342 | break; 343 | case "percentage": 344 | for (let allergyIndex = 0; allergyIndex < data.allergyStats.cities[city].allergies.length; allergyIndex++) { 345 | if (allergy == data.allergyStats.cities[city].allergies[allergyIndex].allergy) { 346 | hasOutgrown = true; 347 | 348 | var percentageToRound = data.allergyStats.cities[city].allergies[allergyIndex].outgrown.percentage*100; 349 | var roundedPercentage = Math.round(percentageToRound * 100) / 100; 350 | 351 | mapData.push({ 352 | "type": "Feature", 353 | "geometry": { 354 | "type": "Point", 355 | "coordinates": coordinateData 356 | }, 357 | "properties": { 358 | "title": data.allergyStats.cities[city].city + " (" + roundedPercentage +"%)", 359 | "data": roundedPercentage 360 | } 361 | }) 362 | 363 | mapChartData.push([data.allergyStats.cities[city].city, 364 | county, 365 | data.allergyStats.cities[city].state, 366 | roundedPercentage]); 367 | ageChartData.push.apply(ageChartData,data.allergyStats.cities[city].allergies[allergyIndex].outgrown.ages); 368 | } 369 | } 370 | 371 | if (!hasOutgrown) { 372 | mapData.push({ 373 | "type": "Feature", 374 | "geometry": { 375 | "type": "Point", 376 | "coordinates": coordinateData 377 | }, 378 | "properties": { 379 | "title": data.allergyStats.cities[city].city + " (0%)", 380 | "data": 0 381 | 382 | } 383 | }) 384 | 385 | mapChartData.push([data.allergyStats.cities[city].city, 386 | county, 387 | data.allergyStats.cities[city].state, 388 | 0]); 389 | } 390 | 391 | if (mapChartData.length == data.allergyStats.cities.length) { 392 | mapChartLabels.push("% of outgrowing " + allergy + " allergy"); 393 | mapChartData = fixDuplicates(mapChartData); 394 | updateMapChart(mapChartLabels, mapChartData, dataValueType); 395 | updateAgeChart(ageChartData, "Frequency of age outgrowing " + allergy + " allergy"); 396 | } 397 | 398 | break; 399 | default: 400 | } 401 | }) 402 | }) 403 | } 404 | 405 | break; 406 | default: 407 | } 408 | 409 | return mapData; 410 | } 411 | 412 | /** 413 | * Gets longitude,latitude coordinates for a city 414 | * 415 | * @param {Stirng} city 416 | * @param {Stirng} state 417 | */ 418 | getCoordinates = async(mapboxAccessToken, city, state) => { 419 | return new Promise(function(resolve, reject) { 420 | if (getSessionStorage(city+"("+state+")Coords")) { 421 | var coordinateArray = getSessionStorage(city+"("+state+")Coords").split(","); 422 | resolve(coordinateArray); 423 | } else { 424 | var updatedCity = city + " " + state; 425 | 426 | var accessToken = mapboxAccessToken; 427 | var url = "https://api.mapbox.com/geocoding/v5/mapbox.places/" + updatedCity.replace(/ /g, "%20") + ".json?access_token="; 428 | 429 | fetch(url+accessToken).then(response => { 430 | if (response.ok) { 431 | response.json().then(coordinateData => { 432 | setSessionStorage(city+"("+state+")Coords",coordinateData.features[0].geometry.coordinates); 433 | resolve(coordinateData.features[0].geometry.coordinates); 434 | }) 435 | } 436 | }) 437 | } 438 | }) 439 | } 440 | 441 | /** 442 | * Gets county for a city 443 | * 444 | * @param {Stirng} city 445 | * @param {[Number,Number]} coordinateData 446 | */ 447 | getCounty = async(city, state, coordinateData) => { 448 | return new Promise(function(resolve,reject) { 449 | if (getSessionStorage(city+"("+state+")County")) { 450 | var county = getSessionStorage(city+"("+state+")County"); 451 | resolve(county); 452 | } else { 453 | var url = "https://geo.fcc.gov/api/census/area?lat=" + coordinateData[1] + "&lon=" + coordinateData[0] + "&format=json"; 454 | 455 | fetch(url).then(response => { 456 | if (response.ok) { 457 | response.json().then(countyData => { 458 | setSessionStorage(city+"("+state+")County",countyData.results[0].county_name + " County"); 459 | resolve(countyData.results[0].county_name + " County"); 460 | }) 461 | } 462 | }) 463 | } 464 | }) 465 | } 466 | 467 | /** 468 | * Modifies duplicate city names and cities with the same name as states 469 | * 470 | * @param {[[String,String,String,Number]]} chartData 471 | */ 472 | function fixDuplicates(chartData) { 473 | var states = []; 474 | var counties = []; 475 | 476 | for (var city = 0; city < chartData.length; city++) { 477 | if (!states.includes(chartData[city][2])) { 478 | states.push(chartData[city][2]); 479 | } 480 | } 481 | 482 | for (var city = 0; city < chartData.length; city++) { 483 | for (var state = 0; state < states.length; state++) { 484 | if (states[state] == chartData[city][0]) { 485 | chartData[city][0] = chartData[city][0] + " (" + chartData[city][2] + ")"; 486 | } 487 | } 488 | } 489 | 490 | for (var city = 0; city < chartData.length - 1; city++) { 491 | for (var otherCity = city + 1; otherCity < chartData.length; otherCity++) { 492 | if (chartData[city][0] == chartData[otherCity][0]) { 493 | chartData[city][0] = chartData[city][0] + " (" + chartData[city][2] + ")"; 494 | chartData[otherCity][0] = chartData[otherCity][0] + " (" + chartData[otherCity][2] + ")"; 495 | } 496 | 497 | if (chartData[city][1] == chartData[otherCity][1] && chartData[city][2] != chartData[otherCity][2]) { 498 | counties.push(chartData[city][1]); 499 | } 500 | } 501 | } 502 | 503 | for (var city = 0; city < chartData.length; city++) { 504 | if (counties.includes(chartData[city][1])) { 505 | chartData[city][1] = chartData[city][1] + " (" + chartData[city][2] + ")"; 506 | } 507 | } 508 | 509 | return chartData; 510 | } 511 | 512 | /** 513 | * Initial function that loads data and renders web page 514 | */ 515 | function load() { 516 | 517 | document.getElementById("loader").style.visibility = "visible"; 518 | 519 | if (!sessionStorage.getItem("dataType")) { 520 | sessionStorage.setItem("dataType", "population"); // Default 521 | } 522 | 523 | var dataType = sessionStorage.getItem("dataType"); 524 | 525 | var dataValueTypeElement = document.getElementById("dataValueType"); 526 | var dataValueType = "total"; 527 | if (dataValueTypeElement.checked) { 528 | dataValueType = "percentage"; 529 | } 530 | 531 | var allergySelectDiv = document.getElementById("allergySelect"); 532 | var allergyElement = document.getElementById("allergy"); 533 | var allergy = allergyElement.options[allergyElement.selectedIndex].value; 534 | 535 | if (dataType == "population") { 536 | allergySelectDiv.style.visibility = "hidden"; 537 | } else { 538 | allergySelectDiv.style.visibility = "visible"; 539 | } 540 | 541 | updateMap(dataType, allergy, dataValueType).then(isUpdated => { 542 | document.getElementById("loader").style.visibility = "hidden"; 543 | }) 544 | } 545 | -------------------------------------------------------------------------------- /web/public/javascripts/mapHandler.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Manages and updates Mapbox map with data from the datalake 3 | */ 4 | 5 | /** 6 | * Updates map with data based on parameters 7 | * 8 | * @param {String} dataType 9 | * @param {String} allergy 10 | * @param {String} dataValueType 11 | */ 12 | function updateMap(dataType, allergy, dataValueType) { 13 | 14 | return new Promise(function(resolve,reject) { 15 | 16 | getData(dataType, allergy, dataValueType).then(mapData => { 17 | 18 | mapboxgl.accessToken = mapData[0]; 19 | 20 | var map = new mapboxgl.Map({ 21 | container: 'map', 22 | style: 'mapbox://styles/antonmc/cjr27i9iw0wbq2rnwtvkrbgog', 23 | center: [-110.5467, 46.0731], 24 | zoom: 2.5, 25 | minZoom: 2.5, 26 | maxZoom: 14 27 | }); 28 | 29 | map.on('load', function () { 30 | 31 | var heatmapMin = 1; 32 | var heatmapMax = 1; 33 | 34 | switch (dataValueType) { 35 | case "total": 36 | for (var data = 1; data < mapData.length; data++) { 37 | if (heatmapMax < mapData[data].properties.data) { 38 | heatmapMax = mapData[data].properties.data; 39 | } 40 | } 41 | 42 | break; 43 | case "percentage": 44 | heatmapMax = 100; 45 | break; 46 | default: 47 | } 48 | 49 | map.addLayer({ 50 | "id": "heat", 51 | "type": "heatmap", 52 | "source": { 53 | "type": "geojson", 54 | "data": { 55 | "type": "FeatureCollection", 56 | "features": mapData.slice(1) 57 | } 58 | }, 59 | "paint": { 60 | // Increase the heatmap weight based on frequency and property magnitude 61 | "heatmap-weight": [ 62 | "interpolate", 63 | ["linear"], 64 | ["get", "data"], 65 | 0, 0, 66 | heatmapMax, heatmapMin 67 | ], 68 | // Increase the heatmap color weight weight by zoom level 69 | // heatmap-intensity is a multiplier on top of heatmap-weight 70 | "heatmap-intensity": [ 71 | "interpolate", 72 | ["linear"], 73 | ["zoom"], 74 | 0, 1, 75 | 9, 3 76 | ], 77 | // Color ramp for heatmap. Domain is 0 (low) to 1 (high). 78 | // Begin color ramp at 0-stop with a 0-transparancy color 79 | // to create a blur-like effect. 80 | "heatmap-color": [ 81 | "interpolate", 82 | ["linear"], 83 | ["heatmap-density"], 84 | 0, "rgba(149,214,198,0)", 85 | 0.2, "rgb(252,210,188)", 86 | 0.4, "rgb(253,237,228)", 87 | 0.6, "rgb(253,219,199)", 88 | 0.8, "rgb(239,138,98)", 89 | 1, "rgb(249,143,88)" 90 | ], 91 | // Adjust the heatmap radius by zoom level 92 | "heatmap-radius": [ 93 | "interpolate", 94 | ["linear"], 95 | ["zoom"], 96 | 0, 2, 97 | 9, 100 98 | ], 99 | } 100 | }, 'waterway-label'); 101 | 102 | map.addLayer({ 103 | "id": "points", 104 | "type": "symbol", 105 | "source": { 106 | "type": "geojson", 107 | "data": { 108 | "type": "FeatureCollection", 109 | "features": mapData.slice(1) 110 | } 111 | }, 112 | "layout": { 113 | "text-field": "{title}", 114 | "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"], 115 | "text-anchor": "top" 116 | }, 117 | "paint": { 118 | "icon-opacity": [ 119 | "interpolate", 120 | ["linear"], 121 | ["zoom"], 122 | 7, 0, 123 | 8, 1 124 | ] 125 | } 126 | }); 127 | resolve(true); 128 | }); 129 | }); 130 | }) 131 | } 132 | -------------------------------------------------------------------------------- /web/public/javascripts/navbarHandler.js: -------------------------------------------------------------------------------- 1 | 2 | function toggleNav() { 3 | if (document.getElementById("navbar").style.width == "250px") { 4 | document.getElementById("navbar").style.width = "0"; 5 | document.getElementById("main").style.marginLeft= "0"; 6 | } else { 7 | document.getElementById("navbar").style.width = "250px"; 8 | document.getElementById("main").style.marginLeft = "250px"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /web/public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: 'IBM Plex Sans', sans-serif; 3 | margin: 0; 4 | padding: 0; 5 | height: 100%; 6 | color: #104D81; 7 | -webkit-font-smoothing: antialiased; 8 | /* -moz-osx-font-smoothing: grayscale; */ 9 | } 10 | 11 | a { 12 | color: #00B7FF; 13 | } 14 | 15 | .control { 16 | justify-content: space-between; 17 | align-items: center; 18 | display: flex; 19 | flex-direction: row; 20 | color: white; 21 | } 22 | 23 | .banner { 24 | height: 60px; 25 | width: 100%; 26 | background: #00ABC0; 27 | display: flex; 28 | flex-direction: row; 29 | align-items: center; 30 | align-content: center; 31 | } 32 | 33 | .brand { 34 | display: flex; 35 | flex-direction: row; 36 | align-items: center; 37 | margin-right: 15em; 38 | } 39 | 40 | .logo { 41 | height: 40px; 42 | width: 40px; 43 | margin: 10px; 44 | } 45 | 46 | .example { 47 | font-weight: medium; 48 | font-size: 16px; 49 | color: white; 50 | width: auto; 51 | } 52 | 53 | .main { 54 | height: 100%; 55 | width: 100%; 56 | justify-content: space-between; 57 | align-items: center; 58 | display: flex; 59 | flex-direction: row; 60 | } 61 | 62 | #map { 63 | position: relative; 64 | display: flex; 65 | width: 60%; 66 | height: 100vh; 67 | } 68 | 69 | .other { 70 | width: 40%; 71 | height: 100vh; 72 | border-left: 1px solid #99DDE5; 73 | display: flex; 74 | flex-direction: column; 75 | } 76 | 77 | .settings { 78 | width: 100%; 79 | display: flex; 80 | height: 50px; 81 | flex-direction: row; 82 | align-content: center; 83 | align-items: center; 84 | border-bottom: 1px solid #99DDE5; 85 | } 86 | 87 | .valuetoggle { 88 | margin: 10px; 89 | width: 50%; 90 | font-size: 14px; 91 | } 92 | 93 | .allergySelect { 94 | border-left: 1px solid #99DDE5; 95 | height: 100%; 96 | display: flex; 97 | background: #CCEEF2; 98 | width: 50%; 99 | flex-direction: row; 100 | align-items: center; 101 | justify-content: center; 102 | align-content: center; 103 | } 104 | 105 | .allergyCombo {} 106 | 107 | .switch { 108 | position: relative; 109 | display: inline-block; 110 | width: 50px; 111 | height: 20px; 112 | } 113 | 114 | .switch input { 115 | opacity: 0; 116 | width: 0; 117 | height: 0; 118 | } 119 | 120 | .allergylist { 121 | border: none; 122 | height: 30px; 123 | width: 90%; 124 | font-size: 14px; 125 | background: #CCEEF2; 126 | color: #104D81; 127 | } 128 | 129 | .datasource{ 130 | width:100%; 131 | } 132 | 133 | select:focus { 134 | outline: none; 135 | } 136 | 137 | .stats { 138 | padding: 10px; 139 | border-bottom: 1px solid #99DDE5; 140 | font-size: 14px; 141 | } 142 | 143 | .maptchart { 144 | /* margin: 10px; */ 145 | height: 50%; 146 | } 147 | 148 | .dataselection { 149 | display: flex; 150 | height: 50px; 151 | flex-direction: row; 152 | align-content: center; 153 | align-items: center; 154 | border-bottom: 1px solid #99DDE5; 155 | outline: none; 156 | } 157 | 158 | .datalabel { 159 | width: 100px; 160 | min-width: 100px; 161 | height: 100%; 162 | margin: 10px; 163 | border-right: 1px solid #99DDE5; 164 | display: flex; 165 | flex-direction: row; 166 | align-content: center; 167 | align-items: center; 168 | border-bottom: 1px solid #99DDE5; 169 | font-size: 14px; 170 | } 171 | 172 | #label { 173 | font-size: 14px; 174 | } 175 | 176 | .datasourcelist { 177 | width: 97%; 178 | border: none; 179 | font-size: 14px; 180 | background: white; 181 | color: #104D81; 182 | } 183 | 184 | .ageChart{ 185 | padding:10px; 186 | } 187 | 188 | .statTitle{ 189 | margin-bottom:5px; 190 | } 191 | 192 | .slider { 193 | position: absolute; 194 | cursor: pointer; 195 | top: 0; 196 | left: 0; 197 | right: 0; 198 | bottom: 0; 199 | background-color: #CCEEF2; 200 | -webkit-transition: .4s; 201 | transition: .4s; 202 | } 203 | 204 | .slider:before { 205 | position: absolute; 206 | content: ""; 207 | height: 12px; 208 | width: 12px; 209 | left: 4px; 210 | bottom: 4px; 211 | background-color: #00ABC0; 212 | -webkit-transition: .4s; 213 | transition: .4s; 214 | } 215 | 216 | input:checked+.slider:before { 217 | -webkit-transform: translateX(26px); 218 | -ms-transform: translateX(26px); 219 | transform: translateX(26px); 220 | } 221 | 222 | /* Rounded sliders */ 223 | .slider.round { 224 | border-radius: 34px; 225 | } 226 | 227 | .slider.round:before { 228 | border-radius: 50%; 229 | } 230 | 231 | .tooltip { 232 | position: relative; 233 | display: inline-block; 234 | } 235 | 236 | /* Tooltip text */ 237 | .tooltip .tooltiptext { 238 | visibility: hidden; 239 | width: 120px; 240 | bottom: 100%; 241 | left: 50%; 242 | margin-left: -60px; 243 | background-color: #00ABC0; 244 | color: white; 245 | text-align: center; 246 | padding: 5px 0; 247 | border-radius: 6px; 248 | 249 | /* Position the tooltip text - see examples below! */ 250 | position: absolute; 251 | z-index: 1; 252 | } 253 | 254 | .tooltip .tooltiptext::after { 255 | content: " "; 256 | position: absolute; 257 | top: 100%; /* At the bottom of the tooltip */ 258 | left: 50%; 259 | margin-left: -5px; 260 | border-width: 5px; 261 | border-style: solid; 262 | border-color: #00ABC0 transparent transparent transparent; 263 | } 264 | 265 | /* Show the tooltip text when you mouse over the tooltip container */ 266 | .tooltip:hover .tooltiptext { 267 | visibility: visible; 268 | } 269 | 270 | .loader { 271 | border: 16px solid #f3f3f3; 272 | border-radius: 50%; 273 | border-top: 16px solid #00ABC0; 274 | width: 50px; 275 | height: 50px; 276 | -webkit-animation: spin 2s linear infinite; /* Safari */ 277 | animation: spin 2s linear infinite; 278 | position:fixed; 279 | top: 50%; 280 | left: 50%; 281 | z-index: 100; 282 | visibility: hidden; 283 | } 284 | 285 | /* Safari */ 286 | @-webkit-keyframes spin { 287 | 0% { -webkit-transform: rotate(0deg); } 288 | 100% { -webkit-transform: rotate(360deg); } 289 | } 290 | 291 | @keyframes spin { 292 | 0% { transform: rotate(0deg); } 293 | 100% { transform: rotate(360deg); } 294 | } 295 | -------------------------------------------------------------------------------- /web/routes/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Router for home page 3 | */ 4 | 5 | var express = require('express'); 6 | var router = express.Router(); 7 | 8 | var analytics = require('../service/analytics'); 9 | 10 | const util = require('util'); 11 | 12 | router.get('/', function(req, res, next) { 13 | 14 | var getPopulationStats = analytics.getPopulationStats(); 15 | var getAllergyStats = analytics.getAllergyStats(); 16 | 17 | getPopulationStats.then(function(populationStats) { 18 | getAllergyStats.then(function(allergyStats) { 19 | //console.log(util.inspect({populationStats: populationStats, allergyStats: allergyStats}, false, null, true /* enable colors */)); 20 | 21 | res.send({mapboxAccessToken: process.env.MAPBOX_ACCESS_TOKEN, populationStats: populationStats, allergyStats: allergyStats}); 22 | }) 23 | }) 24 | }); 25 | 26 | module.exports = router; 27 | -------------------------------------------------------------------------------- /web/service/analytics.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Gets data from datalake using the data-service APIs and calculates analytics on the data 3 | */ 4 | 5 | var request = require('request'); 6 | var API_URL = process.env.DATA_SERVER || "http://localhost:3000"; 7 | 8 | /** 9 | * Gets the population of total patients 10 | */ 11 | function getPopulation() { 12 | return new Promise(function(resolve, reject) { 13 | request(API_URL + "/api/v1/population", function (error, response, body) { 14 | if (!error && response.statusCode == 200) { 15 | body = JSON.parse(body); 16 | var population = body["population"]; 17 | return resolve(population); 18 | } else { 19 | return resolve(0); 20 | } 21 | }); 22 | }) 23 | } 24 | 25 | /** 26 | * Gets the city allergy data of patients 27 | */ 28 | function getCities() { 29 | return new Promise(function(resolve, reject) { 30 | request(API_URL + "/api/v1/cities", function (error, response, body) { 31 | if (!error && response.statusCode == 200) { 32 | body = JSON.parse(body); 33 | var cities = body["cities"]; 34 | return resolve(cities); 35 | } else { 36 | return resolve(0); 37 | } 38 | }); 39 | }) 40 | } 41 | 42 | /** 43 | * Gets the list of allergies found in the cities 44 | */ 45 | function getAllergies() { 46 | return new Promise(function(resolve, reject) { 47 | request(API_URL + "/api/v1/allergies", function (error, response, body) { 48 | if (!error && response.statusCode == 200) { 49 | body = JSON.parse(body); 50 | var allergies = body["allergies"]; 51 | return resolve(allergies); 52 | } else { 53 | return resolve(0); 54 | } 55 | }); 56 | }) 57 | } 58 | 59 | /** 60 | * Creates JSON object for city population data and calculates min, max, and mean cities for population 61 | * See README.md for more information on structure of JSON object created 62 | */ 63 | function getPopulationStats() { 64 | return new Promise(function(resolve, reject) { 65 | 66 | getPopulation().then(function(population) { 67 | 68 | getCities().then(function(cities) { 69 | 70 | var populationStats = { 71 | cities: [], 72 | min: {}, 73 | max: {}, 74 | mean: 0 75 | } 76 | 77 | for (var city = 0; city < cities.length; city++) { 78 | 79 | populationStats.cities.push({city: cities[city].city, 80 | state: cities[city].state, 81 | percentage: cities[city].population/population, 82 | population: cities[city].population}); 83 | 84 | if (!populationStats.min.population || populationStats.min.population > cities[city].population) { 85 | populationStats.min = {city: cities[city].city, 86 | state: cities[city].state, 87 | percentage: cities[city].population/population, 88 | population: cities[city].population}; 89 | } 90 | 91 | if (!populationStats.max.population || populationStats.max.population < cities[city].population) { 92 | populationStats.max = {city: cities[city].city, 93 | state: cities[city].state, 94 | percentage: cities[city].population/population, 95 | population: cities[city].population}; 96 | } 97 | populationStats.mean = populationStats.mean + cities[city].population; 98 | } 99 | 100 | populationStats.mean = populationStats.mean / cities.length; 101 | 102 | resolve(populationStats); 103 | }) 104 | }) 105 | }) 106 | } 107 | 108 | /** 109 | * Creates JSON object for city allergy data and calculates min, max, and mean cities for developed allergies and allergies outgrown 110 | * See README.md for more information on structure of JSON object created 111 | */ 112 | function getAllergyStats() { 113 | return new Promise(function(resolve, reject) { 114 | 115 | getCities().then(function(cities) { 116 | 117 | getAllergies().then(function(allergies) { 118 | 119 | var allergyStats = { 120 | cities: [], 121 | stats: { 122 | outgrown: [], 123 | developed: [] 124 | } 125 | } 126 | 127 | var allergyCityTotals = []; 128 | 129 | for (var city = 0; city < cities.length; city++) { 130 | 131 | var currentCity = { 132 | city: cities[city].city, 133 | state: cities[city].state, 134 | allergies: [] 135 | }; 136 | 137 | 138 | for (var allergy = 0; allergy < cities[city].allergies.length; allergy++) { 139 | 140 | // DEVELOPED & OUTGROWN 141 | 142 | currentCity.allergies.push({ 143 | allergy: cities[city].allergies[allergy].allergy, 144 | type: cities[city].allergies[allergy].type, 145 | outgrown: {total: cities[city].allergies[allergy].outgrown.length, 146 | percentage: cities[city].allergies[allergy].outgrown.length / cities[city].allergies[allergy].developed.length, 147 | ages: cities[city].allergies[allergy].outgrown}, 148 | developed: {total: cities[city].allergies[allergy].developed.length, 149 | percentage: cities[city].allergies[allergy].developed.length / cities[city].population, 150 | ages: cities[city].allergies[allergy].developed} 151 | }); 152 | 153 | } 154 | 155 | // DEVELOPED 156 | 157 | for (var allergy = 0; allergy < allergies.length; allergy++) { 158 | var allergyInCity = false; 159 | var allergyInStats = false; 160 | 161 | for (var cityAllergy = 0; cityAllergy < currentCity.allergies.length; cityAllergy++) { 162 | if (allergies[allergy] == currentCity.allergies[cityAllergy].allergy) { 163 | allergyInCity = true; 164 | break; 165 | } 166 | } 167 | 168 | for (var statAllergy = 0; statAllergy < allergyStats.stats.developed.length; statAllergy++) { 169 | if (allergies[allergy] == allergyStats.stats.developed[statAllergy].allergy) { 170 | allergyInStats = true; 171 | break; 172 | } 173 | } 174 | 175 | if (allergyInCity && allergyInStats) { 176 | if (allergyStats.stats.developed[statAllergy].min.total.min > currentCity.allergies[cityAllergy].developed.total) { 177 | allergyStats.stats.developed[statAllergy].min.total = {city: currentCity.city, 178 | state: currentCity.state, 179 | min: currentCity.allergies[cityAllergy].developed.total}; 180 | } 181 | 182 | if (allergyStats.stats.developed[statAllergy].min.percentage.min > currentCity.allergies[cityAllergy].developed.percentage) { 183 | allergyStats.stats.developed[statAllergy].min.percentage = {city: currentCity.city, 184 | state: currentCity.state, 185 | min: currentCity.allergies[cityAllergy].developed.percentage}; 186 | } 187 | 188 | if (allergyStats.stats.developed[statAllergy].max.total.max < currentCity.allergies[cityAllergy].developed.total) { 189 | allergyStats.stats.developed[statAllergy].max.total = {city: currentCity.city, 190 | state: currentCity.state, 191 | max: currentCity.allergies[cityAllergy].developed.total}; 192 | } 193 | 194 | if (allergyStats.stats.developed[statAllergy].max.percentage.max < currentCity.allergies[cityAllergy].developed.percentage) { 195 | allergyStats.stats.developed[statAllergy].max.percentage = {city: currentCity.city, 196 | state: currentCity.state, 197 | max: currentCity.allergies[cityAllergy].developed.percentage}; 198 | } 199 | 200 | allergyStats.stats.developed[statAllergy].mean.total = allergyStats.stats.developed[statAllergy].mean.total + currentCity.allergies[cityAllergy].developed.total; 201 | allergyStats.stats.developed[statAllergy].mean.percentage = allergyStats.stats.developed[statAllergy].mean.percentage + currentCity.allergies[cityAllergy].developed.percentage; 202 | 203 | } else if (allergyInCity) { 204 | allergyStats.stats.developed.push({ 205 | allergy: currentCity.allergies[cityAllergy].allergy, 206 | min: {total: {city: currentCity.city, state: currentCity.state, min: currentCity.allergies[cityAllergy].developed.total}, 207 | percentage: {city: currentCity.city, state: currentCity.state, min: currentCity.allergies[cityAllergy].developed.percentage}}, 208 | max: {total: {city: currentCity.city, state: currentCity.state, max: currentCity.allergies[cityAllergy].developed.total}, 209 | percentage: {city: currentCity.city, state: currentCity.state, max: currentCity.allergies[cityAllergy].developed.percentage}}, 210 | mean: {total: currentCity.allergies[cityAllergy].developed.total, percentage: currentCity.allergies[cityAllergy].developed.percentage} 211 | }); 212 | 213 | } else if (allergyInStats) { 214 | allergyStats.stats.developed[statAllergy].min.total = {city: currentCity.city, state: currentCity.state, min: 0}; 215 | allergyStats.stats.developed[statAllergy].min.percentage = {city: currentCity.city, state: currentCity.state, min: 0}; 216 | } 217 | } 218 | 219 | // OUTGROWN 220 | 221 | for (var allergy = 0; allergy < allergies.length; allergy++) { 222 | var allergyInCity = false; 223 | var allergyInStats = false; 224 | 225 | for (var cityAllergy = 0; cityAllergy < currentCity.allergies.length; cityAllergy++) { 226 | if (allergies[allergy] == currentCity.allergies[cityAllergy].allergy) { 227 | allergyInCity = true; 228 | break; 229 | } 230 | } 231 | 232 | for (var statAllergy = 0; statAllergy < allergyStats.stats.outgrown.length; statAllergy++) { 233 | if (allergies[allergy] == allergyStats.stats.outgrown[statAllergy].allergy) { 234 | allergyInStats = true; 235 | break; 236 | } 237 | } 238 | 239 | if (allergyInCity && allergyInStats) { 240 | if (allergyStats.stats.outgrown[statAllergy].min.total.min > currentCity.allergies[cityAllergy].outgrown.total) { 241 | allergyStats.stats.outgrown[statAllergy].min.total = {city: currentCity.city, 242 | state: currentCity.state, 243 | min: currentCity.allergies[cityAllergy].outgrown.total}; 244 | } 245 | 246 | if (allergyStats.stats.outgrown[statAllergy].min.percentage.min > currentCity.allergies[cityAllergy].outgrown.percentage) { 247 | allergyStats.stats.outgrown[statAllergy].min.percentage = {city: currentCity.city, 248 | state: currentCity.state, 249 | min: currentCity.allergies[cityAllergy].outgrown.percentage}; 250 | } 251 | 252 | if (allergyStats.stats.outgrown[statAllergy].max.total.max < currentCity.allergies[cityAllergy].outgrown.total) { 253 | allergyStats.stats.outgrown[statAllergy].max.total = {city: currentCity.city, 254 | state: currentCity.state, 255 | max: currentCity.allergies[cityAllergy].outgrown.total}; 256 | } 257 | 258 | if (allergyStats.stats.outgrown[statAllergy].max.percentage.max < currentCity.allergies[cityAllergy].outgrown.percentage) { 259 | allergyStats.stats.outgrown[statAllergy].max.percentage = {city: currentCity.city, 260 | state: currentCity.state, 261 | max: currentCity.allergies[cityAllergy].outgrown.percentage}; 262 | } 263 | 264 | allergyStats.stats.outgrown[statAllergy].mean.total = allergyStats.stats.outgrown[statAllergy].mean.total + currentCity.allergies[cityAllergy].outgrown.total; 265 | allergyStats.stats.outgrown[statAllergy].mean.percentage = allergyStats.stats.outgrown[statAllergy].mean.percentage + currentCity.allergies[cityAllergy].outgrown.percentage; 266 | 267 | allergyCityTotals[statAllergy] = allergyCityTotals[statAllergy] + 1; 268 | 269 | } else if (allergyInCity) { 270 | allergyStats.stats.outgrown.push({ 271 | allergy: currentCity.allergies[cityAllergy].allergy, 272 | min: {total: {city: currentCity.city, state: currentCity.state, min: currentCity.allergies[cityAllergy].outgrown.total}, 273 | percentage: {city: currentCity.city, state: currentCity.state, min: currentCity.allergies[cityAllergy].outgrown.percentage}}, 274 | max: {total: {city: currentCity.city, state: currentCity.state, max: currentCity.allergies[cityAllergy].outgrown.total}, 275 | percentage: {city: currentCity.city, state: currentCity.state, max: currentCity.allergies[cityAllergy].outgrown.percentage}}, 276 | mean: {total: currentCity.allergies[cityAllergy].outgrown.total, percentage: currentCity.allergies[cityAllergy].outgrown.percentage} 277 | }); 278 | 279 | allergyCityTotals.push(1); 280 | } 281 | } 282 | 283 | allergyStats.cities.push(currentCity); 284 | } 285 | 286 | for (var allergy = 0; allergy < allergyStats.stats.developed.length; allergy++) { 287 | allergyStats.stats.developed[allergy].mean.total = allergyStats.stats.developed[allergy].mean.total / cities.length; 288 | allergyStats.stats.developed[allergy].mean.percentage = allergyStats.stats.developed[allergy].mean.percentage / cities.length; 289 | } 290 | 291 | for (var allergy = 0; allergy < allergyStats.stats.outgrown.length; allergy++) { 292 | allergyStats.stats.outgrown[allergy].mean.total = allergyStats.stats.outgrown[allergy].mean.total / allergyCityTotals[allergy]; 293 | allergyStats.stats.outgrown[allergy].mean.percentage = allergyStats.stats.outgrown[allergy].mean.percentage / allergyCityTotals[allergy]; 294 | } 295 | 296 | resolve(allergyStats); 297 | }) 298 | }) 299 | }) 300 | } 301 | 302 | 303 | module.exports.getPopulationStats = getPopulationStats; 304 | module.exports.getAllergyStats = getAllergyStats; 305 | --------------------------------------------------------------------------------