1+ const Table = require ( 'cli-table3' ) ;
2+ const axios = require ( 'axios' ) ;
3+ const _ = require ( 'lodash' ) ;
4+ const chalk = require ( 'chalk' ) ;
5+ const helpers = require ( './helpers' ) ;
6+ const {
7+ getCountry,
8+ getConfirmed,
9+ getActive,
10+ getDeaths,
11+ getRecovered,
12+ getMortalityPer,
13+ getRecoveredPer,
14+ getEmoji,
15+ calActive,
16+ calMortalityPer,
17+ calRecoveredPer,
18+ getOneDayChange,
19+ getOneWeekChange,
20+ getRateOfGrowth,
21+ getTotalStats,
22+ footer,
23+ } = require ( './helpers' ) ;
24+
25+ function getDataByState ( confirmed , deaths , recovered ) {
26+ const countryMap = { } ;
27+ const confirmedMap = _ . keyBy ( confirmed . locations , ( i ) => i . country + i . province ) ;
28+ const recoveredMap = _ . keyBy ( recovered . locations , ( i ) => i . country + i . province ) ;
29+ const deathsMap = _ . keyBy ( deaths . locations , ( i ) => i . country + i . province ) ;
30+ confirmed . locations . forEach ( obj => {
31+ const countryName = obj . country ;
32+ const provinceName = obj . province ;
33+ const mapKey = countryName + provinceName ;
34+ if ( ! countryMap [ mapKey ] && confirmedMap [ mapKey ] . latest > 0 ) {
35+ countryMap [ mapKey ] = {
36+ country : countryName ,
37+ province : provinceName ,
38+ countryCode : obj . country_code ,
39+ confirmed : confirmedMap [ mapKey ] . latest ,
40+ recovered : recoveredMap [ mapKey ] . latest ,
41+ deaths : deathsMap [ mapKey ] . latest ,
42+ confirmedByDay : helpers . historyObjToArr ( confirmedMap [ mapKey ] . history ) ,
43+ recoveredByDay : helpers . historyObjToArr ( recoveredMap [ mapKey ] . history ) ,
44+ deathsByDay : helpers . historyObjToArr ( recoveredMap [ mapKey ] . history ) ,
45+ } ;
46+ }
47+ } ) ;
48+ const countryArr = extraStats (
49+ Object . keys ( countryMap ) . map ( key => countryMap [ key ] )
50+ ) ;
51+ return _ . sortBy ( countryArr , ( o ) => - o . confirmed )
52+ }
53+
54+ function extraStats ( dataArr ) {
55+ return dataArr . map ( obj => Object . assign ( { } , obj ,
56+ {
57+ active : calActive ( obj ) ,
58+ mortalityPer : calMortalityPer ( obj ) ,
59+ recoveredPer : calRecoveredPer ( obj ) ,
60+ } )
61+ ) ;
62+ }
63+
64+ exports . getCountryTable = async ( country ) => {
65+ const head = [
66+ '' ,
67+ 'State' ,
68+ 'Confirmed ✅' ,
69+ 'Recovered 😃' ,
70+ 'Deaths 😞' ,
71+ 'Active 😷' ,
72+ 'Mortality %' ,
73+ 'Recovered %' ,
74+ '1 Day 🔺' ,
75+ '1 Week 🔺' ,
76+ // 'RoG',
77+ '🏳' ,
78+ ] ;
79+ const table = new Table ( {
80+ head,
81+ chars : { 'top' : '═' , 'top-mid' : '╤' , 'top-left' : '╔' , 'top-right' : '╗'
82+ , 'bottom' : '═' , 'bottom-mid' : '╧' , 'bottom-left' : '╚' , 'bottom-right' : '╝'
83+ , 'left' : '║' , 'left-mid' : '╟' , 'mid' : '─' , 'mid-mid' : '┼'
84+ , 'right' : ' ║' , 'right-mid' : '╢' , 'middle' : '│' }
85+ } ) ;
86+ const result = await axios ( 'https://coronavirus-tracker-api.herokuapp.com/all' ) ;
87+ const { latest, confirmed, deaths, recovered } = result . data ;
88+ const countryData = getDataByState ( confirmed , deaths , recovered , country )
89+ . filter ( obj => obj . country === country ) ;
90+ const totalStats = getTotalStats ( countryData ) ;
91+ table . push ( {
92+ [ country ] : [
93+ 'Total' ,
94+ getConfirmed ( totalStats . confirmed ) ,
95+ getRecovered ( totalStats . recovered ) ,
96+ getDeaths ( totalStats . deaths ) ,
97+ getActive ( totalStats . active ) ,
98+ getMortalityPer ( totalStats . mortalityPer ) ,
99+ getRecoveredPer ( totalStats . recoveredPer ) ,
100+ getOneDayChange ( totalStats ) ,
101+ getOneWeekChange ( totalStats ) ,
102+ // '',
103+ getEmoji ( countryData [ 0 ] . countryCode ) ,
104+ ]
105+ } )
106+ if ( countryData . length > 1 ) {
107+ let rank = 1 ;
108+ countryData . forEach ( cd => {
109+ const countryEmoji = getEmoji ( cd . countryCode ) ;
110+ const values = [
111+ cd . province ,
112+ getConfirmed ( cd . confirmed ) ,
113+ getRecovered ( cd . recovered ) ,
114+ getDeaths ( cd . deaths ) ,
115+ getActive ( cd . confirmed , cd . recovered , cd . deaths ) ,
116+ getMortalityPer ( cd . mortalityPer ) ,
117+ getRecoveredPer ( cd . recoveredPer ) ,
118+ getOneDayChange ( cd ) ,
119+ getOneWeekChange ( cd ) ,
120+ // getRateOfGrowth(cd),
121+ getEmoji ( cd . countryCode ) ,
122+ ]
123+ table . push ( { [ rank ++ ] : values } )
124+ } ) ;
125+ }
126+ return table . toString ( ) + footer ;
127+ }
0 commit comments