Skip to content

Commit

Permalink
attempting to split energydata table by asset
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Sep 28, 2023
1 parent ef01cc3 commit ea4e878
Show file tree
Hide file tree
Showing 41 changed files with 648 additions and 291 deletions.
2 changes: 1 addition & 1 deletion database/addAsset.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { Asset } from '../types/recordTypes.js';
export declare function addAsset(asset: Partial<Asset>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addAsset(asset: Partial<Asset>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): Promise<number>;
7 changes: 5 additions & 2 deletions database/addAsset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite from 'better-sqlite3';
import { databasePath } from '../helpers/functions.database.js';
export function addAsset(asset, sessionUser, connectedEmileDB) {
import { ensureEnergyDataTableExists } from './manageEnergyDataTables.js';
export async function addAsset(asset, sessionUser, connectedEmileDB) {
const emileDB = connectedEmileDB === undefined ? sqlite(databasePath) : connectedEmileDB;
const rightNowMillis = Date.now();
const result = emileDB
Expand All @@ -11,8 +12,10 @@ export function addAsset(asset, sessionUser, connectedEmileDB) {
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?, ?, ?)`)
.run(asset.assetName, asset.categoryId, (asset.latitude ?? '') === '' ? undefined : asset.latitude, (asset.longitude ?? '') === '' ? undefined : asset.longitude, sessionUser.userName, rightNowMillis, sessionUser.userName, rightNowMillis);
const assetId = result.lastInsertRowid;
await ensureEnergyDataTableExists(assetId);
if (connectedEmileDB === undefined) {
emileDB.close();
}
return result.lastInsertRowid;
return assetId;
}
12 changes: 9 additions & 3 deletions database/addAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import sqlite from 'better-sqlite3'
import { databasePath } from '../helpers/functions.database.js'
import type { Asset } from '../types/recordTypes.js'

export function addAsset(
import { ensureEnergyDataTableExists } from './manageEnergyDataTables.js'

export async function addAsset(
asset: Partial<Asset>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
): Promise<number> {
const emileDB =
connectedEmileDB === undefined ? sqlite(databasePath) : connectedEmileDB

Expand All @@ -33,9 +35,13 @@ export function addAsset(
rightNowMillis
)

const assetId = result.lastInsertRowid as number

await ensureEnergyDataTableExists(assetId)

if (connectedEmileDB === undefined) {
emileDB.close()
}

return result.lastInsertRowid as number
return assetId
}
4 changes: 3 additions & 1 deletion database/addEnergyData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Debug from 'debug';
import { getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { delay } from '../helpers/functions.utilities.js';
import { ensureEnergyDataTableExists } from './manageEnergyDataTables.js';
const debug = Debug('emile:database:addEnergyData');
export async function addEnergyData(data, sessionUser, connectedEmileDB) {
const emileDB = connectedEmileDB === undefined
Expand All @@ -10,8 +11,9 @@ export async function addEnergyData(data, sessionUser, connectedEmileDB) {
while (true) {
try {
const rightNowMillis = Date.now();
const tableName = await ensureEnergyDataTableExists(data.assetId);
result = emileDB
.prepare(`insert into EnergyData (
.prepare(`insert into ${tableName} (
assetId, dataTypeId, fileId,
timeSeconds, durationSeconds, dataValue, powerOfTenMultiplier,
recordCreate_userName, recordCreate_timeMillis,
Expand Down
8 changes: 7 additions & 1 deletion database/addEnergyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getConnectionWhenAvailable } from '../helpers/functions.database.js'
import { delay } from '../helpers/functions.utilities.js'
import type { EnergyData } from '../types/recordTypes.js'

import { ensureEnergyDataTableExists } from './manageEnergyDataTables.js'

const debug = Debug('emile:database:addEnergyData')

export async function addEnergyData(
Expand All @@ -23,9 +25,13 @@ export async function addEnergyData(
try {
const rightNowMillis = Date.now()

const tableName = await ensureEnergyDataTableExists(
data.assetId as number
)

result = emileDB
.prepare(
`insert into EnergyData (
`insert into ${tableName} (
assetId, dataTypeId, fileId,
timeSeconds, durationSeconds, dataValue, powerOfTenMultiplier,
recordCreate_userName, recordCreate_timeMillis,
Expand Down
2 changes: 1 addition & 1 deletion database/deleteEnergyData.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export declare function deleteEnergyData(dataId: number | string, sessionUser: EmileUser): boolean;
export declare function deleteEnergyData(assetId: number | string, dataId: number | string, sessionUser: EmileUser): Promise<boolean>;
export declare function deleteEnergyDataByFileId(fileId: number | string, sessionUser: EmileUser): Promise<boolean>;
32 changes: 19 additions & 13 deletions database/deleteEnergyData.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sqlite from 'better-sqlite3';
import { databasePath, getConnectionWhenAvailable } from '../helpers/functions.database.js';
export function deleteEnergyData(dataId, sessionUser) {
const emileDB = sqlite(databasePath);
import { getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { ensureEnergyDataTableExists, reloadEnergyDataTableNames } from './manageEnergyDataTables.js';
export async function deleteEnergyData(assetId, dataId, sessionUser) {
const emileDB = await getConnectionWhenAvailable();
const tableName = await ensureEnergyDataTableExists(assetId, emileDB);
const result = emileDB
.prepare(`update EnergyData
.prepare(`update ${tableName}
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
Expand All @@ -14,13 +15,18 @@ export function deleteEnergyData(dataId, sessionUser) {
}
export async function deleteEnergyDataByFileId(fileId, sessionUser) {
const emileDB = await getConnectionWhenAvailable();
const result = emileDB
.prepare(`update EnergyData
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
and fileId = ?`)
.run(sessionUser.userName, Date.now(), fileId);
const tableNames = await reloadEnergyDataTableNames(emileDB);
let count = 0;
for (const tableName of tableNames) {
const result = emileDB
.prepare(`update ${tableName}
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
and fileId = ?`)
.run(sessionUser.userName, Date.now(), fileId);
count += result.changes;
}
emileDB.close();
return result.changes > 0;
return count > 0;
}
46 changes: 30 additions & 16 deletions database/deleteEnergyData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import sqlite from 'better-sqlite3'
import { getConnectionWhenAvailable } from '../helpers/functions.database.js'

import { databasePath, getConnectionWhenAvailable } from '../helpers/functions.database.js'
import {
ensureEnergyDataTableExists,
reloadEnergyDataTableNames
} from './manageEnergyDataTables.js'

export function deleteEnergyData(
export async function deleteEnergyData(
assetId: number | string,
dataId: number | string,
sessionUser: EmileUser
): boolean {
const emileDB = sqlite(databasePath)
): Promise<boolean> {
const emileDB = await getConnectionWhenAvailable()

const tableName = await ensureEnergyDataTableExists(assetId, emileDB)

const result = emileDB
.prepare(
`update EnergyData
`update ${tableName}
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
Expand All @@ -29,17 +35,25 @@ export async function deleteEnergyDataByFileId(
): Promise<boolean> {
const emileDB = await getConnectionWhenAvailable()

const result = emileDB
.prepare(
`update EnergyData
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
and fileId = ?`
)
.run(sessionUser.userName, Date.now(), fileId)
const tableNames = await reloadEnergyDataTableNames(emileDB)

let count = 0

for (const tableName of tableNames) {
const result = emileDB
.prepare(
`update ${tableName}
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
and fileId = ?`
)
.run(sessionUser.userName, Date.now(), fileId)

count += result.changes
}

emileDB.close()

return result.changes > 0
return count > 0
}
14 changes: 10 additions & 4 deletions database/getEnergyData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { powerOfTenMultipliers } from '@cityssm/green-button-parser/lookups.js';
import { dateStringToDate } from '@cityssm/utils-datetime';
import sqlite from 'better-sqlite3';
import { databasePath, getConnectionWhenAvailable, getTempTableName } from '../helpers/functions.database.js';
import { ensureEnergyDataTableExists } from './manageEnergyDataTables.js';
function userFunction_getPowerOfTenMultiplierName(powerOfTenMultiplier) {
if (powerOfTenMultiplier === 0) {
return '';
Expand Down Expand Up @@ -61,7 +62,7 @@ export async function getEnergyData(filters, options) {
filters.startDateString !== filters.endDateString;
const columnNames = options?.formatForExport ?? false
? `d.dataId,
c.category, a.assetName,
c.category, a.assetName,
ts.serviceCategory,
userFunction_getPowerOfTenMultiplierName(d.powerOfTenMultiplier) as powerOfTenMultiplierName,
tu.unit,
Expand Down Expand Up @@ -103,9 +104,13 @@ export async function getEnergyData(filters, options) {
d.fileId, f.originalFileName,
d.timeSeconds, d.durationSeconds, d.endTimeSeconds,
d.dataValue, d.powerOfTenMultiplier`;
const emileDB = await getConnectionWhenAvailable(true);
const tableName = (filters.assetId ?? '') === ''
? 'EnergyData'
: await ensureEnergyDataTableExists(filters.assetId, emileDB);
const { sqlParameters, sqlWhereClause } = buildWhereClause(filters);
let sql = `select ${columnNames}
from EnergyData d
from ${tableName} d
left join Assets a
on d.assetId = a.assetId
left join AssetCategories c
Expand All @@ -131,8 +136,9 @@ export async function getEnergyData(filters, options) {
sql +=
" group by d.assetId, d.dataTypeId, substr(datetime(d.timeSeconds, 'unixepoch', 'localtime'), 1, 10), d.powerOfTenMultiplier";
}
const orderBy = ' order by assetId, dataTypeId, timeSeconds';
const emileDB = await getConnectionWhenAvailable(true);
const orderBy = options?.formatForExport ?? false
? ''
: ' order by assetId, dataTypeId, timeSeconds';
emileDB.function('userFunction_getPowerOfTenMultiplierName', userFunction_getPowerOfTenMultiplierName);
const tempTableName = getTempTableName();
emileDB
Expand Down
22 changes: 17 additions & 5 deletions database/getEnergyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from '../helpers/functions.database.js'
import type { EnergyData } from '../types/recordTypes.js'

import { ensureEnergyDataTableExists } from './manageEnergyDataTables.js'

interface GetEnergyDataFilters {
assetId?: number | string
categoryId?: number | string
Expand Down Expand Up @@ -124,7 +126,7 @@ export async function getEnergyData(
const columnNames =
options?.formatForExport ?? false
? `d.dataId,
c.category, a.assetName,
c.category, a.assetName,
ts.serviceCategory,
userFunction_getPowerOfTenMultiplierName(d.powerOfTenMultiplier) as powerOfTenMultiplierName,
tu.unit,
Expand Down Expand Up @@ -167,10 +169,20 @@ export async function getEnergyData(
d.timeSeconds, d.durationSeconds, d.endTimeSeconds,
d.dataValue, d.powerOfTenMultiplier`

const emileDB = await getConnectionWhenAvailable(true)

const tableName =
(filters.assetId ?? '') === ''
? 'EnergyData'
: await ensureEnergyDataTableExists(
filters.assetId as string | number,
emileDB
)

const { sqlParameters, sqlWhereClause } = buildWhereClause(filters)

let sql = `select ${columnNames}
from EnergyData d
from ${tableName} d
left join Assets a
on d.assetId = a.assetId
left join AssetCategories c
Expand Down Expand Up @@ -198,9 +210,9 @@ export async function getEnergyData(
" group by d.assetId, d.dataTypeId, substr(datetime(d.timeSeconds, 'unixepoch', 'localtime'), 1, 10), d.powerOfTenMultiplier"
}

const orderBy = ' order by assetId, dataTypeId, timeSeconds'

const emileDB = await getConnectionWhenAvailable(true)
const orderBy = options?.formatForExport ?? false
? ''
: ' order by assetId, dataTypeId, timeSeconds'

emileDB.function(
'userFunction_getPowerOfTenMultiplierName',
Expand Down
3 changes: 2 additions & 1 deletion database/initializeDatabase.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare function initializeDatabase(): void;
export declare const recordColumns = " recordCreate_userName varchar(30) not null,\n recordCreate_timeMillis integer not null,\n recordUpdate_userName varchar(30) not null,\n recordUpdate_timeMillis integer not null,\n recordDelete_userName varchar(30),\n recordDelete_timeMillis integer";
export declare function initializeDatabase(): Promise<void>;
25 changes: 4 additions & 21 deletions database/initializeDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import { addEnergyReadingType } from './addEnergyReadingType.js';
import { addEnergyServiceCategory } from './addEnergyServiceCategory.js';
import { addEnergyUnit } from './addEnergyUnit.js';
import { addUser } from './addUser.js';
import { refreshEnergyDataTableView } from './manageEnergyDataTables.js';
const debug = Debug('emile:database:initializeDatabase');
const initializeDatabaseUser = {
userName: 'system.initialize',
canLogin: false,
canUpdate: true,
isAdmin: true
};
const recordColumns = ` recordCreate_userName varchar(30) not null,
export const recordColumns = ` recordCreate_userName varchar(30) not null,
recordCreate_timeMillis integer not null,
recordUpdate_userName varchar(30) not null,
recordUpdate_timeMillis integer not null,
Expand Down Expand Up @@ -217,7 +218,7 @@ function initializeAssetAliasTypes(emileDB) {
}, initializeDatabaseUser);
}
}
export function initializeDatabase() {
export async function initializeDatabase() {
const emileDB = sqlite(databasePath);
const row = emileDB
.prepare(`select name from sqlite_master
Expand Down Expand Up @@ -299,25 +300,7 @@ export function initializeDatabase() {
primary key (groupId, assetId)
)`)
.run();
emileDB
.prepare(`create table if not exists EnergyData (
dataId integer primary key autoincrement,
assetId integer not null references Assets (assetId),
dataTypeId integer not null references EnergyDataTypes (dataTypeId),
fileId integer references EnergyDataFiles (fileId),
timeSeconds integer not null check (timeSeconds > 0),
durationSeconds integer not null check (durationSeconds > 0),
endTimeSeconds integer not null generated always as (timeSeconds + durationSeconds) virtual,
dataValue decimal(10, 2) not null,
powerOfTenMultiplier integer not null default 0,
${recordColumns}
)`)
.run();
emileDB
.prepare(`create index if not exists idx_EnergyData on EnergyData
(assetId, dataTypeId, timeSeconds)
where recordDelete_timeMillis is null`)
.run();
await refreshEnergyDataTableView(emileDB);
emileDB
.prepare(`create table if not exists Users (
userName varchar(30) primary key,
Expand Down
Loading

0 comments on commit ea4e878

Please sign in to comment.