Skip to content

Commit

Permalink
SWATCH-1233: Drop UOM from the swatch
Browse files Browse the repository at this point in the history
UOM is no longer referenced in swatch openapi definitions
Automation tests are no longer referencing UOM
UOM is no longer referenced in SWatch code
UOM is no longer stored as part of events.
Double check that no queries are using UOM
check bin/ scripts to make sure UOM is no longer referenced
  • Loading branch information
Sgitario committed Sep 25, 2024
1 parent f3cfe93 commit 6e49a08
Show file tree
Hide file tree
Showing 40 changed files with 110 additions and 553 deletions.
6 changes: 0 additions & 6 deletions api/rhsm-subscriptions-api-v1-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,6 @@ paths:
schema:
$ref: '#/components/schemas/UsageType'
description: "Include only hosts for the specified usage level."
- name: uom
in: query
deprecated: true
schema:
type: string
description: "Include only hosts with a specific unit of measure. Deprecated: use 'metric_id' instead. "
- name: metric_id
in: query
schema:
Expand Down
2 changes: 1 addition & 1 deletion bin/BASILISK.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
event_type,account,org_id,instance,timestamp,expiration,role,sla,uom,value,service_type,billing_provider,billing_account_id
event_type,account,org_id,instance,timestamp,expiration,role,sla,metric_id,value,service_type,billing_provider,billing_account_id
redhat.com:BASILISK:storage_gb,account123,org123,c5mu16smf1c22rn8e730,2021-10-18T19:00:00Z,2021-10-19T19:00:00Z,BASILISK,Premium,Storage-gibibyte-months,0.2,BASILISK Instance,red hat,dummyId
redhat.com:BASILISK:storage_gb,account123,org123,c5mu16smf1c22rn8e730,2021-10-18T20:00:00Z,2021-10-19T20:00:00Z,BASILISK,Premium,Storage-gibibyte-months,0.5,BASILISK Instance,red hat,dummyId
redhat.com:BASILISK:storage_gb,account123,org123,c5mu16smf1c22rn8e730,2021-10-18T21:00:00Z,2021-10-19T21:00:00Z,BASILISK,Premium,Storage-gibibyte-months,0.75,BASILISK Instance,red hat,dummyId
Expand Down
2 changes: 1 addition & 1 deletion bin/events.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
event_type,account,org_id,instance,timestamp,expiration,role,sla,uom,value,service_type,billing_provider,billing_account_id
event_type,account,org_id,instance,timestamp,expiration,role,sla,metric_id,value,service_type,billing_provider,billing_account_id
redhat.com:openshift_dedicated:4cpu_hour,account123,org123,aeb1f778-af79-4708-8401-35a5a9e8e877,2021-10-18T19:00:00Z,2021-10-19T19:00:00Z,osd,Premium,Cores,7.6,OpenShift Cluster,red hat,dummyId
redhat.com:openshift_dedicated:4cpu_hour,account123,org123,aeb1f778-af79-4708-8401-35a5a9e8e877,2021-10-18T20:00:00Z,2021-10-19T20:00:00Z,osd,Premium,Cores,8.4,OpenShift Cluster,red hat,dummyId
redhat.com:openshift_dedicated:4cpu_hour,account123,org123,aeb1f778-af79-4708-8401-35a5a9e8e877,2021-10-18T21:00:00Z,2021-10-19T21:00:00Z,osd,Premium,Cores,5,OpenShift Cluster,red hat,dummyId
Expand Down
10 changes: 5 additions & 5 deletions bin/import-events.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@


class Measurement:
def __init__(self, uom, value):
self.uom = uom
def __init__(self, metric_id, value):
self.metric_id = metricId
self.value = value


