Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. package com.database;
  2.  
  3.  
  4. import org.apache.tomcat.util.codec.binary.Base64;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6.  
  7. import org.springframework.http.HttpEntity;
  8. import org.springframework.http.HttpHeaders;
  9. import org.springframework.http.HttpMethod;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.web.client.RestTemplate;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.core.env.Environment;
  15. import com.database.ldap.LdapEntity;
  16. import com.database.ldap.LdapRepo;
  17. import com.database.ldap.LdapService;
  18.  
  19. import java.io.*;
  20. import java.nio.charset.Charset;
  21. import java.util.*;
  22. import java.text.DateFormat;
  23. import java.text.SimpleDateFormat;
  24.  
  25.  
  26. @Service
  27. public final class PostgresService {
  28. private final DatabaseRepo databaseRepo;
  29. private final LdapRepo ldapRepo;
  30. private final Environment env;
  31. private final RestTemplate restTemplate = new RestTemplate();
  32. private final static String DATA_URL = "http://example.com:8008/pgsql";
  33. private final static String DB_TYPE = "postgres";
  34. private final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  35.  
  36. private static class DatabaseInfo {
  37. public String size_MB;
  38. public String dbname;
  39. public String hostname;
  40. public String description;
  41. public String gtl;
  42. public String team;
  43. public String contact;
  44.  
  45. public String toString() {
  46. return "ID: " + hostname + ":" + dbname;
  47. }
  48. }
  49.  
  50. private static class EntityInfo {
  51. public String TEAM;
  52. public String tablespace_volume;
  53. public String hostname;
  54. public String TECHCONTACT;
  55. public DatabaseInfo databases[];
  56. public String data_allocated;
  57. public String tablespace_allocated;
  58. public String data_volume;
  59. }
  60.  
  61. @Autowired
  62. public PostgresService(DatabaseRepo databaseRepo, LdapRepo ldapRepo, Environment env) {
  63. this.databaseRepo = databaseRepo;
  64. this.ldapRepo = ldapRepo;
  65. this.env = env;
  66. }
  67.  
  68. public void execute() throws IOException {
  69. System.out.println(":::::Fetching remote data::::");
  70. long startTime = System.nanoTime();
  71.  
  72. EntityInfo[] items = fetchRemoteData();
  73.  
  74. long endTime = System.nanoTime();
  75. long duration = (endTime - startTime) / 1000000; // in ms
  76. System.out.println("Finished reading remote data in: " + duration + "ms");
  77. System.out.println("Total Entries: " + items.length);
  78.  
  79. HashMap<String, DatabaseInfo> map = new HashMap<>();
  80. for(EntityInfo entity : items) {
  81. String hostname = entity.hostname;
  82. LdapEntity ldapEntity = ldapRepo.findByUsername(entity.TECHCONTACT.split("@")[0]);
  83. String gtl = ldapEntity.getGtl();
  84. String team = ldapEntity.getTeam();
  85.  
  86. DatabaseInfo[] databases = entity.databases;
  87. if (databases == null) {
  88. continue;
  89. }
  90. for (DatabaseInfo db : databases) {
  91. db.hostname = hostname;
  92. db.contact = entity.TECHCONTACT;
  93. db.team = team;
  94. db.gtl = gtl;
  95. map.put(hostname + ":" + db.dbname, db);
  96. }
  97. }
  98.  
  99. System.out.println(":::::Fetching data from local database::::");
  100. startTime = System.nanoTime();
  101.  
  102. Set<String> destDBSet = fetchFromDatabase();
  103.  
  104. endTime = System.nanoTime();
  105. duration = (endTime - startTime) / 1000000; // in ms
  106. System.out.println("Finished reading data from local DB in: " + duration + "ms");
  107. System.out.println("Total active records: " + destDBSet.size());
  108.  
  109. int totalToBeCreated = 0;
  110. int totalToBeUpdated = 0;
  111. int totalToBeExpired = 0;
  112.  
  113. Set<String> toBeCreated = new HashSet<>();
  114. toBeCreated.addAll(map.keySet());
  115. toBeCreated.removeAll(destDBSet);
  116. totalToBeCreated += toBeCreated.size();
  117.  
  118. Set<String> toBeUpdated = new HashSet<>();
  119. toBeUpdated.addAll(map.keySet());
  120. toBeUpdated.retainAll(destDBSet);
  121. totalToBeUpdated += toBeUpdated.size();
  122.  
  123. Set<String> toBeExpired = new HashSet<>();
  124. toBeExpired.addAll(destDBSet);
  125. toBeExpired.removeAll(map.keySet());
  126. totalToBeExpired += toBeExpired.size();
  127.  
  128. log("To be created: " + totalToBeCreated);
  129. log("To be updated: " + totalToBeUpdated);
  130. log("To be expired: " + totalToBeExpired);
  131.  
  132. System.out.println(":::::Syncing data::::");
  133. startTime = System.nanoTime();
  134.  
  135. for (String uid : toBeCreated) {
  136. createEntry(map.get(uid));
  137. }
  138.  
  139. for (String uid : toBeUpdated) {
  140. updateEntry(map.get(uid));
  141. }
  142.  
  143. for (String uid : toBeExpired) {
  144. DatabaseInfo info = new DatabaseInfo();
  145. info.hostname = uid.split(":")[0];
  146. info.dbname = uid.split(":")[1];
  147. expireEntry(info);
  148. }
  149.  
  150. endTime = System.nanoTime();
  151. duration = (endTime - startTime) / 1000000; // in ms
  152. System.out.println("Finished syncing data in: " + duration + "ms");
  153. }
  154.  
  155. private EntityInfo[] fetchRemoteData() {
  156. HttpHeaders headers = new HttpHeaders();
  157. headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
  158. headers.set("Authorization", getAuthHeader());
  159. HttpEntity httpEntity = new HttpEntity(headers);
  160.  
  161. String URL = env.getProperty("spring.postgres.url");
  162. ResponseEntity<EntityInfo[]> response = restTemplate.exchange(URL, HttpMethod.GET, httpEntity, EntityInfo[].class);
  163. return response.getBody();
  164. }
  165.  
  166. private Set<String> fetchFromDatabase() {
  167. Set<String> result = new HashSet<>();
  168. for (DatabaseEntity db : databaseRepo.findByResourceTypeAndValidTo(DB_TYPE, null)) { // Fetch only latest resources
  169. String hostname = db.getHostName();
  170. String dbname = db.getDbName();
  171. result.add(hostname + ":" + dbname);
  172. }
  173. return result;
  174. }
  175.  
  176. private void log(String message) {
  177. System.out.println(message);
  178. }
  179.  
  180. private void createEntry(DatabaseInfo info) {
  181. log("Creating: " + info.toString());
  182. DatabaseEntity databaseEntity = new DatabaseEntity(
  183. info.dbname,
  184. info.team,
  185. "", // usage
  186. info.gtl, // gtl
  187. info.size_MB,
  188. info.hostname,
  189. "", // alias
  190. "", // license
  191. "", // dbType
  192. DB_TYPE,
  193. "", // version
  194. "", // const center
  195. info.contact, // contact
  196. UUID.randomUUID(), // uid
  197. "", // service name
  198. null); // valid to
  199. databaseRepo.save(databaseEntity);
  200. }
  201.  
  202. private void updateEntry(DatabaseInfo info) {
  203. DatabaseEntity entity = databaseRepo.findByDbNameAndHostNameAndValidTo(info.dbname, info.hostname, null);
  204. if (entity != null) {
  205. log("Updating: " + info.toString());
  206. entity.setValidTo(dateFormat.format(new Date()));
  207. databaseRepo.save(entity);
  208. createEntry(info);
  209. }
  210. }
  211.  
  212. private void expireEntry(DatabaseInfo info) {
  213. DatabaseEntity entity = databaseRepo.findByDbNameAndHostNameAndValidTo(info.dbname, info.hostname, null);
  214. log(entity.getDbName());
  215. if (entity != null) {
  216. log("Expiring: " + info.toString());
  217. entity.setValidTo(dateFormat.format(new Date()));
  218. databaseRepo.save(entity);
  219. }
  220. }
  221.  
  222. private String getAuthHeader() {
  223. String DB_USER = env.getProperty("spring.postgres.username");
  224. String DB_PASS = env.getProperty("spring.postgres.password");
  225. String auth = DB_USER + ":" + DB_PASS;
  226. byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
  227. String authHeader = "Basic " + new String(encodedAuth);
  228. return authHeader;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement