locatai-ts
Version:
Enterprise-grade AI-powered element locator for Selenium WebDriver - TypeScript implementation
94 lines • 3.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonFileLocatorTiming = void 0;
const path_1 = __importDefault(require("path"));
const JsonFileHelpers_1 = require("./JsonFileHelpers");
/**
* File-based implementation of ILocatorTiming that tracks locator timing statistics
*/
class JsonFileLocatorTiming {
/**
* Creates a new JSON file locator timing tracker
* @param root Optional root directory path
*/
constructor(root = process.cwd()) {
this._gate = Promise.resolve();
this._timings = { version: 1, timings: [] };
this._cacheDir = path_1.default.join(root, 'LocatAICache');
this._filePath = path_1.default.join(this._cacheDir, 'locatai_smart_waits.json');
this._init();
}
/**
* Initialize the timing data by loading from disk
*/
async _init() {
await (0, JsonFileHelpers_1.ensureDir)(this._cacheDir);
this._timings = await (0, JsonFileHelpers_1.readJson)(this._filePath, { version: 1, timings: [] });
}
/**
* Execute a function with lock to prevent concurrent file access
* @param work Function to execute
* @returns Result of the function
*/
async _lock(work) {
const prev = this._gate;
let release;
this._gate = new Promise(r => (release = r));
await prev;
try {
const result = await work();
await (0, JsonFileHelpers_1.writeJson)(this._filePath, this._timings);
return result;
}
finally {
release();
}
}
/**
* @inheritdoc
*/
async record(url, query, domHash, elapsedSeconds) {
await this._lock(async () => {
let timing = this._timings.timings.find(t => t.url === url && t.query === query && t.dom === domHash);
if (!timing) {
timing = {
url,
query,
dom: domHash,
count: 0,
total: 0,
min: elapsedSeconds,
max: elapsedSeconds,
avg: elapsedSeconds
};
this._timings.timings.push(timing);
}
timing.count++;
timing.total += elapsedSeconds;
timing.min = Math.min(timing.min, elapsedSeconds);
timing.max = Math.max(timing.max, elapsedSeconds);
timing.avg = timing.total / timing.count;
});
}
/**
* @inheritdoc
*/
async get(url, query, domHash) {
await this._gate;
const timing = this._timings.timings.find(t => t.url === url && t.query === query && t.dom === domHash);
return timing || null;
}
/**
* @inheritdoc
*/
async clear() {
await this._lock(async () => {
this._timings.timings = [];
});
}
}
exports.JsonFileLocatorTiming = JsonFileLocatorTiming;
//# sourceMappingURL=JsonFileLocatorTiming.js.map