Feature/add call event parser #1
Open
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.
This PR conatins the application code for parsing the call event data CSV files
Explanation
This project consists of the 3 defined components.
The first task I tackled was to create a series of tests that would cover the various errors that could be identified before any file parsing begins . I identified these as :-
The tests in
call-event-parser_test.gofile cover these situations. In all cases the callEventParser.Parse() function returns the err which can be logged by the caller. In this case the caller is themain().Where necessary interfaces were used with test stubs to quickly test drive the flow of this parsing method. See the CallEventStore interface and the stub in the
call-event-parser_test.gofile.The second area of error handling is the validation of the data. The code concerning this task is located in the
call-event-file.gofile.The
CreateCallEventFileFromRaw()function is a factory function that receives the raw parsed CSV in a[][]stringtype and processes each line and validates it.Any errors in a data record results in an error message string being created and that record not being added to a slice records to be inserted into the database.
Extracting this out made writing tests easier and allowed for granular checks on validations to ensure integrity.
This function returns
CallEventFiletype which contains meta data about the file as well as a slice of validated records that are ready for database insertion and a list of errors that have been tracked. The errors are logged and contain line numbers and filenames to aid any any investigation into these errors.The 3rd component of this parser is the
callEventStore. This interface has one implementation which inMySqlCallEventStore.There are 2 Methods to be fulfilled by this interface. The
Prepare()method creates a DB connection to check the database can be accessed and also creates the required table if it does not exists. Any errors from these checks are returned.The
Create()Method receives acallEventFileand using the valid data records builds a batch insert query and inserts this into the DB.The main.go bootstraps these components together, handles the command line arguments, sets up the syslog and logs any parsing errors.
To ensure the application does not run while another process is running a locking file is created at the beginning of the process once the checks have been made and is removed when parsing is finished. This file is checked before any process starts and if it exists the process exits.
The project readme.me contains information on the running this application.