Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/middleware/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export function cacheFor(ttlMs: number) {
const key = req.originalUrl;
const cached = store.get(key);
if (cached && Date.now() < cached.expiresAt) {
res.setHeader('Cache-Control', `public, max-age=${Math.floor(ttlMs / 1000)}`);
return res.json(cached.data);
}
const original = res.json.bind(res);
res.json = (data: unknown) => {
if (store.size > 500) store.clear(); // bound memory
store.set(key, { data, expiresAt: Date.now() + ttlMs });
res.setHeader('Cache-Control', `public, max-age=${Math.floor(ttlMs / 1000)}`);
return original(data);
};
next();
Expand Down
1 change: 1 addition & 0 deletions backend/src/routes/meters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function createMeterRouter(stellar: StellarService) {
/** GET /api/meters/export?format=csv|json — download all meter data */
meterRouter.get(
"/export",
requireAdminKey,
asyncHandler(async (req, res) => {
const format = req.query.format === "json" ? "json" : "csv";
const result = await stellar.query("get_all_meters", []);
Expand Down
6 changes: 5 additions & 1 deletion backend/src/routes/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ async function fetchPaymentEvents(
const EVT_NS = StellarSdk.xdr.ScVal.scvSymbol("solargrid").toXDR("base64");
const ACTION = StellarSdk.xdr.ScVal.scvSymbol("payment").toXDR("base64");

const latestLedgerResp = await (server as any).getLatestLedger();
const latestLedger = latestLedgerResp.sequence;
const startLedger = Math.max(1, latestLedger - Math.floor((days * 24 * 60 * 60) / 6));

const response = await (server as any).getEvents({
startLedger: 1,
startLedger,
filters: [
{
type: "contract",
Expand Down
29 changes: 19 additions & 10 deletions frontend/src/app/dashboard/provider/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useState, useEffect, useCallback, useRef } from "react";
import { useSearchParams, useRouter } from "next/navigation";
import Navbar from "@/components/Navbar";
import { Skeleton } from "@/components/Skeleton";
import { SkeletonCard } from "@/components/SkeletonCard";
import { useToast } from "@/components/ToastProvider";
import { getAllMeters, type MeterData } from "@/services/meterService";
import { parseWalletError } from "@/lib/errors";
Expand Down Expand Up @@ -32,6 +32,7 @@ export default function ProviderDashboardPage() {

const [meters, setMeters] = useState<MeterData[]>([]);
const [fetching, setFetching] = useState(false);
const [fetchError, setFetchError] = useState<string | null>(null);
const searchInputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
Expand Down Expand Up @@ -66,11 +67,13 @@ export default function ProviderDashboardPage() {

const fetchMeters = useCallback(async () => {
setFetching(true);
setFetchError(null);
try {
const allMeters = await getAllMeters();
setMeters(allMeters);
} catch (err: unknown) {
console.error("Failed to fetch meters:", err);
setFetchError(err instanceof Error ? err.message : "Failed to fetch meters");
} finally {
setFetching(false);
}
Expand Down Expand Up @@ -314,30 +317,36 @@ export default function ProviderDashboardPage() {
</tr>
</thead>
<tbody className="divide-y divide-white/5">
{fetching && meters.length === 0 ? (
{fetchError ? (
<tr>
<td colSpan={7} className="px-6 py-12 text-center text-red-500">
{fetchError}
</td>
</tr>
) : fetching && meters.length === 0 ? (
<>
{[1, 2, 3].map((i) => (
{[1, 2, 3, 4, 5].map((i) => (
<tr key={i}>
<td className="px-6 py-4">
<Skeleton width="100px" height={14} />
<SkeletonCard height={14} />
</td>
<td className="px-6 py-4">
<Skeleton width="140px" height={14} />
<SkeletonCard height={14} />
</td>
<td className="px-6 py-4">
<Skeleton width="60px" height={18} />
<SkeletonCard height={18} />
</td>
<td className="px-6 py-4">
<Skeleton width="70px" height={14} />
<SkeletonCard height={14} />
</td>
<td className="px-6 py-4">
<Skeleton width="50px" height={14} />
<SkeletonCard height={14} />
</td>
<td className="px-6 py-4">
<Skeleton width="80px" height={14} />
<SkeletonCard height={14} />
</td>
<td className="px-6 py-4">
<Skeleton width="80px" height={28} />
<SkeletonCard height={28} />
</td>
</tr>
))}
Expand Down