Skip to content

Commit

Permalink
[]string -> []int64
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 8, 2023
1 parent a7740f1 commit 2158ed9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 51 deletions.
21 changes: 14 additions & 7 deletions internal/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/rusq/slackdump/v2/internal/structures"
"github.com/slack-go/slack"
)

Expand Down Expand Up @@ -109,22 +110,28 @@ func (c *Chunk) String() string {
return fmt.Sprintf("%s: %s", c.Type, c.ID())
}

var ErrUnsupChunkType = fmt.Errorf("unsupported chunk type")

// Timestamps returns the timestamps of the messages in the chunk. For files
// and other types of chunks, it returns nil.
func (c *Chunk) Timestamps() []string {
// and other types of chunks, it returns ErrUnsupChunkType.
func (c *Chunk) Timestamps() ([]int64, error) {
switch c.Type {
case CMessages, CThreadMessages:
return c.messageTimestamps()
default:
return nil
return nil, ErrUnsupChunkType
}
// unreachable
}

func (c *Chunk) messageTimestamps() []string {
ts := make([]string, len(c.Messages))
func (c *Chunk) messageTimestamps() ([]int64, error) {
ts := make([]int64, len(c.Messages))
for i := range c.Messages {
ts[i] = c.Messages[i].Timestamp
iTS, err := structures.TS2int(c.Messages[i].Timestamp)
if err != nil {
return nil, fmt.Errorf("invalid timestamp: %q :%w", c.Messages[i].Timestamp, err)
}
ts[i] = iTS
}
return ts
return ts, nil
}
34 changes: 18 additions & 16 deletions internal/chunk/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ type offts map[int64]offsetInfo
type offsetInfo struct {
ID string
Type ChunkType
Timestamps []string
Timestamps []int64
}

func (o offts) MessageCount() int {
Expand All @@ -393,7 +393,7 @@ func (o offts) MessageCount() int {

// offsetTimestamp returns a map of the chunk offset to the message timestamps
// it contains.
func (p *Player) offsetTimestamps() offts {
func (p *Player) offsetTimestamps() (offts, error) {
var ret = make(offts, p.idx.OffsetCount())
for id, offsets := range p.idx {
prefix := id[0]
Expand All @@ -406,20 +406,23 @@ func (p *Player) offsetTimestamps() offts {
if err != nil {
continue
}
ts, err := chunk.Timestamps()
if err != nil {
return nil, err
}
ret[offset] = offsetInfo{
ID: chunk.ID(),
Type: chunk.Type,
Timestamps: chunk.Timestamps(),
Timestamps: ts,
}
}
}
return ret
return ret, nil
}

type TimeOffset struct {
Offset int64 // offset within the chunk file
Timestamp string // original timestamp value
Index int // index of the message within the messages slice in the chunk
Offset int64 // offset within the chunk file
Index int // index of the message within the messages slice in the chunk
}

// timeOffsets returns a map of the timestamp to the chunk offset and index of
Expand All @@ -430,14 +433,9 @@ func timeOffsets(ots offts) map[int64]TimeOffset {
var ret = make(map[int64]TimeOffset, len(ots))
for offset, info := range ots {
for i, ts := range info.Timestamps {
iTS, err := structures.TS2int(ts)
if err != nil {
panic(err) // should not happen
}
ret[iTS] = TimeOffset{
Offset: offset,
Timestamp: ts,
Index: i,
ret[ts] = TimeOffset{
Offset: offset,
Index: i,
}
}
}
Expand All @@ -450,7 +448,11 @@ func (p *Player) Sorted(fn func(ts time.Time, m *slack.Message) error) error {
p.mu.Lock()
defer p.mu.Unlock()

tos := timeOffsets(p.offsetTimestamps())
ots, err := p.offsetTimestamps()
if err != nil {
return err
}
tos := timeOffsets(ots)
var tsList = make([]int64, 0, len(tos))
for ts := range tos {
tsList = append(tsList, ts)
Expand Down
56 changes: 28 additions & 28 deletions internal/chunk/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,24 +478,25 @@ func TestPlayer_offsetTimestamps(t *testing.T) {
lastOffset atomic.Int64
}
tests := []struct {
name string
fields fields
want offts
name string
fields fields
want offts
wantErr bool
}{
{
name: "ok",
fields: fields{
rs: bytes.NewReader(marshalChunks(t, testChunks)),
},
want: offts{
546: offsetInfo{ID: "C1234567890", Timestamps: []string{"1234567890.100000", "1234567890.200000", "1234567890.300000", "1234567890.400000", "1234567890.500000"}},
1382: offsetInfo{ID: "C1234567890", Timestamps: []string{"1234567890.600000", "1234567890.700000"}},
1751: offsetInfo{ID: "C1234567890", Timestamps: []string{"1234567890.800000", "1234567890.800000"}},
2208: offsetInfo{ID: "tC1234567890:1234567890.800000", Type: CThreadMessages, Timestamps: []string{"1234567890.900000", "1234567891.100000"}},
3572: offsetInfo{ID: "C987654321", Timestamps: []string{"1234567890.100000", "1234567890.200000", "1234567890.300000", "1234567890.400000", "1234567890.500000"}},
4407: offsetInfo{ID: "C987654321", Timestamps: []string{"1234567890.600000", "1234567890.700000"}},
4775: offsetInfo{ID: "C987654321", Timestamps: []string{"1234567890.800000", "1234567890.800000"}},
5231: offsetInfo{ID: "tC987654321:1234567890.800000", Type: CThreadMessages, Timestamps: []string{"1234567890.900000", "1234567891.100000"}},
546: offsetInfo{ID: "C1234567890", Timestamps: []int64{1234567890100000, 1234567890200000, 1234567890300000, 1234567890400000, 1234567890500000}},
1382: offsetInfo{ID: "C1234567890", Timestamps: []int64{1234567890600000, 1234567890700000}},
1751: offsetInfo{ID: "C1234567890", Timestamps: []int64{1234567890800000, 1234567890800000}},
2208: offsetInfo{ID: "tC1234567890:1234567890.800000", Type: CThreadMessages, Timestamps: []int64{1234567890900000, 1234567891100000}},
3572: offsetInfo{ID: "C987654321", Timestamps: []int64{1234567890100000, 1234567890200000, 1234567890300000, 1234567890400000, 1234567890500000}},
4407: offsetInfo{ID: "C987654321", Timestamps: []int64{1234567890600000, 1234567890700000}},
4775: offsetInfo{ID: "C987654321", Timestamps: []int64{1234567890800000, 1234567890800000}},
5231: offsetInfo{ID: "tC987654321:1234567890.800000", Type: CThreadMessages, Timestamps: []int64{1234567890900000, 1234567891100000}},
},
},
}
Expand All @@ -511,7 +512,11 @@ func TestPlayer_offsetTimestamps(t *testing.T) {
pointer: tt.fields.pointer,
lastOffset: tt.fields.lastOffset,
}
got := p.offsetTimestamps()
got, err := p.offsetTimestamps()
if (err != nil) != tt.wantErr {
t.Errorf("Player.offsetTimestamps() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
Expand All @@ -530,34 +535,29 @@ func Test_timeOffsets(t *testing.T) {
name: "ok",
args: args{
ots: offts{
546: offsetInfo{ID: "C1234567890", Timestamps: []string{"1234567890.100000", "1234567890.200000", "1234567890.300000", "1234567890.400000", "1234567890.500000"}},
546: offsetInfo{ID: "C1234567890", Timestamps: []int64{1234567890100000, 1234567890200000, 1234567890300000, 1234567890400000, 1234567890500000}},
},
},
want: map[int64]TimeOffset{
1234567890100000: {
Offset: 546,
Timestamp: "1234567890.100000",
Index: 0,
Offset: 546,
Index: 0,
},
1234567890200000: {
Offset: 546,
Timestamp: "1234567890.200000",
Index: 1,
Offset: 546,
Index: 1,
},
1234567890300000: {
Offset: 546,
Timestamp: "1234567890.300000",
Index: 2,
Offset: 546,
Index: 2,
},
1234567890400000: {
Offset: 546,
Timestamp: "1234567890.400000",
Index: 3,
Offset: 546,
Index: 3,
},
1234567890500000: {
Offset: 546,
Timestamp: "1234567890.500000",
Index: 4,
Offset: 546,
Index: 4,
},
},
},
Expand Down

0 comments on commit 2158ed9

Please sign in to comment.