Skip to content

Commit

Permalink
Add distribution points intake endpoint (#1019)
Browse files Browse the repository at this point in the history
* fix model and example generation

* attempt to fix python examples

* remove item created by merge

* Regenerate client from commit 4569968f of spec repo

Co-authored-by: Sherzod K <sherzod.karimov@datadoghq.com>
Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com>
Co-authored-by: ci.datadog-api-spec <packages@datadoghq.com>
  • Loading branch information
3 people authored Jun 13, 2022
1 parent d40d2d6 commit 78014e9
Show file tree
Hide file tree
Showing 17 changed files with 702 additions and 24 deletions.
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.6.2",
"regenerated": "2022-06-09 11:12:56.877550",
"spec_repo_commit": "b9e36dc8"
"regenerated": "2022-06-10 18:23:54.710325",
"spec_repo_commit": "4569968f"
},
"v2": {
"apigentools_version": "1.6.2",
"regenerated": "2022-06-09 11:12:56.889464",
"spec_repo_commit": "b9e36dc8"
"regenerated": "2022-06-10 18:23:54.722867",
"spec_repo_commit": "4569968f"
}
}
}
158 changes: 158 additions & 0 deletions .generator/schemas/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,96 @@ components:
type: integer
readOnly: true
type: object
DistributionPoint:
description: Array of distribution points.
example:
- 1575317847.0
- - 0.5
- 1.0
items:
description: List of distribution point.
oneOf:
- $ref: '#/components/schemas/DistributionPointTimestamp'
- $ref: '#/components/schemas/DistributionPointData'
maxItems: 2
minItems: 2
type: array
DistributionPointData:
description: Distribution point data.
items:
description: List of distribution point data.
format: double
type: number
type: array
DistributionPointTimestamp:
description: Distribution point timestamp. It should be in seconds and current.
format: double
type: number
DistributionPointsContentEncoding:
description: HTTP header used to compress the media-type.
enum:
- deflate
type: string
x-enum-varnames:
- DEFLATE
DistributionPointsPayload:
description: The distribution points payload.
properties:
series:
description: A list of distribution points series to submit to Datadog.
example:
- metric: system.load.1
points:
- - 1475317847.0
- - 1.0
- 2.0
items:
$ref: '#/components/schemas/DistributionPointsSeries'
type: array
required:
- series
type: object
DistributionPointsSeries:
description: A distribution points metric to submit to Datadog.
properties:
host:
description: The name of the host that produced the distribution point metric.
example: test.example.com
type: string
metric:
description: The name of the distribution points metric.
example: system.load.1
type: string
points:
description: Points relating to the distribution point metric. All points
must be tuples with timestamp and a list of values (cannot be a string).
Timestamps should be in POSIX time in seconds.
items:
$ref: '#/components/schemas/DistributionPoint'
type: array
tags:
description: A list of tags associated with the distribution point metric.
example:
- environment:test
items:
description: Individual tags.
type: string
type: array
type:
$ref: '#/components/schemas/DistributionPointsType'
required:
- metric
- points
type: object
DistributionPointsType:
default: distribution
description: The type of the distribution point.
enum:
- distribution
example: distribution
type: string
x-enum-varnames:
- DISTRIBUTION
DistributionWidgetDefinition:
description: "The Distribution visualization is another way of showing metrics\naggregated
across one or several tags, such as hosts.\nUnlike the heat map, a distribution
Expand Down Expand Up @@ -18016,6 +18106,74 @@ paths:
x-menu-order: 6
x-undo:
type: idempotent
/api/v1/distribution_points:
post:
description: "The distribution points end-point allows you to post distribution
data that can be graphed on Datadog\u2019s dashboards."
operationId: SubmitDistributionPoints
parameters:
- description: HTTP header used to compress the media-type.
in: header
name: Content-Encoding
required: false
schema:
$ref: '#/components/schemas/DistributionPointsContentEncoding'
requestBody:
content:
text/json:
examples:
dynamic-points:
description: "Post time-series data that can be graphed on Datadog\u2019s
dashboards."
externalValue: examples/metrics/distribution-points.json.sh
summary: Dynamic Points
x-variables:
NOW: $(date +%s)
schema:
$ref: '#/components/schemas/DistributionPointsPayload'
required: true
responses:
'202':
content:
text/json:
schema:
$ref: '#/components/schemas/IntakePayloadAccepted'
description: Payload accepted
'400':
content:
text/json:
schema:
$ref: '#/components/schemas/APIErrorResponse'
description: Bad Request
'403':
content:
text/json:
schema:
$ref: '#/components/schemas/APIErrorResponse'
description: Authentication error
'408':
content:
text/json:
schema:
$ref: '#/components/schemas/APIErrorResponse'
description: Request timeout
'413':
content:
text/json:
schema:
$ref: '#/components/schemas/APIErrorResponse'
description: Payload too large
'429':
$ref: '#/components/responses/TooManyRequestsResponse'
security:
- apiKeyAuth: []
summary: Submit distribution points
tags:
- Metrics
x-codegen-request-body-name: body
x-menu-order: 1
x-undo:
type: safe
/api/v1/downtime:
get:
description: Get all scheduled downtimes.
Expand Down
19 changes: 19 additions & 0 deletions .generator/src/generator/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


MODEL_IMPORT_TPL = "datadog_api_client.{version}.model.{name}"
PRIMITIVE_TYPES = ["string", "number", "boolean", "integer"]

EDGE_CASES = {}
replacement_file = (
Expand Down Expand Up @@ -237,6 +238,24 @@ def format_data_with_schema_list(
assert version is not None
name, imports = get_name_and_imports(schema, version, imports)

if "oneOf" in schema:
for sub_schema in schema["oneOf"]:
try:
value, one_of_imports = format_data_with_schema(
data,
sub_schema,
replace_values=replace_values,
version=version,
)
except (KeyError, ValueError) as e:
continue
# Workaround to not generate schema for primitive nested oneOfs
if sub_schema.get("items", {}).get("type") in PRIMITIVE_TYPES:
return data, imports

return value, one_of_imports
raise ValueError(f"{data} is not valid oneOf {schema}")

parameters = ""
for d in data:
value, extra_imports = format_data_with_schema(
Expand Down
53 changes: 36 additions & 17 deletions .generator/src/generator/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from . import formatter

PRIMITIVE_TYPES = ["string", "number", "boolean", "integer"]

with (pathlib.Path(__file__).parent / "replacement.json").open() as f:
EDGE_CASES = json.load(f)

Expand All @@ -20,20 +22,7 @@ def load(filename):
return JsonRef.replace_refs(yaml.load(fp, Loader=CSafeLoader))


def type_to_python(schema, alternative_name=None):
"""Return Python type name for the type."""
name = formatter.get_name(schema)
if name:
if "enum" in schema:
return name
if schema.get("type", "object") in ("object", "array"):
return name

type_ = schema.get("type")
if type_ is None:
if "items" in schema:
type_ = "array"

def type_ty_python_helper(type_, schema, alternative_name=None, in_list=False):
if type_ is None:
return "bool, date, datetime, dict, float, int, list, str, none_type"

Expand All @@ -51,7 +40,7 @@ def type_to_python(schema, alternative_name=None):
elif type_ == "boolean":
return "bool"
elif type_ == "array":
return "[{}]".format(type_to_python(schema["items"]))
return "[{}]".format(type_to_python(schema["items"], in_list=True))
elif type_ == "object":
if "additionalProperties" in schema:
nested_schema = schema["additionalProperties"]
Expand All @@ -76,6 +65,32 @@ def type_to_python(schema, alternative_name=None):
raise ValueError(f"Unknown type {type_}")


def type_to_python(schema, alternative_name=None, in_list=False):
"""Return Python type name for the type."""
name = formatter.get_name(schema)
if name:
if "enum" in schema:
return name
if schema.get("type", "object") in ("object", "array"):
return name

type_ = schema.get("type")
if type_ is None:
if "oneOf" in schema and in_list:
type_ = ""
for child in schema["oneOf"]:
# We do not generate model for nested primitive oneOfs
if in_list and "items" in child and child["items"].get("type") in PRIMITIVE_TYPES:
type_ += f"{type_ty_python_helper(child.get('type'), child, in_list=in_list)},"
else:
type_ += f"{type_to_python(child, in_list=in_list)},"
return type_
if "items" in schema:
type_ = "array"

return type_ty_python_helper(type_, schema, alternative_name=alternative_name, in_list=in_list)


def get_type_for_attribute(schema, attribute, current_name=None):
"""Return Python type name for the attribute."""
child_schema = schema.get("properties", {}).get(attribute)
Expand Down Expand Up @@ -120,7 +135,7 @@ def get_enum_default(model):
return model["enum"][0] if len(model["enum"]) == 1 else model.get("default")


def child_models(schema, alternative_name=None, seen=None):
def child_models(schema, alternative_name=None, seen=None, in_list=False):
seen = seen or set()
current_name = formatter.get_name(schema)
name = current_name or alternative_name
Expand All @@ -133,6 +148,9 @@ def child_models(schema, alternative_name=None, seen=None):
if "oneOf" in schema:
has_sub_models = True
for child in schema["oneOf"]:
# Don't generate models for nested primitive types
if in_list and child.get("type") in PRIMITIVE_TYPES:
return
yield from child_models(child, seen=seen)
if "anyOf" in schema:
has_sub_models = True
Expand All @@ -144,6 +162,7 @@ def child_models(schema, alternative_name=None, seen=None):
schema["items"],
None,
seen=seen,
in_list=True
)

if schema.get("type") == "object" or "properties" in schema or has_sub_models:
Expand All @@ -152,7 +171,7 @@ def child_models(schema, alternative_name=None, seen=None):
return

if name is None:
raise ValueError(f"Schema {schema} has no name")
return

if name in seen:
return
Expand Down
4 changes: 2 additions & 2 deletions .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class {{ name }}(ModelSimple):
}
{%- else %}
return {
"value": ([{{ type_to_python(model.get("items")) }}{%- if model["items"].get("nullable") %}, none_type{%- endif %}],),
"value": ([{{ type_to_python(model.get("items"), in_list=True) }}{%- if model["items"].get("nullable") %}, none_type{%- endif %}],),
}
{%- endif %}

Expand All @@ -68,7 +68,7 @@ class {{ name }}(ModelSimple):
{%- if ref %}
:type value: [{{ ref }}]
{%- else %}
:type value: [{{ type_to_python(model.get("items")) }}{%- if model["items"].get("nullable") %}, none_type{%- endif %}]
:type value: [{{ type_to_python(model.get("items"), in_list=True) }}{%- if model["items"].get("nullable") %}, none_type{%- endif %}]
{%- endif %}
"""
super().__init__(kwargs)
Expand Down
35 changes: 35 additions & 0 deletions docs/datadog_api_client.v1.model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,41 @@ deleted\_monitor
:members:
:show-inheritance:

distribution\_point
-------------------

.. automodule:: datadog_api_client.v1.model.distribution_point
:members:
:show-inheritance:

distribution\_points\_content\_encoding
---------------------------------------

.. automodule:: datadog_api_client.v1.model.distribution_points_content_encoding
:members:
:show-inheritance:

distribution\_points\_payload
-----------------------------

.. automodule:: datadog_api_client.v1.model.distribution_points_payload
:members:
:show-inheritance:

distribution\_points\_series
----------------------------

.. automodule:: datadog_api_client.v1.model.distribution_points_series
:members:
:show-inheritance:

distribution\_points\_type
--------------------------

.. automodule:: datadog_api_client.v1.model.distribution_points_type
:members:
:show-inheritance:

distribution\_widget\_definition
--------------------------------

Expand Down
Loading

0 comments on commit 78014e9

Please sign in to comment.