2020-01-05 17:24:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Christian Pierre MOMON <christian.momon@devinsy.fr>
|
|
|
|
*
|
|
|
|
* This file is part of AgirStatool, simple key value database.
|
|
|
|
*
|
|
|
|
* AgirStatool 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.
|
|
|
|
*
|
|
|
|
* AgirStatool 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 AgirStatool. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.april.agirstatool.core;
|
|
|
|
|
2020-01-22 10:39:44 +01:00
|
|
|
import java.time.LocalDate;
|
2020-01-05 17:24:43 +01:00
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
import java.util.Date;
|
2020-01-27 15:47:43 +01:00
|
|
|
import java.util.Locale;
|
2020-01-05 17:24:43 +01:00
|
|
|
|
2020-01-22 10:39:44 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2020-01-05 17:24:43 +01:00
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
2020-01-22 10:39:44 +01:00
|
|
|
import org.april.agirstatool.charts.DateCount;
|
|
|
|
import org.april.agirstatool.charts.DateCountList;
|
|
|
|
import org.april.agirstatool.charts.DateCountMap;
|
|
|
|
import org.april.agirstatool.core.pages.ProjectPage;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-01-05 17:24:43 +01:00
|
|
|
|
2020-01-19 23:33:40 +01:00
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
import fr.devinsy.strings.StringsUtils;
|
|
|
|
|
2020-01-05 17:24:43 +01:00
|
|
|
/**
|
|
|
|
* The Class AgirStatoolUtils.
|
|
|
|
*/
|
|
|
|
public class AgirStatoolUtils
|
|
|
|
{
|
2020-01-22 10:39:44 +01:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(ProjectPage.class);
|
|
|
|
|
2020-01-27 15:47:43 +01:00
|
|
|
public static final DateTimeFormatter PATTERN_SHORTDATE = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.FRANCE);
|
|
|
|
public static final DateTimeFormatter PATTERN_LONGDATE = DateTimeFormatter.ofPattern("dd/MM/yyyy hh':'mm", Locale.FRANCE);
|
2020-01-05 17:24:43 +01:00
|
|
|
|
2020-01-29 00:51:45 +01:00
|
|
|
/**
|
|
|
|
* Builds the week labels.
|
|
|
|
*
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @return the string list
|
|
|
|
*/
|
|
|
|
public static StringList buildWeekLabels(final LocalDate start)
|
|
|
|
{
|
|
|
|
StringList result;
|
|
|
|
|
|
|
|
result = buildWeekLabels(start, LocalDate.now());
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the week labels.
|
|
|
|
*
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @param end
|
|
|
|
* the end
|
|
|
|
* @return the string list
|
|
|
|
*/
|
|
|
|
public static StringList buildWeekLabels(final LocalDate start, final LocalDate end)
|
|
|
|
{
|
|
|
|
StringList result;
|
|
|
|
|
|
|
|
result = new StringList();
|
|
|
|
|
|
|
|
if (start != null)
|
|
|
|
{
|
|
|
|
LocalDate normalizedEnd = AgirStatoolUtils.normaliseWeekDate(end);
|
|
|
|
LocalDate date = AgirStatoolUtils.normaliseWeekDate(start);
|
|
|
|
while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd))
|
|
|
|
{
|
|
|
|
String label = date.format(DateTimeFormatter.ofPattern("yyyy-MMM", Locale.FRANCE));
|
|
|
|
result.add(label);
|
|
|
|
date = date.plusWeeks(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the week max ages.
|
|
|
|
*
|
|
|
|
* @param project
|
|
|
|
* the project
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @param end
|
|
|
|
* the end
|
|
|
|
* @return the string list
|
|
|
|
*/
|
|
|
|
public static StringList buildWeekMaxAges(final Project project, final LocalDate start, final LocalDate end)
|
|
|
|
{
|
|
|
|
StringList result;
|
|
|
|
|
|
|
|
result = new StringList();
|
|
|
|
|
|
|
|
if (start != null)
|
|
|
|
{
|
|
|
|
LocalDate date = AgirStatoolUtils.normaliseWeekDate(start);
|
|
|
|
LocalDate normalizedEnd = AgirStatoolUtils.normaliseWeekDate(end);
|
|
|
|
while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd))
|
|
|
|
{
|
|
|
|
Stat stat = project.issues().extractActivedAt(date).computeStat(date);
|
|
|
|
|
|
|
|
result.add(String.valueOf(stat.getMax()));
|
|
|
|
|
|
|
|
//
|
|
|
|
date = date.plusWeeks(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the week mean ages.
|
|
|
|
*
|
|
|
|
* @param project
|
|
|
|
* the project
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @param end
|
|
|
|
* the end
|
|
|
|
* @return the string list
|
|
|
|
*/
|
|
|
|
public static StringList buildWeekMeanAges(final Project project, final LocalDate start, final LocalDate end)
|
|
|
|
{
|
|
|
|
StringList result;
|
|
|
|
|
|
|
|
result = new StringList();
|
|
|
|
|
|
|
|
if (start != null)
|
|
|
|
{
|
|
|
|
LocalDate date = AgirStatoolUtils.normaliseWeekDate(start);
|
|
|
|
LocalDate normalizedEnd = AgirStatoolUtils.normaliseWeekDate(end);
|
|
|
|
while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd))
|
|
|
|
{
|
|
|
|
Stat stat = project.issues().extractActivedAt(date).computeStat(date);
|
|
|
|
|
|
|
|
result.add(String.format(Locale.ENGLISH, "%.2f", stat.getMean()));
|
|
|
|
|
|
|
|
//
|
|
|
|
date = date.plusWeeks(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the week min ages.
|
|
|
|
*
|
|
|
|
* @param project
|
|
|
|
* the project
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @param end
|
|
|
|
* the end
|
|
|
|
* @return the string list
|
|
|
|
*/
|
|
|
|
public static StringList buildWeekMinAges(final Project project, final LocalDate start, final LocalDate end)
|
|
|
|
{
|
|
|
|
StringList result;
|
|
|
|
|
|
|
|
result = new StringList();
|
|
|
|
|
|
|
|
if (start != null)
|
|
|
|
{
|
|
|
|
LocalDate date = AgirStatoolUtils.normaliseWeekDate(start);
|
|
|
|
LocalDate normalizedEnd = AgirStatoolUtils.normaliseWeekDate(end);
|
|
|
|
while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd))
|
|
|
|
{
|
|
|
|
Stat stat = project.issues().extractActivedAt(date).computeStat(date);
|
|
|
|
|
|
|
|
result.add(String.valueOf(stat.getMin()));
|
|
|
|
|
|
|
|
//
|
|
|
|
date = date.plusWeeks(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:39:44 +01:00
|
|
|
/**
|
|
|
|
* Normalise week date.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the local date
|
|
|
|
*/
|
|
|
|
public static LocalDate normaliseWeekDate(final LocalDate source)
|
|
|
|
{
|
|
|
|
LocalDate result;
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
{
|
|
|
|
result = source;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = source.minusDays(source.getDayOfWeek().getValue() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalized week count list.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @param end
|
|
|
|
* the end
|
|
|
|
* @return the date count list
|
|
|
|
*/
|
|
|
|
public static DateCountList normalizedWeekCountList(final DateCountList source, final LocalDate start, final LocalDate end)
|
|
|
|
{
|
|
|
|
DateCountList result;
|
|
|
|
|
|
|
|
result = new DateCountList();
|
|
|
|
|
|
|
|
LocalDate normalizedEnd = normaliseWeekDate(end);
|
|
|
|
LocalDate date = normaliseWeekDate(start);
|
2020-01-27 15:47:43 +01:00
|
|
|
int index = source.indexOf(toYearWeek(start));
|
2020-01-22 10:39:44 +01:00
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd))
|
|
|
|
{
|
2020-01-27 15:47:43 +01:00
|
|
|
String dateToken = toYearWeek(date);
|
2020-01-22 10:39:44 +01:00
|
|
|
long count;
|
|
|
|
if (index < source.size())
|
|
|
|
{
|
|
|
|
DateCount current = source.get(index);
|
2020-01-22 12:09:49 +01:00
|
|
|
// AgirStatoolUtils.logger.info("===> " + dateToken + " " +
|
|
|
|
// current.getDate());
|
2020-01-22 10:39:44 +01:00
|
|
|
if (StringUtils.equals(current.getDate(), dateToken))
|
|
|
|
{
|
|
|
|
count = current.getCount();
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
result.add(new DateCount(dateToken, count));
|
|
|
|
date = date.plusWeeks(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the week created count list.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @param start
|
|
|
|
* the start
|
|
|
|
* @return the date count list
|
|
|
|
*/
|
|
|
|
public static DateCountList normalizedWeekCountList(final DateCountMap source, final LocalDate start)
|
|
|
|
{
|
|
|
|
DateCountList result;
|
|
|
|
|
|
|
|
result = new DateCountList();
|
|
|
|
|
|
|
|
LocalDate date = normaliseWeekDate(start);
|
|
|
|
LocalDate end = normaliseWeekDate(LocalDate.now());
|
|
|
|
long count = 0;
|
|
|
|
while (date.isBefore(end) || date.isEqual(end))
|
|
|
|
{
|
2020-01-27 15:47:43 +01:00
|
|
|
String dateToken = toYearWeek(date);
|
2020-01-22 10:39:44 +01:00
|
|
|
DateCount current = source.get(dateToken);
|
|
|
|
if (current != null)
|
|
|
|
{
|
|
|
|
count += current.getCount();
|
|
|
|
}
|
|
|
|
result.add(new DateCount(dateToken, count));
|
|
|
|
date = date.plusWeeks(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-05 17:24:43 +01:00
|
|
|
/**
|
|
|
|
* Gets the current time in long format.
|
|
|
|
*
|
|
|
|
* @return the long
|
|
|
|
*/
|
|
|
|
public static long now()
|
|
|
|
{
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
2020-01-18 09:55:56 +01:00
|
|
|
/**
|
|
|
|
* Select stat indicator.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String selectStatIndicator(final long value)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (value < 10)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else if (value < 20)
|
|
|
|
{
|
2020-01-18 23:02:42 +01:00
|
|
|
result = "caution";
|
2020-01-18 09:55:56 +01:00
|
|
|
}
|
|
|
|
else if (value < 50)
|
|
|
|
{
|
2020-01-18 23:02:42 +01:00
|
|
|
result = "warning";
|
2020-01-18 09:55:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-18 23:02:42 +01:00
|
|
|
result = "alert";
|
2020-01-18 09:55:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select unassigned indicator.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String selectUnassignedIndicator(final long value)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-18 23:02:42 +01:00
|
|
|
result = "alert";
|
2020-01-18 09:55:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-05 17:24:43 +01:00
|
|
|
/**
|
|
|
|
* To human long.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanLong(final LocalDateTime value)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = toHumanLong(value, null);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To human long.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @param defaultValue
|
|
|
|
* the default value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanLong(final LocalDateTime value, final String defaultValue)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = value.format(PATTERN_LONGDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To human short.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanShort(final LocalDateTime value)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = toHumanShort(value, null);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-22 19:37:28 +01:00
|
|
|
* To human short.
|
|
|
|
*
|
2020-01-05 17:24:43 +01:00
|
|
|
* @param value
|
2020-01-22 19:37:28 +01:00
|
|
|
* the value
|
2020-01-05 17:24:43 +01:00
|
|
|
* @param defaultValue
|
2020-01-22 19:37:28 +01:00
|
|
|
* the default value
|
|
|
|
* @return the string
|
2020-01-05 17:24:43 +01:00
|
|
|
*/
|
|
|
|
public static String toHumanShort(final LocalDateTime value, final String defaultValue)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = value.format(PATTERN_SHORTDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Integer toInteger(final String value)
|
|
|
|
{
|
|
|
|
Integer result;
|
|
|
|
|
|
|
|
if ((value == null) || (!NumberUtils.isDigits(value)))
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = Integer.parseInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-19 23:33:40 +01:00
|
|
|
/**
|
|
|
|
* To Json numbers.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toJSonNumbers(final StringList source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = StringsUtils.toString(source, "[", ",", "]");
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To Json strings.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toJSonStrings(final StringList source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
StringList target = new StringList();
|
|
|
|
|
|
|
|
target.append("[");
|
|
|
|
for (String string : source)
|
|
|
|
{
|
|
|
|
target.append("'");
|
|
|
|
target.append(string);
|
|
|
|
target.append("'");
|
|
|
|
target.append(",");
|
|
|
|
}
|
|
|
|
target.removeLast();
|
|
|
|
target.append("]");
|
|
|
|
|
|
|
|
result = target.toString();
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:47:43 +01:00
|
|
|
/**
|
|
|
|
* To year week.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toYearWeek(final LocalDate source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = source.format(DateTimeFormatter.ofPattern("yyyyww", Locale.FRANCE));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
2020-01-05 17:24:43 +01:00
|
|
|
}
|