agirstatool/src/org/april/agirstatool/core/AgirStatoolUtils.java

587 lines
11 KiB
Java

/*
* 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;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
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;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
* The Class AgirStatoolUtils.
*/
public class AgirStatoolUtils
{
private static Logger logger = LoggerFactory.getLogger(ProjectPage.class);
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);
public static StringList buildWeekDuration(final Project project, final LocalDate start, final LocalDate end)
{
StringList result;
result = new StringList();
if (start != null)
{
Issues issues = new Issues();
issues.addAll(project.issues());
issues.sort(IssueComparator.Sorting.CLOSEDON);
LocalDate date = AgirStatoolUtils.normaliseWeekDate(start);
LocalDate normalizedEnd = AgirStatoolUtils.normaliseWeekDate(end);
while (!date.isAfter(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 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;
}
/**
* 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);
int index = source.indexOf(toYearWeek(start));
if (index == -1)
{
index = 0;
}
while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd))
{
String dateToken = toYearWeek(date);
long count;
if (index < source.size())
{
DateCount current = source.get(index);
// AgirStatoolUtils.logger.info("===> " + dateToken + " " +
// current.getDate());
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))
{
String dateToken = toYearWeek(date);
DateCount current = source.get(dateToken);
if (current != null)
{
count += current.getCount();
}
result.add(new DateCount(dateToken, count));
date = date.plusWeeks(1);
}
//
return result;
}
/**
* Gets the current time in long format.
*
* @return the long
*/
public static long now()
{
return new Date().getTime();
}
/**
* 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)
{
result = "caution";
}
else if (value < 50)
{
result = "warning";
}
else
{
result = "alert";
}
//
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
{
result = "alert";
}
//
return result;
}
/**
* 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;
}
/**
* To human short.
*
* @param value
* the value
* @param defaultValue
* the default value
* @return the string
*/
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;
}
/**
* 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;
}
/**
* 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;
}
}