Skip to content

Commit

Permalink
fix: some progress towards something working
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Feb 16, 2024
1 parent 77f127e commit cc77ab9
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 46 deletions.
110 changes: 110 additions & 0 deletions gmcserver-server/migrations/202402162056.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
-- DROP SCHEMA public;

CREATE SCHEMA public AUTHORIZATION postgres;

COMMENT ON SCHEMA public IS 'standard public schema';
-- public.users definition

-- Drop table

-- DROP TABLE public.users;

CREATE TABLE public.users (
id uuid NOT NULL DEFAULT gen_random_uuid(),
username varchar NOT NULL,
"password" varchar NULL,
email varchar NOT NULL,
"deviceLimit" int4 NOT NULL,
"gmcId" int8 NOT NULL,
"admin" bool NOT NULL,
mfa bool NOT NULL,
"alertEmails" bool NOT NULL,
"mfaKey" jsonb NULL,
CONSTRAINT users_pk PRIMARY KEY (id)
);
CREATE INDEX users_email_idx ON public.users USING btree (email);
CREATE UNIQUE INDEX users_gmcid_idx ON public.users USING btree ("gmcId");
CREATE INDEX users_username_idx ON public.users USING btree (username);


-- public.calendar definition

-- Drop table

-- DROP TABLE public.calendar;

CREATE TABLE public.calendar (
id uuid NOT NULL DEFAULT gen_random_uuid(),
"deviceId" uuid NOT NULL,
"createdAt" timestamp NOT NULL,
recs jsonb NOT NULL,
"inProgress" bool NOT NULL
);
COMMENT ON TABLE public.calendar IS 'Single day averages of devices';


-- public.devices definition

-- Drop table

-- DROP TABLE public.devices;

