amberflo-metering-typescript
Version:
Amberflo metering client for TypeScript
58 lines • 2.51 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManualIngestClient = void 0;
const ingestApiClient_1 = require("./ingestApiClient");
const uuid_1 = require("uuid");
class ManualIngestClient {
constructor(apiKey, ingestOptions, debug = false) {
this.debug = false;
this.queue = [];
this.apiKey = apiKey;
this.signature = '[amberflo-metering SynchIngestClient]:';
this.debug = debug;
}
start() {
console.log(new Date(), this.signature, `start the client with debug ${this.debug} `);
this.apiClient = new ingestApiClient_1.IngestApiClient(this.apiKey, this.debug);
}
ingestMeter(meter) {
if (this.debug) {
console.log(new Date(), this.signature, 'queuing meter message: ', meter);
}
this.queue.push(meter);
}
flush() {
return __awaiter(this, void 0, void 0, function* () {
if (this.queue.length < 1) {
console.log(new Date(), this.signature, 'no records in the queue to flush');
return;
}
const snapshot = this.queue.splice(0, this.queue.length);
if (this.debug) {
console.log(new Date(), this.signature, 'body', snapshot);
}
const requestId = (0, uuid_1.v1)();
if (this.debug) {
console.log(new Date(), this.signature, 'starting request', requestId);
}
return this.apiClient.postSync(snapshot, requestId);
});
}
shutdown() {
if (this.debug) {
console.log(new Date(), this.signature, 'shutting down the client');
}
return this.flush();
}
}
exports.ManualIngestClient = ManualIngestClient;
//# sourceMappingURL=manualIngestClient.js.map