Skip to content

Instantly share code, notes, and snippets.

@alistairhenderson
Created July 25, 2023 14:05
Show Gist options
  • Save alistairhenderson/db932ac877f2ccb74d6969316bd71bb7 to your computer and use it in GitHub Desktop.
Save alistairhenderson/db932ac877f2ccb74d6969316bd71bb7 to your computer and use it in GitHub Desktop.
create remote server date get and MariaDB insert
[
{
"id": "4857a264a8d2ea44",
"type": "tab",
"label": "Sync Server Time",
"disabled": false,
"info": "",
"env": []
},
{
"id": "5def329def69eb8e",
"type": "inject",
"z": "4857a264a8d2ea44",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "line_no",
"v": "01",
"vt": "str"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "01 02 * * *",
"once": false,
"onceDelay": 0.1,
"topic": "insert into time_date_check_tab values(:line_no,:server_date,:current_date,:timediff)",
"payload": "",
"payloadType": "date",
"x": 110,
"y": 240,
"wires": [
[
"c9d934b9d94467ca",
"e21d225ce40b3edb"
]
]
},
{
"id": "c145632b3d076563",
"type": "bigssh",
"z": "4857a264a8d2ea44",
"name": "Remote server Date",
"commandLine": "date \"+%Y-%m-%d %H:%M:%S\"",
"commandArgs": "",
"minError": 1,
"minWarning": 1,
"noStdin": false,
"format": "utf8",
"payloadIsArg": false,
"myssh": "7d8b905f3c74f087",
"x": 640,
"y": 160,
"wires": [
[
"0e2eed6a8fd7dc60"
],
[],
[]
]
},
{
"id": "ad73eb6b89a3cace",
"type": "debug",
"z": "4857a264a8d2ea44",
"name": "OUT",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1050,
"y": 80,
"wires": []
},
{
"id": "0e2eed6a8fd7dc60",
"type": "bigline",
"z": "4857a264a8d2ea44",
"name": "Line Out",
"filename": "",
"format": "utf8",
"keepEmptyLines": false,
"x": 860,
"y": 160,
"wires": [
[
"ad73eb6b89a3cace",
"264bdf20cd026c88"
],
[]
]
},
{
"id": "2824c8fc9fa72b76",
"type": "bigssh",
"z": "4857a264a8d2ea44",
"name": "Local Server Date",
"commandLine": "date \"+%Y-%m-%d %H:%M:%S\"",
"commandArgs": "",
"minError": 1,
"minWarning": 1,
"noStdin": false,
"format": "utf8",
"payloadIsArg": false,
"myssh": "5160cce2ee7c5981",
"x": 650,
"y": 320,
"wires": [
[
"92ac47b1ae71a0e5"
],
[],
[]
]
},
{
"id": "92ac47b1ae71a0e5",
"type": "bigline",
"z": "4857a264a8d2ea44",
"name": "Line Out",
"filename": "",
"format": "utf8",
"keepEmptyLines": false,
"x": 860,
"y": 320,
"wires": [
[
"14e1fefe06120fdf",
"264bdf20cd026c88"
],
[]
]
},
{
"id": "14e1fefe06120fdf",
"type": "debug",
"z": "4857a264a8d2ea44",
"name": "Out",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1050,
"y": 480,
"wires": []
},
{
"id": "264bdf20cd026c88",
"type": "join",
"z": "4857a264a8d2ea44",
"name": "Merge the messages",
"mode": "custom",
"build": "array",
"property": "",
"propertyType": "full",
"key": "line_no",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "2",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "num",
"reduceFixup": "",
"x": 1100,
"y": 240,
"wires": [
[
"57a42d99da02c532",
"1c1764e217ce87fb"
]
]
},
{
"id": "57a42d99da02c532",
"type": "debug",
"z": "4857a264a8d2ea44",
"name": "Statement",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1370,
"y": 140,
"wires": []
},
{
"id": "c9d934b9d94467ca",
"type": "change",
"z": "4857a264a8d2ea44",
"name": "",
"rules": [
{
"t": "set",
"p": "server",
"pt": "msg",
"to": "remote",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 400,
"y": 180,
"wires": [
[
"c145632b3d076563"
]
]
},
{
"id": "e21d225ce40b3edb",
"type": "change",
"z": "4857a264a8d2ea44",
"name": "",
"rules": [
{
"t": "set",
"p": "server",
"pt": "msg",
"to": "local",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 400,
"y": 320,
"wires": [
[
"2824c8fc9fa72b76"
]
]
},
{
"id": "1439d3ada1e45b02",
"type": "Stackhero-MySQL",
"z": "4857a264a8d2ea44",
"server": "9a3488bb972a44eb",
"name": "Insert values into table",
"x": 1660,
"y": 420,
"wires": [
[]
]
},
{
"id": "1c1764e217ce87fb",
"type": "function",
"z": "4857a264a8d2ea44",
"name": "Create insert values including timediff",
"func": "const inp_payload = msg.payload;\n\nlet payloadArray = [];\n\nfunction convertToDate(dateString) {\n const [datePart, timePart] = dateString.split(\" \");\n const [year, month, day] = datePart.split(\"-\");\n const [hour, minute, second] = timePart.split(\":\");\n return new Date(year, month - 1, day, hour, minute, second);\n}\n\nfunction formatDate(date) {\n const year = date.getFullYear();\n const month = String(date.getMonth() + 1).padStart(2, \"0\");\n const day = String(date.getDate()).padStart(2, \"0\");\n const hour = String(date.getHours()).padStart(2, \"0\");\n const minute = String(date.getMinutes()).padStart(2, \"0\");\n const second = String(date.getSeconds()).padStart(2, \"0\");\n return `${year}-${month}-${day} ${hour}:${minute}:${second}`;\n}\n\nfor (const item of inp_payload) {\n if (item.server === \"local\") {\n const serverDate = convertToDate(inp_payload.find((el) => el.server === \"remote\" && el.line_no === item.line_no).payload);\n const currentDate = convertToDate(item.payload);\n\n // Calculate time difference in milliseconds and convert to seconds (Unix timestamp)\n // @ts-ignore\n const timediff = Math.floor((currentDate - serverDate) / 1000);\n\n payloadArray.push({\n line_no: item.line_no,\n server_date: formatDate(serverDate),\n current_date: formatDate(currentDate),\n timediff: timediff\n });\n }\n}\n\nconst payload = payloadArray[0];\nconst topic = msg.topic.replace(/\\\\\"/g, '\"');\n\nreturn { topic, payload };\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1370,
"y": 340,
"wires": [
[
"bf3355a82b359754",
"1439d3ada1e45b02"
]
]
},
{
"id": "bf3355a82b359754",
"type": "debug",
"z": "4857a264a8d2ea44",
"name": "New Output",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1650,
"y": 240,
"wires": []
},
{
"id": "7d8b905f3c74f087",
"type": "SSH_Credentials",
"host": "10.1.1.1",
"port": "22",
"userlabel": "system@10.1.1.1"
},
{
"id": "5160cce2ee7c5981",
"type": "SSH_Credentials",
"host": "127.0.0.1",
"port": "22",
"userlabel": "rsadmin@127.0.0.1"
},
{
"id": "9a3488bb972a44eb",
"type": "Stackhero-MySQL-Server",
"name": "localhost",
"host": "localhost",
"port": "3306",
"tls": false,
"database": "data"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment