Skip to content

Commit

Permalink
Added functions to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
bgelatti committed Nov 11, 2024
1 parent f9dd161 commit ea22455
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 25 deletions.
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/APIBridge/APIBridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parentPort } from "node:worker_threads";
import { nanoid } from "nanoid";
import type { IPluginMessage } from "../../Types.ts";
import type { IPluginMessage } from "../../Types/index.ts";

/**
* A single callback waiting to be triggered from one of the API responses.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { IActionTriggerModuleSetup, TGenericID } from "../../Types.ts";
import type {
IActionTriggerModuleSetup,
TGenericID,
} from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/ActionTypeModule/ActionTypeModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IActionTypeModuleSetup, TGenericID } from "../../Types.ts";
import type { IActionTypeModuleSetup, TGenericID } from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/Core/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
TGenericID,
TGenericToken,
TLiveInspectorConnectionID,
} from "../../Types.ts";
} from "../../Types/index.ts";
import APIBridge from "../APIBridge/APIBridge.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/DatabaseModule/DatabaseModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import type {
TDeviceType,
TGenericID,
TGenericToken,
} from "../../Types.ts";
} from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/FileSystemModule/FileSystemModule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
IModuleSetupWithoutType,
IPluginFilesystemItem,
} from "../../Types.ts";
} from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/Helpers/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
generateResourceID,
validateResourceID,
} from "../../Shared/ResourceID.ts";
import type { IComputerUsage } from "../../Types.ts";
import type { IComputerUsage } from "../../Types/index.ts";
import APIBridge from "../APIBridge/APIBridge.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/HookModule/HookModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
IDeviceData,
IModuleSetupWithoutType,
TGenericID,
} from "../../Types.ts";
} from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IModuleSetupWithoutType } from "../../Types.ts";
import type { IModuleSetupWithoutType } from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/QueueModule/QueueModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type IModuleSetupWithoutType,
TDeviceType,
type TGenericID,
} from "../../Types.ts";
} from "../../Types/index.ts";
import TCoreModule from "../TCoreModule/TCoreModule.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Lib/TCoreModule/TCoreModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
IPluginMessage,
TModuleMessageType,
TPluginType,
} from "../../Types.ts";
} from "../../Types/index.ts";
import APIBridge from "../APIBridge/APIBridge.ts";

const moduleIDs = new Map<string, boolean>();
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/Lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { csvNameGenerator } from "../Helper/util.ts";
import ActionTriggerModule from "./ActionTriggerModule/ActionTriggerModule.ts";
import ActionTypeModule from "./ActionTypeModule/ActionTypeModule.ts";
import core from "./Core/Core.ts";
Expand All @@ -24,4 +25,5 @@ export {
pluginStorage,
ServiceModule,
TCoreModule,
csvNameGenerator,
};
3 changes: 2 additions & 1 deletion plugins/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"dependencies": {
"knex": "3.1.0",
"pg": "8.13.1"
"pg": "8.13.1",
"pg-query-stream": "4.7.1"
},
"devDependencies": {
"@types/pg": "8.11.10"
Expand Down
4 changes: 2 additions & 2 deletions plugins/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import addDeviceData from "./Providers/DeviceData/addDeviceData.ts";
import applyDeviceDataRetention from "./Providers/DeviceData/applyDeviceDataRetention.ts";
import deleteDeviceData from "./Providers/DeviceData/deleteDeviceData.ts";
import editDeviceData from "./Providers/DeviceData/editDeviceData.ts";
import exportDeviceData from "./Providers/DeviceData/exportDeviceData.ts";
import getDeviceDataAmount from "./Providers/DeviceData/getDeviceDataAmount.ts";
import getDeviceDataAvg from "./Providers/DeviceData/getDeviceDataAvg.ts";
import getDeviceDataCount from "./Providers/DeviceData/getDeviceDataCount.ts";
Expand All @@ -54,6 +55,7 @@ import getDeviceDataLastValue from "./Providers/DeviceData/getDeviceDataLastValu
import getDeviceDataMax from "./Providers/DeviceData/getDeviceDataMax.ts";
import getDeviceDataMin from "./Providers/DeviceData/getDeviceDataMin.ts";
import getDeviceDataSum from "./Providers/DeviceData/getDeviceDataSum.ts";
import importDeviceData from "./Providers/DeviceData/importDeviceData.ts";
import deletePluginStorageItem from "./Providers/PluginStorage/deletePluginStorageItem.ts";
import getAllPluginStorageItems from "./Providers/PluginStorage/getAllPluginStorageItem.ts";
import getPluginStorageItem from "./Providers/PluginStorage/getPluginStorageItem.ts";
Expand All @@ -63,8 +65,6 @@ import getHourlyStatistics from "./Providers/Statistic/getHourlyStatistics.ts";
import getSummary from "./Providers/Summary/getSummary.ts";
import getTagKeys from "./Providers/Tag/getTagKeys.ts";
import configs from "./settings.ts";
import exportDeviceData from "./Providers/DeviceData/exportDeviceData.ts";
import importDeviceData from "./Providers/DeviceData/importDeviceData.ts";

export const postgreSQL = new DatabaseModule({
id: "PostgreSQL",
Expand Down
12 changes: 2 additions & 10 deletions plugins/sqlite/src/Providers/DeviceData/exportDeviceData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "node:fs";
import { csvNameGenerator } from "@tago-io/tcore-sdk";
import type { TDeviceType, TGenericID } from "@tago-io/tcore-sdk/types";
import { stringify } from "csv";
import { getDeviceConnection } from "../../Helpers/DeviceDatabase.ts";
Expand All @@ -14,16 +15,7 @@ async function exportDeviceData(
const client = await getDeviceConnection(deviceID, type);
return new Promise((resolve, reject) => {
const deviceDataStream = client.select("*").from("data").stream();
const date = new Date();
const day = date.getDate().toString();
const month = (date.getMonth() + 1).toString();
const year = date.getFullYear().toString();
const hour = date.getHours().toString();
const minute = date.getMinutes().toString();
const second = date.getSeconds().toString();
const name = `${year}-${month}-${day}_${hour}-${minute}-${second}`;

const filename = `${folder}/data_${deviceID}_${name}.csv`;
const filename = `${folder}/${csvNameGenerator(`data_${deviceID}`)}`;
const file = fs.createWriteStream(filename);
file.setDefaultEncoding("utf8");

Expand Down
21 changes: 21 additions & 0 deletions plugins/sqlite/src/Providers/DeviceData/importDeviceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ async function importDeviceData(
.pipe(parse({ columns: true, encoding: "utf8" }))
.on("data", (row) => {
row.id = generateResourceID();

if (!row.location) {
row.location = null;
}

if (!row.metadata) {
row.metadata = null;
}

if (!row.value) {
row.value = null;
}

if (!row.unit) {
row.unit = null;
}

if (!row.serie) {
row.serie = null;
}

data.push(row);
if (data.length === 1000) {
_insertData(client, data);
Expand Down

0 comments on commit ea22455

Please sign in to comment.