Skip to content

Commit

Permalink
Merge a06d40a into 072fc74
Browse files Browse the repository at this point in the history
  • Loading branch information
joyja committed Mar 23, 2020
2 parents 072fc74 + a06d40a commit c539d40
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/database/model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const logger = require('../logger')

const executeQuery = function(db, sql, params = [], firstRowOnly = false) {
console.log(new Date().toISOString())
console.log(sql)
console.log(params)
return new Promise((resolve, reject) => {
const callback = (error, rows) => {
if (error) {
Expand All @@ -18,6 +21,9 @@ const executeQuery = function(db, sql, params = [], firstRowOnly = false) {
}

const executeUpdate = function(db, sql, params = []) {
console.log(new Date().toISOString())
console.log(sql)
console.log(params)
return new Promise((resolve, reject) => {
db.run(sql, params, function(error) {
if (error) {
Expand Down
22 changes: 10 additions & 12 deletions src/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Mqtt.prototype.publishHistory = async function() {
let historyToPublish = []
for (const host of hosts) {
const history = await host.getHistory(this.recordLimit)
console.log(history)
const newRecords = history.filter((record) => {
return !historyToPublish.some((row) => {
return row.id === record.id
Expand Down Expand Up @@ -346,18 +347,15 @@ Mqtt.prototype.publishHistory = async function() {
return record.id
})
await this.constructor.executeUpdate(sql, params)
sql = `SELECT a.id AS id
FROM mqttHistory AS a
LEFT JOIN mqttPrimaryHostHistory AS b ON a.id = b.mqttHistory
WHERE b.id IS NULL LIMIT 750`
const historyToDelete = await this.constructor.executeQuery(sql, [], false)
sql = `DELETE FROM mqttHistory WHERE id in (${'?,'
.repeat(historyToDelete.length)
.slice(0, -1)})`
params = historyToDelete.map((record) => {
return record.id
})
await this.constructor.executeUpdate(sql, params)
sql = `DELETE FROM mqttHistory
WHERE id IN (
SELECT a.id AS id
FROM mqttHistory AS a
LEFT JOIN mqttPrimaryHostHistory AS b
on a.id = b.mqttHistory
WHERE b.id IS NULL LIMIT 750
)`
await this.constructor.executeUpdate(sql)
}

Object.defineProperties(Mqtt.prototype, {
Expand Down
1 change: 1 addition & 0 deletions src/service/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class Mqtt extends Model {
this.stopPublishing()
}
startPublishing() {
this.interval = clearInterval(this.interval)
this.interval = setInterval(() => {
this.publish()
this.publishHistory()
Expand Down

0 comments on commit c539d40

Please sign in to comment.