| 1 | const statement = require("./statement.js"); |
| 2 | const event = require("./event.js"); |
| 3 | const fs = require("fs"); |
| 4 |
|
| 5 | const FileHandler = require("./fileHandler.js"); |
| 6 |
|
| 7 | class Queue { |
| 8 | constructor(name, location, config) { |
| 9 | if (typeof name != "string") throw new Error(`Expected string, got ${typeof name} for name`); |
| 10 | if (typeof location != "string") throw new Error(`Expected string, got ${typeof location} for location`); |
| 11 | this.name = name; |
| 12 | this.timestamp = new Date(); |
| 13 | this.location = location; |
| 14 | this.filename = this.location + "/" + this.timestamp.toISOString().replace(/\:/g, "-").replace(/\./g, "-") + ".log"; |
| 15 | this.queue = []; |
| 16 |
|
| 17 | this.compact = false; |
| 18 | this.fileStarted = false; |
| 19 | |
| 20 | if (config) { |
| 21 | if (config.compact) { |
| 22 | if (typeof config.compact != "boolean") throw new Error(`Expected boolean, got ${typeof config.compact} for config.compact`); |
| 23 | this.compact = config.compact; |
| 24 | } |
| 25 | } |
| 26 |
|
| 27 | let configFile = this.location + "/metadata.json"; |
| 28 |
|
| 29 | FileHandler.AppendFile(this.filename, ""); // touch the file |
| 30 | FileHandler.AppendFile(configFile, ""); |
| 31 |
|
| 32 | let configContent = fs.readFileSync(configFile).toString(); |
| 33 |
|
| 34 | let queueConfig = []; |
| 35 | if (configContent != "") queueConfig = JSON.parse(configContent); |
| 36 |
|
| 37 | queueConfig.push( |
| 38 | { |
| 39 | "time": this.timestamp, |
| 40 | "log": this.filename |
| 41 | } |
| 42 | ); |
| 43 |
|
| 44 | fs.writeFileSync(configFile, JSON.stringify(queueConfig)); |
| 45 |
|
| 46 | return this; |
| 47 | } |
| 48 |
|
| 49 | push() { |
| 50 | if (arguments.length >= 3) this.queue.push(new statement(arguments[0], arguments[1], arguments[2])); |
| 51 | else if (arguments.length > 0) { |
| 52 | if (arguments[0] instanceof event) this.queue.push(arguments[0].statements); // object is an Event |
| 53 | else if (arguments[0] instanceof statement) this.queue.push(arguments[0]); // object is a Statement |
| 54 | else throw new TypeError("Expected an Event or Statement"); |
| 55 | } else throw new Error("Expected at least one argument"); |
| 56 | return this; |
| 57 | } |
| 58 |
|
| 59 | write() { |
| 60 | let recursiveCompactFunction; |
| 61 | if (this.compact) { |
| 62 | recursiveCompactFunction = function(object) { |
| 63 | if (Array.isArray(object)) { |
| 64 | for (let i = 0; i < object.length; i++) { |
| 65 | object[i] = recursiveCompactFunction(object[i]); |
| 66 | } |
| 67 | return object; |
| 68 | } else { |
| 69 | let fixed = {}; |
| 70 | fixed.tt = object.timestamp; |
| 71 | fixed.v = object.verbosity; |
| 72 | fixed.t = object.tag; |
| 73 | fixed.m = object.message; |
| 74 | return fixed; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 |
|
| 79 | if (arguments.length > 0) { |
| 80 | let toWrite; |
| 81 | if (arguments.length >= 3) toWrite = new statement(arguments[0], arguments[1], arguments[2]); |
| 82 | else { |
| 83 | if (arguments[0] instanceof event) toWrite = arguments[0].statements; // object is an Event |
| 84 | else if (arguments[0] instanceof statement) toWrite = arguments[0]; // object is a Statement |
| 85 | else throw new TypeError("Expected an Event or Statement"); |
| 86 | } |
| 87 |
|
| 88 | if (this.compact) { |
| 89 | toWrite = recursiveCompactFunction(toWrite); |
| 90 | } |
| 91 |
|
| 92 | let appendString = ""; |
| 93 | if (this.fileStarted) appendString += "\n"; |
| 94 | appendString += JSON.stringify(toWrite); |
| 95 | if (!this.fileStarted) this.fileStarted = true; |
| 96 | FileHandler.AppendFile(this.filename, appendString); |
| 97 | } else { |
| 98 | let queueLength = this.queue.length; |
| 99 | if (queueLength > 0) { |
| 100 | let appendString = ""; |
| 101 | let queue = this; |
| 102 | let toWrite = this.queue; |
| 103 | if (this.compact) { |
| 104 | toWrite = recursiveCompactFunction(toWrite); |
| 105 | } |
| 106 | toWrite.forEach(function (item) { |
| 107 | if (queue.fileStarted) appendString += "\n"; |
| 108 | appendString += JSON.stringify(item); |
| 109 | if (!queue.fileStarted) queue.fileStarted = true; |
| 110 | }); |
| 111 | this.queue = this.queue.slice(queueLength); |
| 112 | FileHandler.AppendFile(this.filename, appendString); |
| 113 | } |
| 114 | } |
| 115 | return this; |
| 116 | } |
| 117 | } |
| 118 |
|
| 119 | module.exports = Queue; |
| 120 |
|