hebdobot/src/org/april/hebdobot/model/stats/ReviewDatasFile.java

217 lines
6.8 KiB
Java

/**
* Copyright (C) 2017-2018 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hebdobot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.model.stats;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.HebdobotException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
* The Class StatsFile.
*/
public class ReviewDatasFile
{
private static final Logger logger = LoggerFactory.getLogger(ReviewDatasFile.class);
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
* Instantiates a new stats file.
*/
private ReviewDatasFile()
{
}
/**
* Append.
*
* @param file
* the file
* @param stat
* the stat
* @throws HebdobotException
* the hebdobot exception
*/
public static void append(final File file, final ReviewData stat) throws HebdobotException
{
try
{
if ((file != null) && (file.exists()))
{
String line = String.format("%s\t%d\t%d", stat.getDate().format(DateTimeFormatter.ofPattern("yyyyMMdd-hh'h'mm")), stat.getUserCount(),
stat.getDuration());
StringList lines = StringsUtils.load(file);
lines.add(line);
StringsUtils.save(file, lines);
}
}
catch (FileNotFoundException exception)
{
logger.error("File Not Found: " + exception.getMessage(), exception);
throw new HebdobotException("File Not Found appending file [" + file + "]");
}
catch (IOException exception)
{
logger.error("IO error: " + exception.getMessage(), exception);
throw new HebdobotException("IO Error appending file [" + file + "]");
}
}
/**
* Load.
*
* @param source
* the source
* @return the review datas
* @throws HebdobotException
* the hebdobot exception
*/
public static ReviewDatas load(final File source) throws HebdobotException
{
ReviewDatas result;
//
if (source == null)
{
logger.error("Attempt to read null file.");
throw new IllegalArgumentException("Null parameter.");
}
else
{
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(new FileInputStream(source), DEFAULT_CHARSET_NAME));
result = read(in);
}
catch (UnsupportedEncodingException exception)
{
logger.error("Encoding error reading file: " + exception.getMessage(), exception);
throw new HebdobotException("Encodding error reading file [" + source + "]");
}
catch (FileNotFoundException exception)
{
logger.error("File Not Found: " + exception.getMessage(), exception);
throw new HebdobotException("File Not Found opening file:[" + source + "]");
}
catch (IOException exception)
{
logger.error("IO error: " + exception.getMessage(), exception);
throw new HebdobotException("IO Error opening file: [" + source + "]");
}
finally
{
IOUtils.closeQuietly(in);
}
}
//
return result;
}
/**
* Read.
*
* @param in
* the in
* @return the review datas
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static ReviewDatas read(final BufferedReader in) throws IOException
{
ReviewDatas result;
result = new ReviewDatas();
boolean ended = false;
while (!ended)
{
String line = in.readLine();
if (line == null)
{
ended = true;
}
else if ((StringUtils.isNotBlank(line)) && (!line.startsWith("#")))
{
// System.out.println("line=" + line);
try
{
String[] tokens = line.split("[ \\s]+");
if ((tokens[0] == null) || (!tokens[0].matches("\\d{8}-\\d{2}h(\\d{2})+")))
{
logger.warn("Bad line in review stat file: [{}]", line);
}
else
{
int year = Integer.parseInt(tokens[0].substring(0, 4));
int month = Integer.parseInt(tokens[0].substring(4, 6));
int day = Integer.parseInt(tokens[0].substring(6, 8));
int hour = Integer.parseInt(tokens[0].substring(9, 11));
int minute = Integer.parseInt(tokens[0].substring(12, 14));
LocalDateTime date = LocalDateTime.of(year, month, day, hour, minute);
int userCount = Integer.parseInt(tokens[1]);
Integer duration;
if (tokens.length == 2)
{
duration = null;
}
else
{
duration = Integer.parseInt(tokens[2]);
}
ReviewData stat = new ReviewData(date, userCount, duration);
result.add(stat);
}
}
catch (Exception exception)
{
logger.warn("Decode failed for line: [{}]", line, exception);
}
}
}
//
return result;
}
}