/*
 * Copyright (c) 2018 Redlink GmbH.
 */
package com.rbmhtechnology.vind.monitoring.report;

import com.rbmhtechnology.vind.monitoring.report.writer.HtmlReportWriter;
import org.junit.*;

import java.io.File;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * Created on 08.06.18.
 */
public class createReportIT {

    private String esHost = "172.20.30.95";
    private String esPort = "19200";
    private String esIndex = "logstash-searchanalysis-*";
    private String applicationName = "mediamanager-Media Manager / Editing - Internal / RBMH-Assets";
    private final String messageWrapper = "message_json";
    private ElasticSearchReportService esRepsortService;

    private Report report;
    private HtmlReportWriter reportWriter =  new HtmlReportWriter();
    private File reportFile;


    @Before
    public void setUp() throws IOException {
        final ZonedDateTime from = ZonedDateTime.now().minusYears(1);
        final ZonedDateTime to = ZonedDateTime.now().plusYears(1);

        esRepsortService = new ElasticSearchReportService(esHost, esPort, esIndex, from, to, applicationName);
        esRepsortService.setMessageWrapper(messageWrapper);

        final LinkedHashMap<String, Long> topFaceFields = esRepsortService.getTopFaceFields();
        final ArrayList<String> facetFields = new ArrayList<>(topFaceFields.keySet());

        final LinkedHashMap<String, Long> topSuggestionFields = esRepsortService.getTopSuggestionFields();
        final ArrayList<String> suggestFields = new ArrayList<>(topSuggestionFields.keySet());

        this.report = new Report()
                .setApplicationName(applicationName)
                .setFrom(from)
                .setTo(to)
                .setTopDays(esRepsortService.getTopDays())
                .setRequests(esRepsortService.getTotalRequests())
                .setTopSuggestionFields(topSuggestionFields)
                .setSuggestionFieldsValues(esRepsortService.getSuggestionFieldsValues(suggestFields))
                .setFacetFieldsValues(esRepsortService.getFacetFieldsValues(facetFields))
                .setTopFacetFields(topFaceFields)
                .setTopQueries(esRepsortService.getTopQueries())
                .setTopUsers(esRepsortService.getTopUsers());

        reportFile = File.createTempFile("reportTest", ".html");
    }

    @Test
    @Ignore
    public void createReportIT() {

        Assert.assertTrue(reportWriter.write(this.report,reportFile.getAbsolutePath()));

        System.out.println("Report has been written to " + reportFile.getAbsolutePath());

    }

    //@After
    public void cleanUp() throws Exception {
        esRepsortService.close();

        if (reportFile.exists()) {
            reportFile.delete();
        }
    }


}

@@ -421,20 +421,17 @@
  = %22
-172.20.30.95
+localhost
 %22;%0A 
@@ -458,17 +458,16 @@
 Port = %22
-1
 9200%22;%0A 
@@ -502,30 +502,16 @@
 %22log
-stash-searcha
+A
 nalysis
--*
 %22;%0A 
@@ -551,69 +551,16 @@
  = %22
-mediamanager-Media Manager / Editing - Internal / RBMH-Assets
+App name
 %22;%0A 

reset default config values