Ejemplo n.º 1
0
    def to_influx(self, measurement):
        """
        Converts listen into dict that can be submitted to influx directly.

        Returns:
            a dict with appropriate values of measurement, time, tags and fields
        """

        data = {
            'measurement': measurement,
            'time': self.ts_since_epoch,
            'fields': {
                'user_name':
                escape(self.user_name),
                'artist_name':
                self.data['artist_name'],
                'artist_msid':
                self.artist_msid,
                'artist_mbids':
                ",".join(self.data['additional_info'].get('artist_mbids', [])),
                'release_name':
                self.data.get('release_name', ''),
                'release_msid':
                self.release_msid,
                'release_mbid':
                self.data['additional_info'].get('release_mbid', ''),
                'track_name':
                self.data['track_name'],
                'recording_msid':
                self.recording_msid,
                'recording_mbid':
                self.data['additional_info'].get('recording_mbid', ''),
                'tags':
                ",".join(self.data['additional_info'].get('tags', [])),
                'release_group_mbid':
                self.data['additional_info'].get('release_group_mbid', ''),
                'track_mbid':
                self.data['additional_info'].get('track_mbid', ''),
                'work_mbids':
                ','.join(self.data['additional_info'].get('work_mbids', [])),
                'tracknumber':
                self.data['additional_info'].get('tracknumber', ''),
                'isrc':
                self.data['additional_info'].get('isrc', ''),
                'spotify_id':
                self.data['additional_info'].get('spotify_id', ''),
            }
        }

        # if we need a dedup tag, then add it to the row
        if self.dedup_tag > 0:
            data['tags'] = {'dedup_tag': self.dedup_tag}

        # add the user generated keys present in additional info to fields
        for key, value in self.data['additional_info'].items():
            if key not in Listen.SUPPORTED_KEYS:
                data['fields'][key] = escape(str(value))

        return data
Ejemplo n.º 2
0
    def to_influx(self, measurement):
        """
        Converts listen into dict that can be submitted to influx directly.

        Returns:
            a dict with appropriate values of measurement, time, tags and fields
        """

        data = {
            'measurement' : measurement,
            'time' : self.ts_since_epoch,
            'fields' : {
                'user_name' : escape(self.user_name),
                'artist_name' : self.data['artist_name'],
                'artist_msid' : self.artist_msid,
                'artist_mbids' : ",".join(self.data['additional_info'].get('artist_mbids', [])),
                'release_name' : self.data.get('release_name', ''),
                'release_msid' : self.release_msid,
                'release_mbid' : self.data['additional_info'].get('release_mbid', ''),
                'track_name' : self.data['track_name'],
                'recording_msid' : self.recording_msid,
                'recording_mbid' : self.data['additional_info'].get('recording_mbid', ''),
                'tags' : ",".join(self.data['additional_info'].get('tags', [])),
                'release_group_mbid': self.data['additional_info'].get('release_group_mbid', ''),
                'track_mbid': self.data['additional_info'].get('track_mbid', ''),
                'work_mbids': ','.join(self.data['additional_info'].get('work_mbids', [])),
                'tracknumber': self.data['additional_info'].get('tracknumber', ''),
                'isrc': self.data['additional_info'].get('isrc', ''),
                'spotify_id': self.data['additional_info'].get('spotify_id', ''),
                'inserted_timestamp': int(time.time()),
            }
        }


        # if we need a dedup tag, then add it to the row
        if self.dedup_tag > 0:
            data['tags'] = {'dedup_tag': self.dedup_tag}

        # add the user generated keys present in additional info to fields
        for key, value in self.data['additional_info'].items():
            if key in Listen.PRIVATE_KEYS:
                continue
            if key not in Listen.SUPPORTED_KEYS:
                data['fields'][key] = ujson.dumps(value)

        return data