2018-05-16 09:27:01 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-05-17 09:17:27 +02:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2018-05-16 09:27:01 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-06-12 11:10:23 +02:00
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
import fr.devinsy.strings.StringsUtils;
|
2018-05-17 09:17:27 +02:00
|
|
|
|
2018-05-16 09:27:01 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2018-05-19 10:47:58 +02:00
|
|
|
* the file
|
2018-05-16 09:27:01 +02:00
|
|
|
* @param stat
|
|
|
|
* the stat
|
|
|
|
* @throws HebdobotException
|
2018-05-19 10:47:58 +02:00
|
|
|
* the hebdobot exception
|
2018-05-16 09:27:01 +02:00
|
|
|
*/
|
|
|
|
public static void append(final File file, final ReviewData stat) throws HebdobotException
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-05-19 00:26:47 +02:00
|
|
|
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());
|
2018-05-16 09:27:01 +02:00
|
|
|
|
2018-05-19 00:26:47 +02:00
|
|
|
StringList lines = StringsUtils.load(file);
|
|
|
|
lines.add(line);
|
|
|
|
StringsUtils.save(file, lines);
|
|
|
|
}
|
2018-05-16 09:27:01 +02:00
|
|
|
}
|
|
|
|
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
|
2018-05-19 10:47:58 +02:00
|
|
|
* @return the review datas
|
2018-05-16 09:27:01 +02:00
|
|
|
* @throws HebdobotException
|
2018-05-19 10:47:58 +02:00
|
|
|
* the hebdobot exception
|
2018-05-16 09:27:01 +02:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2018-05-19 10:47:58 +02:00
|
|
|
* @param in
|
|
|
|
* the in
|
|
|
|
* @return the review datas
|
2018-05-16 09:27:01 +02:00
|
|
|
* @throws IOException
|
2018-05-19 10:47:58 +02:00
|
|
|
* Signals that an I/O exception has occurred.
|
2018-05-16 09:27:01 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|