Skip to content

Commit

Permalink
- Add server/db files to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
paschmann committed Jan 17, 2022
1 parent a526d3e commit d238cfe
Show file tree
Hide file tree
Showing 11 changed files with 2,266 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ node_modules
.env
deployment_key.txt
deployment_scripts
server/screenshots
db
server/screenshots
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Running from docker is the quickest and easiet method for testing the applicatio
git clone https://github.com/paschmann/changd
docker-compose up
```
Open your browser to http://localhost:80
Open your browser to http://localhost:80

Create a new account using the "Register" button on the login screen.

## Running Locally
Expand Down
120 changes: 120 additions & 0 deletions server/db/analytics.js
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);
});
}

}
24 changes: 24 additions & 0 deletions server/db/db.js
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;
112 changes: 112 additions & 0 deletions server/db/file_handler.js
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
}
}
Loading

0 comments on commit d238cfe

Please sign in to comment.