DEV Community

Charlie Fubon
Charlie Fubon

Posted on

C1


import java.io.*;
import java.nio.file.*;
import java.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

public class QueryLogParser {
    static class QueryEntry {
        String query;
        int occurrences;

        QueryEntry(String query, int occurrences) {
            this.query = query;
            this.occurrences = occurrences;
        }
    }

    public static void main(String[] args) {
        String inputFile = "final.txt";
        String outputFile = "query_analysis.xlsx";

        try {
            List<QueryEntry> entries = parseQueryLog(inputFile);
            createExcelFile(entries, outputFile);
            System.out.println("Successfully processed " + entries.size() + " queries");
        } catch (IOException e) {
            System.err.println("Error processing file: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static List<QueryEntry> parseQueryLog(String filePath) throws IOException {
        List<QueryEntry> entries = new ArrayList<>();
        List<String> lines = Files.readAllLines(Paths.get(filePath));

        String currentQuery = null;

        for (String line : lines) {
            line = line.trim();

            if (line.startsWith("Query:")) {
                currentQuery = line.substring(6).trim();
            } else if (line.startsWith("Total Occurrences:") && currentQuery != null) {
                int occurrences = Integer.parseInt(line.split(":")[1].trim());
                entries.add(new QueryEntry(currentQuery, occurrences));
                currentQuery = null;
            }
        }

        return entries;
    }

    private static void createExcelFile(List<QueryEntry> entries, String outputFile) throws IOException {
        try (XSSFWorkbook workbook = new XSSFWorkbook()) {
            XSSFSheet sheet = workbook.createSheet("Query Analysis");

            // Create header row
            Row headerRow = sheet.createRow(0);
            Cell headerCell1 = headerRow.createCell(0);
            headerCell1.setCellValue("Query");
            Cell headerCell2 = headerRow.createCell(1);
            headerCell2.setCellValue("Total Occurrences");

            // Create style for headers
            CellStyle headerStyle = workbook.createCellStyle();
            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);
            headerCell1.setCellStyle(headerStyle);
            headerCell2.setCellStyle(headerStyle);

            // Add data rows
            int rowNum = 1;
            for (QueryEntry entry : entries) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(entry.query);
                row.createCell(1).setCellValue(entry.occurrences);
            }

            // Auto-size columns
            sheet.autoSizeColumn(0);
            sheet.autoSizeColumn(1);

            // Write to file
            try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
                workbook.write(outputStream);
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)