CREATE TABLE public.devices (
id uuid NOT NULL DEFAULT gen_random_uuid(),
model varchar NULL,
"name" varchar NULL,
importedfrom varchar NULL,
"location" point NULL,
"owner" uuid NOT NULL,
lastrecordid uuid NULL,
gmcid int8 NULL,
disabled bool NOT NULL,
lastemailalert timestamp NOT NULL,
stddevalertlimit float8 NULL,
proxiessettings jsonb NULL,
CONSTRAINT devices_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX devices_gmcid_idx ON public.devices USING btree (gmcid);


-- public.records definition

-- Drop table

-- DROP TABLE public.records;

CREATE TABLE public.records (
id uuid NOT NULL DEFAULT gen_random_uuid(),
"deviceId" uuid NOT NULL,
"date" timestamp NOT NULL,
ip inet NULL,
"type" varchar NULL,
"location" point NULL,
cpm float8 NULL,
acpm float8 NULL,
usv float8 NULL,
co2 float8 NULL,
hcho float8 NULL,
tmp float8 NULL,
ap float8 NULL,
hmdt float8 NULL,
accy float8 NULL,
CONSTRAINT records_pk PRIMARY KEY (id)
);
CREATE INDEX records_deviceid_date_idx ON public.records USING btree ("deviceId", date);


-- public.calendar foreign keys

ALTER TABLE public.calendar ADD CONSTRAINT calendar_devices_fk FOREIGN KEY ("deviceId") REFERENCES public.devices(id) ON DELETE CASCADE;


-- public.devices foreign keys

ALTER TABLE public.devices ADD CONSTRAINT devices_lastrecordid_fk FOREIGN KEY (lastrecordid) REFERENCES public.records(id) ON DELETE CASCADE;
ALTER TABLE public.devices ADD CONSTRAINT devices_owner_fk FOREIGN KEY ("owner") REFERENCES public.users(id) ON DELETE CASCADE;


-- public.records foreign keys

ALTER TABLE public.records ADD CONSTRAINT records_fk FOREIGN KEY ("deviceId") REFERENCES public.devices(id) ON DELETE CASCADE;
22 changes: 6 additions & 16 deletions gmcserver-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
<gmc.vertx.mail.config.path>./mail.json</gmc.vertx.mail.config.path>
<gmc.vertx.mail.templates.path>./gmcserver-email/out/</gmc.vertx.mail.templates.path>

<vertx.version>4.3.8</vertx.version>
<vertx.version>4.5.3</vertx.version>
<jackson.version>2.15.0</jackson.version>
<log4j.version>2.22.1</log4j.version>
</properties>

<repositories>
Expand Down Expand Up @@ -110,53 +111,42 @@
<artifactId>argon2-jvm</artifactId>
<version>2.7</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency><!--
https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
<version>${log4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.eatthepath/java-otp -->
<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>java-otp</artifactId>
<version>0.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ldaptive/ldaptive -->
<dependency>
<groupId>org.ldaptive</groupId>
<artifactId>ldaptive</artifactId>
<version>2.1.1</version>
<version>2.3.0</version>
</dependency>

<!--
https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

import java.util.Map;

import io.vertx.core.tracing.TracingPolicy;
import io.vertx.pgclient.PgBuilder;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
Expand All @@ -29,24 +31,28 @@
import me.vinceh121.gmcserver.managers.AbstractManager;

public class DatabaseManager extends AbstractManager {
private final PgPool pool;
private final Pool pool;

public DatabaseManager(final GMCServer srv) {
super(srv);

final PgConnectOptions optsConfig;
if (srv.getConfig().contains("db.uri")) {

if (srv.getConfig().containsKey("db.uri")) {
optsConfig = PgConnectOptions.fromUri(srv.getConfig().getProperty("db.uri"));
} else {
optsConfig = new PgConnectOptions();
}

final PgConnectOptions optsEnv = PgConnectOptions.fromEnv();
final PgConnectOptions optsEffective = optsConfig.merge(optsEnv.toJson());
final PgConnectOptions optsEffective = optsEnv.merge(optsConfig.toJson());

optsEffective.setTracingPolicy(TracingPolicy.ALWAYS);

final PoolOptions poolOpts = new PoolOptions();
poolOpts.setMaxSize(5);

this.pool = PgPool.pool(srv.getVertx(), optsEffective, poolOpts);
this.pool = PgBuilder.pool().with(poolOpts).connectingTo(optsEffective).build();

this.checkIndexes();
}
Expand All @@ -55,7 +61,7 @@ public DatabaseManager(final GMCServer srv) {
private void checkIndexes() {
}

public PgPool getPool() {
public Pool getPool() {
return this.pool;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;

import io.vertx.core.Future;
import io.vertx.sqlclient.RowIterator;
import me.vinceh121.gmcserver.GMCServer;
import me.vinceh121.gmcserver.entities.User;
import me.vinceh121.gmcserver.exceptions.AuthenticationException;
Expand All @@ -45,13 +46,15 @@ public Future<User> login(final String username, final String password) {
.mapTo(User.class)
.execute(Collections.singletonMap("username", username))
.onSuccess(rowSet -> {
final User user = rowSet.iterator().next();
final RowIterator<User> iter = rowSet.iterator();

if (user == null) {
if (!iter.hasNext()) {
promise.fail(new EntityNotFoundException("User not found"));
return;
}

final User user = iter.next();

if (user.getPassword() == null) {
promise.fail(new IllegalStateException("User account disabled"));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
*/
package me.vinceh121.gmcserver.entities;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;

import io.vertx.core.json.JsonObject;
import io.vertx.core.json.jackson.DatabindCodec;

public abstract class AbstractEntity {
private UUID id = UUID.randomUUID();
Expand All @@ -46,4 +50,14 @@ public JsonObject toJson() {
public JsonObject toPublicJson() {
return this.toJson();
}

public static String sqlFields(final Class<? extends AbstractEntity> cls) {
final List<BeanPropertyDefinition> list = DatabindCodec.mapper()
.writerFor(cls)
.getConfig()
.introspect(DatabindCodec.mapper().constructType(cls))
.findProperties();

return list.stream().map(p -> "#{" + p.getName() + "}").collect(Collectors.joining(","));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,8 @@ public JsonObject toMapJson() {
obj.put("lastRecord", this.getLastRecord().toPublicJson());
return obj;
}

public static String sqlFields() {
return AbstractEntity.sqlFields(Device.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public boolean isInProgress() {
public void setInProgress(final boolean inProgress) {
this.inProgress = inProgress;
}

public static String sqlFields() {
return AbstractEntity.sqlFields(DeviceCalendar.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ public JsonObject toJson() {
obj.remove("id");
return obj;
}

public static String sqlFields() {
return AbstractEntity.sqlFields(DeviceStats.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ public String toString() {
+ ", location=" + this.location + "]";
}

public static String sqlFields() {
return AbstractEntity.sqlFields(Record.class);
}

public static class Builder { // XXX this will need a big clean up but at least it splits stuff
private final Record record = new Record();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@ public String toString() {
return this.getUsername() + " (" + this.getId().toString() + ")";
}

public static String sqlFields() {
return AbstractEntity.sqlFields(User.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void executeSync(final Promise<DeviceCalendar> promise) {
cal.setInProgress(true);

this.srv.getDatabaseManager()
.update("INSERT INTO calendar VALUES")
.update("INSERT INTO calendar VALUES (" + DeviceCalendar.sqlFields() + ")")
.mapFrom(DeviceCalendar.class)
.execute(cal)
.onSuccess(r -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,15 @@ protected void executeSync(final Promise<Device> promise) {
}
dev.setLocation(location);

promise.complete(dev);

if (this.insertInDb) {
this.srv.getDatabaseManager()
.update("INSERT INTO devices VALUES")
.update("INSERT INTO devices VALUES (" + Device.sqlFields() + ")")
.mapFrom(Device.class)
.execute(dev)
.onSuccess(rs -> promise.complete(dev))
.onFailure(promise::fail);
} else {
promise.complete(dev);
}
})
.onFailure(promise::fail);
Expand Down
Loading

0 comments on commit cc77ab9

Please sign in to comment.