Skip to content

Commit

Permalink
[Bugfix] Pass agency and hostname to each data processor method call
Browse files Browse the repository at this point in the history
  • Loading branch information
levinmr committed Aug 22, 2024
1 parent ff75a15 commit 3a569d4
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 110 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async function runQueuePublish(options = {}) {
),
{
priority: _messagePriority(reportConfig),
singletonKey: `${appConfig.scriptName}-${appConfig.agency}-${reportConfig.name}`,
singletonKey: `${appConfig.scriptName}-${agency.agencyName}-${reportConfig.name}`,
},
);
if (jobId) {
Expand Down
12 changes: 7 additions & 5 deletions src/actions/process_google_analytics_results.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class ProcessGoogleAnalyticsResults extends Action {
async executeStrategy(context) {
context.logger.debug("Processing GA report data");
context.processedAnalyticsData =
await this.#analyticsDataProcessor.processData(
context.reportConfig,
context.rawGoogleAnalyticsReportData[0],
context.googleAnalyticsQuery,
);
await this.#analyticsDataProcessor.processData({
agency: context.appConfig.agency,
hostname: context.appConfig.account.hostname,
report: context.reportConfig,
data: context.rawGoogleAnalyticsReportData[0],
query: context.googleAnalyticsQuery,
});
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const winston = require("winston");

/**
* @param {import('../app_config')} appConfig the application config
* @param {object} reportConfig the name of the report currently being processed
* @param {string} reportConfig.name the name of the report being run for this
* @param {object} params the parameters for the method
* @param {string} params.agencyName the name of the agency for this logger
* instance.
* @param {string} params.reportName the name of the report being run for this
* logger instance.
* @param {string} params.scriptName the name of the script being run for this
* logger instance.
* @returns {string} a standard tag for the logger to identify the specific
* report being processed when writing logs.
Expand Down
36 changes: 14 additions & 22 deletions src/process_results/analytics_data_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const ResultTotalsCalculator = require("./result_totals_calculator");
* Handles processing operations for raw analytics data.
*/
class AnalyticsDataProcessor {
#agency;
#hostname;
#mapping = {
activeUsers: "active_visitors",
fileName: "file_name",
Expand Down Expand Up @@ -36,25 +34,19 @@ class AnalyticsDataProcessor {
};

/**
* @param {import('../app_config')} appConfig an instance of the application config class.
* Provides agency and hostname string values.
*/
constructor(appConfig) {
this.#agency = appConfig.account.agency_name;
this.#hostname = appConfig.account.hostname;
}

/**
* @param {object} report The report object that was requested
* @param {object} data The response object from the Google Analytics Data API
* @param {object} query The query object for the report
* @param {object} params the method parameters
* @param {string} params.agency the agency for the report
* @param {string} params.hostname the hostname for the report
* @param {object} params.report The report object that was requested
* @param {object} params.data The response object from the Google Analytics Data API
* @param {object} params.query The query object for the report
* @returns {object} The response data transformed to flatten the data
* structure, format dates, and map from GA keys to DAP keys. Data is filtered
* as requested in the report object. This object also includes details from
* the original report and query.
*/
processData(report, data, query) {
let result = this.#initializeResult({ report, data, query });
processData({ agency, hostname, report, data, query }) {
let result = this.#initializeResult({ agency, report, data, query });

// If you use a filter that results in no data, you get null
// back from google and need to protect against it.
Expand All @@ -77,7 +69,7 @@ class AnalyticsDataProcessor {

// Process each row
result.data = data.rows.map((row) => {
return this.#processRow({ row, report, data });
return this.#processRow({ hostname, row, data });
});

result.totals = ResultTotalsCalculator.calculateTotals(result, {
Expand All @@ -88,10 +80,10 @@ class AnalyticsDataProcessor {
return result;
}

#initializeResult({ report, data, query }) {
#initializeResult({ agency, report, data, query }) {
return {
name: report.name,
agency: this.#agency,
agency,
sampling: data.metadata?.samplingMetadatas,
query: ((query) => {
query = Object.assign({}, query);
Expand Down Expand Up @@ -164,7 +156,7 @@ class AnalyticsDataProcessor {
return [date.substr(0, 4), date.substr(4, 2), date.substr(6, 2)].join("-");
}

#processRow({ row, data }) {
#processRow({ hostname, row, data }) {
const point = {};

// Iterate through each entry in the object
Expand Down Expand Up @@ -194,8 +186,8 @@ class AnalyticsDataProcessor {
});
}

if (this.#hostname && !("domain" in point)) {
point.domain = this.#hostname;
if (hostname && !("domain" in point)) {
point.domain = hostname;
}

return point;
Expand Down
2 changes: 1 addition & 1 deletion src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Processor {
logger,
),
),
new ProcessGoogleAnalyticsResults(new AnalyticsDataProcessor(appConfig)),
new ProcessGoogleAnalyticsResults(new AnalyticsDataProcessor()),
new FormatProcessedAnalyticsData(),
new WriteAnalyticsDataToDatabase(new PostgresPublisher(appConfig)),
new PublishAnalyticsDataToS3(new S3Service(appConfig)),
Expand Down
12 changes: 6 additions & 6 deletions src/publish/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const path = require("path");
/**
* Publishes report data to a file on the local filesystem.
*
* @param {string} name the name of the file to create.
* @param {string} data the data to write to the file.
* @param {string} format the file extension to use
* @param {string} directory the path for the directory to use for the file
* @param {import('../app_config')} appConfig application config instance. Sets the file
* extension and the path of the file to create.
* @param {object} params the parameters fro the method
* @param {string} params.name the name of the file to create.
* @param {string} params.data the data to write to the file.
* @param {string} params.format the file extension to use
* @param {string} params.directory the path for the directory to use for the
* file
*/
const publish = async ({ name, data, format, directory }) => {
const filename = `${name}.${format}`;
Expand Down
15 changes: 10 additions & 5 deletions test/actions/process_google_analytics_results.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ describe("ProcessGoogleAnalyticsResults", () => {
const googleAnalyticsQuery = { name: "users", realtime: true };
const reportConfig = { name: "foobar" };
const processedAnalyticsData = { fake: "data" };
const agency = "defense";
const hostname = "example.gov";

beforeEach(async () => {
debugLogSpy.resetHistory();
analyticsDataProcessor.processData.resetHistory();
analyticsDataProcessor.processData.returns(processedAnalyticsData);
context = {
appConfig: { agency, account: { hostname } },
rawGoogleAnalyticsReportData: rawGoogleAnalyticsReportData,
googleAnalyticsQuery: googleAnalyticsQuery,
logger: { debug: debugLogSpy },
Expand All @@ -34,11 +37,13 @@ describe("ProcessGoogleAnalyticsResults", () => {

it("calls analyticsDataProcessor.processData with the expected params", () => {
expect(
analyticsDataProcessor.processData.calledWith(
reportConfig,
rawGoogleAnalyticsReportData[0],
googleAnalyticsQuery,
),
analyticsDataProcessor.processData.calledWith({
agency,
hostname,
report: reportConfig,
data: rawGoogleAnalyticsReportData[0],
query: googleAnalyticsQuery,
}),
).to.equal(true);
});

Expand Down
Loading

0 comments on commit 3a569d4

Please sign in to comment.