|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace StatusPageClient.Models |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// An event relating to a status page and/or its subcomponents. |
| 10 | + /// </summary> |
| 11 | + public class Incident : IEquatable<Incident> |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// The unique identifier of this incident. |
| 15 | + /// </summary> |
| 16 | + public string Id { get; private set; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// A friendly name for this incident. |
| 20 | + /// </summary> |
| 21 | + public string Name { get; private set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The URL for this incident. |
| 25 | + /// </summary> |
| 26 | + public string Shortlink { get; private set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// The impact this incident had upon <see cref="ImpactedComponents"/>. |
| 30 | + /// </summary> |
| 31 | + public string Impact { get; private set; } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// UTC date that this incident was created. |
| 35 | + /// </summary> |
| 36 | + public DateTime CreatedOn { get; private set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// UTC date that this incident was last updated. |
| 40 | + /// </summary> |
| 41 | + public DateTime UpdatedOn { get; private set; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// If present, the UTC date that this incident began to impact <see cref="ImpactedComponents"/>. |
| 45 | + /// </summary> |
| 46 | + public DateTime? StartedOn { get; private set; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// If present, the UTC date that this incident was resolved. |
| 50 | + /// </summary> |
| 51 | + public DateTime? ResolvedOn { get; private set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Current status of this incident. |
| 55 | + /// </summary> |
| 56 | + public StatusDescription Status { get; private set; } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// All updates/changes of status for this incident. |
| 60 | + /// </summary> |
| 61 | + public IEnumerable<IncidentUpdate> Updates { get; private set; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// All components impacted by this incident. |
| 65 | + /// </summary> |
| 66 | + public IEnumerable<Component> ImpactedComponents { get; private set; } |
| 67 | + |
| 68 | + internal Incident(ResponseObjects.Incident respIncident, IEnumerable<Component> componentsLookup) |
| 69 | + { |
| 70 | + Id = respIncident.Id; |
| 71 | + Name = respIncident.Name; |
| 72 | + Shortlink = respIncident.Shortlink; |
| 73 | + Impact = respIncident.Impact; |
| 74 | + |
| 75 | + CreatedOn = respIncident.CreatedOn.UtcDateTime; |
| 76 | + UpdatedOn = respIncident.UpdatedOn.UtcDateTime; |
| 77 | + |
| 78 | + if (respIncident.StartedOn.HasValue) |
| 79 | + { |
| 80 | + StartedOn = respIncident.StartedOn.Value.UtcDateTime; |
| 81 | + } |
| 82 | + if (respIncident.ResolvedOn.HasValue) |
| 83 | + { |
| 84 | + ResolvedOn = respIncident.ResolvedOn.Value.UtcDateTime; |
| 85 | + } |
| 86 | + |
| 87 | + Status = new StatusDescription(respIncident.Status, null); |
| 88 | + |
| 89 | + if (respIncident.ComponentImpacts != null) |
| 90 | + { |
| 91 | + var componentDictionary = componentsLookup.ToDictionary(c => c.Id); |
| 92 | + |
| 93 | + ImpactedComponents = (from ic in respIncident.ComponentImpacts |
| 94 | + where componentDictionary.ContainsKey(ic.Id) |
| 95 | + select componentDictionary[ic.Id]).ToArray(); |
| 96 | + } |
| 97 | + |
| 98 | + if (respIncident.Updates != null) |
| 99 | + { |
| 100 | + Updates = respIncident.Updates.Select(iu => new IncidentUpdate(iu, componentsLookup)).ToArray(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Returns a string that represents the current object. |
| 106 | + /// </summary> |
| 107 | + /// <returns>A string that represents the current object.</returns> |
| 108 | + public override string ToString() |
| 109 | + { |
| 110 | + return $"{Name} ({Id})"; |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Returns true if the other <see cref="Incident"/> instance has the same <see cref="Id"/> as this one. |
| 115 | + /// </summary> |
| 116 | + /// <param name="other">The other <see cref="Incident"/> to compare.</param> |
| 117 | + /// <returns>True if the two objects match, false otherwise</returns> |
| 118 | + public bool Equals(Incident other) |
| 119 | + { |
| 120 | + return other != null && string.Equals(Id, other.Id, StringComparison.InvariantCultureIgnoreCase); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Determines whether the specified object is equal to the current object. |
| 125 | + /// </summary> |
| 126 | + /// <param name="obj">The object to compare with the current object.</param> |
| 127 | + /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns> |
| 128 | + public override bool Equals(object obj) |
| 129 | + { |
| 130 | + return Equals(obj as Incident); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Serves as the default hash function. |
| 135 | + /// </summary> |
| 136 | + /// <returns>A hash code for the current object.</returns> |
| 137 | + public override int GetHashCode() |
| 138 | + { |
| 139 | + return Id.GetHashCode(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments