|
| 1 | +import logging |
| 2 | +import json |
| 3 | +import collections |
| 4 | +from airflow.hooks.S3_hook import S3Hook |
| 5 | +from airflow.models import BaseOperator |
| 6 | +from airflow.utils.decorators import apply_defaults |
| 7 | +from intercom_plugin.hooks.intercom_hook import IntercomHook |
| 8 | +from tempfile import NamedTemporaryFile |
| 9 | + |
| 10 | + |
| 11 | +# TODO: Inherit from BaseOperator |
| 12 | +class IntercomToS3Operator(BaseOperator): |
| 13 | + """ |
| 14 | + Make a query against Intercom and write the resulting data to s3. |
| 15 | + """ |
| 16 | + |
| 17 | + @apply_defaults |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + intercom_conn_id, |
| 21 | + intercom_obj, |
| 22 | + intercom_method='all', |
| 23 | + s3_conn_id='', |
| 24 | + s3_bucket='', |
| 25 | + s3_key='', |
| 26 | + output='', |
| 27 | + fields=None, |
| 28 | + replication_key_name=None, |
| 29 | + replication_key_value=0, |
| 30 | + *args, |
| 31 | + **kwargs |
| 32 | + ): |
| 33 | + """ |
| 34 | + Initialize the operator |
| 35 | + :param intercom_conn_id: name of the Airflow connection that has |
| 36 | + your Intercom tokens |
| 37 | + :param intercom_obj: name of the Intercom object we are |
| 38 | + fetching data from |
| 39 | + :param s3_conn_id: name of the Airflow connection that has |
| 40 | + your Amazon S3 conection params |
| 41 | + :param s3_bucket: name of the destination S3 bucket |
| 42 | + :param s3_key: name of the destination file from bucket |
| 43 | + :param output: name of the temporary file where the results |
| 44 | + should be saved |
| 45 | + :param fields: *(optional)* list of fields that you want |
| 46 | + to get from the object. |
| 47 | + If *None*, then this will get all fields |
| 48 | + for the object |
| 49 | + :param replication_key_name: *(optional)* name of the replication key, |
| 50 | + if needed. |
| 51 | + :param replication_key_value: *(optional)* value of the replication key, |
| 52 | + if needed. The operator will import only |
| 53 | + results with the property from replication_key |
| 54 | + grater than the value of this param. |
| 55 | + :param intercom_method *(optional)* method to call from python-intercom |
| 56 | + Default to "all". |
| 57 | + :param \**kwargs: Extra params for the intercom query, based on python |
| 58 | + intercom module |
| 59 | + """ |
| 60 | + |
| 61 | + super().__init__(*args, **kwargs) |
| 62 | + |
| 63 | + # TODO: update with get_conn(intercom_conn_id) |
| 64 | + self.intercom_conn_id = intercom_conn_id |
| 65 | + self.intercom_obj = intercom_obj |
| 66 | + self.intercom_method = intercom_method |
| 67 | + |
| 68 | + self.s3_conn_id = s3_conn_id |
| 69 | + self.s3_bucket = s3_bucket |
| 70 | + self.s3_key = s3_key |
| 71 | + self.output = output |
| 72 | + |
| 73 | + self.fields = fields |
| 74 | + self.replication_key_name = replication_key_name |
| 75 | + self.replication_key_value = replication_key_value |
| 76 | + self._kwargs = kwargs |
| 77 | + |
| 78 | + def filter_fields(self, result): |
| 79 | + """ |
| 80 | + Filter the fields from an resulting object. |
| 81 | +
|
| 82 | + This will return a object only with fields given |
| 83 | + as parameter in the constructor. |
| 84 | +
|
| 85 | + All fields are returned when "fields" param is None. |
| 86 | + """ |
| 87 | + if not self.fields: |
| 88 | + return result |
| 89 | + obj = {} |
| 90 | + for field in self.fields: |
| 91 | + obj[field] = result[field] |
| 92 | + return obj |
| 93 | + |
| 94 | + def filter(self, results): |
| 95 | + """ |
| 96 | + Filter the results. |
| 97 | + This will filter the results if there's a replication key given as param. |
| 98 | + """ |
| 99 | + if not isinstance(results, collections.Iterable): |
| 100 | + return json.loads((json.dumps(results, default=lambda o: o.__dict__))) |
| 101 | + |
| 102 | + filtered = [] |
| 103 | + for result in results: |
| 104 | + result_json = json.loads((json.dumps(result, |
| 105 | + default=lambda o: o.__dict__))) |
| 106 | + |
| 107 | + if not self.replication_key_name or \ |
| 108 | + int(result_json[self.replication_key_name]) >= int(self.replication_key_value): |
| 109 | + filtered.append(self.filter_fields(result_json)) |
| 110 | + logging.info(filtered) |
| 111 | + |
| 112 | + return filtered |
| 113 | + |
| 114 | + def execute(self, context): |
| 115 | + """ |
| 116 | + Execute the operator. |
| 117 | + This will get all the data for a particular Intercom model |
| 118 | + and write it to a file. |
| 119 | + """ |
| 120 | + logging.info("Prepping to gather data from Intercom") |
| 121 | + hook = IntercomHook( |
| 122 | + conn_id=self.intercom_conn_id, |
| 123 | + ) |
| 124 | + |
| 125 | + # attempt to login to Intercom |
| 126 | + # if this process fails, it will raise an error and die right here |
| 127 | + # we could wrap it |
| 128 | + hook.get_conn() |
| 129 | + |
| 130 | + logging.info( |
| 131 | + "Making request for" |
| 132 | + " {0} object".format(self.intercom_obj) |
| 133 | + ) |
| 134 | + |
| 135 | + # fetch the results from intercom and filter them |
| 136 | + |
| 137 | + results = hook.run_query(self.intercom_obj, self.intercom_method) |
| 138 | + filterd_results = self.filter(results) |
| 139 | + |
| 140 | + # write the results to a temporary file and save that file to s3 |
| 141 | + with NamedTemporaryFile("w") as tmp: |
| 142 | + for result in filterd_results: |
| 143 | + tmp.write(json.dumps(result) + '\n') |
| 144 | + |
| 145 | + tmp.flush() |
| 146 | + |
| 147 | + dest_s3 = S3Hook(s3_conn_id=self.s3_conn_id) |
| 148 | + dest_s3.load_file( |
| 149 | + filename=tmp.name, |
| 150 | + key=self.output, |
| 151 | + bucket_name=self.s3_bucket, |
| 152 | + replace=True |
| 153 | + |
| 154 | + ) |
| 155 | + dest_s3.connection.close() |
| 156 | + tmp.close() |
| 157 | + |
| 158 | + logging.info("Query finished!") |
0 commit comments