Skip to content

Commit

Permalink
limit dashboard date ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Aug 17, 2023
1 parent 435ee92 commit d0eaeb5
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 3 deletions.
9 changes: 9 additions & 0 deletions database/getEnergyDataStatistics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface EnergyDataStatistics {
dataIdCount: number;
assetIdDistinctCount: number;
fileIdDistinctCount: number;
timeSecondsMin: number;
endTimeSecondsMax: number;
}
export declare function getEnergyDataStatistics(): EnergyDataStatistics;
export {};
18 changes: 18 additions & 0 deletions database/getEnergyDataStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sqlite from 'better-sqlite3';
import { databasePath } from '../helpers/functions.database.js';
export function getEnergyDataStatistics() {
const emileDB = sqlite(databasePath, {
readonly: true
});
const statistics = emileDB
.prepare(`select count(dataId) as dataIdCount,
count(distinct assetId) as assetIdDistinctCount,
count(distinct fileId) as fileIdDistinctCount,
min(timeSeconds) as timeSecondsMin,
max(endTimeSeconds) as endTimeSecondsMax
from EnergyData
where recordDelete_timeMillis is null`)
.get();
emileDB.close();
return statistics;
}
36 changes: 36 additions & 0 deletions database/getEnergyDataStatistics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sqlite from 'better-sqlite3'

import { databasePath } from '../helpers/functions.database.js'

interface EnergyDataStatistics {
dataIdCount: number

assetIdDistinctCount: number

fileIdDistinctCount: number

timeSecondsMin: number
endTimeSecondsMax: number
}

export function getEnergyDataStatistics(): EnergyDataStatistics {
const emileDB = sqlite(databasePath, {
readonly: true
})

const statistics = emileDB
.prepare(
`select count(dataId) as dataIdCount,
count(distinct assetId) as assetIdDistinctCount,
count(distinct fileId) as fileIdDistinctCount,
min(timeSeconds) as timeSecondsMin,
max(endTimeSeconds) as endTimeSecondsMax
from EnergyData
where recordDelete_timeMillis is null`
)
.get() as EnergyDataStatistics

emileDB.close()

return statistics
}
5 changes: 4 additions & 1 deletion handlers/dashboard-get/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { getAssetGroups } from '../../database/getAssetGroups.js';
import { getAssets } from '../../database/getAssets.js';
import { getEnergyDataStatistics } from '../../database/getEnergyDataStatistics.js';
import { getAssetCategories } from '../../helpers/functions.cache.js';
export function handler(request, response) {
const assets = getAssets();
const assetGroups = getAssetGroups(request.session.user);
const assetCategories = getAssetCategories();
const energyDataStatistics = getEnergyDataStatistics();
response.render('dashboard', {
headTitle: 'Dashboard',
assets,
assetGroups,
assetCategories
assetCategories,
energyDataStatistics
});
}
export default handler;
6 changes: 5 additions & 1 deletion handlers/dashboard-get/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import type { Request, Response } from 'express'

import { getAssetGroups } from '../../database/getAssetGroups.js'
import { getAssets } from '../../database/getAssets.js'
import { getEnergyDataStatistics } from '../../database/getEnergyDataStatistics.js'
import { getAssetCategories } from '../../helpers/functions.cache.js'

export function handler(request: Request, response: Response): void {
const assets = getAssets()
const assetGroups = getAssetGroups(request.session.user as EmileUser)
const assetCategories = getAssetCategories()

const energyDataStatistics = getEnergyDataStatistics()

response.render('dashboard', {
headTitle: 'Dashboard',
assets,
assetGroups,
assetCategories
assetCategories,
energyDataStatistics
})
}

Expand Down
2 changes: 1 addition & 1 deletion views/_header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<li>
<a id="cityssm-theme--logout-button" href="#">
<span class="icon"><i class="fas fa-sign-out-alt" aria-hidden="true"></i></span>
<span>Log Out</span>
<span>Log Out <%= user.userName %></span>
</a>
</li>
</ul>
Expand Down
11 changes: 11 additions & 0 deletions views/dashboard.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
</div>
</div>
<div class="level-right">
<%
const dateMax = dateTimeFunctions.dateToString(new Date())
const dateMin = (energyDataStatistics.timeSecondsMin ?? 0) === 0
? dateMax
: dateTimeFunctions.dateToString(new Date(energyDataStatistics.timeSecondsMin * 1000))
%>
<div class="level-item">
<div class="field has-addons">
<div class="control">
Expand All @@ -43,6 +50,8 @@
id="dashboard--startDate"
name="startDate"
type="date"
min="<%= dateMin %>"
max="<%= dateMax %>"
required
/>
</div>
Expand All @@ -63,6 +72,8 @@
id="dashboard--endDate"
name="endDate"
type="date"
min="<%= dateMin %>"
max="<%= dateMax %>"
required
/>
</div>
Expand Down

0 comments on commit d0eaeb5

Please sign in to comment.