How to use compressReports method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

healthcheck.js

Source: healthcheck.js Github

copy
1"use strict";
2
3const express = require("express");
4const {
5	service,
6	writeError,
7} = require("./common");
8
9const router = express.Router();
10
11var statusListeners = [];
12var services = [];
13const reportsPerGroup = 10; // 5 minutes (1 report = 30 seconds)
14const historySize = 288; // 24 hours
15var nextGroup = [];
16var toSend = {services: [], data: []};
17
18service.redis.smembers("status:services", (err, result) => {
19	if (!!err) {
20		console.error(err);
21		return;
22	}
23
24	services = result;
25});
26
27function mergeReports(reports) {
28	const merged = {};
29
30	for (let name of services) {
31		let count = 0,
32			ping = 0,
33			success = 0,
34			errors = 0;
35
36		for (var i = 0; i < reports.length; i++) {
37			let serviceReport = reports[i][name];
38			if (!serviceReport) { continue; }
39
40			count += 1;
41			ping += serviceReport.ping;
42			success += serviceReport.success;
43			errors += serviceReport.errors;
44		}
45
46		merged[name] = {
47			success,
48			errors,
49			ping: Math.ceil(ping / Math.max(count, 1)),
50		};
51	}
52
53	return merged;
54}
55
56function compressReports(reports) {
57	const compressed = [];
58
59	for (var i = 0; i < reports.length; i++) {
60		let report = reports[i];
61		let compressedReport = [];
62
63		for (let name of services) {
64			let serviceReport = report[name];
65
66			if (!serviceReport) {
67				compressedReport.push(null);
68				continue;
69			}
70
71			compressedReport.push([
72				serviceReport.ping,
73				serviceReport.success,
74				serviceReport.errors,
75			]);
76		}
77
78		compressed.push(compressedReport);
79	}
80
81	return compressed;
82}
83
84function parsePings(pings) {
85	let report = {};
86
87	// format with structure
88	// {
89	// 	"service": {
90	// 		"success": 0,
91	// 		"errors": 0,
92	// 		"ping": average
93	// 	}
94	// }
95
96	const workers = Object.keys(pings);
97	for (let i = 0; i < workers.length; i++) {
98		const [service, worker] = workers[i].split("@", 2);
99		let ping = pings[ workers[i] ];
100
101		if (!report[service]) {
102			report[service] = {
103				count: 1,
104				...ping,
105			};
106		} else {
107			report[service].count += 1;
108			report[service].ping += ping.ping;
109			report[service].success += ping.success;
110			report[service].errors += ping.errors;
111		}
112	}
113
114	let unknownServices = [];
115	const serviceKeys = Object.keys(report);
116	for (let i = 0; i < serviceKeys.length; i++) {
117		const service = serviceKeys[i];
118
119		if (!services.includes(service)) {
120			unknownServices.push(service);
121		}
122
123		report[service].ping = report[service].ping / report[service].count;
124		delete report[service].count;
125	}
126
127	return [ report, unknownServices ];
128}
129
130// Triggered when this service sends a ping request and it gets completed
131service.onPing((responsible, pings) => {
132	const [ report, unknownServices ] = parsePings(pings);
133
134	if (unknownServices.length > 0) {
135		for (let i = 0; i < unknownServices.length; i++) {
136			services.push(unknownServices[i]);
137		}
138
139		if (responsible) {
140			service.redis.sadd("status:services", unknownServices, (err) => {
141				if (!!err) {
142					console.log(err);
143				}
144			});
145		}
146	}
147
148	nextGroup.push(report);
149	if (nextGroup.length >= reportsPerGroup) {
150		if (responsible) {
151			const group = mergeReports(nextGroup);
152			service.redis.rpush(
153				"status:reports",
154				JSON.stringify(group),
155				(err, items) => {
156					if (!!err) {
157						console.error(err);
158						return;
159					}
160
161					if (items > historySize) {
162						service.redis.lpop(
163							"status:reports",
164							items - historySize,
165							(err) => {
166								if (!!err) {
167									console.error(err);
168									return;
169								}
170							}
171						);
172					}
173				}
174			);
175		}
176		nextGroup = [];
177	}
178
179	toSend = {
180		services,
181		data: compressReports([report])[0],
182	};
183	let forWS = JSON.stringify(toSend);
184	// Notify all the clients listening for this event
185	for (let i = 0; i < statusListeners.length; i++) {
186		statusListeners[i].send(forWS);
187	}
188});
189
190// A new client wants to listen to service status
191router.ws("/status", (ws, req) => {
192	statusListeners.push(ws); // Register the client
193
194	ws.on("close", () => {
195		// The client disconnected. Remove from list.
196		let index = statusListeners.indexOf(ws);
197		statusListeners.splice(index, 1);
198	});
199});
200
201router.get("/status/current", (req, res) => {
202	res.send(toSend);
203});
204
205router.get("/status/past", (req, res) => {
206	let { interval } = req.query;
207	interval = parseInt(interval) || 2;
208
209	if (interval > 12) {
210		writeError(res, 400, "Requested interval is too big.");
211		return;
212	}
213
214	service.redis.lrange("status:reports", 0, -1, (err, result) => {
215		if (!!err) {
216			console.error(err);
217			writeError(res, 500);
218			return;
219		}
220
221		let reports = [];
222		let forInterval = [];
223		if (interval > 1) {
224			// Some intervals are skipped, so let's merge them
225			for (let i = 0; i < result.length; i++) {
226				forInterval.push(JSON.parse(result[i]));
227
228				if (i > 0 && ((i + 1) % interval === 0)) {
229					reports.push(mergeReports(forInterval));
230					forInterval = [];
231				}
232			}
233
234			forInterval = compressReports(forInterval);
235		} else {
236			for (let i = 0; i < result.length; i++) {
237				reports.push(JSON.parse(result[i]));
238			}
239		}
240
241		res.send({
242			interval,
243			services,
244			forInterval,
245			data: compressReports(reports)
246		});
247	});
248});
249
250module.exports = router;
251
Full Screen

