generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_aggregated_metrics.py
30 lines (27 loc) · 1.35 KB
/
03_aggregated_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Databricks notebook source
# MAGIC %md
# MAGIC ### 3. Aggregated Metrics
# MAGIC In our final set of calculations, we'll create an aggregate (gold layer) table that calculates defect rates by device, factory, and model id. This will give us some insight into the effectiveness of the physics-based rules from our field maintenance engineering team and allow us to monitor trends over time. In our dashboard in the next notebook, this table will serve as the basis for monitoring defect rate by factory.
# COMMAND ----------
# DBTITLE 1,Gold Inspection Table
from util.configuration import config
import dlt
from pyspark.sql.functions import lit, col, sum as ps_sum, when, avg, count
@dlt.table(
name=config['gold_name'],
comment='Aggregates defects by categorical variables'
)
def aggregate_gold_table():
silver = dlt.read(config['silver_name'])
return (
silver
.groupBy('device_id', 'factory_id', 'model_id', 'defect')
.agg(
count('*').alias('count'),
avg(col('sensor_temperature')).alias('average_temperature'),
avg(col('sensor_density')).alias('average_density'),
avg(col('sensor_delay')).alias('average_delay'),
avg(col('sensor_rotation_speed')).alias('average_rotation_speed'),
avg(col('sensor_air_pressure')).alias('average_air_pressure')
)
)