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                                              17x       17x     20x   17x 5820x 5820x     17x 12100x 12100x     17x 13010x 13010x     17x 276x 276x 10x 10x 10x   276x     17x 185x 185x 185x   185x 3610x 3610x 3610x   185x 185x     17x 44x 11044x     17x 47x 11797x     17x 26x 26x 6526x 26x     17x 159x 159x 39909x 159x 5x 72x 72x             159x 1548x 159x       17x     40x   17x 33x 311x     17x 37x   17x 14x 14x 14x   122x 122x     122x 14x     17x 4x 4x   12x 12x           17x    
/*
 *      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 './Usage';
import { DateTime } from 'luxon';
import { toIPv4 } from '../utils/ConversionUtils';
 
// 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);
        usage.forEach((value) => (value.percent = +((value.total * 100) / total.total).toFixed(1)));
        return { usage, 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;
            })
        );
        usage.forEach((value) => (value.percent = +((value.total * 100) / total.total).toFixed(1)));
        return { usage, 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;