class Event:
def __init__(self, event_type, account, org_id, instance, timestamp, expiration, role, sla, uom, value, service_type, billing_provider, billing_account_id):
def __init__(self, event_type, account, org_id, instance, timestamp, expiration, role, sla, metric_id, value, service_type, billing_provider, billing_account_id):
self.event_source = "prometheus"
self.event_type = "snapshot_" + event_type
self.account_number = account
Expand All @@ -28,7 +28,7 @@ def __init__(self, event_type, account, org_id, instance, timestamp, expiration,
self.timestamp = timestamp
self.expiration = expiration
self.display_name = instance
self.measurements = [ Measurement(uom, value)]
self.measurements = [ Measurement(metric_id, value)]
self.role = role
self.sla = sla
self.service_type = service_type
Expand Down Expand Up @@ -68,7 +68,7 @@ def post_events(url, events, dry_run=False):
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="""Insert Events from CSV over internal API.
The CSV must have a header containing the following rows:
event_type, account, org_id, instance, timestamp, expiration, role, sla, uom, value, service_type
event_type, account, org_id, instance, timestamp, expiration, role, sla, metric_id, value, service_type
""")
parser.add_argument('--host', default='localhost', help='Event endpoint host')
parser.add_argument('--port', default='8000', help='Event endpoint port')
Expand Down
21 changes: 11 additions & 10 deletions bin/insert-mock-hosts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def generate_host(inventory_id=None, insights_id=None, account_number=None, org_
hardware_type=None, measurement_type=None, num_of_guests=None, last_seen=None, product=None,
sla=None, usage=None, is_unmapped_guest=None, is_hypervisor=None, cloud_provider=None,
skip_buckets=False, is_hbi=False, is_marketplace=False, conversions_activity=None, host_type=None,
billing_provider=None, billing_account_id=None, uom=None):
billing_provider=None, billing_account_id=None, metric_id=None):
account = account_number or "account123"
org = org_id or "org123"
host_id = uuid.uuid4()
Expand Down Expand Up @@ -86,7 +86,7 @@ def generate_host(inventory_id=None, insights_id=None, account_number=None, org_

_generate_insert('hosts', **host_fields)
if not is_hbi:
_generate_measurements(host_id, uom, 1.0)
_generate_measurements(host_id, metric_id, 1.0)
if not is_hbi and not skip_buckets:
_generate_buckets(host_id, product, sla, usage, as_hypervisor=is_hypervisor, cores=cores, sockets=sockets, measurement_type=measurement_type,
billing_provider=billing_provider, billing_account_id=billing_account_id)
Expand Down Expand Up @@ -241,9 +241,9 @@ parser.add_argument('--billing-provider',
parser.add_argument('--billing-account-id',
default="_ANY",
help='Set the billing_account_id for the inserted records')
parser.add_argument('--uom',
parser.add_argument('--metric_id',
default="CORES",
help='Set the uom for the inserted records')
help='Set the metric id for the inserted records')

args = parser.parse_args()
if args.is_hbi:
Expand Down Expand Up @@ -289,15 +289,15 @@ for i in range(args.num_accounts):
is_hbi=args.is_hbi, num_of_guests=args.num_guests,
cloud_provider='AWS', is_marketplace=args.is_marketplace, conversions_activity=args.conversions_activity,
host_type=args.host_type, billing_provider=args.billing_provider, billing_account_id=args.billing_account_id,
uom=args.uom)
metric_id=args.metric_id)

for i in range(args.num_gcp):
generate_host(account_number=account, org_id=args.org, hardware_type='VIRTUALIZED', measurement_type='GOOGLE',
product=args.product, sla=args.sla, usage=args.usage, cores=args.cores, sockets=args.sockets,
is_hbi=args.is_hbi, num_of_guests=args.num_guests,
cloud_provider='GCP', is_marketplace=args.is_marketplace, conversions_activity=args.conversions_activity, host_type=args.host_type,
billing_provider=args.billing_provider, billing_account_id=args.billing_account_id,
uom=args.uom)
metric_id=args.metric_id)

for i in range(args.num_physical):
generate_host(account_number=account, org_id=args.org, hardware_type='PHYSICAL', measurement_type='PHYSICAL',
Expand All @@ -306,7 +306,7 @@ for i in range(args.num_accounts):
is_marketplace=args.is_marketplace, conversions_activity=args.conversions_activity,
host_type=args.host_type,
billing_provider=args.billing_provider, billing_account_id=args.billing_account_id,
uom=args.uom)
metric_id=args.metric_id)

hypervisor = None
for i in range(args.num_hypervisors):
Expand All @@ -317,7 +317,7 @@ for i in range(args.num_accounts):
is_marketplace=args.is_marketplace, conversions_activity=args.conversions_activity,
host_type=args.host_type,
billing_provider=args.billing_provider, billing_account_id=args.billing_account_id,
uom=args.uom)
metric_id=args.metric_id)
# create a physical entry for the host as well.
if not args.is_hbi:
generate_host(account_number=account, org_id=args.org, hardware_type='PHYSICAL', measurement_type='PHYSICAL',
Expand All @@ -326,7 +326,8 @@ for i in range(args.num_accounts):
is_hbi=args.is_hbi,
is_marketplace=args.is_marketplace, conversions_activity=args.conversions_activity,
host_type=args.host_type,
billing_provider=args.billing_provider, billing_account_id=args.billing_account_id, uom=args.uom)
billing_provider=args.billing_provider, billing_account_id=args.billing_account_id,
metric_id=args.metric_id)

