Created
June 25, 2018 18:56
-
-
Save sunahsuh/07abea4d099872818db59d26bf485c54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import argparse | |
| import csv | |
| import json | |
| BASE_SCHEMA = { | |
| "eventGroupName": "dt", | |
| "filters": { | |
| "docType": [ | |
| "main" | |
| ], | |
| "appName": [ | |
| "Firefox", | |
| ], | |
| }, | |
| "events": [] | |
| } | |
| def get_event(name, categories, methods, objects, values, amplitude_props, user_props, descr=""): | |
| props = { | |
| "timestamp": { | |
| "type": "number", | |
| "minimum": 0 | |
| }, | |
| "category": { | |
| "type": "string", | |
| "enum": categories | |
| }, | |
| "method": { | |
| "type": "string", | |
| "enum": methods | |
| }, | |
| "object": { | |
| "type": "string", | |
| "enum": objects | |
| } | |
| } | |
| if values: | |
| props["value"] = { | |
| "type": "string", | |
| "enum": values | |
| } | |
| return { | |
| "name": name, | |
| "description": descr, | |
| "amplitudeProperties": amplitude_props, | |
| "userProperties": user_props, | |
| "schema": { | |
| "$schema": "http://json-schema.org/schema#", | |
| "type": "object", | |
| "properties": props, | |
| "required": list(props.keys()) | |
| } | |
| }, | |
| def sep(val): | |
| return [e.strip() for e in val.split(",") if e.strip()] | |
| def convert(filename): | |
| default = BASE_SCHEMA | |
| with open(filename, 'r') as f: | |
| events = list(csv.reader(f, delimiter=',', quotechar='"')) | |
| for e in events: | |
| name, desc, cs, ms, os, vs, aps, ups = e | |
| amplitude_props = {} | |
| if aps: | |
| separated = sep(aps) | |
| for e in separated: | |
| k, v = e.split("=") | |
| amplitude_props[k] = v | |
| user_props = {} | |
| if ups: | |
| separated = sep(ups) | |
| for e in separated: | |
| k, v = e.split("=") | |
| user_props[k] = v | |
| default["events"].append( | |
| get_event( | |
| name, | |
| sep(cs), | |
| sep(ms), | |
| sep(os), | |
| sep(vs), | |
| amplitude_props, | |
| user_props | |
| ) | |
| ) | |
| return default | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser( | |
| description="CSV to Schema converter for Events to Amplitude", | |
| formatter_class=argparse.ArgumentDefaultsHelpFormatter | |
| ) | |
| parser.add_argument( | |
| "filename", | |
| help = "CSV file with the data to convert" | |
| ) | |
| args = parser.parse_args() | |
| print json.dumps(convert(args.filename), sort_keys = True, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment