Skip to content

Conversation

@kaschula
Copy link
Owner

@kaschula kaschula commented Jul 11, 2019

This PR conatins the application code for parsing the call event data CSV files

Explanation
This project consists of the 3 defined components.

  • CallEventParser - a type that handles parsing the CSV data, having the data validated, storing valid records and logging any errors.
  • CallEventFile, this handles data about the file, valid records and errors. This file also contains a factory function for this type.
  • CallEventStore, this handles setting up the database and storing the CallEventFile data

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 :-

  1. The parsing process should not start if it is already running in another process
  2. The target upload directory does not exist
  3. The target upload directory contains no CSV files
  4. A Database connection can not be established
  5. The database table must be created if it does not exist

The tests in call-event-parser_test.go file 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 the main().

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.go file.

The second area of error handling is the validation of the data. The code concerning this task is located in the call-event-file.go file.

The CreateCallEventFileFromRaw() function is a factory function that receives the raw parsed CSV in a [][]string type 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 CallEventFile type 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 in MySqlCallEventStore.

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 a callEventFile and 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.

@kaschula kaschula force-pushed the feature/add-call-event-parser branch from 70d3b89 to 05ecd67 Compare July 11, 2019 10:21
@kaschula kaschula force-pushed the feature/add-call-event-parser branch from 05ecd67 to 396e2b4 Compare July 11, 2019 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants