-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit JSON schema and BigQuery schema #6
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
470016c
Add dfe:analytics:schema Rake task
duncanjbrown 09fd8cb
Provide an interface to the JSON schema
duncanjbrown 1014169
Conform event-schema.json to what's in BigQuery
duncanjbrown 45b6fe5
Do not truncate RSpec match_array output
duncanjbrown 4c7c404
Map JSON schema to BigQuery schema
duncanjbrown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module DfE | ||
module Analytics | ||
class EventSchema | ||
def self.as_json | ||
path = "#{Gem.loaded_specs['dfe-analytics'].gem_dir}/config/event-schema.json" | ||
File.read(path) | ||
end | ||
|
||
def self.as_bigquery_schema | ||
schema = JSON.parse(as_json) | ||
required_fields = schema['required'] | ||
|
||
properties = schema['properties'] | ||
|
||
schema = properties.keys.reduce([]) do |bq_schema, json_schema_entry_name| | ||
json_schema_entry = properties[json_schema_entry_name] | ||
bigquery_field_type = resolve_bigquery_type(json_schema_entry) | ||
|
||
bigquery_schema_entry = { | ||
'mode' => resolve_bigquery_mode(json_schema_entry_name, json_schema_entry, required_fields), | ||
'name' => json_schema_entry_name, | ||
'type' => bigquery_field_type | ||
} | ||
|
||
if bigquery_field_type == 'RECORD' | ||
bigquery_schema_entry['fields'] = [ | ||
{ | ||
'mode' => 'REQUIRED', | ||
'name' => 'key', | ||
'type' => 'STRING' | ||
}, | ||
{ | ||
'mode' => 'REPEATED', | ||
'name' => 'value', | ||
'type' => 'STRING' | ||
} | ||
] | ||
end | ||
|
||
bq_schema << bigquery_schema_entry | ||
bq_schema | ||
end | ||
|
||
schema.to_json | ||
end | ||
|
||
def self.resolve_bigquery_mode(json_schema_entry_name, json_schema_entry, required_fields) | ||
if required_fields.include?(json_schema_entry_name) | ||
'REQUIRED' | ||
elsif json_schema_entry['type'] == 'array' | ||
'REPEATED' | ||
else | ||
'NULLABLE' | ||
end | ||
end | ||
|
||
def self.resolve_bigquery_type(json_schema_entry) | ||
json_type = json_schema_entry['type'] | ||
json_format = json_schema_entry['format'] | ||
|
||
if json_type == 'array' | ||
'RECORD' | ||
elsif json_type == 'string' && json_format == 'date-time' | ||
'TIMESTAMP' | ||
elsif json_type == 'string' | ||
'STRING' | ||
elsif json_type == 'integer' | ||
'INTEGER' | ||
end | ||
end | ||
end | ||
end | ||
end |
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,13 @@ | ||
namespace :dfe do | ||
namespace :analytics do | ||
desc 'Print out the dfe-analytics JSON schema' | ||
task :schema do | ||
puts DfE::Analytics::EventSchema.as_json | ||
end | ||
|
||
desc 'Print out the dfe-analytics BigQuery schema' | ||
task :big_query_schema do | ||
puts DfE::Analytics::EventSchema.as_bigquery_schema | ||
end | ||
end | ||
end |
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,23 @@ | ||
RSpec.describe DfE::Analytics::EventSchema do | ||
describe '.as_json' do | ||
it 'returns the JSON schema as an object' do | ||
schema_on_disk = File.read("#{Gem.loaded_specs['dfe-analytics'].gem_dir}/config/event-schema.json") | ||
|
||
output = described_class.as_json | ||
|
||
expect(output).to be_present | ||
expect(output).to eq schema_on_disk | ||
end | ||
end | ||
|
||
describe '.as_bigquery_schema' do | ||
it 'transforms the JSON schema into a BQ schema' do | ||
bq_schema_on_disk = File.read('spec/examples/bigquery_schema.json') | ||
|
||
output = JSON.parse(described_class.as_bigquery_schema) | ||
|
||
expect(output).to be_present | ||
expect(output).to match_array JSON.parse(bq_schema_on_disk) | ||
end | ||
end | ||
end |
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,106 @@ | ||
[ | ||
{ | ||
"mode": "REQUIRED", | ||
"name": "occurred_at", | ||
"type": "TIMESTAMP" | ||
}, | ||
{ | ||
"mode": "REQUIRED", | ||
"name": "event_type", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "REQUIRED", | ||
"name": "environment", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "namespace", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "user_id", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "request_uuid", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "request_method", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "request_path", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "request_user_agent", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "request_referer", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"fields": [ | ||
{ | ||
"mode": "REQUIRED", | ||
"name": "key", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "REPEATED", | ||
"name": "value", | ||
"type": "STRING" | ||
} | ||
], | ||
"mode": "REPEATED", | ||
"name": "request_query", | ||
"type": "RECORD" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "response_content_type", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "response_status", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"fields": [ | ||
{ | ||
"mode": "REQUIRED", | ||
"name": "key", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "REPEATED", | ||
"name": "value", | ||
"type": "STRING" | ||
} | ||
], | ||
"mode": "REPEATED", | ||
"name": "data", | ||
"type": "RECORD" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "entity_table_name", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"mode": "NULLABLE", | ||
"name": "anonymised_user_agent_and_ip", | ||
"type": "STRING" | ||
} | ||
] |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a class and not a module?