dashboard_compressed_v1.js

Source: dashboard_compressed_v1.js Github

copy
1/**
2 * Copyright (c) Microsoft Corporation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const {SimpleBlob, flattenSpecs} = require('./utils.js');
18
19async function processDashboardCompressedV1(context, reports, commitSHA) {
20  const timestamp = Date.now();
21  const dashboardBlob = await SimpleBlob.create('dashboards', `compressed_v1/${commitSHA}.json`);
22  await dashboardBlob.uploadGzipped(compressReports(reports));
23
24  context.log(`
25  ===== started dashboard compressed v1 =====
26    SHA: ${commitSHA}
27  ===== complete in ${Date.now() - timestamp}ms =====
28  `);
29}
30
31module.exports = {processDashboardCompressedV1, compressReports};
32
33function compressReports(reports) {
34  const files = {};
35  for (const report of reports) {
36    for (const spec of flattenSpecs(report)) {
37      let specs = files[spec.file];
38      if (!specs) {
39        specs = new Map();
40        files[spec.file] = specs;
41      }
42      const specId = spec.file + '---' + spec.title + ' --- ' + spec.line;
43      let specObject = specs.get(specId);
44      if (!specObject) {
45        specObject = {
46          title: spec.title,
47          line: spec.line,
48          column: spec.column,
49          tests: new Map(),
50        };
51        specs.set(specId, specObject);
52      }
53      for (const test of spec.tests || []) {
54        // It's unclear how many results we get in the new test runner - let's
55        // stay on the safe side and skip test without any results.
56        if (!test.results || !test.results.length)
57          continue;
58        // We get tests with a single result without status for sharded
59        // tests that are inside shard that we don't run.
60        if (test.results.length === 1 && !test.results[0].status)
61          continue;
62        // Folio currently reports `data` as part of test results.
63        // In our case, all data will be identical - so pick
64        // from the first result.
65        const testParameters = test.results[0].data;
66        // Prefer test platform when it exists, and fallback to
67        // the host platform when it doesn't. This way we can attribute
68        // android tests to android.
69        let platform = testParameters.platform;
70        if (!platform) {
71          const osName = report.metadata.osName.toUpperCase().startsWith('MINGW') ? 'Windows' : report.metadata.osName;
72          const arch = report.metadata.arch && !report.metadata.arch.includes('x86') ? report.metadata.arch : '';
73          platform = (osName + ' ' + report.metadata.osVersion + ' ' + arch).trim();
74        }
75        const browserName = testParameters.browserName || 'N/A';
76
77        const testName = getTestName(browserName, platform, testParameters);
78        let testObject = specObject.tests.get(testName);
79        if (!testObject) {
80          testObject = {
81            parameters: {
82              ...testParameters,
83              browserName,
84              platform,
85            },
86          };
87          // By default, all tests are expected to pass. We can have this as a hidden knowledge.
88          if (test.expectedStatus !== 'passed')
89            testObject.expectedStatus = test.expectedStatus;
90          if (test.annotations.length)
91            testObject.annotations = test.annotations;
92          specObject.tests.set(testName, testObject);
93        }
94
95        for (const run of test.results) {
96          // Record duration of slow tests only, i.e. > 1s.
97          if (run.status === 'passed' && run.duration > 1000) {
98            testObject.minTime = Math.min((testObject.minTime || Number.MAX_VALUE), run.duration);
99            testObject.maxTime = Math.max((testObject.maxTime || 0), run.duration);
100          }
101          if (run.status === 'failed') {
102            if (!Array.isArray(testObject.failed))
103              testObject.failed = [];
104            testObject.failed.push(run.error);
105          } else {
106            testObject[run.status] = (testObject[run.status] || 0) + 1;
107          }
108        }
109      }
110    }
111  }
112
113  const pojo = Object.entries(files).map(([file, specs]) => ({
114    file,
115    specs: [...specs.values()].map(specObject => ({
116      ...specObject,
117      tests: [...specObject.tests.values()],
118    })),
119  }));
120  return pojo;
121}
122
123function getTestName(browserName, platform, parameters) {
124  return [browserName, platform, ...Object.entries(parameters).filter(([key, value]) => !!value).map(([key, value]) => {
125    if (key === 'browserName' || key === 'platform')
126      return;
127    if (typeof value === 'string')
128      return value;
129    if (typeof value === 'boolean')
130      return key;
131    return `${key}=${value}`;
132  }).filter(Boolean)].join(' / ');
133}
134
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)