Skip to content

Commit

Permalink
perf!: getEntries filters 24 hour timestamp by default (#955)
Browse files Browse the repository at this point in the history
* feat!: getEntries filters 24 hour timestamp by default

* chore: clean up comments

* test: fix tests breaking on millisecond difference
  • Loading branch information
0xSage authored Dec 1, 2020
1 parent 1fa8cd3 commit 3d63d5f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,20 @@ class Logging {
opts?: GetEntriesRequest | GetEntriesCallback
): Promise<GetEntriesResponse> {
const options = opts ? (opts as GetEntriesRequest) : {};
const reqOpts = extend(
{
orderBy: 'timestamp desc',
},
options
);

// By default, sort entries by descending timestamp
let reqOpts = extend({orderBy: 'timestamp desc'}, options);

// By default, filter entries to last 24 hours only
const time = new Date();
time.setDate(time.getDate() - 1);
const timeFilter = `timestamp >= "${time.toISOString()}"`;
if (!options.filter) {
reqOpts = extend({filter: timeFilter}, reqOpts);
} else if (!options.filter.includes('timestamp')) {
reqOpts.filter += ` AND ${timeFilter}`;
}

reqOpts.resourceNames = arrify(reqOpts.resourceNames!);
this.projectId = await this.auth.getProjectId();
const resourceName = 'projects/' + this.projectId;
Expand Down
41 changes: 36 additions & 5 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,26 +477,29 @@ describe('Logging', () => {
logging.auth.getProjectId = async () => PROJECT_ID;
});

it('should exec without options', async () => {
it('should exec without options (with defaults)', async () => {
logging.loggingService.listLogEntries = async (
reqOpts: {},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reqOpts: any,
gaxOpts: {}
) => {
assert.deepStrictEqual(reqOpts, {
filter: reqOpts?.filter,
orderBy: 'timestamp desc',
resourceNames: ['projects/' + logging.projectId],
});
assert.deepStrictEqual(gaxOpts, {
autoPaginate: undefined,
});
assert.ok(reqOpts?.filter.includes('timestamp'));
return [[]];
};

await logging.getEntries();
});

it('should accept options', async () => {
const options = {filter: 'test'};
it('should accept options (and not overwrite timestamp)', async () => {
const options = {filter: 'timestamp > "2020-11-11T15:01:23.045123456Z"'};

logging.loggingService.listLogEntries = async (
reqOpts: {},
Expand All @@ -505,7 +508,7 @@ describe('Logging', () => {
assert.deepStrictEqual(
reqOpts,
extend(options, {
filter: 'test',
filter: 'timestamp > "2020-11-11T15:01:23.045123456Z"',
orderBy: 'timestamp desc',
resourceNames: ['projects/' + logging.projectId],
})
Expand All @@ -520,6 +523,32 @@ describe('Logging', () => {
await logging.getEntries(options);
});

it('should append default timestamp to existing filters', async () => {
const options = {filter: 'test'};

logging.loggingService.listLogEntries = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reqOpts: any,
gaxOpts: {}
) => {
assert.deepStrictEqual(
reqOpts,
extend(options, {
filter: reqOpts?.filter,
orderBy: 'timestamp desc',
resourceNames: ['projects/' + logging.projectId],
})
);
assert.deepStrictEqual(gaxOpts, {
autoPaginate: undefined,
});
assert.ok(reqOpts?.filter.includes('test AND timestamp'));
return [[]];
};

await logging.getEntries(options);
});

it('should not push the same resourceName again', async () => {
const options = {
resourceNames: ['projects/' + logging.projectId],
Expand Down Expand Up @@ -567,12 +596,14 @@ describe('Logging', () => {
assert.deepStrictEqual(reqOpts, {
a: 'b',
c: 'd',
filter: reqOpts?.filter,
orderBy: 'timestamp desc',
resourceNames: ['projects/' + logging.projectId],
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.strictEqual((reqOpts as any).gaxOptions, undefined);
assert.deepStrictEqual(gaxOpts, options.gaxOptions);
assert.ok(reqOpts?.filter.includes('timestamp'));
return [[]];
};

Expand Down

0 comments on commit 3d63d5f

Please sign in to comment.