Skip to content

Commit

Permalink
fix dedup logs (#977)
Browse files Browse the repository at this point in the history
* fix dedup logs

* fmt
  • Loading branch information
pepoviola authored May 1, 2023
1 parent 83a27d3 commit 1236136
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
1 change: 0 additions & 1 deletion javascript/packages/orchestrator/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ export class Network {

replaceWithNetworInfo(placeholder: string): string {
return placeholder.replace(
///{{ZOMBIE:(.*?):(.*?)}}/gi,
TOKEN_PLACEHOLDER,
(_substring, nodeName, key: keyof NetworkNode) => {
const node = this.getNodeByName(nodeName);
Expand Down
32 changes: 19 additions & 13 deletions javascript/packages/orchestrator/src/networkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export class NetworkNode implements NetworkNodeInterface {
userDefinedTypes: any;
para?: PARA;
parachainId?: number;
lastLogLineCheckedTimestamp?: string;
lastLogLineCheckedIndex?: number;
group?: string;

constructor(
Expand Down Expand Up @@ -415,6 +413,8 @@ export class NetworkNode implements NetworkNodeInterface {
timeout: number = DEFAULT_INDIVIDUAL_TEST_TIMEOUT,
): Promise<boolean> {
try {
let lastLogLineCheckedTimestamp: string;
let lastLogLineCheckedIndex: number;
const re = isGlob ? makeRe(pattern) : new RegExp(pattern, "ig");
if (!re) throw new Error(`Invalid glob pattern: ${pattern} `);
const client = getClient();
Expand All @@ -425,7 +425,10 @@ export class NetworkNode implements NetworkNodeInterface {
const dedupedLogs = this._dedupLogs(
logs.split("\n"),
client.providerName === "native",
lastLogLineCheckedTimestamp,
lastLogLineCheckedIndex,
);

const index = dedupedLogs.findIndex((line) => {
if (client.providerName !== "native") {
// remove the extra timestamp
Expand All @@ -436,11 +439,9 @@ export class NetworkNode implements NetworkNodeInterface {

if (index >= 0) {
done = true;
this.lastLogLineCheckedTimestamp = dedupedLogs[index];
this.lastLogLineCheckedIndex = index;
debug(
this.lastLogLineCheckedTimestamp.split(" ").slice(1).join(" "),
);
lastLogLineCheckedTimestamp = dedupedLogs[index];
lastLogLineCheckedIndex = index;
debug(lastLogLineCheckedTimestamp.split(" ").slice(1).join(" "));
} else {
await new Promise((resolve) => setTimeout(resolve, 1000));
logs = await client.getNodeLogs(this.name, 2, true);
Expand Down Expand Up @@ -538,12 +539,17 @@ export class NetworkNode implements NetworkNodeInterface {
return spanNames;
}

// prevent to seach in the same log line twice.
_dedupLogs(logs: string[], useIndex = false): string[] {
if (!this.lastLogLineCheckedTimestamp) return logs;
if (useIndex) return logs.slice(this.lastLogLineCheckedIndex);

const lastLineTs = this.lastLogLineCheckedTimestamp.split(" ")[0];
// prevent to search in the same log line twice.
_dedupLogs(
logs: string[],
useIndex = false,
lastLogLineCheckedTimestamp: string,
lastLogLineCheckedIndex: number,
): string[] {
if (!lastLogLineCheckedTimestamp) return logs;
if (useIndex) return logs.slice(lastLogLineCheckedIndex);

const lastLineTs = lastLogLineCheckedTimestamp.split(" ")[0];
const index = logs.findIndex((logLine) => {
const thisLineTs = logLine.split(" ")[0];
return thisLineTs > lastLineTs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
validateRuntimeCode,
} from "../jsapi-helpers";
import { Network } from "../network";
import { NetworkNode } from "../networkNode";
import { FnArgs } from "../types";
const utilCrypto = require("@polkadot/util-crypto");

Expand Down Expand Up @@ -127,7 +128,9 @@ const LogMatch = ({ node_name, pattern, match_type, timeout }: FnArgs) => {
return async (network: Network) => {
const nodes = network.getNodes(node_name!);
const results = await Promise.all(
nodes.map((node: any) => node.findPattern(pattern!, isGlob, timeout)),
nodes.map((node: NetworkNode) =>
node.findPattern(pattern!, isGlob, timeout),
),
);

const found = results.every(Boolean);
Expand Down

0 comments on commit 1236136

Please sign in to comment.