Skip to content

Commit

Permalink
passing milestone
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Adamski committed Jan 21, 2025
1 parent bbad27b commit c806155
Show file tree
Hide file tree
Showing 6 changed files with 909 additions and 524 deletions.
50 changes: 34 additions & 16 deletions bin/validate_live_pixel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import JSON5 from 'json5';
import { ParamsValidator } from '../src/params_validator.mjs';
import { logErrors } from '../src/error_utils.mjs';
import { spawnSync } from 'child_process';
import { exit } from 'process';

const clickhouseHost = process.env.CLICKHOUSE_HOST

Expand All @@ -16,9 +15,14 @@ if (!clickhouseHost) {
const args = process.argv.slice(2);
const mainDir = args[0];

const pixelDefs = JSON5.parse(fs.readFileSync(`${args[1]}`));
const commonParams = JSON5.parse(fs.readFileSync(`${mainDir}/common_params.json`));
const commonSuffixes = JSON5.parse(fs.readFileSync(`${mainDir}/common_suffixes.json`));
const productDef = JSON5.parse(fs.readFileSync(`${mainDir}/product.json`).toString());
// Whether to force all schemas and pixels to lowercase
const forceLowerCase = productDef.forceLowerCase;

const pixelDefs = JSON5.parse(getNormalizedCase(fs.readFileSync(`${args[1]}`).toString()));
const commonParams = JSON5.parse(getNormalizedCase(fs.readFileSync(`${mainDir}/common_params.json`).toString()));
const ignoreParams = JSON5.parse(getNormalizedCase(fs.readFileSync(`${mainDir}/ignore_params.json`).toString()));
const commonSuffixes = JSON5.parse(getNormalizedCase(fs.readFileSync(`${mainDir}/common_suffixes.json`).toString()));
const paramsValidator = new ParamsValidator(commonParams, commonSuffixes);

function main() {
Expand All @@ -33,18 +37,22 @@ function main() {
} else {
const pixelQueryResults = queryClickhouse(pixelDefs);
for (const prefix of Object.keys(pixelQueryResults)) {
console.log('Validating', prefix, pixelQueryResults)
validateQueryForPixels(prefix, pixelQueryResults[prefix], paramsValidator)
}
}
}

function queryClickhouse(pixelDefs) {
var agents = "'" + productDef.agents.join("','") + "'";

Check warning on line 46 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

Unexpected var, use let or const instead
const agentString = productDef.agents.length ? `AND agent IN (${agents})` : '';

const pixelQueryResults = {};
for(const pixel of Object.keys(pixelDefs)) {
console.log('Querying for', pixel)
const pixel_id = pixel.split('.')[0];
const clickhouseQuery = spawnSync( 'clickhouse-client', [ '--host', clickhouseHost, '--query', `SELECT DISTINCT request FROM metrics.pixels WHERE pixel_id = '${pixel_id}' AND date > now() - INTERVAL 30 DAY AND pixel LIKE '${pixel}%' LIMIT 1000` ] );
const pixel_id = pixel.split(/[-\.]/)[0];

Check failure on line 52 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

Identifier 'pixel_id' is not in camel case

Check failure on line 52 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \.
const queryString = `SELECT DISTINCT request FROM metrics.pixels WHERE pixel_id = '${pixel_id}' AND date > now() - INTERVAL 30 DAY AND pixel ILIKE '${pixel}%' ${agentString} LIMIT 1000`;

Check failure on line 53 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

Identifier 'pixel_id' is not in camel case
console.log('queryString', queryString)
const clickhouseQuery = spawnSync( 'clickhouse-client', [ '--host', clickhouseHost, '--query', queryString ] );
const resultString = clickhouseQuery.stdout.toString();
const resultErr = clickhouseQuery.stderr.toString();
if (resultErr) {
Expand All @@ -53,27 +61,37 @@ function queryClickhouse(pixelDefs) {
if (resultString) pixelQueryResults[pixel] = resultString;
}
}
console.log('pixelQueryResults',pixelQueryResults);

return pixelQueryResults;
}

function validateQueryForPixels(prefix, pixelQuery, paramsValidator) {
var minVersion = productDef.target;

Check warning on line 69 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

Unexpected var, use let or const instead

const lines = pixelQuery.split('\n');
for (const line of lines) {
if (line == '') continue;
const pixelRequest = line.split('/')[2];
const parts = pixelRequest.split('?');
const url = parts[1];
const pixelDef = pixelDefs[prefix];
const lines = pixelQuery.split('\n');
console.log(`Received ${lines.length} results`)
for (let line of lines) {
if (line == '') continue;

Check failure on line 74 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

Expected '===' and instead saw '=='
line = getNormalizedCase(line);
const pixelRequest = line.split('/')[2];
const parts = pixelRequest.split('?');
const url = parts[1];

Check failure on line 78 in bin/validate_live_pixel.mjs

View workflow job for this annotation

GitHub Actions / lint

'url' is assigned a value but never used
const pixelDef = pixelDefs[prefix];

logErrors(`ERRORS for '${pixelRequest}\n`, paramsValidator.validateLivePixels(pixelDef, prefix, line));
logErrors(`ERRORS for '${pixelRequest}\n`, paramsValidator.validateLivePixels(pixelDef, ignoreParams, prefix, line, minVersion));
}
}

function validateSinglePixel(pixelDefs, prefix, url) {
logErrors('ERRORS:', paramsValidator.validateLivePixels(pixelDefs[prefix], prefix, url));
}

function getNormalizedCase(value) {
if (forceLowerCase) {
return value.toLowerCase();
}

return value;

}
main();
Empty file modified bin/validate_schema.mjs
100644 → 100755
Empty file.
Loading

0 comments on commit c806155

Please sign in to comment.