Skip to content

Commit f04bf8e

Browse files
Tyler BuffingtonTyler Buffington
authored andcommitted
Added a sync prefix
1 parent bd3df5a commit f04bf8e

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

eppo_metrics_sync/__main__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@
33
from eppo_metrics_sync.eppo_metrics_sync import EppoMetricsSync
44

55
if __name__ == '__main__':
6-
6+
77
parser = argparse.ArgumentParser(
88
description="Scan specified directory for Eppo yaml files and sync with Eppo"
99
)
1010
parser.add_argument("directory", help="The directory of yaml files to process")
1111
parser.add_argument("--dryrun", action="store_true", help="Run in dry run mode")
1212
parser.add_argument("--schema", help="One of: eppo[default], dbt-model", default='eppo')
13+
parser.add_argument("--sync-prefix", help="Used for testing in a shared Q/A workspace. "
14+
"Will use this as a sync tag and append all fact and metric definitions with this prefix.",
15+
required=False
16+
)
1317
parser.add_argument(
14-
"--dbt-model-prefix",
15-
help="The warehouse and schema where the dbt models live",
18+
"--dbt-model-prefix",
19+
help="The warehouse and schema where the dbt models live",
1620
default=None
1721
)
1822

1923
args = parser.parse_args()
2024

2125
eppo_metrics_sync = EppoMetricsSync(
22-
directory = args.directory,
26+
directory=args.directory,
2327
schema_type=args.schema,
24-
dbt_model_prefix=args.dbt_model_prefix
28+
dbt_model_prefix=args.dbt_model_prefix,
29+
sync_prefix=args.sync_prefix
2530
)
26-
31+
2732
if args.dryrun:
2833
eppo_metrics_sync.read_yaml_files()
2934
eppo_metrics_sync.validate()

eppo_metrics_sync/eppo_metrics_sync.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import jsonschema
33
import os
44
import requests
5-
import yaml
65

76
from eppo_metrics_sync.validation import (
87
unique_names,
@@ -13,31 +12,31 @@
1312
from eppo_metrics_sync.dbt_model_parser import DbtModelParser
1413
from eppo_metrics_sync.helper import load_yaml
1514

16-
1715
API_ENDPOINT = 'https://eppo.cloud/api/v1/metrics/sync'
1816

1917

2018
class EppoMetricsSync:
2119
def __init__(
22-
self,
23-
directory,
24-
schema_type = 'eppo',
25-
dbt_model_prefix = None
20+
self,
21+
directory,
22+
schema_type='eppo',
23+
dbt_model_prefix=None,
24+
sync_prefix=None
2625
):
2726
self.directory = directory
2827
self.fact_sources = []
2928
self.metrics = []
3029
self.validation_errors = []
3130
self.schema_type = schema_type
3231
self.dbt_model_prefix = dbt_model_prefix
32+
self.sync_prefix = sync_prefix
3333

3434
# temporary: ideally would pull this from Eppo API
3535
package_root = os.path.dirname(os.path.abspath(__file__))
3636
schema_path = os.path.join(package_root, 'schema', 'eppo_metric_schema.json')
3737
with open(schema_path) as schema_file:
3838
self.schema = json.load(schema_file)
3939

40-
4140
def load_eppo_yaml(self, path):
4241
yaml_data = load_yaml(path)
4342
if 'fact_sources' in yaml_data:
@@ -84,18 +83,25 @@ def read_yaml_files(self):
8483
self.validation_errors.append(
8584
f"Schema violation in {yaml_path}: \n{valid.error_message}"
8685
)
87-
86+
8887
elif self.schema_type == 'dbt-model':
8988
self.load_dbt_yaml(yaml_path)
90-
89+
9190
else:
9291
raise ValueError(f'Unexpected schema_type: {self.schema_type}')
93-
92+
9493
if len(self.fact_sources) == 0 and len(self.metrics) == 0:
9594
raise ValueError(
9695
'No valid yaml files found. ' + ', '.join(self.validation_errors)
9796
)
9897

98+
def _add_sync_prefix(self):
99+
for source in self.fact_sources:
100+
source['name'] = f"[{self.sync_prefix}] {source['name']}"
101+
102+
for metric in self.metrics:
103+
metric['name'] = f"[{self.sync_prefix}] {metric['name']}"
104+
99105
def validate(self):
100106

101107
if len(self.fact_sources) == 0 and len(self.metrics) == 0:
@@ -113,15 +119,23 @@ def validate(self):
113119

114120
return True
115121

122+
def _determine_sync_tag(self):
123+
if self.sync_prefix is not None:
124+
return self.sync_prefix
125+
126+
os.getenv('EPPO_SYNC_TAG')
127+
116128
def sync(self):
117129
self.read_yaml_files()
130+
if self.sync_prefix is not None:
131+
self._add_sync_prefix()
118132
self.validate()
119133

120134
api_key = os.getenv('EPPO_API_KEY')
121135
if not api_key:
122136
raise Exception('EPPO_API_KEY not set in environment variables. Please set and try again')
123137

124-
sync_tag = os.getenv('EPPO_SYNC_TAG')
138+
sync_tag = self._determine_sync_tag()
125139
if not api_key:
126140
raise Exception('EPPO_SYNC_TAG not set in environment variables. Please set and try again')
127141

0 commit comments

Comments
 (0)