From 76df9f7ecdbfc311d4eae5ed343c6077611ae77a Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Sun, 22 Mar 2020 01:38:21 +0100 Subject: [PATCH] Initial Postgres migration --- classes/postgres.js | 20 ++++ classes/postgres.ts | 24 ++++ package.json | 13 +- web.js | 3 +- worker.js | 258 +++++++++++++++++++++------------------- worker.ts | 281 +++++++++++++++++++++++--------------------- 6 files changed, 337 insertions(+), 262 deletions(-) create mode 100644 classes/postgres.js create mode 100644 classes/postgres.ts diff --git a/classes/postgres.js b/classes/postgres.js new file mode 100644 index 0000000..3d71d8e --- /dev/null +++ b/classes/postgres.js @@ -0,0 +1,20 @@ +"use strict"; +exports.__esModule = true; +var _a = require("pg"), Pool = _a.Pool, Client = _a.Client; +var connectionString = process.env.DATABASE_URL; +var pool = new Pool(); +var client = new Client({ + connectionString: connectionString +}); +client.connect(); +console.log(client); +console.log("---------------Trying connection-----------------"); +client.query("SELECT NOW()", function (err, res) { + console.log(err, res); + // client.end(); +}); +// module.exports.postgres = function() { +// return client; +// }; +// export {}; +module.exports.client = client; diff --git a/classes/postgres.ts b/classes/postgres.ts new file mode 100644 index 0000000..1b30722 --- /dev/null +++ b/classes/postgres.ts @@ -0,0 +1,24 @@ +const { Pool, Client } = require("pg"); +const connectionString = process.env.DATABASE_URL; +const pool = new Pool(); + +const client = new Client({ + connectionString: connectionString +}); +client.connect(); + +console.log(client); +console.log("---------------Trying connection-----------------"); + +client.query("SELECT NOW()", (err, res) => { + console.log(err, res); + // client.end(); +}); + +// module.exports.postgres = function() { +// return client; +// }; +// export {}; + +module.exports.client = client; +export {}; diff --git a/package.json b/package.json index 306e051..9491635 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,7 @@ { "scripts": { "prettier": "prettier worker.ts web.ts reminder.ts classes/*.ts --write", - "typescript": "tsc web.ts worker.ts reminder.ts classes/google_sheets.ts classes/telegram.ts classes/config.ts", - + "typescript": "tsc web.ts worker.ts reminder.ts classes/google_sheets.ts classes/postgres.ts classes/telegram.ts classes/config.ts", "dev": "source .keys && npm run prettier && npm run typescript && heroku local", "web": "npm run prettier && npm run typescript && node web.js", "worker": "npm run prettier && npm run typescript && node worker.js", @@ -10,10 +9,10 @@ }, "dependencies": { "moment": "^2.24.0", - "needle": "^2.2.4", - "pg": "^7.16.0", - "prettier": "^1.16.4", - "telegraf": "^3.28.0", - "typescript": "^3.4.1" + "needle": "^2.3.3", + "pg": "^7.18.2", + "prettier": "^1.19.1", + "telegraf": "^3.36.0", + "typescript": "^3.8.3" } } diff --git a/web.js b/web.js index 583f934..5d694ff 100644 --- a/web.js +++ b/web.js @@ -24,7 +24,8 @@ function initAPI(rawDataSheet, lastRunSheet) { "macrosFat", "weeklyComputerTime", "weeklyPhoneTime", - "dexaFatPercent" + "dexaFatPercent", + "meditated" ]; var _loop_1 = function (i) { var key = keys[i]; diff --git a/worker.js b/worker.js index 1985105..460f730 100644 --- a/worker.js +++ b/worker.js @@ -6,20 +6,14 @@ var needle = require("needle"); var _a = require("telegraf"), Router = _a.Router, Markup = _a.Markup, Extra = _a.Extra; // Internal dependencies var config = require("./classes/config.js"); -var google_sheets = require("./classes/google_sheets.js"); +var postgres = require("./classes/postgres.js"); var telegram = require("./classes/telegram.js"); var bot = telegram.bot; // State var currentlyAskedQuestionObject = null; var currentlyAskedQuestionMessageId = null; // The Telegram message ID reference var currentlyAskedQuestionQueue = []; // keep track of all the questions about to be asked -var rawDataSheet = null; -var lastRunSheet = null; -google_sheets.setupGoogleSheets(function (rawDataSheetRef, lastRunSheetRef) { - rawDataSheet = rawDataSheetRef; - lastRunSheet = lastRunSheetRef; - initBot(); -}); +initBot(); function getButtonText(number) { var emojiNumber = { "0": "0️⃣", @@ -52,69 +46,80 @@ function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue, skip loadingMessageID = message_id; }); } - rawDataSheet.getRows({ - offset: 0, - limit: 100, - orderby: "timestamp", - reverse: true, - query: "key=" + key - }, function (error, rows) { - if (error) { - console.error(error); - ctx.reply(error); - return; - } - var allValues = []; - var allTimes = []; - var rawText = []; - var minimum = 10000; - var maximum = 0; - for (var i = 0; i < rows.length; i++) { - var time = moment(Number(rows[i].timestamp)); - var value = Number(rows[i].value); - allValues.unshift(value); - allTimes.unshift(time.format("MM-DD")); - if (i < numberOfRecentValuesToPrint - 1) { - rawText.unshift(time.format("YYYY-MM-DD") + ": " + value.toFixed(2)); - } - if (value < minimum) { - minimum = value; - } - if (value > maximum) { - maximum = value; - } - } - if (additionalValue) { - allValues.push(additionalValue); - allTimes.push(moment()); - rawText.push(moment().format("YYYY-MM-DD") + - ": " + - Number(additionalValue).toFixed(2)); - } - // Print the raw values - if (numberOfRecentValuesToPrint > 0 && loadingMessageID) { - ctx.telegram.editMessageText(ctx.update.message.chat.id, loadingMessageID, null, rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum); - } - // Generate the graph - if (!skipImage) { - minimum -= 2; - maximum += 2; - var url = "https://chart.googleapis.com/chart?cht=lc&chd=t:" + - allValues.join(",") + - "&chs=800x350&chl=" + - allTimes.join("%7C") + - "&chtt=" + - key + - "&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" + - minimum + - "," + - maximum; - console.log(url); - ctx.replyWithPhoto({ - url: url - }); - } - }); + // rawDataSheet.getRows( + // { + // offset: 0, + // limit: 100, + // orderby: "timestamp", + // reverse: true, + // query: "key=" + key + // }, + // function(error, rows) { + // if (error) { + // console.error(error); + // ctx.reply(error); + // return; + // } + // let allValues = []; + // let allTimes = []; + // let rawText = []; + // let minimum = 10000; + // let maximum = 0; + // for (let i = 0; i < rows.length; i++) { + // let time = moment(Number(rows[i].timestamp)); + // let value = Number(rows[i].value); + // allValues.unshift(value); + // allTimes.unshift(time.format("MM-DD")); + // if (i < numberOfRecentValuesToPrint - 1) { + // rawText.unshift(time.format("YYYY-MM-DD") + ": " + value.toFixed(2)); + // } + // if (value < minimum) { + // minimum = value; + // } + // if (value > maximum) { + // maximum = value; + // } + // } + // if (additionalValue) { + // allValues.push(additionalValue); + // allTimes.push(moment()); + // rawText.push( + // moment().format("YYYY-MM-DD") + + // ": " + + // Number(additionalValue).toFixed(2) + // ); + // } + // // Print the raw values + // if (numberOfRecentValuesToPrint > 0 && loadingMessageID) { + // ctx.telegram.editMessageText( + // ctx.update.message.chat.id, + // loadingMessageID, + // null, + // rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum + // ); + // } + // // Generate the graph + // if (!skipImage) { + // minimum -= 2; + // maximum += 2; + // let url = + // "https://chart.googleapis.com/chart?cht=lc&chd=t:" + + // allValues.join(",") + + // "&chs=800x350&chl=" + + // allTimes.join("%7C") + + // "&chtt=" + + // key + + // "&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" + + // minimum + + // "," + + // maximum; + // console.log(url); + // ctx.replyWithPhoto({ + // url: url + // }); + // } + // } + // ); } function triggerNextQuestionFromQueue(ctx) { var keyboard = Extra.markup(function (m) { return m.removeKeyboard(); }); // default keyboard @@ -230,32 +235,46 @@ function insertNewValue(parsedUserValue, ctx, key, type, fakeDate) { Type: type, Value: parsedUserValue }; - rawDataSheet.addRow(row, function (error, row) { - if (error) { - console.error(error); - if (ctx) { - ctx.reply("Error saving value: " + error); - ctx.reply("Value: " + parsedUserValue); - ctx.reply("Key: " + key); - } + postgres.client.query({ + text: "INSERT INTO raw_data (" + + Object.keys(row).join(",") + + ") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", + values: Object.values(row) + }, function (err, res) { + console.log(res); + if (err) { + console.log(err.stack); } - if (ctx) { - // we don't use this for location sending as we have many values for that, so that's when `ctx` is nil - // Show that we saved the value - // Currently the Telegram API doens't support updating of messages that have a custom keyboard - // for no good reason, as mentioned here https://github.com/TelegramBots/telegram.bot/issues/176 - // - // Bad Request: Message can't be edited - // - // Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards - // ctx.telegram.editMessageText( - // ctx.update.message.chat.id, - // currentlyAskedQuestionMessageId, - // null, - // "✅ " + lastQuestionAskedDupe + " ✅" - // ); + else { + console.log(res.rows[0]); } }); + // rawDataSheet.addRow(row, function(error, row) { + // if (error) { + // console.error(error); + // if (ctx) { + // ctx.reply("Error saving value: " + error); + // ctx.reply("Value: " + parsedUserValue); + // ctx.reply("Key: " + key); + // } + // } + // }); + if (ctx) { + // we don't use this for location sending as we have many values for that, so that's when `ctx` is nil + // Show that we saved the value + // Currently the Telegram API doens't support updating of messages that have a custom keyboard + // for no good reason, as mentioned here https://github.com/TelegramBots/telegram.bot/issues/176 + // + // Bad Request: Message can't be edited + // + // Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards + // ctx.telegram.editMessageText( + // ctx.update.message.chat.id, + // currentlyAskedQuestionMessageId, + // null, + // "✅ " + lastQuestionAskedDupe + " ✅" + // ); + } } function parseUserInput(ctx, text) { if (text === void 0) { text = null; } @@ -341,30 +360,33 @@ function sendAvailableCommands(ctx) { }); } function saveLastRun(command) { - lastRunSheet.getRows({ - offset: 1, - limit: 100 - }, function (error, rows) { - var updatedExistingRow = false; - for (var i = 0; i < rows.length; i++) { - var currentRow = rows[i]; - var currentCommand = currentRow.command; - if (command == currentCommand) { - updatedExistingRow = true; - currentRow.lastrun = moment().valueOf(); // unix timestamp - currentRow.save(); - } - } - if (!updatedExistingRow) { - var row = { - Command: command, - LastRun: moment().valueOf() // unix timestamp - }; - lastRunSheet.addRow(row, function (error, row) { - console.log("Stored timestamp of last run for " + command); - }); - } - }); + // lastRunSheet.getRows( + // { + // offset: 1, + // limit: 100 + // }, + // function(error, rows) { + // var updatedExistingRow = false; + // for (let i = 0; i < rows.length; i++) { + // let currentRow = rows[i]; + // let currentCommand = currentRow.command; + // if (command == currentCommand) { + // updatedExistingRow = true; + // currentRow.lastrun = moment().valueOf(); // unix timestamp + // currentRow.save(); + // } + // } + // if (!updatedExistingRow) { + // let row = { + // Command: command, + // LastRun: moment().valueOf() // unix timestamp + // }; + // // lastRunSheet.addRow(row, function(error, row) { + // // console.log("Stored timestamp of last run for " + command); + // // }); + // } + // } + // ); } function initBot() { console.log("Launching up Telegram bot..."); diff --git a/worker.ts b/worker.ts index 0bd9c4f..cee30c1 100644 --- a/worker.ts +++ b/worker.ts @@ -5,7 +5,7 @@ const { Router, Markup, Extra } = require("telegraf"); // Internal dependencies let config = require("./classes/config.js"); -let google_sheets = require("./classes/google_sheets.js"); +let postgres = require("./classes/postgres.js"); let telegram = require("./classes/telegram.js"); import { Command, QuestionToAsk } from "./classes/config.js"; @@ -16,15 +16,8 @@ let bot = telegram.bot; var currentlyAskedQuestionObject: QuestionToAsk = null; var currentlyAskedQuestionMessageId: String = null; // The Telegram message ID reference let currentlyAskedQuestionQueue: Array = []; // keep track of all the questions about to be asked -let rawDataSheet = null; -let lastRunSheet = null; -google_sheets.setupGoogleSheets(function(rawDataSheetRef, lastRunSheetRef) { - rawDataSheet = rawDataSheetRef; - lastRunSheet = lastRunSheetRef; - - initBot(); -}); +initBot(); function getButtonText(number) { let emojiNumber = { @@ -68,88 +61,88 @@ function printGraph( }); } - rawDataSheet.getRows( - { - offset: 0, - limit: 100, - orderby: "timestamp", - reverse: true, - query: "key=" + key - }, - function(error, rows) { - if (error) { - console.error(error); - ctx.reply(error); - return; - } + // rawDataSheet.getRows( + // { + // offset: 0, + // limit: 100, + // orderby: "timestamp", + // reverse: true, + // query: "key=" + key + // }, + // function(error, rows) { + // if (error) { + // console.error(error); + // ctx.reply(error); + // return; + // } - let allValues = []; - let allTimes = []; - let rawText = []; - let minimum = 10000; - let maximum = 0; - for (let i = 0; i < rows.length; i++) { - let time = moment(Number(rows[i].timestamp)); - let value = Number(rows[i].value); - allValues.unshift(value); - allTimes.unshift(time.format("MM-DD")); + // let allValues = []; + // let allTimes = []; + // let rawText = []; + // let minimum = 10000; + // let maximum = 0; + // for (let i = 0; i < rows.length; i++) { + // let time = moment(Number(rows[i].timestamp)); + // let value = Number(rows[i].value); + // allValues.unshift(value); + // allTimes.unshift(time.format("MM-DD")); - if (i < numberOfRecentValuesToPrint - 1) { - rawText.unshift(time.format("YYYY-MM-DD") + ": " + value.toFixed(2)); - } + // if (i < numberOfRecentValuesToPrint - 1) { + // rawText.unshift(time.format("YYYY-MM-DD") + ": " + value.toFixed(2)); + // } - if (value < minimum) { - minimum = value; - } - if (value > maximum) { - maximum = value; - } - } + // if (value < minimum) { + // minimum = value; + // } + // if (value > maximum) { + // maximum = value; + // } + // } - if (additionalValue) { - allValues.push(additionalValue); - allTimes.push(moment()); + // if (additionalValue) { + // allValues.push(additionalValue); + // allTimes.push(moment()); - rawText.push( - moment().format("YYYY-MM-DD") + - ": " + - Number(additionalValue).toFixed(2) - ); - } + // rawText.push( + // moment().format("YYYY-MM-DD") + + // ": " + + // Number(additionalValue).toFixed(2) + // ); + // } - // Print the raw values - if (numberOfRecentValuesToPrint > 0 && loadingMessageID) { - ctx.telegram.editMessageText( - ctx.update.message.chat.id, - loadingMessageID, - null, - rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum - ); - } + // // Print the raw values + // if (numberOfRecentValuesToPrint > 0 && loadingMessageID) { + // ctx.telegram.editMessageText( + // ctx.update.message.chat.id, + // loadingMessageID, + // null, + // rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum + // ); + // } - // Generate the graph - if (!skipImage) { - minimum -= 2; - maximum += 2; + // // Generate the graph + // if (!skipImage) { + // minimum -= 2; + // maximum += 2; - let url = - "https://chart.googleapis.com/chart?cht=lc&chd=t:" + - allValues.join(",") + - "&chs=800x350&chl=" + - allTimes.join("%7C") + - "&chtt=" + - key + - "&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" + - minimum + - "," + - maximum; - console.log(url); - ctx.replyWithPhoto({ - url: url - }); - } - } - ); + // let url = + // "https://chart.googleapis.com/chart?cht=lc&chd=t:" + + // allValues.join(",") + + // "&chs=800x350&chl=" + + // allTimes.join("%7C") + + // "&chtt=" + + // key + + // "&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" + + // minimum + + // "," + + // maximum; + // console.log(url); + // ctx.replyWithPhoto({ + // url: url + // }); + // } + // } + // ); } function triggerNextQuestionFromQueue(ctx) { @@ -276,33 +269,51 @@ function insertNewValue(parsedUserValue, ctx, key, type, fakeDate = null) { Value: parsedUserValue }; - rawDataSheet.addRow(row, function(error, row) { - if (error) { - console.error(error); - if (ctx) { - ctx.reply("Error saving value: " + error); - ctx.reply("Value: " + parsedUserValue); - ctx.reply("Key: " + key); + postgres.client.query( + { + text: + "INSERT INTO raw_data (" + + Object.keys(row).join(",") + + ") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", + values: Object.values(row) + }, + (err, res) => { + console.log(res); + if (err) { + console.log(err.stack); + } else { + console.log(res.rows[0]); } } + ); - if (ctx) { - // we don't use this for location sending as we have many values for that, so that's when `ctx` is nil - // Show that we saved the value - // Currently the Telegram API doens't support updating of messages that have a custom keyboard - // for no good reason, as mentioned here https://github.com/TelegramBots/telegram.bot/issues/176 - // - // Bad Request: Message can't be edited - // - // Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards - // ctx.telegram.editMessageText( - // ctx.update.message.chat.id, - // currentlyAskedQuestionMessageId, - // null, - // "✅ " + lastQuestionAskedDupe + " ✅" - // ); - } - }); + // rawDataSheet.addRow(row, function(error, row) { + // if (error) { + // console.error(error); + // if (ctx) { + // ctx.reply("Error saving value: " + error); + // ctx.reply("Value: " + parsedUserValue); + // ctx.reply("Key: " + key); + // } + // } + // }); + + if (ctx) { + // we don't use this for location sending as we have many values for that, so that's when `ctx` is nil + // Show that we saved the value + // Currently the Telegram API doens't support updating of messages that have a custom keyboard + // for no good reason, as mentioned here https://github.com/TelegramBots/telegram.bot/issues/176 + // + // Bad Request: Message can't be edited + // + // Please note, that it is currently only possible to edit messages without reply_markup or with inline keyboards + // ctx.telegram.editMessageText( + // ctx.update.message.chat.id, + // currentlyAskedQuestionMessageId, + // null, + // "✅ " + lastQuestionAskedDupe + " ✅" + // ); + } } function parseUserInput(ctx, text = null) { @@ -425,35 +436,33 @@ function sendAvailableCommands(ctx) { } function saveLastRun(command) { - lastRunSheet.getRows( - { - offset: 1, - limit: 100 - }, - function(error, rows) { - var updatedExistingRow = false; - for (let i = 0; i < rows.length; i++) { - let currentRow = rows[i]; - let currentCommand = currentRow.command; - if (command == currentCommand) { - updatedExistingRow = true; - - currentRow.lastrun = moment().valueOf(); // unix timestamp - currentRow.save(); - } - } - - if (!updatedExistingRow) { - let row = { - Command: command, - LastRun: moment().valueOf() // unix timestamp - }; - lastRunSheet.addRow(row, function(error, row) { - console.log("Stored timestamp of last run for " + command); - }); - } - } - ); + // lastRunSheet.getRows( + // { + // offset: 1, + // limit: 100 + // }, + // function(error, rows) { + // var updatedExistingRow = false; + // for (let i = 0; i < rows.length; i++) { + // let currentRow = rows[i]; + // let currentCommand = currentRow.command; + // if (command == currentCommand) { + // updatedExistingRow = true; + // currentRow.lastrun = moment().valueOf(); // unix timestamp + // currentRow.save(); + // } + // } + // if (!updatedExistingRow) { + // let row = { + // Command: command, + // LastRun: moment().valueOf() // unix timestamp + // }; + // // lastRunSheet.addRow(row, function(error, row) { + // // console.log("Stored timestamp of last run for " + command); + // // }); + // } + // } + // ); } function initBot() {