Skip to content

Commit

Permalink
Change InfluxDB client package to influxdb-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
salatamartin committed Aug 28, 2023
1 parent 4d6443d commit ce973d1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 115 deletions.
23 changes: 11 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@
},
"homepage": "https://test-results-reporter.github.io",
"dependencies": {
"@influxdata/influxdb-client": "^1.33.2",
"async-retry": "^1.3.3",
"dotenv": "^14.3.2",
"influxdb-v1": "^1.0.4",
"pactum-matchers": "^1.1.6",
"influxdb-v2": "^1.0.0",
"performance-results-parser": "latest",
"phin-retry": "^1.0.3",
"pretty-ms": "^7.0.1",
Expand Down
102 changes: 26 additions & 76 deletions src/targets/influx2.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { Point, InfluxDB, WriteApi } = require("@influxdata/influxdb-client");
const Influxdb = require("influxdb-v2");
const Metric = require('performance-results-parser/src/models/Metric');
const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult');
const Transaction = require('performance-results-parser/src/models/Transaction');
const TestCase = require('test-results-parser/src/models/TestCase');
const TestResult = require('test-results-parser/src/models/TestResult');
const TestSuite = require('test-results-parser/src/models/TestSuite');


const { STATUS } = require('../helpers/constants');

/**
Expand All @@ -19,85 +18,35 @@ async function run({ result, target }) {
target.inputs = Object.assign({}, default_inputs, target.inputs);
const metrics = getMetrics({ result, target });

const influxDBClient = getInfluxDBClient(target.inputs.url,
target.inputs.token,
target.inputs.organization,
target.inputs.bucket,
target.inputs.precision,
target.inputs.tags,
target.inputs.gzipTransport)

influxDBClient.writePoints(metrics);

await influxDBClient.close()
}

/**
* Creates an API client for writing to InfluxDB 2.x
* @param url
* @param token
* @param organization
* @param bucket
* @param precision
* @param defaultTags
* @param gzipTransport
* @returns {WriteApi}
*/
function getInfluxDBClient(url, token, organization, bucket, precision, defaultTags, gzipTransport) {
const writeOptions = {}
if(!gzipTransport) {
writeOptions['gzipThreshold'] = undefined
const influxDBClient = new Influxdb({
host: target.inputs.host,
protocol: target.inputs.protocol,
port: target.inputs.port,
token: target.inputs.token
})

const writeOptions = {
org: target.inputs.org,
bucket: target.inputs.bucket,
precision: target.inputs.precision
}

const client = new InfluxDB({ url, token})
.getWriteApi(organization, bucket, precision, writeOptions)
client.useDefaultTags(defaultTags)
return client
await influxDBClient.write(writeOptions, metrics);
}

/**
* Creates a {Point} object
* Creates a metric object
* @param measurement
* @param tags
* @param fields
* @returns {Point}
*/
function createPoint(measurement, tags, fields) {
const point = new Point(measurement)

for (const [tagName, tagValue] of Object.entries(tags)) {
point.tag(tagName, tagValue)
}

for (const [fieldName, fieldValue] of Object.entries(fields)) {
addFieldToPoint(point, fieldName, fieldValue)
}

return point
}

/**
* Adds a single field to {Point} based on the data type
* @param point
* @param fieldName
* @param fieldValue
* @returns
*/
function addFieldToPoint(point, fieldName, fieldValue) {
if (typeof fieldValue === 'boolean') {
point.booleanField(fieldName, fieldValue)
return
}

if (typeof fieldValue === 'number') {
point.floatField(fieldName, fieldValue)
return
}

point.stringField(fieldName, fieldValue)
function createMetric(measurement, tags, fields) {
return { measurement, tags, fields }
}

/**
* Converts the parsed metrics to InfluxDB point format
* Converts the parsed metrics to InfluxDB metric format
* @param {object} param0
* @param {PerformanceTestResult | TestResult} param0.result
* @param {import('..').Target} param0.target
Expand Down Expand Up @@ -143,7 +92,7 @@ function getPerfRunInfluxMetric({ result, target }) {
setPerfInfluxMetricFields(metric, fields);
}

return createPoint(target.inputs.measurement_perf_run, tags, fields)
return createMetric(target.inputs.measurement_perf_run, tags, fields)
}

/**
Expand Down Expand Up @@ -184,7 +133,7 @@ function getTransactionInfluxMetric(transaction, target) {
setPerfInfluxMetricFields(metric, fields);
}

return createPoint(target.inputs.measurement_perf_run, tags, fields)
return createMetric(target.inputs.measurement_perf_transaction, tags, fields)
}

/**
Expand All @@ -205,7 +154,7 @@ function getTestInfluxMetric({ result, target }, measurement) {
fields.failed = result.failed;
fields.duration = result.duration;

return createPoint(measurement, tags, fields)
return createMetric(measurement, tags, fields)
}

/**
Expand All @@ -223,16 +172,17 @@ function getTestCaseInfluxMetric({ result, target }) {
fields.status = result.status === 'PASS' ? 0 : 1;
fields.duration = result.duration;

return createPoint(target.inputs.measurement_test_case, tags, fields)
return createMetric(target.inputs.measurement_test_case, tags, fields)
}

const default_inputs = {
url: '',
host: '',
protocol: 'https',
port: 443,
token: '',
organization: '',
org: '',
bucket: '',
precision: 'ns',
gzipTransport: true,
environment: 'test',
measurement_perf_run: 'PerfRun',
measurement_perf_transaction: 'PerfTransaction',
Expand Down
26 changes: 17 additions & 9 deletions test/mocks/influx2.mock.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { addInteractionHandler } = require('pactum').handler;
const { regex } = require('pactum-matchers');

addInteractionHandler('save perf results to influx2', () => {
return {
strict: false,
request: {
method: 'POST',
path: '/api/v2/write',
Expand All @@ -15,7 +13,9 @@ addInteractionHandler('save perf results to influx2', () => {
"bucket": "testbucket",
"precision": "ns"
},
body: regex('PerfRun', /PerfRun,Name=TOTAL,Status=PASS data_received_rate=5166.44,data_received_sum=362818330,data_sent_rate=38.87,data_sent_sum=2729683,duration_avg=4660,duration_max=15513,duration_med=3318,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_rate=0,errors_sum=0,samples_rate=0.55535,samples_sum=39,status=0,transactions=2,transactions_failed=0,transactions_passed=2 \d+\nPerfRun,Name=S01_T01_Application_Launch,Status=PASS data_received_rate=2662.79,data_received_sum=184633892,data_sent_rate=5.36,data_sent_sum=371654,duration_avg=3086,duration_max=3797,duration_med=2832,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_rate=0.001,errors_sum=0,samples_rate=0.14422,samples_sum=10,status=0 \d+\nPerfRun,Name=S01_T02_Application_Login,Status=PASS data_received_rate=2754.9,data_received_sum=169706365,data_sent_rate=12.94,data_sent_sum=797125,duration_avg=4355,duration_max=10786,duration_med=3273,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_rate=0,errors_sum=0,samples_rate=0.1461,samples_sum=9,status=0 \d+/)
body: "PerfRun,Name=TOTAL,Status=PASS status=0,transactions=2,transactions_passed=2,transactions_failed=0,samples_sum=39,samples_rate=0.55535,duration_avg=4660,duration_med=3318,duration_max=15513,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_sum=0,errors_rate=0,data_sent_sum=2729683,data_sent_rate=38.87,data_received_sum=362818330,data_received_rate=5166.44\n"+
"PerfTransaction,Name=S01_T01_Application_Launch,Status=PASS status=0,samples_sum=10,samples_rate=0.14422,duration_avg=3086,duration_med=2832,duration_max=3797,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_sum=0,errors_rate=0.001,data_sent_sum=371654,data_sent_rate=5.36,data_received_sum=184633892,data_received_rate=2662.79\n"+
"PerfTransaction,Name=S01_T02_Application_Login,Status=PASS status=0,samples_sum=9,samples_rate=0.1461,duration_avg=4355,duration_med=3273,duration_max=10786,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_sum=0,errors_rate=0,data_sent_sum=797125,data_sent_rate=12.94,data_received_sum=169706365,data_received_rate=2754.9"
},
response: {
status: 204
Expand All @@ -25,7 +25,6 @@ addInteractionHandler('save perf results to influx2', () => {

addInteractionHandler('save perf results with custom tags and fields to influx2', () => {
return {
strict: false,
request: {
method: 'POST',
path: '/api/v2/write',
Expand All @@ -37,7 +36,9 @@ addInteractionHandler('save perf results with custom tags and fields to influx2'
"bucket": "testbucket",
"precision": "ns"
},
body: regex("PerfRun", /PerfRun,App=PactumJS,Name=TOTAL,Status=PASS,Team=QA data_received_rate=5166.44,data_received_sum=362818330,data_sent_rate=38.87,data_sent_sum=2729683,duration_avg=4660,duration_max=15513,duration_med=3318,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_rate=0,errors_sum=0,id=123,samples_rate=0.55535,samples_sum=39,status=0,transactions=2,transactions_failed=0,transactions_passed=2 \d+\nPerfRun,App=PactumJS,Name=S01_T01_Application_Launch,Status=PASS,Team=QA data_received_rate=2662.79,data_received_sum=184633892,data_sent_rate=5.36,data_sent_sum=371654,duration_avg=3086,duration_max=3797,duration_med=2832,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_rate=0.001,errors_sum=0,id=123,samples_rate=0.14422,samples_sum=10,status=0 \d+\nPerfRun,App=PactumJS,Name=S01_T02_Application_Login,Status=PASS,Team=QA data_received_rate=2754.9,data_received_sum=169706365,data_sent_rate=12.94,data_sent_sum=797125,duration_avg=4355,duration_max=10786,duration_med=3273,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_rate=0,errors_sum=0,id=123,samples_rate=0.1461,samples_sum=9,status=0 \d+/)
body: "PerfRun,Team=QA,App=PactumJS,Name=TOTAL,Status=PASS id=123,status=0,transactions=2,transactions_passed=2,transactions_failed=0,samples_sum=39,samples_rate=0.55535,duration_avg=4660,duration_med=3318,duration_max=15513,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_sum=0,errors_rate=0,data_sent_sum=2729683,data_sent_rate=38.87,data_received_sum=362818330,data_received_rate=5166.44\n"+
"PerfTransaction,Team=QA,App=PactumJS,Name=S01_T01_Application_Launch,Status=PASS id=123,status=0,samples_sum=10,samples_rate=0.14422,duration_avg=3086,duration_med=2832,duration_max=3797,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_sum=0,errors_rate=0.001,data_sent_sum=371654,data_sent_rate=5.36,data_received_sum=184633892,data_received_rate=2662.79\n"+
"PerfTransaction,Team=QA,App=PactumJS,Name=S01_T02_Application_Login,Status=PASS id=123,status=0,samples_sum=9,samples_rate=0.1461,duration_avg=4355,duration_med=3273,duration_max=10786,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_sum=0,errors_rate=0,data_sent_sum=797125,data_sent_rate=12.94,data_received_sum=169706365,data_received_rate=2754.9"
},
response: {
status: 204
Expand All @@ -47,7 +48,6 @@ addInteractionHandler('save perf results with custom tags and fields to influx2'

addInteractionHandler('save test results to influx2', () => {
return {
strict: false,
request: {
method: 'POST',
path: '/api/v2/write',
Expand All @@ -59,7 +59,12 @@ addInteractionHandler('save test results to influx2', () => {
"bucket": "testbucket",
"precision": "ns"
},
body: regex("TestRun", /TestRun,Name=Default\\ suite,Status=PASS duration=2000,failed=0,passed=4,status=0,total=4 \d+\nTestSuite,Name=Default\\ test,Status=PASS duration=2000,failed=0,passed=4,status=0,total=4 \d+\nTestCase,Name=c2,Status=PASS duration=0,status=0 \d+\nTestCase,Name=c3,Status=PASS duration=10,status=0 \d+\nTestCase,Name=c1,Status=PASS duration=0,status=0 \d+\nTestCase,Name=c4,Status=PASS duration=0,status=0 \d+/)
body: "TestRun,Name=Default\\ suite,Status=PASS status=0,total=4,passed=4,failed=0,duration=2000\n"+
"TestSuite,Name=Default\\ test,Status=PASS status=0,total=4,passed=4,failed=0,duration=2000\n"+
"TestCase,Name=c2,Status=PASS status=0,duration=0\n"+
"TestCase,Name=c3,Status=PASS status=0,duration=10\n"+
"TestCase,Name=c1,Status=PASS status=0,duration=0\n"+
"TestCase,Name=c4,Status=PASS status=0,duration=0"
},
response: {
status: 204
Expand All @@ -69,7 +74,6 @@ addInteractionHandler('save test results to influx2', () => {

addInteractionHandler('save test results with custom tags and fields to influx2', () => {
return {
strict: false,
request: {
method: 'POST',
path: '/api/v2/write',
Expand All @@ -81,7 +85,11 @@ addInteractionHandler('save test results with custom tags and fields to influx2'
"bucket": "testbucket",
"precision": "ns"
},
body: regex("TestRun", /TestRun,App=PactumJS,Name=Staging\\ -\\ UI\\ Smoke\\ Test\\ Run,Status=FAIL,Team=QA duration=1883597,failed=1,id=123,passed=1,status=1,stringfield="coolvalue",total=2 \d+\nTestSuite,App=PactumJS,Name=desktop-chrome,Status=PASS,Team=QA duration=1164451,failed=0,id=123,passed=1,status=0,stringfield="coolvalue",total=1 \d+\nTestCase,App=PactumJS,Name=GU,Status=PASS,Team=QA duration=243789,id=123,status=0,stringfield="coolvalue" \d+\nTestSuite,App=PactumJS,Name=mobile-andoid,Status=FAIL,Team=QA duration=714100,failed=1,id=123,passed=0,status=1,stringfield="coolvalue",total=1 \d+\nTestCase,App=PactumJS,Name=GU,Status=FAIL,Team=QA duration=156900,id=123,status=1,stringfield="coolvalue" \d+/)
body: "TestRun,Team=QA,App=PactumJS,Name=Staging\\ -\\ UI\\ Smoke\\ Test\\ Run,Status=FAIL id=123,stringfield=\"coolvalue\",status=1,total=2,passed=1,failed=1,duration=1883597\n" +
"TestSuite,Team=QA,App=PactumJS,Name=desktop-chrome,Status=PASS id=123,stringfield=\"coolvalue\",status=0,total=1,passed=1,failed=0,duration=1164451\n" +
"TestCase,Team=QA,App=PactumJS,Name=GU,Status=PASS id=123,stringfield=\"coolvalue\",status=0,duration=243789\n" +
"TestSuite,Team=QA,App=PactumJS,Name=mobile-andoid,Status=FAIL id=123,stringfield=\"coolvalue\",status=1,total=1,passed=0,failed=1,duration=714100\n" +
"TestCase,Team=QA,App=PactumJS,Name=GU,Status=FAIL id=123,stringfield=\"coolvalue\",status=1,duration=156900"
},
response: {
status: 204
Expand Down
Loading

0 comments on commit ce973d1

Please sign in to comment.