Skip to content

Commit

Permalink
Merge pull request #233 from palsp/staging
Browse files Browse the repository at this point in the history
Deploy to Prod
  • Loading branch information
palsp authored Aug 30, 2021
2 parents 25579c4 + 61b1268 commit b020ec2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 39 deletions.
66 changes: 57 additions & 9 deletions functions/src/controller/dashboard/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const { count } = require("console");
const { admin, collection } = require("../../init");
const { statusListReverse } = require("../../api/const");
const { success } = require("../../response/success");

exports.getAccumulative = async() => {
const [g1,g2,r1,r2,y1,y2,users,unknown,legacyRef] = await Promise.all([getCount(collection.userCount,"G1"),getCount(collection.userCount,"G2"),getCount(collection.userCount,"R1"),getCount(collection.userCount,"R2"),getCount(collection.userCount,"Y1"),getCount(collection.userCount,"Y2"),getCount(collection.userCount,"users"),getCount(collection.userCount,"unknown"),getCount(collection.legacyStat,"stat")]);
const temp = {
G1:g1,
G2:g2,
R1:r1,
R2:r2,
Y1:y1,
Y2:y2,
users:users,
unknown:unknown,
G1: g1,
G2: g2,
R1: r1,
R2: r2,
Y1: y1,
Y2: y2,
users: users,
unknown: unknown,
legacyCount: legacyRef
};
return temp;
Expand All @@ -27,4 +30,49 @@ const getCount = async(collection, document) =>{
}else{
return 0;
}
}
}

exports.resetUserCount = async() => {
const batch = admin.firestore().batch();
const docs = ["G1","G2","R1","R2","Y1","Y2","users","unknown"];
const data = await getPatient();
const tempdoc = admin.firestore().collection(collection.userCount);
const temps = await Promise.all([tempdoc.doc("G1").get(),tempdoc.doc("G2").get(),tempdoc.doc("R1").get(),tempdoc.doc("R2").get(),tempdoc.doc("Y1").get(),tempdoc.doc("Y2").get(),tempdoc.doc("users").get(),tempdoc.doc("unknown").get()]);
let check = 0;
docs.forEach((doc)=>{
const docRef = tempdoc.doc(doc);
if(temps[check].exists){
console.log("Update " + doc);
batch.update(docRef,{count : data[doc]});
}else{
console.log("Create " + doc);
batch.create(docRef,{count : data[doc]});
}
check++;
});
await batch.commit();
return success();
}

const getPatient = async() => {
const snapshot = await admin.firestore().collection(collection.patient).get();
let counts={
G1: 0,
G2: 0,
R1: 0,
R2: 0,
Y1: 0,
Y2: 0,
users:0,
unknown:0
};
snapshot.forEach((doc)=>{
const count = doc.data();
if(count.toAmed==0){
counts.users += 1;
}
counts[statusListReverse[count.status]] += 1 ;
});
console.log(counts);
return counts;
}
38 changes: 27 additions & 11 deletions functions/src/controller/exportController/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const { calculateAge, convertTZ, getDateID } = require("../../utils/date");
const utils = require("./utils");
const { success } = require("../../response/success");


