Commit c5b76f65 authored by ReemyHasan's avatar ReemyHasan

Add statistics functions

parent 4357b7ba
......@@ -11,6 +11,9 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -33,4 +36,75 @@ public class RethinkController {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping("/severity-statistics")
public ResponseEntity<Map<String, Map<String, Integer>>> getSeverityStatistics() {
List<Map<String, Object>> rawResults = rethinkDBService.getData();
if (rawResults != null) {
Map<String, Map<String, Integer>> processedData = processRawResults(rawResults);
return ResponseEntity.ok(processedData);
} else {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
private Map<String, Map<String, Integer>> processRawResults(List<Map<String, Object>> rawResults) {
Map<String, Map<String, Integer>> processedData = new HashMap<>();
for (Map<String, Object> rawEntry : rawResults) {
String severityLevel = rawEntry.get("severity").toString();
long timestamp = (long) rawEntry.get("timestamp"); // Assuming timestamp is a long value representing Unix timestamp in milliseconds
// Convert Unix timestamp to a Date and extract hour
String hour = convertTimestampToHour(timestamp);
processedData.putIfAbsent(hour, new HashMap<>());
Map<String, Integer> hourMap = processedData.get(hour);
hourMap.put(severityLevel, hourMap.getOrDefault(severityLevel, 0) + 1);
}
return processedData;
}
private String convertTimestampToHour(long timestamp) {
Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // Customize the format as needed
return sdf.format(date);
}
@GetMapping("/getTrapCount")
public long getTrapCount() {
try {
return rethinkDBService.getTrapCount();
}catch (Exception e){
return 0;
}
}
@GetMapping("/getErrorTrapCount")
public long getErrorTrapCount() {
try {
return rethinkDBService.getErrorTrapCount();
}catch (Exception e){
return 0;
}
}
@GetMapping("/getWarningTrapCount")
public long getWarningTrapCount() {
try {
return rethinkDBService.getWarningTrapCount();
}catch (Exception e){
return 0;
}
}
@GetMapping("/getInfoTrapCount")
public long getInfoTrapCount() {
try {
return rethinkDBService.getInfoTrapCount();
}catch (Exception e){
return 0;
}
}
}
......@@ -54,16 +54,15 @@ public class RethinkDBConnectionFactory {
if (!tables.contains(dbTableName)) {
System.out.println("Creating Table");
r.db(dbName).tableCreate(dbTableName).run(connection);
//r.db(dbName).table(dbTableName).indexCreate("timestamp").run(connection);
//r.db("my_database").table("my_table").indexCreate("trap").run(connection);
}
if (!dbList.contains(dbAbout)) {
System.out.println("Creating DATABASE");
r.dbCreate(dbAbout).run(connection);
}
List<String> aboutTables = r.db(dbAbout).tableList().run(connection);
if (!tables.contains(dbTableAbout)) {
System.out.println("Creating Table");
if (!aboutTables.contains(dbTableAbout)) {
System.out.println("Creating About Table");
r.db(dbAbout).tableCreate(dbTableAbout).run(connection);
}
} catch (Exception e) {
......
......@@ -106,5 +106,56 @@ public class RethinkDBService{
return null;
}
}
public long getTrapCount() {
try {
long count = r.db(connectionFactory.getDbName())
.table(connectionFactory.getDbTableName())
.count()
.run(connectionFactory.getConnection());
return count;
} catch (Exception e) {
log.error("Error getting trap count from RethinkDB", e);
return 0;
}
}
public long getErrorTrapCount() {
try {
long count = r.db(connectionFactory.getDbName())
.table(connectionFactory.getDbTableName())
.filter(r.hashMap("severity", "ERROR"))
.count()
.run(connectionFactory.getConnection());
return count;
} catch (Exception e) {
log.error("Error getting error trap count from RethinkDB", e);
return 0;
}
}
public long getWarningTrapCount() {
try {
long count = r.db(connectionFactory.getDbName())
.table(connectionFactory.getDbTableName())
.filter(r.hashMap("severity", "WARNING"))
.count()
.run(connectionFactory.getConnection());
return count;
} catch (Exception e) {
log.error("Error getting warning trap count from RethinkDB", e);
return 0;
}
}
public long getInfoTrapCount() {
try {
long count = r.db(connectionFactory.getDbName())
.table(connectionFactory.getDbTableName())
.filter(r.hashMap("severity", "INFO"))
.count()
.run(connectionFactory.getConnection());
return count;
} catch (Exception e) {
log.error("Error getting info trap count from RethinkDB", e);
return 0;
}
}
}
......@@ -60,5 +60,23 @@ public class UserController {
return false;
}
}
@GetMapping("/getAdminCount")
public long getAdminsCount() {
try {
return userService.getAdminsCount();
}catch (Exception e){
return 0;
}
}
@GetMapping("/getUsersCount")
public long getUsersCount() {
try {
return userService.getUsersCount();
}catch (Exception e){
return 0;
}
}
}
package com.example.userService.repository;
import com.example.userService.entity.UserCredential;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
......@@ -9,4 +10,9 @@ import java.util.Optional;
@Repository
public interface UserCredentialRepository extends CrudRepository<UserCredential,Integer> {
Optional<UserCredential> findByUsername(String username);
@Query("SELECT COUNT(u) FROM UserCredential u WHERE u.role = 'admin'")
long countAdmins();
@Query("SELECT COUNT(u) FROM UserCredential u WHERE u.role = 'user'")
long countUsers();
}
......@@ -69,4 +69,11 @@ public class UserService {
return false;
}
}
public long getAdminsCount() {
return repository.countAdmins();
}
public long getUsersCount() {
return repository.countUsers();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment