Skip to content

Commit

Permalink
updated schema format
Browse files Browse the repository at this point in the history
  • Loading branch information
skarim committed Aug 25, 2024
1 parent 5ecbb45 commit 00e4398
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 78 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ npx @flisk/analyze-tracking /path/to/project [options]
```

Optional arguments:
- `--repository <repository_url>`: URL of the repository where the code is hosted (defaults to git remote origin in project directory)
- `--output <output_file>`: Name of the output file (default: `tracking-schema.yaml`)


## Output Schema
A YAML file with the following structure is generated:

```yaml
version: 1.0
version: 1
source:
repository: <repository_url>
commit: <commit_sha>
timestamp: <commit_timestamp>
events:
<event_name>:
sources:
- repository: <repository_name>
path: <path_to_file>
implementations:
- path: <path_to_file>
line: <line_number>
function: <function_name>
destinations:
- <destination_name>
destination: <platform_name>
properties:
<property_name>:
type: <property_type>
Expand Down
23 changes: 2 additions & 21 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

const path = require('path');
const { execSync } = require('child_process');
const commandLineArgs = require('command-line-args')
const { run } = require('../src/index');

Expand All @@ -12,11 +11,6 @@ const optionDefinitions = [
type: String,
defaultOption: true,
},
{
name: 'repository',
alias: 'r',
type: String,
},
{
name: 'output',
alias: 'o',
Expand All @@ -25,24 +19,11 @@ const optionDefinitions = [
},
]
const options = commandLineArgs(optionDefinitions);
const { targetDir, output, repository } = options;
const { targetDir, output } = options;

if (!targetDir) {
console.error('Please provide the path to the repository.');
process.exit(1);
}

// Get the repository URL using Git
function getRepositoryUrl() {
try {
const repoUrl = execSync('git config --get remote.origin.url', { cwd: targetDir, encoding: 'utf8' });
return repoUrl.trim();
} catch (error) {
console.warn('Could not retrieve repository URL. Using default value "unknown".');
return 'unknown';
}
}

const repositoryUrl = repository || getRepositoryUrl();

run(path.resolve(targetDir), repositoryUrl, output);
run(path.resolve(targetDir), output);
83 changes: 50 additions & 33 deletions schema.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-07/schema",
"title": "@flisk/analyze-tracking output schema",
"type": "object",
"properties": {
"version": {
"type": "number",
"enum": [
1.0
1
],
"description": "Version of the schema"
},
"source": {
"type": "object",
"properties": {
"repository": {
"type": "string",
"description": "URL of git repository that was used to generate the schema"
},
"commit": {
"type": "string",
"description": "Git commit hash when this schema was generated"
},
"timestamp": {
"type": "string",
"description": "Git commit timestamp when this schema was generated"
}
},
"required": [
"timestamp"
],
"additionalProperties": false
},
"events": {
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9_-]+$": {
"type": "object",
"properties": {
"sources": {
"implementations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"repository": {
"type": "string",
"description": "Repository URL or name"
},
"path": {
"type": "string",
"description": "Relative path to the file where the event is tracked"
Expand All @@ -36,31 +53,30 @@
"function": {
"type": "string",
"description": "Name of the function where the event is tracked"
},
"destination": {
"type": "string",
"enum": [
"googleanalytics",
"segment",
"mixpanel",
"amplitude",
"rudderstack",
"mparticle",
"posthog",
"snowplow",
"unknown"
],
"description": "Name of the platform where the event is sent"
}
},
"required": [
"repository",
"path",
"line",
"function"
]
}
},
"destinations": {
"type": "array",
"items": {
"type": "string",
"enum": [
"googleanalytics",
"segment",
"mixpanel",
"amplitude",
"rudderstack",
"mparticle",
"snowplow",
"unknown"
"function",
"destination"
],
"description": "Destination analytics platform"
"additionalProperties": false
}
},
"properties": {
Expand All @@ -71,7 +87,7 @@
"properties": {
"type": {
"type": "string",
"description": "Data type of the property (e.g., string, number)"
"description": "Data type of the property (e.g., string, number, any)"
},
"required": {
"type": "boolean",
Expand All @@ -84,23 +100,24 @@
},
"required": [
"type"
]
],
"additionalProperties": false
}
}
}
},
"required": [
"sources",
"destinations",
"implementations",
"properties"
]
],
"additionalProperties": false
}
},
"additionalProperties": false
}
}
},
"required": [
"version",
"source",
"events"
]
}
19 changes: 7 additions & 12 deletions src/analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { getAllFiles } = require('../fileProcessor');
const ts = require('typescript');
const path = require('path');

function analyzeDirectory(dirPath, repository) {
function analyzeDirectory(dirPath) {
const files = getAllFiles(dirPath);
const allEvents = {};

Expand All @@ -23,27 +23,22 @@ function analyzeDirectory(dirPath, repository) {

if (!allEvents[event.eventName]) {
allEvents[event.eventName] = {
sources: [{
repository: repository,
implementations: [{
path: relativeFilePath,
line: event.line,
function: event.functionName
function: event.functionName,
destination: event.source
}],
destinations: [event.source],
properties: event.properties,
};
} else {
allEvents[event.eventName].sources.push({
repository: repository,
allEvents[event.eventName].implementations.push({
path: relativeFilePath,
line: event.line,
function: event.functionName
function: event.functionName,
destination: event.source
});

if (!allEvents[event.eventName].destinations.includes(event.source)) {
allEvents[event.eventName].destinations.push(event.source);
}

allEvents[event.eventName].properties = {
...allEvents[event.eventName].properties,
...event.properties,
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const { analyzeDirectory } = require('./analyze');
const { getRepoDetails } = require('./repoDetails');
const { generateYamlSchema } = require('./yamlGenerator');

function run(targetDir, repository, outputPath) {
const events = analyzeDirectory(targetDir, repository);
generateYamlSchema(events, outputPath);
function run(targetDir, outputPath) {
const events = analyzeDirectory(targetDir);
const repoDetails = getRepoDetails(targetDir);
generateYamlSchema(events, repoDetails, outputPath);
}

module.exports = { run };
61 changes: 61 additions & 0 deletions src/repoDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { execSync } = require('child_process');

function getRepositoryUrl(targetDir) {
try {
const repoUrl = execSync('git config --get remote.origin.url', { cwd: targetDir, encoding: 'utf8' });
return repoUrl.trim();
} catch (error) {
console.warn('Could not determine repository URL. Will exclude.');
return null;
}
}

function getCommitHash(targetDir) {
try {
const commitHash = execSync('git rev-parse HEAD', { cwd: targetDir, encoding: 'utf8' });
return commitHash.trim();
} catch (error) {
console.warn('Could not determine latest commit hash. Will exclude.');
return null;
}
}

function getCommitTimestamp(targetDir, commitHash) {
try {
const commitTimestamp = execSync(`git --no-pager show -s --format=%ct ${commitHash}`, { cwd: targetDir, encoding: 'utf8' });
const unixTimeSeconds = commitTimestamp.trim();
return new Date(unixTimeSeconds * 1000);
} catch (error) {
console.warn('Could not retrieve commit timestamp. Using current timestamp as default.')
return new Date();
}
}

function pad(n) {
return n<10 ? '0'+n : n
}

function toISODateString(date) {
return date.getUTCFullYear()+'-'
+ pad(date.getUTCMonth()+1)+'-'
+ pad(date.getUTCDate())+'T'
+ pad(date.getUTCHours())+':'
+ pad(date.getUTCMinutes())+':'
+ pad(date.getUTCSeconds())+'Z'
}

function getRepoDetails(targetDir) {
const repoUrl = getRepositoryUrl(targetDir);
const commitHash = getCommitHash(targetDir);
const commitEpochTime = getCommitTimestamp(targetDir, commitHash);
const commitTimestamp = toISODateString(commitEpochTime);

const repoDetails = {};
if (!!repoUrl) repoDetails.repository = repoUrl;
if (!!commitHash) repoDetails.commit = commitHash;
repoDetails.timestamp = commitTimestamp

return repoDetails;
}

module.exports = { getRepoDetails };
7 changes: 5 additions & 2 deletions src/yamlGenerator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const fs = require('fs');
const yaml = require('js-yaml');

function generateYamlSchema(events, outputPath) {
const version = 1

function generateYamlSchema(events, repository, outputPath) {
const schema = {
version: 1.0,
version,
source: repository,
events,
};

Expand Down

0 comments on commit 00e4398

Please sign in to comment.