/* * Copyright (C) 2020 Christian Pierre MOMON * * 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 . */ package org.april.agirstatool.core.pages; import java.io.IOException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import org.apache.commons.codec.digest.DigestUtils; import org.april.agirstatool.charts.DateCountList; import org.april.agirstatool.core.AgirStatool; import org.april.agirstatool.core.AgirStatoolException; import org.april.agirstatool.core.AgirStatoolUtils; import org.april.agirstatool.core.Project; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.strings.StringList; import fr.devinsy.xidyn.utils.XidynUtils; /** * The Class projectsRawPageBuilder. */ public class CreatedConcludedCountChartView { private static Logger logger = LoggerFactory.getLogger(CreatedConcludedCountChartView.class); /** * Builds the. * * @param projects * the projects * @return the string * @throws AgirStatoolException * the agir statool exception */ public static String build(final String title, final Project project) throws AgirStatoolException { String result; try { logger.info("Building created/concluded chart view…"); if (project.hasIssue()) { String source = XidynUtils.load(AgirStatool.class.getResource("/org/april/agirstatool/core/pages/chartLineView.xhtml")); String code = XidynUtils.extractBodyContent(source); code = code.replaceAll("myChart", "myChart_" + DigestUtils.md5Hex(title + "lineBar")); StringList labels = buildWeekLabels(project.issueStats().getFirstCreate().toLocalDate()); code = code.replaceAll("labels: \\[.*\\]", "labels: " + AgirStatoolUtils.toJSonStrings(labels)); StringList values = project.issueStats().getWeekCreatedIssueCounts().toValueList(); code = code.replaceAll("data: \\[.*\\]", "data: " + AgirStatoolUtils.toJSonNumbers(values)); values = project.issueStats().getWeekConcludedIssueCounts().toValueList(); code = code.replaceAll("data: \\[.*\\] ", "data: " + AgirStatoolUtils.toJSonNumbers(values)); result = code.toString(); } else { result = "No issue."; } } catch (IOException exception) { throw new AgirStatoolException("Error building ProjectsRaw view: " + exception.getMessage(), exception); } // return result; } /** * Builds the 3 months. * * @param title * the title * @param project * the project * @return the string * @throws AgirStatoolException * the agir statool exception */ public static String build(final String title, final Project project, final int monthCount) throws AgirStatoolException { String result; try { logger.info("Building created/closed x months chart view…"); if (project.hasIssue() && !project.getName().equals("*")) { String source = XidynUtils.load(AgirStatool.class.getResource("/org/april/agirstatool/core/pages/chartLineView.xhtml")); String code = XidynUtils.extractBodyContent(source); code = code.replaceAll("myChart", "myChart_" + DigestUtils.md5Hex(title + "lineChartXMonths")); LocalDate start = LocalDate.now().minusMonths(monthCount); LocalDate end = LocalDate.now(); StringList labels = buildWeekLabels(start); code = code.replaceAll("labels: \\[.*\\]", "labels: " + AgirStatoolUtils.toJSonStrings(labels)); DateCountList dates = project.issueStats().getWeekCreatedIssueCounts(); StringList values = AgirStatoolUtils.normalizedWeekCountList(dates, start, end).toValueList(); code = code.replaceAll("data: \\[.*\\]", "data: " + AgirStatoolUtils.toJSonNumbers(values)); dates = project.issueStats().getWeekConcludedIssueCounts(); values = AgirStatoolUtils.normalizedWeekCountList(dates, start, end).toValueList(); code = code.replaceAll("data: \\[.*\\] ", "data: " + AgirStatoolUtils.toJSonNumbers(values)); result = code.toString(); } else { result = "No issue."; } } catch (IOException exception) { throw new AgirStatoolException("Error building ProjectsRaw view: " + exception.getMessage(), exception); } // return result; } /** * Builds the week labels. * * @param start * the start * @return the string list */ private static StringList buildWeekLabels(final LocalDate start) { StringList result; result = new StringList(); if (start != null) { LocalDate end = AgirStatoolUtils.normaliseWeekDate(LocalDate.now()); LocalDate date = AgirStatoolUtils.normaliseWeekDate(start); while (date.isBefore(end) || date.isEqual(end)) { String label = date.format(DateTimeFormatter.ofPattern("yyyy-MMM")); result.add(label); date = date.plusWeeks(1); } } // return result; } }