LCOV - code coverage report
Current view: top level - lib/src/repository - vendor_repository_impl.dart (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 43 43
Test Date: 2025-08-17 13:02:53 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:async';
       2              : 
       3              : import 'package:drift/drift.dart';
       4              : import 'package:injectable/injectable.dart';
       5              : import 'package:logging/logging.dart';
       6              : import 'package:network_tools/network_tools.dart';
       7              : import 'package:network_tools/src/database/database_service.dart';
       8              : import 'package:network_tools/src/database/drift_database.dart';
       9              : import 'package:network_tools/src/device_info/vendor_table.dart';
      10              : import 'package:network_tools/src/repository/repository.dart';
      11              : 
      12              : @Injectable(as: Repository<Vendor>)
      13              : class VendorRepository implements Repository<Vendor> {
      14            2 :   VendorRepository(this._database);
      15            6 :   static final vendorDriftLogger = Logger("vendor-drift-logger");
      16              :   final DatabaseService<AppDatabase> _database;
      17              : 
      18            2 :   @override
      19              :   Future<void> build() async {
      20            4 :     final database = await _database.open();
      21            2 :     final oldEntries = await this.entries();
      22            2 :     if (oldEntries.isNotEmpty) {
      23            4 :       vendorDriftLogger.fine(
      24              :         "Skipping Mac Vendor table build, old entries found",
      25              :       );
      26              :       return;
      27              :     }
      28            4 :     vendorDriftLogger.fine("Building Vendor table...");
      29            2 :     final entries = (await VendorTable.fetchVendorTable())
      30            2 :         .map(
      31            4 :           (e) => VendorDriftCompanion.insert(
      32            2 :             macPrefix: e.macPrefix,
      33            2 :             vendorName: e.vendorName,
      34            2 :             private: e.private,
      35            2 :             blockType: e.blockType,
      36            2 :             lastUpdate: e.lastUpdate,
      37              :           ),
      38              :         )
      39            2 :         .toList();
      40            2 :     if (entries.isNotEmpty) {
      41            4 :       await database!.batch((batch) {
      42            4 :         batch.insertAll(database.vendorDrift, entries);
      43              :       });
      44            4 :       vendorDriftLogger.fine(
      45            4 :         "Mac Vendor table built with ${entries.length} entries",
      46              :       );
      47              :     }
      48              :   }
      49              : 
      50            1 :   @override
      51              :   Future<void> close() async {
      52            2 :     final database = await _database.open();
      53            1 :     database!.close();
      54              :   }
      55              : 
      56            2 :   @override
      57              :   Future<List<String>> entries() async {
      58            4 :     final database = await _database.open();
      59            2 :     final records = await (database!.select(
      60            2 :       database.vendorDrift,
      61           12 :     )..orderBy([(t) => OrderingTerm(expression: t.id)])).get();
      62              : 
      63            8 :     return records.map((e) => e.macPrefix).toList();
      64              :   }
      65              : 
      66            1 :   @override
      67              :   Future<Vendor?> entryFor(String address) async {
      68            2 :     final database = await _database.open();
      69              :     final records =
      70            3 :         await (database!.select(database.vendorDrift)..where(
      71            4 :               (t) => t.macPrefix.equals(VendorTable.noColonString(address)),
      72              :             ))
      73            1 :             .get();
      74            1 :     if (records.isNotEmpty) {
      75            3 :       vendorDriftLogger.fine("Found Vendor entry for address: $address");
      76            2 :       return Vendor.fromDriftData(records.first);
      77              :     }
      78            3 :     vendorDriftLogger.fine("No Vendor entry found for address: $address");
      79              :     return null;
      80              :   }
      81              : 
      82            1 :   @override
      83              :   Future<bool> clear() async {
      84            2 :     final database = await _database.open();
      85            3 :     await database!.delete(database.vendorDrift).go();
      86              :     return true;
      87              :   }
      88              : }
        

Generated by: LCOV version 2.0-1