create_unmapped = args.unmapped_guests
for i in range(args.num_guests):
Expand All @@ -349,7 +350,7 @@ for i in range(args.num_accounts):
is_marketplace=args.is_marketplace, conversions_activity=args.conversions_activity,
host_type=args.host_type,
billing_provider=args.billing_provider, billing_account_id=args.billing_account_id,
uom=args.uom)
metric_id=args.metric_id)

output.write('commit;')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.candlepin.subscriptions.db.EventRecordRepository;
import org.candlepin.subscriptions.db.model.EventKey;
import org.candlepin.subscriptions.db.model.EventRecord;
Expand Down Expand Up @@ -127,7 +128,6 @@ public List<EventRecord> resolveIncomingEvents(List<Event> incomingEvents) {
Measurement measurement =
new Measurement()
.withMetricId(conflictingMeasurement.getMetricId())
.withUom(conflictingMeasurement.getUom())
.withValue(conflictingMeasurement.getValue() * -1);
deductionEvent.setMeasurements(List.of(measurement));
newlyResolvedEvents.add(deductionEvent);
Expand Down Expand Up @@ -175,7 +175,7 @@ private boolean amendmentRequired(Event incomingEvent, Event resolvedEvent, Stri

private Measurement findMeasurement(Event event, String metricId) {
return event.getMeasurements().stream()
.filter(m -> UsageConflictKey.getMetricId(m).equals(metricId))
.filter(m -> StringUtils.equals(m.getMetricId(), metricId))
.findFirst()
.orElseThrow(
() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ public Event normalizeEvent(Event event) {
&& event.getServiceType().equals(ANSIBLE_INFRASTRUCTURE_HOUR)) {
event.setServiceType("Ansible Managed Node");
}
// normalize UOM to metric_id
event
.getMeasurements()
.forEach(
measurement -> {
if (measurement.getUom() != null) {
measurement.setMetricId(measurement.getUom());
measurement.setUom(null);
}
});
return event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.candlepin.subscriptions.json.Measurement;

/**
* A UsageConflictKey represents specific usage shared across multiple Events. Usage conflicts are
Expand All @@ -40,10 +38,4 @@
public class UsageConflictKey {
private String productTag;
private String metricId;

public static String getMetricId(Measurement measurement) {
return !StringUtils.isEmpty(measurement.getMetricId())
? measurement.getMetricId()
: measurement.getUom();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ public UsageConflictKey getConflictKeyForEvent(Event event) {
}
return new UsageConflictKey(
event.getProductTag().stream().findFirst().get(),
UsageConflictKey.getMetricId(event.getMeasurements().get(0)));
event.getMeasurements().get(0).getMetricId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public InstanceResponse getInstancesByProduct(
Integer limit,
ServiceLevelType sla,
UsageType usage,
String uom,
String metricId,
BillingProviderType billingProviderType,
String billingAccountId,
Expand All @@ -162,7 +161,7 @@ public InstanceResponse getInstancesByProduct(
log.debug("Get instances api called for org_id: {} and product: {}", orgId, productId);

Optional<MetricId> metricIdOptional =
Stream.of(metricId, uom)
Stream.of(metricId)
.filter(Objects::nonNull)
.map(
m -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ public RhmUsageContext lookupRhmUsageContext(BillableUsage billableUsage)
}

/**
* UsageEvents include a list of usage measurements. This data includes metric id equivalent of
* uom, and the rhmMetricId (RHM terminology) which is a configuration value of the product is
* for.
* UsageEvents include a list of usage measurements. This data includes metric id and the
* rhmMetricId (RHM terminology) which is a configuration value of the product is for.
*
* @param billableUsage billable usage to transform
* @return List&lt;UsageMeasurement%gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private void updateSnapshotWithHardwareMeasurements(
log.debug("Updating snapshot with hardware measurement: {}", type);
calculatedTotals
.getMeasurements()
.forEach((uom, value) -> snapshot.setMeasurement(type, uom, value));
.forEach((metricId, value) -> snapshot.setMeasurement(type, metricId, value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.candlepin.subscriptions.db.model.TallySnapshot;
import org.candlepin.subscriptions.db.model.Usage;
import org.candlepin.subscriptions.json.Event;
import org.candlepin.subscriptions.json.Measurement;
import org.candlepin.subscriptions.tally.UsageCalculation.Key;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -175,19 +174,12 @@ private void updateInstanceFromEvent(Event event, Host instance) {
.forEach(
measurement -> {
if (!isUsageAlreadyApplied(instance, event)) {
instance.setMeasurement(
!StringUtils.isEmpty(measurement.getMetricId())
? measurement.getMetricId()
: measurement.getUom(),
measurement.getValue());
instance.setMeasurement(measurement.getMetricId(), measurement.getValue());
}
// Every event should be applied to the totals.
instance.addToMonthlyTotal(
event.getTimestamp(),
MetricId.fromString(
!StringUtils.isEmpty(measurement.getMetricId())
? measurement.getMetricId()
: measurement.getUom()),
MetricId.fromString(measurement.getMetricId()),
measurement.getValue());
});

Expand Down Expand Up @@ -398,7 +390,7 @@ private void updateUsage(AccountUsageCalculation calc, Event event) {
.getMeasurements()
.forEach(
measurement -> {
var metricId = toMetricId(measurement);
var metricId = MetricId.fromString(measurement.getMetricId());
if (isMetricSupportedByProduct(metricId, productId)) {
calc.addUsage(
usageKey, hardwareMeasurementType, metricId, measurement.getValue());
Expand All @@ -413,11 +405,6 @@ private void updateUsage(AccountUsageCalculation calc, Event event) {
});
}

private MetricId toMetricId(Measurement measurement) {
return MetricId.fromString(
Optional.ofNullable(measurement.getMetricId()).orElse(measurement.getUom()));
}

private boolean isMetricSupportedByProduct(MetricId metric, String productId) {
return Variant.getMetricsForTag(productId).stream()
.map(Metric::getId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected TallySnapshot createSnapshotFromProductUsageCalculation(
log.debug("Updating snapshot with hardware measurement: {}", type);
calculatedTotals
.getMeasurements()
.forEach((uom, value) -> snapshot.setMeasurement(type, uom, value));
.forEach((metricId, value) -> snapshot.setMeasurement(type, metricId, value));
} else {
log.debug("Skipping hardware measurement {} since it was not found.", type);
}
Expand Down Expand Up @@ -220,12 +220,12 @@ private boolean updateTotals(

HashMap<TallyMeasurementKey, Double> beforeUpdate = new HashMap<>(snap.getTallyMeasurements());

updateUomTotals(override, snap, measurementType, prodCalcTotals);
updateMetricsTotals(override, snap, measurementType, prodCalcTotals);

return override || !beforeUpdate.equals(snap.getTallyMeasurements());
}

private void updateUomTotals(
private void updateMetricsTotals(
boolean override,
TallySnapshot snap,
HardwareMeasurementType measurementType,
Expand All @@ -234,11 +234,12 @@ private void updateUomTotals(
prodCalcTotals
.getMeasurements()
.forEach(
(uom, value) -> {
Double prodCalcMeasurement = prodCalcTotals.getMeasurement(uom);
(metricId, value) -> {
Double prodCalcMeasurement = prodCalcTotals.getMeasurement(metricId);
if (override
|| mustUpdate(snap.getMeasurement(measurementType, uom), prodCalcMeasurement)) {
snap.setMeasurement(measurementType, uom, prodCalcMeasurement);
|| mustUpdate(
snap.getMeasurement(measurementType, metricId), prodCalcMeasurement)) {
snap.setMeasurement(measurementType, metricId, prodCalcMeasurement);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/spec/internal-tally-api-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ paths:
display_name: c5mu16smf1c22rn8e730
measurements:
- value: 1.5
uom: Storage-gibibyte-months
metric_id: Storage-gibibyte-months
role: BASILISK
sla: Premium
billing_provider: red hat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ void setupTestData() {
Host host9 = createHost("inventory9", "account123");
Host host10 = createHost("inventory10", "account123");

for (MetricId uom : MetricId.getAll()) {
for (MetricId metricId : MetricId.getAll()) {
host8.addToMonthlyTotal(
OffsetDateTime.of(LocalDateTime.of(2021, 1, 1, 0, 0, 0), ZoneOffset.UTC), uom, 100.0);
OffsetDateTime.of(LocalDateTime.of(2021, 1, 1, 0, 0, 0), ZoneOffset.UTC),
metricId,
100.0);
host9.addToMonthlyTotal(
OffsetDateTime.of(LocalDateTime.of(2021, 1, 1, 0, 0, 0), ZoneOffset.UTC), uom, 0.0);
OffsetDateTime.of(LocalDateTime.of(2021, 1, 1, 0, 0, 0), ZoneOffset.UTC), metricId, 0.0);
host10.addToMonthlyTotal(
OffsetDateTime.of(LocalDateTime.of(2021, 2, 1, 0, 0, 0), ZoneOffset.UTC), uom, 50.0);
OffsetDateTime.of(LocalDateTime.of(2021, 2, 1, 0, 0, 0), ZoneOffset.UTC), metricId, 50.0);
}

addBucketToHost(host8, RHEL, ServiceLevel._ANY, Usage._ANY, HardwareMeasurementType.PHYSICAL);
Expand Down
Loading

0 comments on commit 6e49a08

Please sign in to comment.