-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move tracking config checks into classes
- move parsing logic into separate classes to keep the code in the executing for-loop small https://phabricator.wikimedia.org/T380052
- Loading branch information
Showing
6 changed files
with
107 additions
and
43 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import chalk from 'chalk'; | ||
|
||
export default class CampaignName { | ||
/** | ||
* @param {string} campaignName | ||
*/ | ||
constructor( campaignName ) { | ||
const testNumberRegexResult = campaignName.match( /_(\d{2})/ ); | ||
if ( testNumberRegexResult === null ) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Cannot parse test number ( "_dd" format) for ${ campaignName } ` ); | ||
process.exit( 1 ); | ||
} | ||
this.testNumber = testNumberRegexResult[ 1 ]; | ||
} | ||
} |
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,22 @@ | ||
import chalk from 'chalk'; | ||
|
||
export default class CampaignTrackingCode { | ||
/** | ||
* @param {string} campaignTrackingCode | ||
*/ | ||
constructor( campaignTrackingCode ) { | ||
const textNumberRegexResult = campaignTrackingCode.match( /^\D*(\d{2})([-_])/ ); | ||
if ( textNumberRegexResult === null ) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Wrong test number format for ${ campaignTrackingCode } ` ); | ||
process.exit( 1 ); | ||
} | ||
this.testNumber = textNumberRegexResult[ 1 ]; | ||
|
||
const startDateRegexResult = campaignTrackingCode.match( /-(\d{6})$/ ); | ||
if ( startDateRegexResult === null ) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Wrong start date format (should be 6-digit) for ${ campaignTrackingCode } ` ); | ||
process.exit( 1 ); | ||
} | ||
this.startDate = startDateRegexResult[ 1 ]; | ||
} | ||
} |
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,30 @@ | ||
import chalk from 'chalk'; | ||
|
||
export default class KeywordTrackingCode { | ||
/** | ||
* @param {string} trackingCode | ||
*/ | ||
constructor( trackingCode ) { | ||
const testNumberRegexResult = trackingCode.match( /^\D*(\d{2})([-_])/ ); | ||
if ( testNumberRegexResult === null ) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Cannot parse test number ( = "first pair of digits" ) for ${ trackingCode } ` ); | ||
process.exit( 1 ); | ||
} | ||
this.testNumber = testNumberRegexResult[ 1 ]; | ||
|
||
const startDateRegexResult = trackingCode.match( /-(\d{6})-/ ); | ||
if ( startDateRegexResult === null ) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Cannot parse 6-digit start date for ${ trackingCode } ` ); | ||
process.exit( 1 ); | ||
} | ||
this.startDate = startDateRegexResult[ 1 ]; | ||
} | ||
|
||
/** | ||
* @param {CampaignTrackingCode|KeywordTrackingCode} trackingCode | ||
* @return {boolean} | ||
*/ | ||
matchesDate( trackingCode ) { | ||
return this.startDate === trackingCode.startDate; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tools/CampaignConfigurationCheck/check-tracking-number-and-date.mjs
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,38 @@ | ||
import { parse as parseTOML } from 'toml'; | ||
import { readFileSync } from 'fs'; | ||
import { campaignInfoToCampaignConfig } from '../../webpack/convert_info_to_type.js'; | ||
import chalk from 'chalk'; | ||
import CampaignName from './CampaignName.mjs'; | ||
import KeywordTrackingCode from './KeywordTrackingCode.mjs'; | ||
import CampaignTrackingCode from './CampaignTrackingCode.mjs'; | ||
|
||
const rawConfigForAllCampaigns = parseTOML( readFileSync( 'campaign_info.toml', 'utf-8' ) ); | ||
const configObject = campaignInfoToCampaignConfig( rawConfigForAllCampaigns ); | ||
|
||
for ( const channelConfig of Object.values( configObject ) ) { | ||
|
||
const campaignName = new CampaignName( channelConfig.campaign ); | ||
const campaignTracking = new CampaignTrackingCode( channelConfig.tracking ); | ||
const ctrlTracking = new KeywordTrackingCode( channelConfig.banners.ctrl.tracking ); | ||
const varTracking = new KeywordTrackingCode( channelConfig.banners.var.tracking ); | ||
|
||
if ( campaignName.testNumber !== varTracking.testNumber || | ||
campaignName.testNumber !== ctrlTracking.testNumber || | ||
campaignName.testNumber !== campaignTracking.testNumber | ||
) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Faulty test number for ${ channelConfig.campaign } ` ); | ||
console.log( campaignName.testNumber ); | ||
console.log( campaignTracking.testNumber ); | ||
console.log( ctrlTracking.testNumber ); | ||
console.log( varTracking.testNumber ); | ||
process.exit( 1 ); | ||
} | ||
if ( !ctrlTracking.matchesDate( varTracking ) || !ctrlTracking.matchesDate( campaignTracking ) ) { | ||
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Faulty start date for ${ channelConfig.campaign } ` ); | ||
console.log( campaignTracking.startDate ); | ||
console.log( ctrlTracking.startDate ); | ||
console.log( varTracking.startDate ); | ||
process.exit( 1 ); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.