astro-accelerator-utils 0.2.34 → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ export class Accelerator {
11
11
  siteUrl: string;
12
12
  subfolder: string;
13
13
  useTrailingUrlSlash: boolean;
14
+ captureStatistics: boolean;
14
15
  get authors(): Authors;
15
16
  get cache(): Cache;
16
17
  get dateFormatter(): DateFormatter;
@@ -20,6 +21,7 @@ export class Accelerator {
20
21
  get posts(): Posts;
21
22
  get taxonomy(): Taxonomy;
22
23
  get urlFormatter(): UrlFormatter;
24
+ get statistics(): typeof StatisticsStub;
23
25
  }
24
26
  export type Site = import("../../types/Site").Site;
25
27
  import { Authors } from "./authors.mjs";
@@ -31,3 +33,4 @@ import { Paging } from "./paging.mjs";
31
33
  import { Posts } from "./posts.mjs";
32
34
  import { Taxonomy } from "./taxonomy.mjs";
33
35
  import { UrlFormatter } from "./urls.mjs";
36
+ import { StatisticsStub } from "./statistics-stub.mjs";
@@ -7,6 +7,8 @@ import { Paging } from './paging.mjs';
7
7
  import { Posts } from './posts.mjs';
8
8
  import { Taxonomy } from './taxonomy.mjs';
9
9
  import { UrlFormatter } from './urls.mjs';
10
+ import { Statistics } from './statistics.mjs';
11
+ import { StatisticsStub } from './statistics-stub.mjs';
10
12
 
11
13
  /**
12
14
  * @typedef { import("../../types/Site").Site } Site
@@ -22,6 +24,7 @@ export class Accelerator {
22
24
  this.siteUrl = site.url;
23
25
  this.subfolder = site.subfolder;
24
26
  this.useTrailingUrlSlash = site.useTrailingUrlSlash;
27
+ this.captureStatistics = site.captureStatistics;
25
28
  }
26
29
 
27
30
  get authors() {
@@ -57,6 +60,12 @@ export class Accelerator {
57
60
  }
58
61
 
59
62
  get urlFormatter() {
60
- return new UrlFormatter(this.siteUrl, this.subfolder, this.useTrailingUrlSlash)
63
+ return new UrlFormatter(this.siteUrl, this.subfolder, this.useTrailingUrlSlash);
64
+ }
65
+
66
+ get statistics() {
67
+ return (this.captureStatistics)
68
+ ? Statistics
69
+ : StatisticsStub;
61
70
  }
62
71
  }
@@ -30,9 +30,9 @@ export class Cache {
30
30
  */
31
31
  clear(): Promise<void>;
32
32
  /**
33
- * Get's the path of the cache files
34
- * @returns {string}
35
- */
33
+ * Get's the path of the cache files
34
+ * @returns {string}
35
+ */
36
36
  getCachePath(): string;
37
37
  /**
38
38
  * Gets the file path for a cache item
package/lib/v1/cache.mjs CHANGED
@@ -78,23 +78,23 @@ export class Cache {
78
78
  }
79
79
  }
80
80
 
81
- /**
81
+ /**
82
82
  * Get's the path of the cache files
83
83
  * @returns {string}
84
84
  */
85
- getCachePath () {
86
- const cachePath = path.join(process.cwd(), '.cache/');
87
- fs.mkdirSync(cachePath, { recursive: true })
88
- return cachePath;
89
- }
90
-
91
- /**
92
- * Gets the file path for a cache item
93
- * @param {string} key
94
- * @returns {string}
95
- */
96
- getItemPath (key) {
97
- const cachePath = this.getCachePath();
98
- return path.join(cachePath, key + '.json');
99
- }
85
+ getCachePath () {
86
+ const cachePath = path.join(process.cwd(), '.cache/');
87
+ fs.mkdirSync(cachePath, { recursive: true })
88
+ return cachePath;
89
+ }
90
+
91
+ /**
92
+ * Gets the file path for a cache item
93
+ * @param {string} key
94
+ * @returns {string}
95
+ */
96
+ getItemPath (key) {
97
+ const cachePath = this.getCachePath();
98
+ return path.join(cachePath, key + '.json');
99
+ }
100
100
  }
