1 | const moment = require('moment')
|
2 | module.exports = {
|
3 | createProtoTimestamps: logEntry => {
|
4 | if (logEntry && logEntry.entries && logEntry.entries.length > 0) {
|
5 | logEntry.entries = logEntry.entries.map(entry => {
|
6 | const dt = moment(entry.ts).valueOf()
|
7 | return {
|
8 | timestamp: {
|
9 | seconds: Math.floor(dt / 1000),
|
10 | nanos: (dt % 1000) * 1000
|
11 | },
|
12 | line: entry.line
|
13 | }
|
14 | })
|
15 | }
|
16 | return logEntry
|
17 | },
|
18 | sortBatch: batch => {
|
19 | let maxValue = 0
|
20 | if (
|
21 | batch.streams[0] &&
|
22 | batch.streams[0].entries &&
|
23 | batch.streams[0].entries.find(
|
24 | entry => entry.timestamp && entry.timestamp.seconds
|
25 | )
|
26 | ) {
|
27 | batch.streams = batch.streams.map(stream => {
|
28 | stream.entries = stream.entries.sort(
|
29 | (a, b) => a.timestamp.seconds - b.timestamp.seconds
|
30 | )
|
31 | stream.entries = stream.entries.map(entry => {
|
32 | const currValue = Number(
|
33 | String(entry.timestamp.seconds).concat(
|
34 | String(entry.timestamp.nanos / 1000)
|
35 | )
|
36 | )
|
37 | if (maxValue === 0 || maxValue < currValue) {
|
38 | maxValue = currValue
|
39 | } else {
|
40 | entry.timestamp = {
|
41 | seconds: Math.floor(maxValue / 1000),
|
42 | nanos: (maxValue % 1000) * 1000 + 1
|
43 | }
|
44 | maxValue = Number(
|
45 | String(entry.timestamp.seconds).concat(
|
46 | String(entry.timestamp.nanos / 1000)
|
47 | )
|
48 | )
|
49 | }
|
50 | return entry
|
51 | })
|
52 | return stream
|
53 | })
|
54 | } else {
|
55 | batch.streams = batch.streams.sort(
|
56 | (a, b) => a.entries[0].ts - b.entries[0].ts
|
57 | )
|
58 | batch.streams = batch.streams.map(stream => {
|
59 | if (maxValue === 0 || maxValue < stream.entries[0].ts) {
|
60 | maxValue = stream.entries[0].ts
|
61 | } else {
|
62 | stream.entries[0].ts = maxValue + 1
|
63 | maxValue = stream.entries[0].ts
|
64 | }
|
65 | return stream
|
66 | })
|
67 | }
|
68 | return batch
|
69 | }
|
70 | }
|