Skip to content

Commit

Permalink
[TASK] Flatten lib (ericyd#123)
Browse files Browse the repository at this point in the history
* place logging in main lib directory

* update timer constant naming
  • Loading branch information
ericyd authored Sep 1, 2019
1 parent 4c795d0 commit 0b6f946
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import MimeType from './MimeType';
import Constants from './Constants';
import ErrorMessages from './ErrorMessages';
import FeatureFlag from './FeatureFlag';
import Logging from './util/Logging';
import Logging from './Logging';

export default class FileService {
gDriveService: GDriveService;
Expand Down
6 changes: 3 additions & 3 deletions lib/util/Logging.ts → lib/Logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FileService from '../FileService';
import Util from '../Util';
import ErrorMessages from '../ErrorMessages';
import FileService from './FileService';
import Util from './Util';
import ErrorMessages from './ErrorMessages';

// would be nice to call this "Logger" but that already exists in the Google Apps Script namespace
export default class Logging {
Expand Down
12 changes: 8 additions & 4 deletions lib/Timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ export default class Timer {
// https://developers.google.com/apps-script/guides/services/quotas
static MAX_RUNTIME_PER_DAY: number = 88 * 1000 * 60;
static MAX_RUNTIME: number = 4.7 * 1000 * 60;

// durations used for setting Triggers
static oneDay: number = 24 * 60 * 60 * 1000;
static sixMinutes: number = 6.2 * 1000 * 60;
static SLEEP_TIME_ONE_DAY: number = 24 * 60 * 60 * 1000;

// Trigger time includes runtime and sleep time (1.5 mins) because the trigger
// is set at the beginning of execution, not the end.
static TRIGGER_TIME: number = Timer.MAX_RUNTIME + (1.5 * 1000 * 60);

START_TIME: number;
runtime: number;
Expand Down Expand Up @@ -46,8 +50,8 @@ export default class Timer {
*/
calculateTriggerDuration(properties: Properties): number {
return properties.checkMaxRuntime()
? Timer.oneDay
: Timer.sixMinutes - this.runtime;
? Timer.SLEEP_TIME_ONE_DAY
: Timer.TRIGGER_TIME - this.runtime;
}

static now(): number {
Expand Down
4 changes: 2 additions & 2 deletions lib/TriggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Util from './Util';
import Timer from './Timer';
import Logging from './util/Logging';
import Logging from './Logging';

export default class TriggerService {
/**
Expand All @@ -14,7 +14,7 @@ export default class TriggerService {
static createTrigger(duration: number): void {
// default is 6.2 minutes from now
// Timer will stop execution after 4.7 minutes, so this gives about 1.5 minutes buffer
duration = duration || Timer.sixMinutes;
duration = duration || Timer.TRIGGER_TIME;
var trigger = ScriptApp.newTrigger('copy')
.timeBased()
.after(duration)
Expand Down
2 changes: 1 addition & 1 deletion lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import GDriveService from './GDriveService';
import Timer from './Timer';
import Constants from './Constants';
import ErrorMessages from './ErrorMessages';
import Logging from './util/Logging';
import Logging from './Logging';

export default class Util {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Properties from './Properties';
import Timer from './Timer';
import TriggerService from './TriggerService';
import ErrorMessages from './ErrorMessages';
import Logging from './util/Logging';
import Logging from './Logging';

/**
* Copy folders and files from source to destination.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"scripts": {
"pretest": "tsc lib/*.ts --module commonjs || true",
"posttest": "rm lib/*.js && rm lib/**/*.js",
"posttest": "rm lib/*.js",
"test": "nyc mocha --grep formatting --invert --compilers js:babel-core/register",
"prettier-test": "mocha test/formatting/prettier.test.js --compilers js:babel-core/register",
"watch": "gulp watch",
Expand Down
2 changes: 1 addition & 1 deletion test/FileService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Timer from '../lib/Timer';
import Properties from '../lib/Properties';
import Constants from '../lib/Constants';
import FeatureFlag from '../lib/FeatureFlag';
import Logging from '../lib/util/Logging';
import Logging from '../lib/Logging';
const PropertiesService = require('./mocks/PropertiesService');
const assert = require('assert');
const fs = require('fs');
Expand Down
6 changes: 3 additions & 3 deletions test/Timer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('timers', function() {
const duration = this.timer.calculateTriggerDuration(properties);
assert.equal(
duration,
Timer.sixMinutes,
Timer.TRIGGER_TIME,
'duration not equal to six minutes'
);
});
Expand All @@ -77,7 +77,7 @@ describe('timers', function() {
const duration = this.timer.calculateTriggerDuration(properties);
assert.equal(
duration,
Timer.sixMinutes - runtime,
Timer.TRIGGER_TIME - runtime,
'duration not equal to six minutes'
);
});
Expand All @@ -86,6 +86,6 @@ describe('timers', function() {
const properties = new Properties();
properties.incrementTotalRuntime(Timer.MAX_RUNTIME_PER_DAY);
const duration = this.timer.calculateTriggerDuration(properties);
assert.equal(duration, Timer.oneDay, 'duration not equal to one day');
assert.equal(duration, Timer.SLEEP_TIME_ONE_DAY, 'duration not equal to one day');
});
});
2 changes: 1 addition & 1 deletion test/TriggerService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('TriggerService', function() {
const triggerId = JSON.parse(
PropertiesService.getUserProperties().getProperty('triggerId')
);
assert.equal(triggerId.duration, Timer.sixMinutes);
assert.equal(triggerId.duration, Timer.TRIGGER_TIME);
assert.equal(triggerId.basis, 'time');
assert.equal(triggerId.created, true);
assert.equal(triggerId.name, 'copy');
Expand Down
4 changes: 2 additions & 2 deletions test/Util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Timer from '../lib/Timer';
import TriggerService from '../lib/TriggerService';
import Properties from '../lib/Properties';
import Constants from '../lib/Constants';
import Logging from '../lib/util/Logging';
import Logging from '../lib/Logging';
const userProperties = require('./mocks/PropertiesService').getUserProperties();
const sinon = require('sinon');
const assert = require('assert');
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Util', function() {
const stubSaveState = sinon.stub(Util, 'saveState');
const stubDeleteTrigger = sinon.stub(TriggerService, 'deleteTrigger');
const timer = new Timer();
this.clock.tick(Timer.sixMinutes);
this.clock.tick(Timer.TRIGGER_TIME);
timer.update(userProperties);
const fileList = [{ id: 1 }, { id: 2 }, { id: 3 }];
const properties = new Properties();
Expand Down
2 changes: 1 addition & 1 deletion test/toHumanReadable.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Logging from '../lib/util/Logging';
import Logging from '../lib/Logging';

describe('bytesToHumanReadable', function() {
it('should return bytes when less than 1 KB', () => {
Expand Down

0 comments on commit 0b6f946

Please sign in to comment.