All files / src/services API.js

100% Statements 82/82
100% Branches 17/17
100% Functions 28/28
100% Lines 72/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                              23x       23x     23x   23x 5820x 5820x     23x 12100x 12100x     23x 13010x 13010x     23x 296x 296x 10x 10x 10x   296x     23x 200x 200x 200x   200x 3555x 3555x 3555x   200x 200x     23x 46x 11546x     23x 50x 12550x     23x 25x 25x 6275x 25x     23x 175x 175x 43925x 175x 5x 72x 72x             175x 1652x       175x       23x     43x   23x 34x 322x     23x 39x   23x 16x 16x 16x   136x 136x     136x       16x     23x 3x 3x   9x 9x           23x    
/*
 *      Copyright (C) 2010 - 2025 VREM Software Development <VREMSoftwareDevelopment@gmail.com>
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *           http: //www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *
 * Bandwidth Monitor
 */
 
import usage from '@services/Usage';
import { DateTime } from 'luxon';
import { toIPv4 } from '@utils';
 
// reload every 10 minutes
const RELOAD_TIME = 10;
 
class Store {
    constructor() {
        this.last = DateTime.local();
    }
 
    orderById = (a, b) => b.id - a.id;
 
    orderByIP = (a, b) => {
        const result = a.IP - b.IP;
        return result === 0 ? this.orderById(a, b) : result;
    };
 
    orderByMonth = (a, b) => {
        const result = b.month - a.month;
        return result === 0 ? this.orderByIP(a, b) : result;
    };
 
    orderBy = (a, b) => {
        const result = b.year - a.year;
        return result === 0 ? this.orderByMonth(a, b) : result;
    };
 
    data = async () => {
        const current = DateTime.local();
        if (!this.cached || current.diff(this.last, ['minutes']).minutes >= RELOAD_TIME) {
            const data = await usage.request(import.meta.env.BASE_URL);
            this.cached = data.sort(this.orderBy);
            this.last = current;
        }
        return this.cached;
    };
 
    sum = (entries, days, id) => {
        let download = 0,
            upload = 0,
            total = 0;
 
        entries.forEach((entry) => {
            download += entry.download;
            upload += entry.upload;
            total += entry.total;
        });
        const average = +(total / days).toFixed(3);
        return { id, download, upload, total, average, days };
    };
 
    getYears = async () => {
        const result = await this.data();
        return [...new Set(result.map((element) => element.year))];
    };
 
    getMonths = async (year) => {
        const result = await this.data();
        return [...new Set(result.filter((element) => element.year === year).map((element) => element.month))];
    };
 
    getUsageByYear = async (year) => {
        const result = await this.data();
        const days = DateTime.local(year, 12, 31).daysInYear;
        const usage = result.filter((element) => element.year === year);
        return this.sum(usage, days, year);
    };
 
    getUsageByUser = async (year, month, filter) => {
        const result = await this.data();
        const days = DateTime.local(year, month).daysInMonth;
        let usage = result.filter((element) => element.year === year && element.month === month);
        if (filter) {
            usage = usage.filter((entry) => {
                let filterLowerCase = filter.toLowerCase();
                return (
                    toIPv4(entry.IP).toLowerCase().indexOf(filterLowerCase) !== -1 ||
                    entry.MAC.toLowerCase().indexOf(filterLowerCase) !== -1 ||
                    entry.user.toLowerCase().indexOf(filterLowerCase) !== -1
                );
            });
        }
        const total = this.sum(usage, days, month);
        const usageWithPercent = usage.map((value) => ({
            ...value,
            percent: +((value.total * 100) / total.total).toFixed(1),
        }));
        return { usage: usageWithPercent, total };
    };
}
 
const store = new Store();
 
class Service {
    getYears = async () => await store.getYears();
 
    getMonths = async (year) => {
        const months = await store.getMonths(year);
        return months.map((month) => DateTime.local(year, month).toFormat('MMMM'));
    };
 
    getUsageByUser = async (year, month, filter) =>
        await store.getUsageByUser(year, DateTime.fromFormat(year + '-' + month, 'yyyy-MMMM').month, filter);
 
    getUsageByMonth = async (year) => {
        const months = await store.getMonths(year);
        const total = await store.getUsageByYear(year);
        let usage = await Promise.all(
            months.map(async (entry) => {
                const result = await store.getUsageByUser(year, entry);
                return result.total;
            })
        );
        const usageWithPercent = usage.map((value) => ({
            ...value,
            percent: +((value.total * 100) / total.total).toFixed(1),
        }));
        return { usage: usageWithPercent, total };
    };
 
    getUsageByYear = async () => {
        const years = await store.getYears();
        return await Promise.all(
            years.map(async (entry) => {
                const result = await store.getUsageByYear(entry);
                return result;
            })
        );
    };
}
 
const API = new Service();
export default API;