Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
breck7 committed Oct 11, 2024
1 parent 8559454 commit 77b6d4b
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions dashboard.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require("fs").promises
const fs = require("fs")
const readline = require("readline")

class Dashboard {
constructor(inputFile) {
this.inputFile = inputFile
this.logEntries = []
this.dailyStats = {}
}

Expand All @@ -22,31 +22,29 @@ class Dashboard {
return null
}

generateDailyStatistics() {
this.dailyStats = {}
this.logEntries.forEach(entry => {
if (entry) {
const date = entry.timestamp.toISOString().split("T")[0]
if (!this.dailyStats[date]) {
this.dailyStats[date] = {
reads: 0,
writes: 0,
uniqueReaders: new Set(),
uniqueWriters: new Set()
}
updateDailyStats(entry) {
if (entry) {
const date = entry.timestamp.toISOString().split("T")[0]
if (!this.dailyStats[date]) {
this.dailyStats[date] = {
reads: 0,
writes: 0,
uniqueReaders: new Set(),
uniqueWriters: new Set()
}
}

if (entry.method.toUpperCase() === "GET") {
this.dailyStats[date].reads++
this.dailyStats[date].uniqueReaders.add(entry.ip)
} else if (entry.method.toUpperCase() === "POST") {
this.dailyStats[date].writes++
this.dailyStats[date].uniqueWriters.add(entry.ip)
}
if (entry.method.toUpperCase() === "GET") {
this.dailyStats[date].reads++
this.dailyStats[date].uniqueReaders.add(entry.ip)
} else if (entry.method.toUpperCase() === "POST") {
this.dailyStats[date].writes++
this.dailyStats[date].uniqueWriters.add(entry.ip)
}
})
}
}

// Convert Sets to counts
finalizeDailyStats() {
Object.keys(this.dailyStats).forEach(date => {
this.dailyStats[date].readers = this.dailyStats[date].uniqueReaders.size
this.dailyStats[date].writers = this.dailyStats[date].uniqueWriters.size
Expand All @@ -56,14 +54,27 @@ class Dashboard {
}

async processLogFile() {
try {
const logContent = await fs.readFile(this.inputFile, "utf-8")
this.logEntries = logContent.split("\n").map(this.parseLogEntry).filter(Boolean)
this.generateDailyStatistics()
} catch (error) {
console.error("Error processing log file:", error)
throw error
}
return new Promise((resolve, reject) => {
const fileStream = fs.createReadStream(this.inputFile)
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
})

rl.on("line", line => {
const entry = this.parseLogEntry(line)
this.updateDailyStats(entry)
})

rl.on("close", () => {
this.finalizeDailyStats()
resolve()
})

fileStream.on("error", error => {
reject(error)
})
})
}

get csv() {
Expand Down

0 comments on commit 77b6d4b

Please sign in to comment.