Skip to content

Commit

Permalink
Merge branch 'main' into create-delete-user
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeuerstein authored Oct 30, 2023
2 parents f6ff1d6 + 62463b8 commit 58fd9d2
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 12 deletions.
89 changes: 89 additions & 0 deletions backend/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,35 @@
}
}
}
]
"/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": {
Expand All @@ -360,29 +389,74 @@
}
}
},

"/users/": {
"get": {
"description": "",
"parameters": [],

"/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"
}
}
}
},

"/users/add/{number}/{claim}": {
"post": {
"description": "",
"parameters": [
{
"name": "number",

"/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": "claim",

"name": "maxDate",

"in": "path",
"required": true,
"type": "string"
Expand All @@ -395,12 +469,27 @@
}
}
},

"/users/delete/{number}": {
"delete": {
"description": "",
"parameters": [
{
"name": "number",

"/clusters/netModelData/{modelName}/{modelCost}": {
"get": {
"description": "",
"parameters": [
{
"name": "modelName",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "modelCost",

"in": "path",
"required": true,
"type": "string"
Expand Down
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;

0 comments on commit 58fd9d2

Please sign in to comment.