src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt

Sun, 08 Jan 2023 17:07:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Jan 2023 17:07:26 +0100
changeset 268
ca5501d851fa
parent 267
d8ec2d8ffa82
child 271
f8f5e82944fa
permissions
-rw-r--r--

#15 add issue filters

     1 /*
     2  * Copyright 2021 Mike Becker. All rights reserved.
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions are met:
     6  *
     7  * 1. Redistributions of source code must retain the above copyright
     8  * notice, this list of conditions and the following disclaimer.
     9  *
    10  * 2. Redistributions in binary form must reproduce the above copyright
    11  * notice, this list of conditions and the following disclaimer in the
    12  * documentation and/or other materials provided with the distribution.
    13  *
    14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    24  */
    26 package de.uapcore.lightpit.viewmodel
    28 import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
    29 import com.vladsch.flexmark.ext.tables.TablesExtension
    30 import com.vladsch.flexmark.html.HtmlRenderer
    31 import com.vladsch.flexmark.parser.Parser
    32 import com.vladsch.flexmark.util.data.MutableDataSet
    33 import com.vladsch.flexmark.util.data.SharedDataKeys
    34 import de.uapcore.lightpit.HttpRequest
    35 import de.uapcore.lightpit.entities.*
    36 import de.uapcore.lightpit.types.*
    37 import kotlin.math.roundToInt
    39 class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
    40     enum class Field {
    41         DONE, PHASE, STATUS, CATEGORY, ETA, UPDATED, CREATED
    42     }
    44     data class Criteria(val field: Field, val asc: Boolean = true)
    46     override fun compare(left: Issue, right: Issue): Int {
    47         if (left == right) {
    48             return 0
    49         }
    50         for (c in criteria) {
    51             val result = when (c.field) {
    52                 Field.PHASE -> left.status.phase.compareTo(right.status.phase)
    53                 Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
    54                 Field.STATUS -> left.status.compareTo(right.status)
    55                 Field.CATEGORY -> left.category.compareTo(right.category)
    56                 Field.ETA -> left.compareEtaTo(right.eta)
    57                 Field.UPDATED -> left.updated.compareTo(right.updated)
    58                 Field.CREATED -> left.created.compareTo(right.created)
    59             }
    60             if (result != 0) {
    61                 return if (c.asc) result else -result
    62             }
    63         }
    64         return 0
    65     }
    66 }
    68 class IssueSummary {
    69     var open = 0
    70     var active = 0
    71     var done = 0
    73     val total get() = open + active + done
    75     val openPercent get() = 100 - activePercent - donePercent
    76     val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
    77     val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
    79     /**
    80      * Adds the specified issue to the summary by incrementing the respective counter.
    81      * @param issue the issue
    82      */
    83     fun add(issue: Issue) {
    84         when (issue.status.phase) {
    85             IssueStatusPhase.Open -> open++
    86             IssueStatusPhase.WorkInProgress -> active++
    87             IssueStatusPhase.Done -> done++
    88         }
    89     }
    90 }
    92 class IssueDetailView(
    93     val issue: Issue,
    94     val comments: List<IssueComment>,
    95     val project: Project,
    96     val version: Version?,
    97     val component: Component?,
    98     projectIssues: List<Issue>,
    99     val currentRelations: List<IssueRelation>,
   100     /**
   101      * Optional resource key to an error message for the relation editor.
   102      */
   103     val relationError: String?
   104 ) : View() {
   105     val relationTypes = RelationType.values()
   106     val linkableIssues = projectIssues.filterNot { it.id == issue.id }
   108     private val parser: Parser
   109     private val renderer: HtmlRenderer
   111     init {
   112         val options = MutableDataSet()
   113             .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
   114         parser = Parser.builder(options).build()
   115         renderer = HtmlRenderer.builder(
   116             options
   117                 .set(HtmlRenderer.ESCAPE_HTML, true)
   118         ).build()
   120         issue.description = formatMarkdown(issue.description ?: "")
   121         for (comment in comments) {
   122             comment.commentFormatted = formatMarkdown(comment.comment)
   123         }
   124     }
   126     private fun formatEmojis(text: String) = text
   127         .replace("(/)", "&#9989;")
   128         .replace("(x)", "&#10060;")
   129         .replace("(!)", "&#9889;")
   131     private fun formatMarkdown(text: String) =
   132         renderer.render(parser.parse(formatEmojis(text)))
   133 }
   135 class IssueEditView(
   136     val issue: Issue,
   137     val versions: List<Version>,
   138     val components: List<Component>,
   139     val users: List<User>,
   140     val project: Project, // TODO: allow null values to create issues from the IssuesServlet
   141     val version: Version? = null,
   142     val component: Component? = null
   143 ) : EditView() {
   145     val versionsUpcoming: List<Version>
   146     val versionsRecent: List<Version>
   148     val issueStatus = IssueStatus.values()
   149     val issueCategory = IssueCategory.values()
   151     init {
   152         val recent = mutableListOf<Version>()
   153         issue.affected?.let { recent.add(it) }
   154         val upcoming = mutableListOf<Version>()
   155         issue.resolved?.let { upcoming.add(it) }
   157         for (v in versions) {
   158             if (v.status.isReleased) {
   159                 if (v.status != VersionStatus.Deprecated) recent.add(v)
   160             } else {
   161                 upcoming.add(v)
   162             }
   163         }
   164         versionsRecent = recent.distinct()
   165         versionsUpcoming = upcoming.distinct()
   166     }
   167 }
   169 class IssueFilter(http: HttpRequest) {
   171     val issueStatus = IssueStatus.values()
   172     val issueCategory = IssueCategory.values()
   173     val flagIncludeDone = "f.0"
   174     val flagMine = "f.1"
   175     val flagBlocker = "f.2"
   177     val includeDone: Boolean = evalFlag(http, flagIncludeDone)
   178     val onlyMine: Boolean = evalFlag(http, flagMine)
   179     val onlyBlocker: Boolean = evalFlag(http, flagBlocker)
   180     val status: List<IssueStatus> = evalEnum(http, "s")
   181     val category: List<IssueCategory> = evalEnum(http, "c")
   183     private fun evalFlag(http: HttpRequest, name: String): Boolean {
   184         val param = http.paramArray("filter")
   185         if (param.isNotEmpty()) {
   186             if (param.contains(name)) {
   187                 http.session.setAttribute(name, true)
   188             } else {
   189                 http.session.removeAttribute(name)
   190             }
   191         }
   192         return http.session.getAttribute(name) != null
   193     }
   195     private inline fun <reified T : Enum<T>> evalEnum(http: HttpRequest, prefix: String): List<T> {
   196         val sattr = "f.${prefix}"
   197         val param = http.paramArray("filter")
   198         if (param.isNotEmpty()) {
   199             val list = param.filter { it.startsWith("${prefix}.") }
   200                 .map { it.substring(prefix.length + 1) }
   201                 .map {
   202                     try {
   203                         // quick and very dirty validation
   204                         enumValueOf<T>(it)
   205                     } catch (_: IllegalArgumentException) {
   206                         // skip
   207                     }
   208                 }
   209             if (list.isEmpty()) {
   210                 http.session.removeAttribute(sattr)
   211             } else {
   212                 http.session.setAttribute(sattr, list.joinToString(","))
   213             }
   214         }
   216         return http.session.getAttribute(sattr)
   217             ?.toString()
   218             ?.split(",")
   219             ?.map { enumValueOf(it) }
   220             ?: emptyList()
   221     }
   222 }

mercurial