Skip to content

Commit

Permalink
Hit count
Browse files Browse the repository at this point in the history
  • Loading branch information
kucingbasah737 committed Nov 26, 2023
1 parent a63dd1d commit af16557
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/get-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const logger = require('./logger');
* @property {Date} created
* @property {string} target_url
* @property {number} disabled
* @property {number} hit_count
*/

/**
Expand Down
28 changes: 28 additions & 0 deletions lib/increment-hit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const MODULE_NAME = 'INCREMENT-HIT';

const TABLE_NAME = 'targets';

const logger = require('./logger');
const mysql = require('./mysql');

module.exports = async (xid, uuid) => {
const query = 'UPDATE ?? SET hit_count = hit_count + 1 WHERE uuid = ?';
const values = [
TABLE_NAME,
uuid,
];

try {
await mysql.poolPromise.query(query, values);
} catch (e) {
const newE = new Error(`${MODULE_NAME} F0667167: Exception`);
logger.warn(newE.message, {
xid,
eCode: e.code,
eMessage: e.message,
query: mysql.formatSimplified(query, values),
});

throw newE;
}
};
2 changes: 2 additions & 0 deletions lib/webserver/target-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const logger = require('../logger');
const insertHit = require('../insert-hit');

const touchGeoIp = require('../touch-geo-ip');
const incrementHit = require('../increment-hit');

let hasSendResponse = false;

Expand Down Expand Up @@ -67,6 +68,7 @@ module.exports = async (req, res, next) => {
: ip;

await insertHit(xid, target.uuid, ipv6, req.get('user-agent'), req.get('referer'));
await incrementHit(xid, target.uuid);
await touchGeoIp(xid, ipv6);
} catch (e) {
logger.warn(`${MODULE_NAME} 9E4990CC: Exception`, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231126184511-alter-table-targets-add-field-hit-count-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231126184511-alter-table-targets-add-field-hit-count-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
53 changes: 53 additions & 0 deletions migrations/20231126184816-update-hit-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231126184816-update-hit-count-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231126184816-update-hit-count-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `targets` DROP IF EXISTS `hit_count`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `targets` ADD IF NOT EXISTS `hit_count` BIGINT UNSIGNED NOT NULL DEFAULT '0' AFTER `disabled`;
1 change: 1 addition & 0 deletions migrations/sqls/20231126184816-update-hit-count-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Replace with your SQL commands */
6 changes: 6 additions & 0 deletions migrations/sqls/20231126184816-update-hit-count-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE `targets` t
SET
t.hit_count = (
SELECT COUNT(1) FROM hits h WHERE h.target_uuid = t.uuid
)
;
8 changes: 6 additions & 2 deletions views/my-links.html.njk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
<li>
created at {{ link.created | simpleDateTime }}
</li>


<li>
Hit count: {{ link.hit_count }}
</li>

{% if link.disabled %}
<li>this link has DISABLED</li>
{% endif %}
Expand All @@ -37,7 +41,7 @@
Create a new link
</a>

<canvas class="my-4 w-100" id="hitsPerDayChart" width="900" height="200"></canvas>
<canvas class="my-4 w-100" id="hitsPerDayChart" width="900" height="150"></canvas>

{% endblock %}

Expand Down
9 changes: 5 additions & 4 deletions views/my-links.view.html.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

{% block content %}

<p>
<a href="https://{{ target.hostname }}/{{ target.name }}">https://{{ target.hostname }}/{{ target.name }}</a>
</p>
<ul>
<li>https://{{ target.hostname }}/{{ target.name }}</li>
<li>{{ target.hit_count }} hits</li>
</ul>

{% include "my-links.form-edit.html.njk" %}

<p>
<a href="{{ basePath }}">Back to link list</a>
</p>

<canvas class="my-4 w-100" id="hitsPerDayChart" width="900" height="200"></canvas>
<canvas class="my-4 w-100" id="hitsPerDayChart" width="900" height="150"></canvas>

{% endblock %}

Expand Down

0 comments on commit af16557

Please sign in to comment.