|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package com.redhat.devtools.intellij.kubernetes.editor.describe |
| 12 | + |
| 13 | +import com.redhat.devtools.intellij.kubernetes.editor.describe.paragraphs.NamedSequence |
| 14 | +import com.redhat.devtools.intellij.kubernetes.editor.describe.paragraphs.NamedValue |
| 15 | +import com.redhat.devtools.intellij.kubernetes.editor.describe.paragraphs.Paragraph |
| 16 | +import java.time.DateTimeException |
| 17 | +import java.time.Duration |
| 18 | +import java.time.LocalDateTime |
| 19 | +import java.time.ZonedDateTime |
| 20 | +import java.time.format.DateTimeFormatter |
| 21 | + |
| 22 | +object DescriptionConstants { |
| 23 | + |
| 24 | + object Labels { |
| 25 | + const val NAME = "Name" |
| 26 | + const val NAMESPACE = "Namespace" |
| 27 | + } |
| 28 | + |
| 29 | + object Values { |
| 30 | + const val NONE = "<none>" |
| 31 | + const val UNSET = "<unset>" |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fun createValueOrSequence(title: String, items: List<String>?): Paragraph? { |
| 36 | + return if (items.isNullOrEmpty()) { |
| 37 | + null |
| 38 | + } else if (items.size == 1) { |
| 39 | + NamedValue(title, items.first()) |
| 40 | + } else { |
| 41 | + NamedSequence(title, items) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +fun createValues(map: Map<String, String>?): List<NamedValue> { |
| 46 | + if (map.isNullOrEmpty()) { |
| 47 | + return emptyList() |
| 48 | + } |
| 49 | + return map.entries.map { entry -> NamedValue(entry.key, entry.value) } |
| 50 | +} |
| 51 | + |
| 52 | +fun toString(items: List<String>?): String? { |
| 53 | + return items?.joinToString("\n") |
| 54 | +} |
| 55 | + |
| 56 | +fun toRFC1123Date(dateTime: String?): String? { |
| 57 | + if (dateTime == null) { |
| 58 | + return null |
| 59 | + } |
| 60 | + val parsed = LocalDateTime.parse(dateTime, DateTimeFormatter.ISO_ZONED_DATE_TIME) |
| 61 | + val zoned = parsed.atOffset(ZonedDateTime.now().offset) |
| 62 | + return DateTimeFormatter.RFC_1123_DATE_TIME.format(zoned) |
| 63 | +} |
| 64 | + |
| 65 | +fun toRFC1123DateOrUnrecognized(dateTime: String?): String? { |
| 66 | + return try { |
| 67 | + toRFC1123Date(dateTime) |
| 68 | + } catch (e: DateTimeException) { |
| 69 | + "Unrecognized Date: $dateTime" |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Returns a human-readable form of the given date/time since the given date/time. |
| 75 | + * Returns `null` if the given dateTime is not understood. |
| 76 | + * The logic is copied from k8s.io/apimachinery/util/duration/ duration/HumanDuration. |
| 77 | + * |
| 78 | + * @see [k8s.io/apimachinery/util/duration/duration/HumanDuration](https://github.com/kubernetes/apimachinery/blob/d7e1c5311169d5ece2db0ae0118066859aa6f7d8/pkg/util/duration/duration.go#L48) |
| 79 | + * @see |
| 80 | + */ |
| 81 | +fun toHumanReadableDurationSince(dateTime: String?, since: LocalDateTime): String? { |
| 82 | + if (dateTime == null) { |
| 83 | + return null |
| 84 | + } |
| 85 | + return try { |
| 86 | + val parsed = LocalDateTime.parse(dateTime, DateTimeFormatter.ISO_ZONED_DATE_TIME) |
| 87 | + val difference = if (since.isBefore(parsed)) { |
| 88 | + Duration.between(since, parsed) |
| 89 | + } else { |
| 90 | + Duration.between(parsed, since) |
| 91 | + } |
| 92 | + val seconds = difference.toSeconds() |
| 93 | + return when { |
| 94 | + seconds < 60 * 2 -> |
| 95 | + // < 2 minutes |
| 96 | + "${seconds}s" |
| 97 | + |
| 98 | + seconds < 60 * 10 -> |
| 99 | + // < 10 minutes |
| 100 | + "${difference.toMinutesPart()}m${difference.toSecondsPart()}s" |
| 101 | + |
| 102 | + seconds < 60 * 60 * 3 -> |
| 103 | + // < 3 hours |
| 104 | + "${difference.toMinutes()}m" |
| 105 | + |
| 106 | + seconds < 60 * 60 * 8 -> |
| 107 | + // < 8 hours |
| 108 | + "${difference.toHoursPart()}h" |
| 109 | + |
| 110 | + seconds < 60 * 60 * 48 -> |
| 111 | + // < 48 hours |
| 112 | + "${difference.toHours()}h${difference.toMinutesPart()}m" |
| 113 | + |
| 114 | + seconds < 60 * 60 * 24 * 8 -> { |
| 115 | + // < 192 hours |
| 116 | + if (difference.toHoursPart() == 0) { |
| 117 | + "${difference.toDaysPart()}d" |
| 118 | + } else { |
| 119 | + "${difference.toDaysPart()}d${difference.toHoursPart()}h" |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + seconds < 60 * 60 * 24 * 365 * 2 -> |
| 124 | + // < 2 years |
| 125 | + "${difference.toDaysPart()}d" |
| 126 | + |
| 127 | + seconds < 60 * 60 * 24 * 365 * 8 -> { |
| 128 | + // < 8 years |
| 129 | + val years = difference.toDaysPart() / 365 |
| 130 | + "${years}y${difference.toDaysPart() % 365}d" |
| 131 | + } |
| 132 | + |
| 133 | + else -> |
| 134 | + "${difference.toDaysPart() / 365}y" |
| 135 | + } |
| 136 | + } catch (e: DateTimeException) { |
| 137 | + null |
| 138 | + } |
| 139 | +} |
0 commit comments