@@ -18,23 +18,53 @@ dayjs.tz.setDefault('America/Chicago');
1818
1919module . exports = ( robot ) => {
2020 // Configure dayjs
21- const baseUrl = 'https://services2.arcgis.com/HdTo6HJqh92wn4D8/arcgis/rest/services/Nashville_Fire_Department_Active_Incidents_view/FeatureServer/0/query?where=1%3D1&objectIds=&time=&resultType=none&outFields=*&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnDistinctValues=false&cacheHint=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&sqlFormat=none&f=pjson&token= ' ;
21+ const baseUrl = 'https://services2.arcgis.com/HdTo6HJqh92wn4D8/arcgis/rest/services/Nashville_Fire_Department_Active_Incidents_view/FeatureServer/0/query' ;
2222
2323 const formatTable = ( data ) => {
24- const table = new AsciiTable ( 'Nashville Fire Active Incidents' ) ;
25- table . setHeading ( 'Time' , 'Postal Code' , 'Type' , 'Unit Dispatched' ) ;
26- const sortedData = data . features . sort (
27- ( a , b ) => b . attributes . DispatchDateTime - a . attributes . DispatchDateTime ,
28- ) ;
29- sortedData . forEach ( ( row ) => {
24+ const table = new AsciiTable ( '🔥 Nashville Fire Active Incidents 🚒 ' ) ;
25+ table . setHeading ( 'Time' , 'Postal Code' , 'Type' , 'Units Dispatched' ) ;
26+
27+ const events = { } ;
28+
29+ data . features . forEach ( ( row ) => {
3030 const { attributes } = row ;
31+ const eventId = attributes . event_number ;
32+
33+ if ( ! events [ eventId ] ) {
34+ events [ eventId ] = {
35+ DispatchDateTime : attributes . DispatchDateTime ,
36+ PostalCode : attributes . PostalCode ,
37+ incident_type_id : attributes . incident_type_id ,
38+ units : new Set ( ) ,
39+ } ;
40+ }
41+
42+ if ( attributes . Unit_ID ) {
43+ events [ eventId ] . units . add ( attributes . Unit_ID ) ;
44+ }
45+ } ) ;
46+
47+ const sortedEvents = Object . values ( events ) . sort (
48+ ( a , b ) => b . DispatchDateTime - a . DispatchDateTime ,
49+ ) ;
50+
51+ sortedEvents . forEach ( ( event ) => {
52+ const units = Array . from ( event . units ) . sort ( ) ;
53+ const visibleUnits = units . slice ( 0 , 5 ) ;
54+ const remaining = units . length - visibleUnits . length ;
55+
3156 table . addRow ( [
32- dayjs . tz ( attributes . DispatchDateTime , 'America/Chicago' ) . fromNow ( ) ,
33- attributes . PostalCode ,
34- attributes . incident_type_id ,
35- attributes . Unit_ID ,
57+ dayjs
58+ . tz ( event . DispatchDateTime , 'America/Chicago' )
59+ . fromNow ( ) ,
60+ event . PostalCode ,
61+ event . incident_type_id ,
62+ remaining > 0
63+ ? `${ visibleUnits . join ( ', ' ) } + ${ remaining } more`
64+ : visibleUnits . join ( ', ' ) ,
3665 ] ) ;
3766 } ) ;
67+
3868 return table . toString ( ) ;
3969 } ;
4070
@@ -43,11 +73,12 @@ module.exports = (robot) => {
4373 const query = {
4474 where : '1=1' ,
4575 outFields : '*' ,
46- outSR : 4326 ,
47- f : 'json ' ,
76+ returnGeometry : false ,
77+ f : 'pjson ' ,
4878 } ;
79+
4980 if ( zip ) {
50- query . where = `PostalCode=${ zip } ` ;
81+ query . where = `PostalCode=' ${ zip } ' ` ;
5182 }
5283
5384 return robot . http ( baseUrl )
@@ -59,7 +90,8 @@ module.exports = (robot) => {
5990 return ;
6091 }
6192
62- if ( / s l a c k / . test ( robot . adapterName ) ) {
93+ const adapterName = robot . adapterName ?? robot . adapter ?. name ;
94+ if ( / s l a c k / . test ( adapterName ) ) {
6395 msg . send ( `\`\`\`\n${ formatTable ( data , msg ) } \n\`\`\`` ) ;
6496 return ;
6597 }
0 commit comments