Aggregating Instances: Aggregate and Histogram¶
We assume that you have generated a SDK for the Windmill
model and have a client ready to go.
pygen
automatically generates an aggregate and a histogram method for the properties in the views.
In [2]:
Copied!
from windmill import WindmillClient
from windmill import WindmillClient
In [3]:
Copied!
client = WindmillClient.from_toml("config.toml")
client = WindmillClient.from_toml("config.toml")
In [4]:
Copied!
result = client.sensor_position.aggregate("avg", "position")
result
result = client.sensor_position.aggregate("avg", "position")
result
Out[4]:
[AvgValue(property='position', value=8.15)]
The aggregate
methods combines all the aggrgagations and properties pased in
In [6]:
Copied!
result = client.windmill.aggregate("count", ["name", "windfarm", "capacity"])
result
result = client.windmill.aggregate("count", ["name", "windfarm", "capacity"])
result
Out[6]:
[CountValue(property='capacity', value=5), CountValue(property='windfarm', value=5), CountValue(property='name', value=5)]
Typically, you have to specify which properties you want to aggregate on, however, for count you can skip the properties and pygen will just take the first property it finds.
In [9]:
Copied!
result = client.windmill.aggregate("count")
result
result = client.windmill.aggregate("count")
result
Out[9]:
[CountValue(property='capacity', value=5)]
You can also group by when you aggregate
In [11]:
Copied!
result = client.windmill.aggregate("avg", "capacity", group_by="windfarm")
result
result = client.windmill.aggregate("avg", "capacity", group_by="windfarm")
result
Out[11]:
aggregates | group | |
---|---|---|
0 | [{'aggregate': 'avg', 'property': 'capacity', ... | {'windfarm': 'Hornsea 1'} |
In [12]:
Copied!
result[0].group, result[0].aggregates
result[0].group, result[0].aggregates
Out[12]:
({'windfarm': 'Hornsea 1'}, [AvgValue(property='capacity', value=7.0)])
In addition, we have histogram
aggregation on a separate method
In [29]:
Copied!
result = client.sensor_position.histogram("position", interval=5.0)
result
result = client.sensor_position.histogram("position", interval=5.0)
result
Out[29]:
HistogramValue(property='position', interval=5.0, buckets=[Bucket(start=0, count=30), Bucket(start=5, count=30), Bucket(start=10, count=15), Bucket(start=15, count=15)])
In [30]:
Copied!
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
Next section: Querying
In [ ]:
Copied!