{"size": 3838, "ext": "js", "max_stars_repo_path": "sample-code/data-with-elasticsearch.js", "max_stars_repo_name": "alex-silvagomes/conector-nodejs-jira-to-elk", "max_stars_repo_stars_event_min_datetime": "2020-10-15T20:33:07.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-24T14:19:48.000Z", "max_issues_repo_path": "sample-code/data-with-elasticsearch.js", "max_issues_repo_name": "alex-silvagomes/conector-pipeline-devops-nodejs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "sample-code/data-with-elasticsearch.js", "max_forks_repo_name": "alex-silvagomes/conector-pipeline-devops-nodejs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.9833333333, "max_line_length": 89, "alphanum_fraction": 0.4843668577}
'use strict'

require('log-timestamp');
const config = require('../../config');
const express = require('express');
const router = express.Router();
const axios = require('axios')

// elasticsearch connection:
const client_elasticsearch = require('../../elasticsearch/connection/connection');

// jira connection:
//const client_jira = require('jira/connection/connection');

// Mockup JSON TESTE:
const URL = `https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson`;






//================== Official API Call ==================\\
router.get('/earthquakes', async function (req, res) {
    console.log('Loading Application...')

    var indexObjetcRequest = null
    
    //res.json('Running Application...')

    //setInterval(() => { 
        //======= Check that Elasticsearch is up and running =======\\
        // pingElasticsearch = async () => {
        //     await client_elasticsearch.ping(
        //         function(error,res) {
        //             if (error) {
        //                 console.error('elasticsearch cluster is down!');
        //             } else {
        //                 console.log('Elasticsearch Ready');
        //             }
        //         }
        //     );
        // }

    // ====== Get Data From DataSource and then index into Elasticsearch
    const indexAllDocs = async () => {
        try {
            console.log('Getting Data From Host')
            
            const EARTHQUAKES = await axios.get(`${URL}`,{
                headers: {
                    'Content-Type': [
                        'application/json',  
                        'charset=utf-8' 
                    ]
                }
            });

            console.log('Data Received!')

            var results = EARTHQUAKES.data.features

            console.log('Indexing Data...')

            
            results.map(async results => (
                indexObjetcRequest = {
                    place: results.properties.place,
                    time: results.properties.time,
                    tiamp: results.properties.time,
                    updmestated: results.properties.updated,
                    tz: results.properties.tz,
                    url: results.properties.url,
                    detail: results.properties.detail,
                    felt: results.properties.felt,
                    cdi: results.properties.cdi,
                    alert: results.properties.alert,
                    status: results.properties.status,
                    tsunami: results.properties.tsunami,
                    sig: results.properties.sig,
                    net: results.properties.net,
                    code: results.properties.code,
                    sources: results.properties.sources,
                    nst: results.properties.nst,
                    dmin: results.properties.dmin,
                    rms: results.properties.rms,
                    mag: results.properties.mag,
                    magType: results.properties.magType,
                    type: results.properties.type
                }
                ,await client_elasticsearch.index({ 
                    index: 'earthquakes',
                    id: results.id,
                    body: indexObjetcRequest
                }), (err, resp, status) => {
                    console.log(resp);
                }
            ));
            
            if (EARTHQUAKES.data.length) {
                indexAllDocs();
            } else {
                console.log('All Data Has Been Indexed!');
            };

            res.json(indexObjetcRequest)

        } catch (err) {
            console.log(err)
        };

        console.log('Preparing For The Next Data Check...');
    }

    //pingElasticsearch()
    indexAllDocs()
    
});
 
module.exports = router;