package yanagishima.servlet;

import me.geso.tinyorm.TinyORM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yanagishima.row.Query;
import yanagishima.util.JsonUtil;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Singleton
public class QueryHistoryServlet extends HttpServlet {

    private static Logger LOGGER = LoggerFactory
            .getLogger(QueryHistoryServlet.class);

    private static final long serialVersionUID = 1L;

    @Inject
    private TinyORM db;

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {

        HashMap<String, Object> retVal = new HashMap<String, Object>();

        try {
            Optional<String> limitOptional = Optional.ofNullable(request.getParameter("limit"));
            limitOptional.ifPresent(limitStr -> {
                String datasource = Optional.ofNullable(request.getParameter("datasource")).get();
                List<Query> queryList = db.search(Query.class).where("datasource=?", datasource).orderBy("query_id DESC").limit(Integer.parseInt(limitStr)).execute();
                List<List<String>> rowDataList = new ArrayList<List<String>>();
                for (Query query : queryList) {
                    List<String> row = new ArrayList<>();
                    row.add(query.getQueryId());
                    row.add(query.getQueryString());
                    row.add(query.getFetchResultTimeString());
                    rowDataList.add(row);
                }
                retVal.put("headers", Arrays.asList("Id", "Query", "Time"));
                retVal.put("results", rowDataList);
            });


        } catch (Throwable e) {
            LOGGER.error(e.getMessage(), e);
            retVal.put("error", e.getMessage());
        }

        JsonUtil.writeJSON(response, retVal);

    }

}

@@ -451,14 +451,220 @@
 ava.
-util.*
+time.LocalDateTime;%0Aimport java.time.ZoneId;%0Aimport java.time.ZonedDateTime;%0Aimport java.time.format.DateTimeFormatter;%0Aimport java.time.temporal.ChronoUnit;%0Aimport java.util.*;%0Aimport java.util.stream.Collectors
 ;%0A%0A@
@@ -1186,38 +1186,25 @@
     
-Optional%3CString%3E limitOptional
+String datasource
  = O
@@ -1248,67 +1248,28 @@
 er(%22
-limit%22));%0A            limitOptional.ifPresent(limitStr -%3E %7B
+datasource%22)).get();
 %0A   
@@ -1277,37 +1277,33 @@
         
-    
 String
- datasource
+%5B%5D queryids
  = Optio
@@ -1343,33 +1343,145 @@
 er(%22
-datasource%22)).get(
+queryids%22)).get().split(%22,%22
 );%0A
+%0A
     
+        String placeholder = Arrays.stream(queryids).map(r -%3E %22?%22).collect(Collectors.joining(%22, %22));%0A
     
@@ -1521,16 +1521,21 @@
 b.search
+BySQL
 (Query.c
@@ -1542,17 +1542,104 @@
 lass
-).where(%22
+,%0A                    %22SELECT query_id, fetch_result_time_string, query_string FROM query WHERE 
 data
@@ -1649,11 +1649,13 @@
 rce=
-?%22,
+%5C'%22 +
  dat
@@ -1665,84 +1665,131 @@
 urce
-).orderBy(%22query_id DESC%22).limit(Integer.parseInt(limitStr)).execute();%0A    
+ + %22%5C' and query_id IN (%22 + placeholder + %22)%22,%0A                    Arrays.stream(queryids).collect(Collectors.toList()));%0A%0A
     
@@ -1810,24 +1810,29 @@
 ist%3C
-String%3E%3E rowData
+Object%3E%3E queryHistory
 List
@@ -1857,24 +1857,20 @@
 ist%3C
-String
+Object
 %3E%3E();%0A
-    
     
@@ -1929,23 +1929,19 @@
     
-    
 List%3C
-String
+Object
 %3E ro
@@ -1975,36 +1975,41 @@
             
-    row.add(
+String queryid = 
 query.getQue
@@ -2014,17 +2014,16 @@
 ueryId()
-)
 ;%0A      
@@ -2021,32 +2021,62 @@
 );%0A             
+   
+row.add(queryid);%0A         
        row.add(q
@@ -2091,32 +2091,33 @@
 QueryString());%0A
+%0A
                 
@@ -2112,36 +2112,324 @@
             
-    row.add(
+LocalDateTime submitTimeLdt = LocalDateTime.parse(queryid.substring(0, %22yyyyMMdd_HHmmss%22.length()), DateTimeFormatter.ofPattern(%22yyyyMMdd_HHmmss%22));%0A                ZonedDateTime submitTimeZdt = submitTimeLdt.atZone(ZoneId.of(%22GMT%22, ZoneId.SHORT_IDS));%0A                String fetchResultTimeString = 
 query.getFet
@@ -2448,17 +2448,16 @@
 String()
-)
 ;%0A      
@@ -2470,34 +2470,229 @@
     
-    rowDataList.add(row
+ZonedDateTime fetchResultTime = ZonedDateTime.parse(fetchResultTimeString);%0A                long elapsedTimeMillis = ChronoUnit.MILLIS.between(submitTimeZdt, fetchResultTime);%0A                row.add(elapsedTimeMillis
 );%0A
+%0A
     
@@ -2707,14 +2707,49 @@
     
-%7D%0A    
+queryHistoryList.add(row);%0A            %7D%0A
     
@@ -2821,36 +2821,32 @@
 ));%0A            
-    
 retVal.put(%22resu
@@ -2855,38 +2855,26 @@
 s%22, 
-rowDataList);%0A            %7D);%0A
+queryHistoryList);
 %0A%0A  

Update queryHistory API