Skip to content

Commit

Permalink
Merge branch 'main' into modify-user
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeuerstein authored Oct 30, 2023
2 parents 79687a1 + d46aba8 commit 384ded2
Show file tree
Hide file tree
Showing 16 changed files with 3,550 additions and 68 deletions.
122 changes: 121 additions & 1 deletion backend/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"clusterID": {
"example": "any"
},
"type": {
"model": {
"example": "any"
}
}
Expand Down Expand Up @@ -320,6 +320,126 @@
}
}
}
},
"/clusters/netModelData/{modelName}/{modelCost}/dates/{minDate}/{maxDate}/": {
"get": {
"description": "",
"parameters": [
{
"name": "modelName",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "modelCost",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "minDate",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "maxDate",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/clusters/netModelData/{modelName}/{modelCost}/min/{minDate}": {
"get": {
"description": "",
"parameters": [
{
"name": "modelName",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "modelCost",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "minDate",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/clusters/netModelData/{modelName}/{modelCost}/max/{maxDate}": {
"get": {
"description": "",
"parameters": [
{
"name": "modelName",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "modelCost",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "maxDate",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/clusters/netModelData/{modelName}/{modelCost}": {
"get": {
"description": "",
"parameters": [
{
"name": "modelName",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "modelCost",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
76 changes: 65 additions & 11 deletions backend/src/customers/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,23 @@ const getNetByClusterId = async (id: string) =>
/**
* Inserts new net into DB
* @param clusterID cluster id
* @param type type of net
* @param model model of fog net
* @returns promise with new net doc or error
*/
const insertNet = async (clusterID: string, model: Model) =>
NetModel.create(new Net(clusterID, model));

/**
* Removes net by id from DB
* @param id net id
* @returns doc containg bool acknowledged and number deletedCount
*/
const deleteNet = async (id: string) => {
console.log(id);
NetModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) });
DataModel.deleteMany({ netID: id }); // FIX: does not delete associated data docs
};

/**
* Finds all data docs in DB
* @returns promise with all data docs or error
Expand Down Expand Up @@ -132,6 +143,30 @@ const deleteCluster = async (id: string) => {

/** NOTE: END OF TEMPORARY FUNCTIONS */

/**
* Helper function finds all data docs from list of netIDs
* @param netIds list of netIDs
* @param minDate earliest date to query data
* @param minDate latest date to query data
* @returns data docs or error
*/
const getAllDocsByNetIDs = async (
netIds: string[],
minDate: Date,
maxDate: Date
) => {
const cursor = DataModel.find({
netID: { $in: netIds },
date: { $gte: minDate, $lte: maxDate }
});
const datas = [];
for await (const doc of cursor) {
datas.push(doc);
}

return datas
};

/**
* Finds all data docs from clusters in list clusterIds
* @param clusterIds list of clusterIDs
Expand All @@ -144,22 +179,40 @@ const getAllDocsByClusterIDs = async (
minDate: Date,
maxDate: Date
) => {
const cursor1 = NetModel.find({ clusterID: { $in: clusterIds } });
const cursor = NetModel.find({ clusterID: { $in: clusterIds } });
const netIds = [];
for await (const doc of cursor1) {
for await (const doc of cursor) {
netIds.push(doc.id);
}

const cursor2 = DataModel.find({
netID: { $in: netIds },
date: { $gte: minDate, $lte: maxDate },
return getAllDocsByNetIDs(netIds, minDate, maxDate)
};

/**
* Finds all data docs from clusters in list clusterIds that have same model type
* @param clusterIds list of clusterIDs
* @param minDate earliest date to query data
* @param minDate latest date to query data
* @param model model of fog net
* @returns data docs or error
*/
const getAllDocsByModelAndClusterIDs = async (
clusterIds: string[],
minDate: Date,
maxDate: Date,
netModel: Model
) => {
const cursor = NetModel.find({
clusterID: { $in: clusterIds },
"model.name": netModel.name,
"model.cost": netModel.cost
});
const datas = [];
for await (const doc of cursor2) {
datas.push(doc);
const netIds = [];
for await (const doc of cursor) {
netIds.push(doc.id);
}

return datas;
return getAllDocsByNetIDs(netIds, minDate, maxDate)
};


Expand Down Expand Up @@ -195,5 +248,6 @@ export default {
insertData,
deleteData,
getClustersByLocation,
getAllDocsByClusterIDs
getAllDocsByClusterIDs,
getAllDocsByModelAndClusterIDs
};
14 changes: 14 additions & 0 deletions backend/src/customers/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { getModelForClass, prop } from "@typegoose/typegoose";

class Model {
constructor(name: string, cost: number) {
this.name = name;
this.cost = cost;
}

@prop()
public name!: string;

@prop()
public cost!: number;
}

class Data {
constructor(
net_id: string,
Expand Down
47 changes: 46 additions & 1 deletion backend/src/customers/views.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from "express";
import mongoose from "mongoose";
import DocController from "./controllers";
import { Model } from "./models";

// Note: we should use a try/catch to choose successJson or errorJson for responses
// this has been left out of this snippet for brevity
Expand Down Expand Up @@ -82,7 +83,7 @@ docRouter.delete("/data/delete/:id", async (req, res) => {
});
// NOTE: END OF TEMPORARY ROUTES

const clusterIds = ["65258f01ba4c14948edd4526", "65258d88899b7e221c1d33eb"]; // NOTE: temp hardcoded list of clusterIDs for testing
const clusterIds = ["65258f01ba4c14948edd4526", "6528685b77174a7928f4ef6d"] // NOTE: temp hardcoded list of clusterIDs for testing
docRouter.get("/clusterData/dates/:minDate/:maxDate/", async (req, res) => {
let minDate = new Date(req.params.minDate);
let maxDate = new Date(req.params.maxDate);
Expand Down Expand Up @@ -145,4 +146,48 @@ docRouter.post("/get-clusters-in-range/", async (req, res) => {
);
});

docRouter.get("/netModelData/:modelName/:modelCost/dates/:minDate/:maxDate/", async (req, res) => {
let minDate = new Date(req.params.minDate);
let maxDate = new Date(req.params.maxDate);
let name = req.params.modelName;
let cost = Number(req.params.modelCost);
let model = new Model(name, cost);
res
.status(200)
.send(successJson(await DocController.getAllDocsByModelAndClusterIDs(clusterIds, minDate, maxDate, model)));
});

docRouter.get("/netModelData/:modelName/:modelCost/min/:minDate", async (req, res) => {
let minDate = new Date(req.params.minDate);
let maxDate = new Date(new Date().toJSON().slice(0, 10));
let name = req.params.modelName;
let cost = Number(req.params.modelCost);
let model = new Model(name, cost);
res
.status(200)
.send(successJson(await DocController.getAllDocsByModelAndClusterIDs(clusterIds, minDate, maxDate, model)));
});

docRouter.get("/netModelData/:modelName/:modelCost/max/:maxDate", async (req, res) => {
let minDate = new Date("2023-01-01");
let maxDate = new Date(req.params.maxDate);
let name = req.params.modelName;
let cost = Number(req.params.modelCost);
let model = new Model(name, cost);
res
.status(200)
.send(successJson(await DocController.getAllDocsByModelAndClusterIDs(clusterIds, minDate, maxDate, model)));
});

docRouter.get("/netModelData/:modelName/:modelCost", async (req, res) => {
let minDate = new Date("2023-01-01");
let maxDate = new Date(new Date().toJSON().slice(0, 10));
let name = req.params.modelName;
let cost = Number(req.params.modelCost);
let model = new Model(name, cost);
res
.status(200)
.send(successJson(await DocController.getAllDocsByModelAndClusterIDs(clusterIds, minDate, maxDate, model)));
});

export default docRouter;
5 changes: 5 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@react-navigation/native": "^6.1.9",
"@react-navigation/native-stack": "^6.9.16",
"@react-navigation/stack": "^6.3.20",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^14.4.3",
Expand All @@ -12,6 +15,8 @@
"@types/react-dom": "^18.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-native": "^0.72.6",
"react-router-dom": "^6.17.0",
"react-scripts": "5.0.1",
"react-simple-maps": "^3.0.0",
"typescript": "^4.9.5",
Expand Down
26 changes: 26 additions & 0 deletions frontend/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,30 @@ path {
.App {
font-family: sans-serif;
text-align: center;

}

ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: olivedrab;
}

li {
float: left;
}

li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
Loading

0 comments on commit 384ded2

Please sign in to comment.