| | | 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 | | |
| | 0 | 84 | | bool add_machine(const char *name, const char *model, const char *location, const char *status) { |
| | 0 | 85 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 86 | | |
| | 0 | 87 | | if (!conn_wrapper) { |
| | 0 | 88 | | printf("[DATABASE ERROR] Could not acquire connection to add machine!\n"); |
| | 0 | 89 | | return false; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | char query[1024]; |
| | 0 | 93 | | snprintf(query, sizeof(query), |
| | | 94 | | "INSERT INTO machines (name, model, location, status) VALUES ('%s', '%s', '%s', '%s');", |
| | | 95 | | name, model, location, status); |
| | 0 | 96 | | printf("[DATABASE DEBUG] Executing: %s\n", query); |
| | 0 | 97 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | 0 | 98 | | bool success = (PQresultStatus(res) == PGRES_COMMAND_OK); |
| | | 99 | | |
| | 0 | 100 | | if (!success) { |
| | 0 | 101 | | printf("[DATABASE ERROR] INSERT failed: %s\n", PQerrorMessage(conn_wrapper->pg_conn)); |
| | | 102 | | } else { |
| | 0 | 103 | | printf("[DATABASE SUCCESS] Machine '%s' added to DB.\n", name); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | PQclear(res); |
| | 0 | 107 | | db_pool_release(conn_wrapper); |
| | 0 | 108 | | return success; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | #endif // TEST_MODE |
| | | 112 | | |
| | | 113 | | |
| | | 114 | | |
| | | 115 | | #ifdef TEST_MODE |
| | | 116 | | |
| | | 117 | | // ===================================== |
| | | 118 | | // MOCK IMPLEMENTATION (COVERAGE BOOST) |
| | | 119 | | // ===================================== |
| | | 120 | | |
| | 0 | 121 | | int get_all_machines(Machine *machines, int max_count) { |
| | 0 | 122 | | if (!machines) |
| | 0 | 123 | | return 0; |
| | | 124 | | |
| | 0 | 125 | | if (max_count <= 0) |
| | 0 | 126 | | return 0; |
| | | 127 | | |
| | 0 | 128 | | machines[0].id = 1; |
| | 0 | 129 | | strcpy(machines[0].name, "CNC-01"); |
| | 0 | 130 | | strcpy(machines[0].model, "X100"); |
| | 0 | 131 | | strcpy(machines[0].location, "Line A"); |
| | 0 | 132 | | strcpy(machines[0].status, "active"); |
| | 0 | 133 | | machines[0].health_score = 88.5; |
| | 0 | 134 | | return 1; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | int get_machine_by_id(int id, Machine *machine) { |
| | 0 | 138 | | if (!machine) |
| | 0 | 139 | | return 0; |
| | | 140 | | |
| | 0 | 141 | | if (id <= 0) |
| | 0 | 142 | | return 0; |
| | | 143 | | |
| | 0 | 144 | | machine->id = id; |
| | 0 | 145 | | strcpy(machine->name, "Mock Machine"); |
| | 0 | 146 | | strcpy(machine->model, "Mock Model"); |
| | 0 | 147 | | strcpy(machine->location, "Test Lab"); |
| | 0 | 148 | | strcpy(machine->status, "active"); |
| | 0 | 149 | | machine->health_score = 90.0; |
| | 0 | 150 | | return 1; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | double get_machine_health_score(int id) { |
| | 0 | 154 | | if (id <= 0) |
| | 0 | 155 | | return 0.0; |
| | | 156 | | |
| | 0 | 157 | | return 88.5; |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | bool add_machine(const char *name, const char *model, const char *location, const char *status) { |
| | | 161 | | (void)name; |
| | | 162 | | (void)model; |
| | | 163 | | (void)location; |
| | | 164 | | (void)status; |
| | 0 | 165 | | return true; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | #endif |