def timeseries()

in blueprints/cloud-operations/network-quota-monitoring/src/plugins/series-firewall-rules.py [0:0]


def timeseries(resources):
  'Returns used/available/ratio firewall timeseries by project and network.'
  LOGGER.info('timeseries')
  # return a single descriptor for network as we don't have limits
  yield MetricDescriptor(f'network/firewall_rules_used',
                         'Firewall rules used per network', ('project', 'name'))
  # return used/available/ratio descriptors for project
  for dtype, name in DESCRIPTOR_ATTRS.items():
    yield MetricDescriptor(f'project/{dtype}', name, ('project',),
                           dtype.endswith('ratio'))
  # group firewall rules by network then prepare and return timeseries
  grouped = itertools.groupby(
      sorted(resources['firewall_rules'].values(), key=lambda i: i['network']),
      lambda i: i['network'])
  for network_id, rules in grouped:
    count = len(list(rules))
    labels = {
        'name': resources['networks'][network_id]['name'],
        'project': resources['networks'][network_id]['project_id']
    }
    yield TimeSeries('network/firewall_rules_used', count, labels)
  # group firewall rules by project then prepare and return timeseries
  grouped = itertools.groupby(
      sorted(resources['firewall_rules'].values(),
             key=lambda i: i['project_id']), lambda i: i['project_id'])
  for project_id, rules in grouped:
    count = len(list(rules))
    limit = int(resources['quota'][project_id]['global']['FIREWALLS'])
    labels = {'project': project_id}
    yield TimeSeries('project/firewall_rules_used', count, labels)
    yield TimeSeries('project/firewall_rules_available', limit, labels)
    yield TimeSeries('project/firewall_rules_used_ratio', count / limit, labels)