| | | 1 | | #include "machine_service.h" |
| | | 2 | | #include "db_connection.h" |
| | | 3 | | #include "logger.h" |
| | | 4 | | #include <string.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | #include <stdio.h> |
| | | 7 | | |
| | | 8 | | #ifndef TEST_MODE |
| | | 9 | | |
| | | 10 | | // ==================================================================== |
| | | 11 | | // GERÇEK IMPLEMENTASYON (PostgreSQL) |
| | | 12 | | // ==================================================================== |
| | | 13 | | |
| | 6 | 14 | | int get_all_machines(Machine *machines, int max_count) { |
| | 6 | 15 | | if (!machines || max_count <= 0) |
| | 2 | 16 | | return 0; |
| | | 17 | | |
| | 4 | 18 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 19 | | |
| | 4 | 20 | | if (!conn_wrapper) { |
| | 1 | 21 | | LOG_ERROR("Could not acquire connection to fetch machines."); |
| | 1 | 22 | | return 0; |
| | | 23 | | } |
| | | 24 | | |
| | 3 | 25 | | PGresult *res = PQexec( |
| | | 26 | | conn_wrapper->pg_conn, |
| | | 27 | | "SELECT id, name, model, location, status FROM machines"); |
| | | 28 | | |
| | 3 | 29 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 1 | 30 | | LOG_ERROR("SELECT machines failed: %s", |
| | | 31 | | PQerrorMessage(conn_wrapper->pg_conn)); |
| | 1 | 32 | | PQclear(res); |
| | 1 | 33 | | db_pool_release(conn_wrapper); |
| | 1 | 34 | | return 0; |
| | | 35 | | } |
| | | 36 | | |
| | 2 | 37 | | int rows = PQntuples(res); |
| | 2 | 38 | | int count = (rows < max_count) ? rows : max_count; |
| | | 39 | | |
| | 8 | 40 | | for (int i = 0; i < count; i++) { |
| | 6 | 41 | | machines[i].id = atoi(PQgetvalue(res, i, 0)); |
| | 6 | 42 | | strncpy(machines[i].name, PQgetvalue(res, i, 1), 99); |
| | 6 | 43 | | machines[i].name[99] = '\0'; |
| | 6 | 44 | | strncpy(machines[i].model, PQgetvalue(res, i, 2), 49); |
| | 6 | 45 | | machines[i].model[49] = '\0'; |
| | 6 | 46 | | strncpy(machines[i].location, PQgetvalue(res, i, 3), 99); |
| | 6 | 47 | | machines[i].location[99] = '\0'; |
| | 6 | 48 | | strncpy(machines[i].status, PQgetvalue(res, i, 4), 19); |
| | 6 | 49 | | machines[i].status[19] = '\0'; |
| | 6 | 50 | | machines[i].health_score = |
| | 6 | 51 | | get_machine_health_score(machines[i].id); |
| | | 52 | | } |
| | | 53 | | |
| | 2 | 54 | | PQclear(res); |
| | 2 | 55 | | db_pool_release(conn_wrapper); |
| | 2 | 56 | | LOG_INFO("Successfully fetched %d machines from DB.", count); |
| | 2 | 57 | | return count; |
| | | 58 | | } |
| | | 59 | | |
| | 2 | 60 | | int get_machine_by_id(int id, Machine *machine) { |
| | 2 | 61 | | if (!machine) |
| | 1 | 62 | | return -1; |
| | | 63 | | |
| | 1 | 64 | | machine->id = id; |
| | 1 | 65 | | snprintf(machine->name, sizeof(machine->name), |
| | | 66 | | "Machine %d", id); |
| | 1 | 67 | | strncpy(machine->model, "Standard Model", sizeof(machine->model) - 1); |
| | 1 | 68 | | machine->model[sizeof(machine->model) - 1] = '\0'; |
| | 1 | 69 | | strncpy(machine->location, "Production Line", |
| | | 70 | | sizeof(machine->location) - 1); |
| | 1 | 71 | | machine->location[sizeof(machine->location) - 1] = '\0'; |
| | 1 | 72 | | strncpy(machine->status, "running", |
| | | 73 | | sizeof(machine->status) - 1); |
| | 1 | 74 | | machine->status[sizeof(machine->status) - 1] = '\0'; |
| | 1 | 75 | | machine->health_score = 90.0; |
| | 1 | 76 | | return 0; |
| | | 77 | | } |
| | | 78 | | |
| | 7 | 79 | | double get_machine_health_score(int machine_id) { |
| | | 80 | | (void)machine_id; |
| | 7 | 81 | | return 94.7; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | #endif // TEST_MODE |
| | | 85 | | |
| | | 86 | | |
| | | 87 | | |
| | | 88 | | #ifdef TEST_MODE |
| | | 89 | | |
| | | 90 | | // ===================================== |
| | | 91 | | // MOCK IMPLEMENTATION (COVERAGE BOOST) |
| | | 92 | | // ===================================== |
| | | 93 | | |
| | | 94 | | int get_all_machines(Machine *machines, int max_count) { |
| | | 95 | | if (!machines) |
| | | 96 | | return 0; |
| | | 97 | | |
| | | 98 | | if (max_count <= 0) |
| | | 99 | | return 0; |
| | | 100 | | |
| | | 101 | | machines[0].id = 1; |
| | | 102 | | strcpy(machines[0].name, "CNC-01"); |
| | | 103 | | strcpy(machines[0].model, "X100"); |
| | | 104 | | strcpy(machines[0].location, "Line A"); |
| | | 105 | | strcpy(machines[0].status, "active"); |
| | | 106 | | machines[0].health_score = 88.5; |
| | | 107 | | return 1; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | int get_machine_by_id(int id, Machine *machine) { |
| | | 111 | | if (!machine) |
| | | 112 | | return 0; |
| | | 113 | | |
| | | 114 | | if (id <= 0) |
| | | 115 | | return 0; |
| | | 116 | | |
| | | 117 | | machine->id = id; |
| | | 118 | | strcpy(machine->name, "Mock Machine"); |
| | | 119 | | strcpy(machine->model, "Mock Model"); |
| | | 120 | | strcpy(machine->location, "Test Lab"); |
| | | 121 | | strcpy(machine->status, "active"); |
| | | 122 | | machine->health_score = 90.0; |
| | | 123 | | return 1; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | double get_machine_health_score(int id) { |
| | | 127 | | if (id <= 0) |
| | | 128 | | return 0.0; |
| | | 129 | | |
| | | 130 | | return 88.5; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | #endif |