exports.exportR2R = async (data, _context) => {
const { value, error } = validateExportRequestToCallSchema(data);
if (error) {
Expand Down Expand Up @@ -346,20 +345,37 @@ exports.exportRequestToCallDayOne = async (data, _context) => {

exports.exportTimeSeries = async (req, res) => {
try {
const snapshot = await admin.firestore().collection(collection.timeSeries).get();
const snapshot = await admin
.firestore()
.collection(collection.timeSeries)
.get();

const headers = ['date','active users','drop off Rate', 'r2cccount', 'terminate users','activebtw 36 to 72 hrs']
const result = [headers]
const headers = [
"date",
"active users",
"drop off Rate",
"r2cccount",
"terminate users",
"activebtw 36 to 72 hrs",
];
const result = [headers];
snapshot.forEach((doc) => {
const data = doc.data();
console.log(data)
result.push([doc.id,data.activeUser,data.dropoffrate,data.r2account,data.terminateUser,data.usersbtw36hrsto72hrs])
})
console.log(result)
console.log(data);
result.push([
doc.id,
data.activeUser,
data.dropoffrate,
data.r2account,
data.terminateUser,
data.usersbtw36hrsto72hrs,
]);
});
console.log(result);
const wb = XLSX.utils.book_new();

const ws = XLSX.utils.aoa_to_sheet(result);
XLSX.utils.book_append_sheet(wb, ws,"statistics");
XLSX.utils.book_append_sheet(wb, ws, "statistics");
const filename = `daily_statistics.xlsx`;
const opts = { bookType: "xlsx", type: "binary" };

Expand All @@ -376,7 +392,7 @@ exports.exportTimeSeries = async (req, res) => {
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
stream.pipe(res);
} catch (err) {
console.log(err)
console.log(err);
return res.json({ success: false });
}
}
};
8 changes: 6 additions & 2 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ exports.thisEndpointNeedsAuth = functions.region(region).https.onCall(
);

exports.accumulativeData = functions
.region(region)
.https.onCall(authenticateVolunteer(dashboard.getAccumulative));
.region(region)
.https.onCall(authenticateVolunteer(dashboard.getAccumulative));

exports.resetUserCount = functions
.region(region)
.https.onCall(authenticateVolunteer(dashboard.resetUserCount));

exports.backupFirestore = functions
.region(region)
Expand Down
24 changes: 13 additions & 11 deletions functions/src/utils/zip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import XLSX from "xlsx";
import JSZip from "jszip";
const XLSX = require("xlsx");
const JSZip = require("jszip");

export const makeAoA = (size) => {
const makeAoA = (size) => {
const aoa = new Array(size);

for (let i = 0; i < size; i++) {
Expand All @@ -16,7 +16,7 @@ export const makeAoA = (size) => {
* @param {any[][]} aoa
* @param {any[]} data
*/
export const fillWith = (aoa, data) => {
const fillWith = (aoa, data) => {
const size = aoa.length;

for (let i = 0; i < data.length; i++) {
Expand All @@ -32,7 +32,7 @@ export const fillWith = (aoa, data) => {
* @param {() => any[]} formatter
* @returns
*/
export const prepareZipFile = (zip, aoa, headers, formatter) => {
const prepareZipFile = (zip, aoa, headers, formatter) => {
aoa.forEach((arr, i) => {
const result = [[...headers]];
arr.forEach((el) => {
Expand All @@ -53,12 +53,7 @@ export const prepareZipFile = (zip, aoa, headers, formatter) => {
* @param {string[]} headers - array of headers
* @param {() => any[]} formatter - function that return element of each row
*/
export const generateZipFileRoundRobin = async (
size,
data,
headers,
formatter
) => {
const generateZipFileRoundRobin = async (size, data, headers, formatter) => {
const aoa = makeAoA(size);

fillWith(aoa, data);
Expand All @@ -70,3 +65,10 @@ export const generateZipFileRoundRobin = async (
const content = await zip.generateAsync({ type: "base64" });
return { ok: true, title: "report.zip", content };
};

module.exports = {
makeAoA,
fillWith,
prepareZipFile,
generateZipFileRoundRobin,
};
12 changes: 6 additions & 6 deletions functions/src/utils/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jest.mock("jszip", () => {
});
});

const {
makeAoA,
fillWith,
prepareZipFile,
generateZipFileRoundRobin,
} = require("./zip");

describe("zip", () => {

const {
makeAoA,
fillWith,
prepareZipFile,
generateZipFileRoundRobin,
} = require("./zip");

const JSZip = require("jszip");

Expand Down

0 comments on commit b020ec2

Please sign in to comment.