-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
2,266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ node_modules | |
.env | ||
deployment_key.txt | ||
deployment_scripts | ||
server/screenshots | ||
db | ||
server/screenshots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
|
||
const db = require('./db'); | ||
var users = require('./users'); | ||
var logger = require('./logger'); | ||
|
||
module.exports = { | ||
getUsage: getUsage | ||
}; | ||
|
||
function getUsage(req, res, next) { | ||
var userid = users.getUserID(req); | ||
|
||
if (req.query.status == "monthly") { | ||
db.task('grouped-activity', t => { | ||
return t.batch([ | ||
t.any(`SELECT * | ||
FROM ( | ||
SELECT to_char(month::date, 'Month') as date, 'Checks' as name, to_char(month::date, 'MM') as MonthNo | ||
FROM generate_series(timestamp '2021-01-01' | ||
, now() | ||
, interval '1 month') month | ||
) d | ||
LEFT JOIN ( | ||
SELECT to_char(date_trunc('month', datetime)::date, 'Month') AS date | ||
, coalesce(count(*),0)::INTEGER AS Value | ||
FROM history | ||
INNER JOIN JOBS using (job_id) | ||
WHERE | ||
history.status = 1 | ||
AND datetime >= now() - '12 month'::interval | ||
AND datetime <= now() | ||
AND user_id = $1 | ||
GROUP BY 1 | ||
) t USING (date) | ||
ORDER BY monthno`, [userid]), | ||
t.any(`SELECT * | ||
FROM ( | ||
SELECT to_char(month::date, 'Month') as date, 'Changes' as name, to_char(month::date, 'MM') as MonthNo | ||
FROM generate_series(timestamp '2021-01-01' | ||
, now() | ||
, interval '1 month') month | ||
) d | ||
LEFT JOIN ( | ||
SELECT to_char(date_trunc('month', datetime)::date, 'Month') AS date | ||
, coalesce(count(*),0)::INTEGER AS Value | ||
FROM history | ||
INNER JOIN JOBS using (job_id) | ||
WHERE | ||
history.status = 2 | ||
AND datetime >= now() - '12 month'::interval | ||
AND datetime <= now() | ||
AND user_id = $1 | ||
GROUP BY 1 | ||
) t USING (date) | ||
ORDER BY monthno`, [userid]), | ||
]); | ||
}) | ||
.then(function (data) { | ||
res.status(200).json(data[0].concat(data[1])); | ||
}) | ||
.catch(function (err) { | ||
logger.logError('getJobDetail: ' + err); | ||
return next(err); | ||
}); | ||
} else { | ||
db.task('grouped-activity', t => { | ||
return t.batch([ | ||
t.any(` | ||
SELECT * | ||
FROM ( | ||
SELECT to_char(day::date, 'dd') as date, 'Checks' as name | ||
FROM generate_series(now() - '30 day'::interval | ||
, now() | ||
, interval '1 day') day | ||
) d | ||
LEFT JOIN ( | ||
SELECT to_char(date_trunc('day', datetime)::date, 'dd') AS date | ||
, coalesce(count(*),0)::INTEGER AS Value | ||
FROM history | ||
INNER JOIN JOBS using (job_id) | ||
WHERE | ||
history.status = 1 | ||
AND datetime >= now() - '30 day'::interval | ||
AND datetime <= now() | ||
AND user_id = $1 | ||
GROUP BY 1 | ||
) t USING (date) | ||
ORDER BY date`, [userid]), | ||
t.any(`SELECT * | ||
FROM ( | ||
SELECT to_char(day::date, 'dd') as date, 'Changes' as name | ||
FROM generate_series(now() - '30 day'::interval | ||
, now() | ||
, interval '1 day') day | ||
) d | ||
LEFT JOIN ( | ||
SELECT to_char(date_trunc('day', datetime)::date, 'dd') AS date | ||
, coalesce(count(*),0)::INTEGER AS Value | ||
FROM history | ||
INNER JOIN JOBS using (job_id) | ||
WHERE | ||
history.status = 2 | ||
AND datetime >= now() - '30 day'::interval | ||
AND datetime <= now() | ||
AND user_id = $1 | ||
GROUP BY 1 | ||
) t USING (date) | ||
ORDER BY date`, [userid]), | ||
]); | ||
}) | ||
.then(function (data) { | ||
res.status(200).json(data[0].concat(data[1])); | ||
}) | ||
.catch(function (err) { | ||
logger.logError('getJobDetail: ' + err); | ||
return next(err); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var promise = require('bluebird'); | ||
|
||
var options = { | ||
// Initialization Options | ||
promiseLib: promise | ||
}; | ||
|
||
if (process.env.NODE_ENV === 'dev') { | ||
options.query = function(e) { | ||
console.log(" "); | ||
console.log(" "); | ||
console.log(e.query); //Logs all SQL to console | ||
console.log(" "); | ||
console.log(" "); | ||
} | ||
} | ||
|
||
options.schema = process.env.DB_SCHEMA; | ||
|
||
var pgp = require('pg-promise')(options); | ||
var connectionString = 'postgres://' + process.env.DB_USERNAME + ':' + process.env.DB_PASSWORD + '@' + process.env.DB_HOST + ':' + process.env.DB_PORT + '/' + process.env.DB_NAME; | ||
var db = pgp(connectionString); | ||
|
||
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const S3Client = require("aws-sdk/clients/s3"); | ||
const s3 = new S3Client({ region: process.env.S3_REGION }); | ||
var fs = require('fs'); | ||
const PNG = require('pngjs').PNG; | ||
|
||
module.exports = { | ||
deleteFile, | ||
createFile, | ||
getFile, | ||
createFilePath, | ||
checkDirectory, | ||
deleteFolder, | ||
createLocalFilePath | ||
} | ||
|
||
function checkDirectory(dir) { | ||
if (process.env.FILESYSTEM !== "S3" && !fs.existsSync(dir)){ | ||
fs.mkdirSync(dir); | ||
} | ||
} | ||
|
||
function createFilePath(filename) { | ||
if (process.env.FILESYSTEM === "S3") { | ||
return "https://" + process.env.S3_BUCKET + ".s3.amazonaws.com/" + filename | ||
} else { | ||
return "./" + filename | ||
} | ||
} | ||
|
||
function createLocalFilePath(filename) { | ||
return "./" + filename | ||
} | ||
|
||
async function deleteFolder(path) { | ||
if (process.env.FILESYSTEM === "S3") { | ||
try { | ||
const listParams = { | ||
Bucket: process.env.S3_BUCKET, | ||
Prefix: path | ||
}; | ||
|
||
const listedObjects = await s3.listObjectsV2(listParams).promise(); | ||
|
||
if (listedObjects.Contents.length === 0) return; | ||
|
||
const deleteParams = { | ||
Bucket: process.env.S3_BUCKET, | ||
Delete: { Objects: [] } | ||
}; | ||
|
||
listedObjects.Contents.forEach(({ Key }) => { | ||
deleteParams.Delete.Objects.push({ Key }); | ||
}); | ||
|
||
await s3.deleteObjects(deleteParams).promise(); | ||
|
||
} catch (err) { | ||
console.log(err); | ||
} | ||
|
||
return | ||
|
||
} else { | ||
fs.unlinkSync("./" + path) | ||
return | ||
} | ||
} | ||
|
||
async function deleteFile(path) { | ||
if (process.env.FILESYSTEM === "S3") { | ||
return await s3 | ||
.deleteObject({ | ||
Bucket: process.env.S3_BUCKET, | ||
Key: path | ||
}) | ||
.promise(); | ||
} else { | ||
fs.unlinkSync("./" + path) | ||
return | ||
} | ||
} | ||
|
||
async function createFile(path, data) { | ||
if (process.env.FILESYSTEM === "S3") { | ||
return await s3 | ||
.upload({ | ||
Bucket: process.env.S3_BUCKET, | ||
Key: path, | ||
Body: data, | ||
ContentType: "image/png", | ||
ACL: "public-read" | ||
}) | ||
.promise(); | ||
} else { | ||
fs.writeFileSync("./" + path, data); | ||
return | ||
} | ||
} | ||
|
||
async function getFile(path) { | ||
if (process.env.FILESYSTEM === "S3") { | ||
var img = await s3 | ||
.getObject({ Bucket: process.env.S3_BUCKET, Key: path }) | ||
.promise(); | ||
var response = PNG.sync.read(img.Body) | ||
return response | ||
} else { | ||
var img = fs.readFileSync("./" + path); | ||
var response = PNG.sync.read(img) | ||
return response | ||
} | ||
} |
Oops, something went wrong.