@@ -0,0 +1,5 @@
1
+ export class StatisticsStub {
2
+ constructor(operation: any);
3
+ start(): void;
4
+ stop(): void;
5
+ }
@@ -0,0 +1,10 @@
1
+ export class StatisticsStub {
2
+ constructor(operation) {
3
+ }
4
+
5
+ start() {
6
+ }
7
+
8
+ stop() {
9
+ }
10
+ }
@@ -0,0 +1,39 @@
1
+ export class Statistics {
2
+ /**
3
+ * Clears the stats file
4
+ */
5
+ static purge(): void;
6
+ /**
7
+ * Get's the path of the log files
8
+ * @returns {string}
9
+ */
10
+ static getLogPath(): string;
11
+ /**
12
+ * Gets the file path for a log item
13
+ * @returns {string}
14
+ */
15
+ static getItemPath(): string;
16
+ /**
17
+ * Creates a new stats instance - all instances append to the stats file
18
+ * Use Statistics.purge() to reset the file
19
+ * After creating a timer, use start() and stop() to record an event
20
+ * @param {string} operation
21
+ */
22
+ constructor(operation: string);
23
+ operation: string;
24
+ /**
25
+ * Gets high resolution time in ms
26
+ * @returns number
27
+ */
28
+ getMilliseconds(): number;
29
+ /**
30
+ * Start the timer
31
+ */
32
+ start(): void;
33
+ /**
34
+ * Stop the timer (logs to .logs/statistics.csv)
35
+ */
36
+ stop(): void;
37
+ end: number;
38
+ duration: number;
39
+ }
@@ -0,0 +1,74 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import process from 'process';
4
+ import { EOL } from 'os';
5
+
6
+ export class Statistics {
7
+ /**
8
+ * Creates a new stats instance - all instances append to the stats file
9
+ * Use Statistics.purge() to reset the file
10
+ * After creating a timer, use start() and stop() to record an event
11
+ * @param {string} operation
12
+ */
13
+ constructor(operation) {
14
+ this.operation = operation.replace(/"/g, '');
15
+ }
16
+
17
+ /**
18
+ * Clears the stats file
19
+ */
20
+ static purge() {
21
+ const path = this.getItemPath();
22
+ if (fs.existsSync(path)) {
23
+ fs.unlinkSync(path);
24
+ }
25
+
26
+ fs.writeFileSync(path, `Operation,Date,Duration${EOL}`);
27
+ }
28
+
29
+ /**
30
+ * Gets high resolution time in ms
31
+ * @returns number
32
+ */
33
+ getMilliseconds() {
34
+ const hrtime = process.hrtime();
35
+ return (hrtime[0] * 1000) + (hrtime[1] / 1000000);
36
+ }
37
+
38
+ /**
39
+ * Start the timer
40
+ */
41
+ start() {
42
+ this.start = this.getMilliseconds();
43
+ }
44
+
45
+ /**
46
+ * Stop the timer (logs to .logs/statistics.csv)
47
+ */
48
+ stop() {
49
+ this.end = this.getMilliseconds();
50
+ this.duration = this.end - this.start;
51
+
52
+ const path = Statistics.getItemPath();
53
+ fs.appendFileSync(path, `"${this.operation}",${console.timeStamp},${this.duration}${EOL}`);
54
+ }
55
+
56
+ /**
57
+ * Get's the path of the log files
58
+ * @returns {string}
59
+ */
60
+ static getLogPath () {
61
+ const logPath = path.join(process.cwd(), '.log/');
62
+ fs.mkdirSync(logPath, { recursive: true })
63
+ return logPath;
64
+ }
65
+
66
+ /**
67
+ * Gets the file path for a log item
68
+ * @returns {string}
69
+ */
70
+ static getItemPath () {
71
+ const cachePath = this.getLogPath();
72
+ return path.join(cachePath, 'statistics.csv');
73
+ }
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.2.34",
3
+ "version": "0.2.36",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",
package/types/Site.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export type Site = {
2
2
  url: string;
3
3
  useTrailingUrlSlash?: boolean;
4
+ captureStatistics: boolean;
4
5
  dateOptions: Intl.DateTimeFormatOptions;
5
6
  subfolder: string;
6
7
  feedUrl: string;