balena-io-modules / resin-device-logs

Resin.io device logs utilities.
Apache License 2.0
3 stars 2 forks source link

Need the clear logs functionality #18

Closed emirotin closed 7 years ago

emirotin commented 7 years ago
channel = "device-#{logsChannel}-logs"
clearLogsChannel = "device-#{logsChannel}-clear-logs"

PubNub.ngSubscribe(channel: channel)
PubNub.ngSubscribe(channel: clearLogsChannel)

Before getting the historical logs (from angular code, needs adaptation):

PubNub.jsapi.history
    channel: clearLogsChannel
    count: 1
    callback: (messages) ->
        endTime = 0
        if messages[0][0]?
            endTime = messages[0][0]

        # We fetch the last 200 logs and then broadcast a 'history-loaded'
        # event so that interested listeners are notified. The implementation
        # is a bit hacky, hence the explanation below:
        #
        # re ngHistoryQ: that's a new method in pubnub-angular@1.2.0-beta
        # that fetches history and returns a promise. Lib authors found it
        # a good idea to make this work differently than ngHistory and *not*
        # broadcast events for incoming messages.
        #
        # re _ngFireMessages: this is private API that broadcasts events
        # for incoming messages. Given that the authors haven't released a
        # new version for more than a year (as of Jan 2016), and that the
        # version in bower.json is pinned, we expect no future issues with this.
        #
        # We do use a beta release of pubnub-angular that's pinned to a
        # specific version. Should you upgrade the version in bower.json,
        # make sure you test this part. If it breaks, just implement a custom
        # callback where you broadcast events for incoming messages manually.
        PubNub.ngHistoryQ
            channel: channel
            count: 200
            end: endTime
            callback: PubNub._ngFireMessages channel
        .then ->
            $timeout ->
                $rootScope.$broadcast("history-loaded-#{uuid}")

And to clear the logs:

clearLogs = (logsChannel) ->
    PubNub.jsapi.time (time) ->
        PubNub.ngPublish
            channel: clearLogsChannel
            message: time