Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.database;
- import org.apache.tomcat.util.codec.binary.Base64;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpMethod;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- import org.springframework.http.MediaType;
- import org.springframework.core.env.Environment;
- import com.database.ldap.LdapEntity;
- import com.database.ldap.LdapRepo;
- import com.database.ldap.LdapService;
- import java.io.*;
- import java.nio.charset.Charset;
- import java.util.*;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- @Service
- public final class PostgresService {
- private final DatabaseRepo databaseRepo;
- private final LdapRepo ldapRepo;
- private final Environment env;
- private final RestTemplate restTemplate = new RestTemplate();
- private final static String DATA_URL = "http://example.com:8008/pgsql";
- private final static String DB_TYPE = "postgres";
- private final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- private static class DatabaseInfo {
- public String size_MB;
- public String dbname;
- public String hostname;
- public String description;
- public String gtl;
- public String team;
- public String contact;
- public String toString() {
- return "ID: " + hostname + ":" + dbname;
- }
- }
- private static class EntityInfo {
- public String TEAM;
- public String tablespace_volume;
- public String hostname;
- public String TECHCONTACT;
- public DatabaseInfo databases[];
- public String data_allocated;
- public String tablespace_allocated;
- public String data_volume;
- }
- @Autowired
- public PostgresService(DatabaseRepo databaseRepo, LdapRepo ldapRepo, Environment env) {
- this.databaseRepo = databaseRepo;
- this.ldapRepo = ldapRepo;
- this.env = env;
- }
- public void execute() throws IOException {
- System.out.println(":::::Fetching remote data::::");
- long startTime = System.nanoTime();
- EntityInfo[] items = fetchRemoteData();
- long endTime = System.nanoTime();
- long duration = (endTime - startTime) / 1000000; // in ms
- System.out.println("Finished reading remote data in: " + duration + "ms");
- System.out.println("Total Entries: " + items.length);
- HashMap<String, DatabaseInfo> map = new HashMap<>();
- for(EntityInfo entity : items) {
- String hostname = entity.hostname;
- LdapEntity ldapEntity = ldapRepo.findByUsername(entity.TECHCONTACT.split("@")[0]);
- String gtl = ldapEntity.getGtl();
- String team = ldapEntity.getTeam();
- DatabaseInfo[] databases = entity.databases;
- if (databases == null) {
- continue;
- }
- for (DatabaseInfo db : databases) {
- db.hostname = hostname;
- db.contact = entity.TECHCONTACT;
- db.team = team;
- db.gtl = gtl;
- map.put(hostname + ":" + db.dbname, db);
- }
- }
- System.out.println(":::::Fetching data from local database::::");
- startTime = System.nanoTime();
- Set<String> destDBSet = fetchFromDatabase();
- endTime = System.nanoTime();
- duration = (endTime - startTime) / 1000000; // in ms
- System.out.println("Finished reading data from local DB in: " + duration + "ms");
- System.out.println("Total active records: " + destDBSet.size());
- int totalToBeCreated = 0;
- int totalToBeUpdated = 0;
- int totalToBeExpired = 0;
- Set<String> toBeCreated = new HashSet<>();
- toBeCreated.addAll(map.keySet());
- toBeCreated.removeAll(destDBSet);
- totalToBeCreated += toBeCreated.size();
- Set<String> toBeUpdated = new HashSet<>();
- toBeUpdated.addAll(map.keySet());
- toBeUpdated.retainAll(destDBSet);
- totalToBeUpdated += toBeUpdated.size();
- Set<String> toBeExpired = new HashSet<>();
- toBeExpired.addAll(destDBSet);
- toBeExpired.removeAll(map.keySet());
- totalToBeExpired += toBeExpired.size();
- log("To be created: " + totalToBeCreated);
- log("To be updated: " + totalToBeUpdated);
- log("To be expired: " + totalToBeExpired);
- System.out.println(":::::Syncing data::::");
- startTime = System.nanoTime();
- for (String uid : toBeCreated) {
- createEntry(map.get(uid));
- }
- for (String uid : toBeUpdated) {
- updateEntry(map.get(uid));
- }
- for (String uid : toBeExpired) {
- DatabaseInfo info = new DatabaseInfo();
- info.hostname = uid.split(":")[0];
- info.dbname = uid.split(":")[1];
- expireEntry(info);
- }
- endTime = System.nanoTime();
- duration = (endTime - startTime) / 1000000; // in ms
- System.out.println("Finished syncing data in: " + duration + "ms");
- }
- private EntityInfo[] fetchRemoteData() {
- HttpHeaders headers = new HttpHeaders();
- headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
- headers.set("Authorization", getAuthHeader());
- HttpEntity httpEntity = new HttpEntity(headers);
- String URL = env.getProperty("spring.postgres.url");
- ResponseEntity<EntityInfo[]> response = restTemplate.exchange(URL, HttpMethod.GET, httpEntity, EntityInfo[].class);
- return response.getBody();
- }
- private Set<String> fetchFromDatabase() {
- Set<String> result = new HashSet<>();
- for (DatabaseEntity db : databaseRepo.findByResourceTypeAndValidTo(DB_TYPE, null)) { // Fetch only latest resources
- String hostname = db.getHostName();
- String dbname = db.getDbName();
- result.add(hostname + ":" + dbname);
- }
- return result;
- }
- private void log(String message) {
- System.out.println(message);
- }
- private void createEntry(DatabaseInfo info) {
- log("Creating: " + info.toString());
- DatabaseEntity databaseEntity = new DatabaseEntity(
- info.dbname,
- info.team,
- "", // usage
- info.gtl, // gtl
- info.size_MB,
- info.hostname,
- "", // alias
- "", // license
- "", // dbType
- DB_TYPE,
- "", // version
- "", // const center
- info.contact, // contact
- UUID.randomUUID(), // uid
- "", // service name
- null); // valid to
- databaseRepo.save(databaseEntity);
- }
- private void updateEntry(DatabaseInfo info) {
- DatabaseEntity entity = databaseRepo.findByDbNameAndHostNameAndValidTo(info.dbname, info.hostname, null);
- if (entity != null) {
- log("Updating: " + info.toString());
- entity.setValidTo(dateFormat.format(new Date()));
- databaseRepo.save(entity);
- createEntry(info);
- }
- }
- private void expireEntry(DatabaseInfo info) {
- DatabaseEntity entity = databaseRepo.findByDbNameAndHostNameAndValidTo(info.dbname, info.hostname, null);
- log(entity.getDbName());
- if (entity != null) {
- log("Expiring: " + info.toString());
- entity.setValidTo(dateFormat.format(new Date()));
- databaseRepo.save(entity);
- }
- }
- private String getAuthHeader() {
- String DB_USER = env.getProperty("spring.postgres.username");
- String DB_PASS = env.getProperty("spring.postgres.password");
- String auth = DB_USER + ":" + DB_PASS;
- byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
- String authHeader = "Basic " + new String(encodedAuth);
- return authHeader;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement