'use strict';
var logger = require('logger');
const ctRegisterMicroservice = require('ct-register-microservice-node');

class GLADPresenter {

  static * transform(results, layer, subscription) {
    results.alert_count = results.value;
    let uri = '/query' + results.downloadUrls.csv.split('download')[1];
    uri = uri.split('&format')[0].replace('&geostore', ' order by julian_day desc, year desc limit 10&geostore');

    logger.debug('Last alerts endpoint ', uri);

    try {
      let alerts = yield ctRegisterMicroservice.requestToMicroservice({
          uri: uri,
          method: 'GET',
          json: true
      });
      results.alerts = alerts.data.map(el => {
        el.date = new Date(new Date(el.year, 0, 1).getTime() + el.julian_day*24*3600*1000);
        return el;
      });
    } catch (err) {
      logger.error(err);
      //throw(err);
      results.alerts = [];
    }
    logger.info('Glad P Results ', results);
    return results;
  }

}

module.exports = GLADPresenter;

@@ -11,11 +11,13 @@
 t';%0A
-var
+const
  log
@@ -351,30 +351,46 @@
 ce('
-&geostore', ' order by
+ORDER BY year, julian_day', ' ORDER BY
  jul
@@ -401,41 +401,32 @@
 day 
-desc
+DESC
 , year 
-desc limit 10&geostore
+DESC LIMIT 10
 ');%0A
@@ -474,17 +474,16 @@
 , uri);%0A
-%0A
     try 
@@ -687,19 +687,22 @@
         
-el.
+const 
 date = n
@@ -776,27 +776,223 @@
 000)
-;%0A        return el
+%0A            .toISOString()%0A            .split('T');%0A        return %7B%0A            acq_date: date%5B0%5D,%0A            acq_time: date%5B1%5D.substr(0, 8),%0A            latitude: el.lat,%0A            longitude: el.long%0A        %7D
 ;%0A  
@@ -1048,28 +1048,8 @@
 r);%0A
-      //throw(err);%0A
     

Format alerts points properly