|
| 1 | +package io.sqooba.oss.timeseries.entity |
| 2 | + |
| 3 | +import io.sqooba.oss.timeseries.labels.TsLabel |
| 4 | + |
| 5 | +import scala.util.Try |
| 6 | + |
| 7 | +trait TsId { |
| 8 | + val entityId: TimeSeriesEntityId |
| 9 | + val label: TsLabel |
| 10 | + val id: String |
| 11 | +} |
| 12 | + |
| 13 | +/** Identifies a single materialized time series. |
| 14 | + * |
| 15 | + * @param entityId An entity which has time series data |
| 16 | + * @param label The label of the time series (e.g sensor data) |
| 17 | + */ |
| 18 | +case class TripTsId(entityId: TimeSeriesEntityId, label: TsLabel) extends TsId { |
| 19 | + import TripTsId._ |
| 20 | + |
| 21 | + /** Formats the [[TsId]] into a string of the format: LABEL-PREFIX_ID */ |
| 22 | + override val id: String = s"${label.value}$keySeparator${entityId.formatted}" |
| 23 | +} |
| 24 | + |
| 25 | +object TripTsId { |
| 26 | + |
| 27 | + val keySeparator = "-" |
| 28 | + val prefixSeparator = "_" |
| 29 | + |
| 30 | + /** Tries to construct a [[TsId]] from a raw string representation. By using the |
| 31 | + * given entityParser it will try to parse the entity type and unit of the label. |
| 32 | + * |
| 33 | + * The label is a string (anything is accepted). |
| 34 | + * The idRaw is in the format PREFIX_ID. |
| 35 | + * - PREFIX must be a known prefix, by the given entityParser |
| 36 | + * - ID must be a number |
| 37 | + */ |
| 38 | + def from(label: String, idRaw: String)(implicit entityParser: EntityParser): Try[TsId] = { |
| 39 | + val idTokens = idRaw.split(prefixSeparator) |
| 40 | + |
| 41 | + for { |
| 42 | + idLong <- Try(idTokens(1).toLong) |
| 43 | + id <- TimeSeriesEntityId.from(idTokens(0), idLong) |
| 44 | + } yield TripTsId(id, TsLabel(label)) |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +/** Identifies a single materialized time series. |
| 50 | + * |
| 51 | + * @param entityId An entity which has time series data |
| 52 | + * @param label The label of the time series (e.g sensor data) |
| 53 | + */ |
| 54 | +case class GenericTsId(entityId: TimeSeriesEntityId, label: TsLabel) extends TsId { |
| 55 | + import GenericTsId._ |
| 56 | + |
| 57 | + /** Formats the [[TsId]] into a string of the format: LABEL-PREFIX_ID */ |
| 58 | + override val id: String = s"${label.value}$keySeparator${entityId.formatted}" |
| 59 | +} |
| 60 | + |
| 61 | +object GenericTsId { |
| 62 | + |
| 63 | + val keySeparator = "-" |
| 64 | + val prefixSeparator = "_" |
| 65 | + |
| 66 | + /** Tries to construct a [[TsId]] from a raw string representation. By using the |
| 67 | + * given entityParser it will try to parse the entity type and unit of the label. |
| 68 | + * |
| 69 | + * The label is a string (anything is accepted). |
| 70 | + * The idRaw is in the format PREFIX_ID. |
| 71 | + * - PREFIX must be a known prefix, by the given entityParser |
| 72 | + * - ID must be a number |
| 73 | + */ |
| 74 | + def from(label: String, idRaw: String)(implicit entityParser: EntityParser): Try[TsId] = { |
| 75 | + val idTokens = idRaw.split(prefixSeparator) |
| 76 | + |
| 77 | + for { |
| 78 | + idLong <- Try(idTokens(1).toLong) |
| 79 | + id <- TimeSeriesEntityId.from(idTokens(0), idLong) |
| 80 | + } yield GenericTsId(id, TsLabel(label)) |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments