-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e63378
commit 4cae7d3
Showing
32 changed files
with
256 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
from .snmp import snmp | ||
from .topology import topology | ||
from .rrd import rrd | ||
from .postgres import postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from flask import jsonify, Blueprint, request | ||
from agent import pipeline, data_extractor | ||
from agent.api.routes import needs_pipeline | ||
|
||
postgres = Blueprint('postgres_source', __name__) | ||
|
||
|
||
@postgres.route('/data_extractors/postgresql/<pipeline_id>', methods=['GET']) | ||
@needs_pipeline | ||
def read(pipeline_id: str): | ||
pipeline_ = pipeline.repository.get_by_id(pipeline_id) | ||
offset = request.args.get('offset') | ||
if not offset: | ||
return jsonify('No offset provided'), 400 | ||
metrics = data_extractor.postgres.extract_metrics(pipeline_, int(offset)) | ||
return jsonify(metrics) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
from . import topology | ||
from . import rrd | ||
from . import actian | ||
from . import postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .postgres import extract_metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import psycopg2 | ||
import psycopg2.extras | ||
from agent import source | ||
from agent.pipeline import Pipeline, TimestampType | ||
from agent.modules import logger | ||
|
||
logger_ = logger.get_logger(__name__) | ||
|
||
|
||
def extract_metrics(pipeline_: Pipeline, offset: int) -> list: | ||
cnx = psycopg2.connect(pipeline_.source.config[source.PostgresPySource.CONNECTION_STRING]) | ||
cursor = cnx.cursor(cursor_factory=psycopg2.extras.DictCursor) | ||
timestamp_condition = f'{pipeline_.timestamp_path} >= {offset} AND {pipeline_.timestamp_path} < {offset} + {pipeline_.interval}' | ||
query = pipeline_.query.replace('{TIMESTAMP_CONDITION}', timestamp_condition) | ||
logger_.info(f'Executing query: {query}') | ||
cursor.execute(query) | ||
return [dict(row) for row in cursor] | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ | |
from . import prtg | ||
from . import zabbix | ||
from . import actian | ||
from . import postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from agent.modules.logger import get_logger | ||
from agent.pipeline.config import stages | ||
from agent.pipeline.config.handlers.base import SchemaConfigHandler | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
class PostgresPyConfigHandler(SchemaConfigHandler): | ||
stages_to_override = { | ||
'offset': stages.jdbc.JDBCOffsetScript, | ||
'source': stages.source.postgres.PostgresPySource, | ||
'JavaScriptEvaluator_01': stages.js_convert_metrics.JSConvertMetrics30, | ||
'ExpressionEvaluator_02': stages.expression_evaluator.AddProperties30, | ||
'destination': stages.destination.Destination, | ||
'destination_watermark': stages.destination.WatermarkDestination, | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import urllib.parse | ||
from agent import monitoring | ||
from agent.pipeline.config.stages.base import JythonProcessor | ||
|
||
|
||
class PostgresPySource(JythonProcessor): | ||
JYTHON_SCRIPT = 'agent_data_extractor_sql.py' | ||
DATA_EXTRACTOR_API_ENDPOINT = 'data_extractors/postgresql' | ||
|
||
def _get_script_params(self) -> list[dict]: | ||
return [ | ||
{ | ||
'key': 'AGENT_DATA_EXTRACTOR_URL', | ||
'value': urllib.parse.urljoin( | ||
self.pipeline.streamsets.agent_external_url, '/'.join([ | ||
self.DATA_EXTRACTOR_API_ENDPOINT, | ||
'${pipeline:id()}', | ||
]) | ||
) | ||
}, | ||
{ | ||
'key': 'TIMEOUT', | ||
'value': str(self.pipeline.source.query_timeout) | ||
}, | ||
{ | ||
'key': 'MONITORING_URL', | ||
'value': monitoring.get_monitoring_source_error_url(self.pipeline) | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
agent/src/agent/pipeline/json_builder/agent_data_extractor_sql_builder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from agent.pipeline.json_builder import Builder | ||
|
||
|
||
class AgentDataExtractorSQLBuilder(Builder): | ||
VALIDATION_SCHEMA_FILE_NAME = 'agent_data_extractor_sql' |
24 changes: 24 additions & 0 deletions
24
agent/src/agent/pipeline/json_builder/json_schema_definitions/agent_data_extractor_sql.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"query": {"type": "string"}, | ||
"days_to_backfill": {"type": "integer"}, | ||
"interval": {"type": "integer"}, | ||
"delay": {"type": "integer"}, | ||
"values": {"type": "object"}, | ||
"count_records": {"type": "boolean"}, | ||
"dimensions": {"type": "array", "items": {"type": "string"}}, | ||
"watermark_in_local_timezone": {"type": "boolean"}, | ||
"query_missing_data_interval": {"type": "number"}, | ||
"timestamp": {"type": "object", "properties": { | ||
"name": {"type": "string", "minLength": 1}, | ||
"type": {"type": "string", "enum": ["unix"]} | ||
}, "required": ["name", "type"]}, | ||
"properties": {"type": "object"}, | ||
"tags": { | ||
"type": "object", | ||
"patternProperties": {"[^ \\.]+": {"type": "array", "items": {"type": "string"}}} | ||
} | ||
}, | ||
"required": ["query", "interval", "dimensions", "timestamp"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[ | ||
{ | ||
"source": "test_postgres_py", | ||
"pipeline_id": "test_postgres_py_file_short", | ||
"values": {"clicks": "gauge", "impressions": "gauge"}, | ||
"dimensions": ["adsize", "country"], | ||
"timestamp": { | ||
"type": "unix", | ||
"name": "timestamp_unix" | ||
}, | ||
"interval": 86400, | ||
"query": "SELECT * FROM test WHERE {TIMESTAMP_CONDITION}", | ||
"days_to_backfill": 2305 | ||
}, | ||
{ | ||
"source": "test_postgres_py", | ||
"pipeline_id": "test_postgres_py_file_full", | ||
"values": {"clicks": "gauge", "impressions": "gauge"}, | ||
"count_records": true, | ||
"count_records_measurement_name": "test", | ||
"dimensions": ["adsize", "country"], | ||
"timestamp": { | ||
"type": "unix", | ||
"name": "extract(epoch from timestamp_datetime)", | ||
"alias": "timestamp_unix_converted" | ||
}, | ||
"properties": {"key1": "val1", "key2": "val2"}, | ||
"interval": 86400, | ||
"query": "SELECT extract(epoch from timestamp_datetime) as timestamp_unix_converted, adsize, country, clicks, impressions FROM test WHERE {TIMESTAMP_CONDITION} AND not country is NULL", | ||
"days_to_backfill": 2305 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[ | ||
{ | ||
"type": "postgres-py", | ||
"name": "test_postgres_py", | ||
"config": { | ||
"connection_string": "postgresql://postgres:password@postgres:5432/test" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
from datetime import datetime | ||
from ..test_zpipeline_base import TestInputBase | ||
|
||
|
||
def _get_days_to_backfill(): | ||
return (datetime.now() - datetime(year=2017, month=12, day=10)).days + 1 | ||
|
||
|
||
class TestPostgreSQL(TestInputBase): | ||
__test__ = True | ||
params = { | ||
'test_create_source_with_file': [{'file_name': 'postgres_py_sources'}], | ||
'test_create_with_file': [{'file_name': 'postgres_py_pipelines', | ||
'override_config': {'days_to_backfill': _get_days_to_backfill()}}], | ||
} |
Oops, something went wrong.